Move the userlist code in a separate file

This commit is contained in:
yflory
2017-05-05 15:47:46 +02:00
parent 5a5b02b82b
commit 6847906ac9
6 changed files with 120 additions and 100 deletions
+97
View File
@@ -0,0 +1,97 @@
define([
'jquery',
'/common/cryptget.js',
], function ($, Cryptget) {
var module = {};
module.create = function (info, onLocal, Cryptpad) {
var exp = {};
var userData = exp.userData = {};
var userList = exp.userList = info.userList;
var myData = exp.myData = {};
var myUserName = exp.myUserName = info.myID;
var myNetfluxId = exp.myNetfluxId = info.myID;
var users = userList.users;
var addToUserData = exp.addToUserData = function(data) {
for (var attrname in data) { userData[attrname] = data[attrname]; }
if (users && users.length) {
for (var userKey in userData) {
if (users.indexOf(userKey) === -1) {
delete userData[userKey];
}
}
}
if(userList && typeof userList.onChange === "function") {
userList.onChange(userData);
}
};
exp.getToolbarConfig = function () {
return {
data: userData,
list: userList,
userNetfluxId: myNetfluxId
};
};
var setName = exp.setName = function (newName, cb) {
if (typeof(newName) !== 'string') { return; }
var myUserNameTemp = newName.trim();
if(newName.trim().length > 32) {
myUserNameTemp = myUserNameTemp.substr(0, 32);
}
myUserName = myUserNameTemp;
myData[myNetfluxId] = {
name: myUserName,
uid: Cryptpad.getUid(),
};
addToUserData(myData);
Cryptpad.setAttribute('username', myUserName, function (err) {
if (err) {
console.log("Couldn't set username");
console.error(err);
return;
}
if (typeof cb === "function") { onLocal(); }
});
};
exp.getLastName = function ($changeNameButton, isNew) {
Cryptpad.getLastName(function (err, lastName) {
if (err) {
console.log("Could not get previous name");
console.error(err);
return;
}
// Update the toolbar list:
// Add the current user in the metadata
if (typeof(lastName) === 'string') {
setName(lastName, onLocal);
} else {
myData[myNetfluxId] = {
name: "",
uid: Cryptpad.getUid(),
};
addToUserData(myData);
onLocal();
$changeNameButton.click();
}
if (isNew) {
Cryptpad.selectTemplate('code', info.realtime, Cryptget);
}
});
};
Cryptpad.onDisplayNameChanged(function (newName) {
setName(newName, onLocal);
});
return exp;
};
return module;
});
+5 -1
View File
@@ -7,11 +7,12 @@ define([
'/common/common-hash.js',
'/common/common-interface.js',
'/common/common-history.js',
'/common/common-userlist.js',
'/common/clipboard.js',
'/common/pinpad.js',
'/customize/application_config.js'
], function ($, Config, Messages, Store, Util, Hash, UI, History, Clipboard, Pinpad, AppConfig) {
], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Clipboard, Pinpad, AppConfig) {
/* This file exposes functionality which is specific to Cryptpad, but not to
any particular pad type. This includes functions for committing metadata
@@ -84,6 +85,9 @@ define([
common.findStronger = Hash.findStronger;
common.serializeHash = Hash.serializeHash;
// Userlist
common.createUserList = UserList.create;
// History
common.getHistory = function (config) { return History.create(common, config); };
+3 -4
View File
@@ -121,16 +121,15 @@ define([
// Display only one time each user (if he is connected in multiple tabs)
var myUid = userData[userNetfluxId] ? userData[userNetfluxId].uid : undefined;
var uids = [];
userList.forEach(function(user) {
if(user !== userNetfluxId) {
if (user !== userNetfluxId) {
var data = userData[user] || {};
var userName = data.name;
var userId = data.uid;
if (userName && uids.indexOf(userId) === -1 && (!myUid || userId !== myUid)) {
uids.push(userId);
list.push(userName);
} else { i++; }
} else if (userName) { i++; }
}
});
return {
@@ -674,7 +673,7 @@ define([
}
Cryptpad.createUserAdminMenu(userMenuCfg);
var $userButton = $userAdmin.find('a.' + USERBUTTON_CLS);
var $userButton = toolbar.$userNameButton = $userAdmin.find('a.' + USERBUTTON_CLS);
$userButton.click(function (e) {
e.preventDefault();
e.stopPropagation();