From 5a5b02b82b9abe3db45ae7ded35fe4df9adaa798 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 5 May 2017 11:55:19 +0200 Subject: [PATCH 1/4] Don't store in the drive pads without a hash --- www/common/cryptpad-common.js | 10 +++++++--- www/common/userObject.js | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 104b67a9b..8096e3044 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -299,10 +299,14 @@ define([ pads.forEach(function (pad, i) { if (pad && typeof(pad) === 'object') { var hash = checkObjectData(pad); - if (!hash || !common.parseHash(hash)) { return; } + if (!hash || !common.parseHash(hash)) { + console.error("[Cryptpad.checkRecentPads] pad had unexpected value", pad); + getStore().removeData(i); + return; + } return pad; } - console.error("[Cryptpad.migrateRecentPads] pad had unexpected value"); + console.error("[Cryptpad.checkRecentPads] pad had unexpected value", pad); getStore().removeData(i); }); }; @@ -571,7 +575,7 @@ define([ return pad; }); - if (!contains) { + if (!contains && href) { var data = makePad(href, name); getStore().pushData(data, function (e) { if (e) { diff --git a/www/common/userObject.js b/www/common/userObject.js index aa61c8573..a7c21f3f3 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -883,6 +883,11 @@ define([ toClean.push(el); return; } + if (!el.href) { + debug("Rmoving an element in filesData with a missing href.", el); + toClean.push(el); + return; + } if (rootFiles.indexOf(el.href) === -1) { debug("An element in filesData was not in ROOT, TEMPLATE or TRASH.", el); var name = el.title || NEW_FILE_NAME; From 6847906ac94c5526556cdab221bb6cde12638920 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 5 May 2017 15:47:46 +0200 Subject: [PATCH 2/4] Move the userlist code in a separate file --- customize.dist/src/less/toolbar.less | 6 +- customize.dist/toolbar.css | 5 +- www/code/main.js | 99 ++++------------------------ www/common/common-userlist.js | 97 +++++++++++++++++++++++++++ www/common/cryptpad-common.js | 6 +- www/common/toolbar2.js | 7 +- 6 files changed, 120 insertions(+), 100 deletions(-) create mode 100644 www/common/common-userlist.js diff --git a/customize.dist/src/less/toolbar.less b/customize.dist/src/less/toolbar.less index dd8063e01..a199730cd 100644 --- a/customize.dist/src/less/toolbar.less +++ b/customize.dist/src/less/toolbar.less @@ -440,11 +440,9 @@ .cryptpad-toolbar-leftside { float: left; margin-bottom: -1px; - .cryptpad-user-list { - //float: right; + .cryptpad-dropdown-users { pre { - white-space: pre; - margin: 0; + margin: 5px 0px; } } button { diff --git a/customize.dist/toolbar.css b/customize.dist/toolbar.css index 4689803c4..d2385dfc1 100644 --- a/customize.dist/toolbar.css +++ b/customize.dist/toolbar.css @@ -517,9 +517,8 @@ float: left; margin-bottom: -1px; } -.cryptpad-toolbar-leftside .cryptpad-user-list pre { - white-space: pre; - margin: 0; +.cryptpad-toolbar-leftside .cryptpad-dropdown-users pre { + margin: 5px 0px; } .cryptpad-toolbar-leftside button { margin: 2px 4px 2px 0px; diff --git a/www/code/main.js b/www/code/main.js index 8e709bc1b..2390aa670 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -120,33 +120,7 @@ define([ editor.setOption('readOnly', !bool); }; - var userData = module.userData = {}; // List of pretty name of all users (mapped with their server ID) - var userList; // List of users still connected to the channel (server IDs) - var addToUserData = function(data) { - var users = module.users; - 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); - } - }; - - var myData = {}; - var myUserName = ''; // My "pretty name" - var myID; // My server ID - - var setMyID = function(info) { - myID = info.myID || null; - myUserName = myID; - }; + var UserList; var config = { initialState: '{}', @@ -156,7 +130,6 @@ define([ validateKey: secret.keys.validateKey || undefined, readOnly: readOnly, crypto: Crypto.createEncryptor(secret.keys), - setMyID: setMyID, network: Cryptpad.getNetwork(), transformFunction: JsonOT.validate, }; @@ -182,7 +155,7 @@ define([ var obj = { content: textValue, metadata: { - users: userData, + users: UserList.userData, defaultTitle: defaultName } }; @@ -213,28 +186,6 @@ define([ } }; - var setName = module.setName = function (newName) { - if (typeof(newName) !== 'string') { return; } - var myUserNameTemp = newName.trim(); - if(newName.trim().length > 32) { - myUserNameTemp = myUserNameTemp.substr(0, 32); - } - myUserName = myUserNameTemp; - myData[myID] = { - 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; - } - onLocal(); - }); - }; - var getHeadingText = function () { var lines = editor.getValue().split(/\n/); @@ -362,7 +313,7 @@ define([ if (json.metadata.users) { var userData = json.metadata.users; // Update the local user data - addToUserData(userData); + UserList.addToUserData(userData); } if (json.metadata.defaultTitle) { updateDefaultTitle(json.metadata.defaultTitle); @@ -378,17 +329,11 @@ define([ }; config.onInit = function (info) { - userList = info.userList; + UserList = Cryptpad.createUserList(info, config.onLocal, Cryptpad); var configTb = { displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], - userList: { - data: userData, - list: userList, - userNetfluxId: info.myID - }, - readOnly: readOnly, - ifrw: ifrw, + userList: UserList.getToolbarConfig(), share: { secret: secret, channel: info.channel @@ -399,14 +344,15 @@ define([ suggestName: suggestName }, common: Cryptpad, + readOnly: readOnly, + ifrw: ifrw, realtime: info.realtime, network: info.network, $container: $bar }; toolbar = module.toolbar = Toolbar.create(configTb); - var $rightside = $bar.find('.' + Toolbar.constants.rightside); - module.$userNameButton = $($bar.find('.' + Toolbar.constants.changeUsername)); + var $rightside = toolbar.$rightside; var editHash; if (!readOnly) { @@ -555,7 +501,6 @@ define([ // set the hash if (!readOnly) { Cryptpad.replaceHash(editHash); } - Cryptpad.onDisplayNameChanged(setName); }; var unnotify = module.unnotify = function () { @@ -573,7 +518,6 @@ define([ }; config.onReady = function (info) { - module.users = info.userList.users; if (module.realtime !== info.realtime) { var realtime = module.realtime = info.realtime; module.patchText = TextPatcher.create({ @@ -633,30 +577,9 @@ define([ //Cryptpad.log("Your document is ready"); onLocal(); // push local state to avoid parse errors later. - 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 he has edit rights - if (readOnly) { return; } - if (typeof(lastName) === 'string') { - setName(lastName); - } else { - myData[myID] = { - name: "", - uid: Cryptpad.getUid(), - }; - addToUserData(myData); - onLocal(); - module.$userNameButton.click(); - } - if (isNew) { - Cryptpad.selectTemplate('code', info.realtime, Cryptget); - } - }); + + if (readOnly) { return; } + UserList.getLastName(toolbar.$userNameButton, isNew); }; var cursorToPos = function(cursor, oldText) { diff --git a/www/common/common-userlist.js b/www/common/common-userlist.js new file mode 100644 index 000000000..ce9a2f820 --- /dev/null +++ b/www/common/common-userlist.js @@ -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; +}); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 8096e3044..5194d9021 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -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); }; diff --git a/www/common/toolbar2.js b/www/common/toolbar2.js index b91e16793..49ab10ad3 100644 --- a/www/common/toolbar2.js +++ b/www/common/toolbar2.js @@ -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(); From 5a883d5a579ad539cf7615b5dd20b7565a4e68e3 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 5 May 2017 17:56:49 +0200 Subject: [PATCH 3/4] Use the new toolbar and move the userlist code in a separate file --- www/code/main.js | 4 +- www/common/common-hash.js | 1 + www/common/common-userlist.js | 16 +- www/common/toolbar.js | 3 +- www/common/toolbar2.js | 14 +- www/drive/main.js | 27 +- www/pad/main.js | 122 ++-------- www/poll/main.js | 447 +++++++++++++++------------------- www/slide/main.js | 110 ++------- www/whiteboard/main.js | 109 ++------- 10 files changed, 293 insertions(+), 560 deletions(-) diff --git a/www/code/main.js b/www/code/main.js index 2390aa670..c382b38e4 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -329,7 +329,7 @@ define([ }; config.onInit = function (info) { - UserList = Cryptpad.createUserList(info, config.onLocal, Cryptpad); + UserList = Cryptpad.createUserList(info, config.onLocal, Cryptget, Cryptpad); var configTb = { displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], @@ -500,7 +500,6 @@ define([ // set the hash if (!readOnly) { Cryptpad.replaceHash(editHash); } - }; var unnotify = module.unnotify = function () { @@ -574,7 +573,6 @@ define([ Cryptpad.removeLoadingScreen(); setEditable(true); initializing = false; - //Cryptpad.log("Your document is ready"); onLocal(); // push local state to avoid parse errors later. diff --git a/www/common/common-hash.js b/www/common/common-hash.js index 98e9152fb..d4c2bb112 100644 --- a/www/common/common-hash.js +++ b/www/common/common-hash.js @@ -41,6 +41,7 @@ define([ var ret = {}; if (!href) { return ret; } + if (href.slice(-1) !== '/') { href += '/'; } if (!/^https*:\/\//.test(href)) { var idx = href.indexOf('/#'); diff --git a/www/common/common-userlist.js b/www/common/common-userlist.js index ce9a2f820..46071779b 100644 --- a/www/common/common-userlist.js +++ b/www/common/common-userlist.js @@ -1,10 +1,9 @@ define([ 'jquery', - '/common/cryptget.js', -], function ($, Cryptget) { +], function ($) { var module = {}; - module.create = function (info, onLocal, Cryptpad) { + module.create = function (info, onLocal, Cryptget, Cryptpad) { var exp = {}; var userData = exp.userData = {}; @@ -13,6 +12,9 @@ define([ var myUserName = exp.myUserName = info.myID; var myNetfluxId = exp.myNetfluxId = info.myID; + var parsed = Cryptpad.parsePadUrl(window.location.href); + var appType = parsed ? parsed.type : undefined; + var users = userList.users; var addToUserData = exp.addToUserData = function(data) { for (var attrname in data) { userData[attrname] = data[attrname]; } @@ -41,7 +43,7 @@ define([ var setName = exp.setName = function (newName, cb) { if (typeof(newName) !== 'string') { return; } var myUserNameTemp = newName.trim(); - if(newName.trim().length > 32) { + if(myUserNameTemp.length > 32) { myUserNameTemp = myUserNameTemp.substr(0, 32); } myUserName = myUserNameTemp; @@ -56,7 +58,7 @@ define([ console.error(err); return; } - if (typeof cb === "function") { onLocal(); } + if (typeof cb === "function") { cb(); } }); }; @@ -80,8 +82,8 @@ define([ onLocal(); $changeNameButton.click(); } - if (isNew) { - Cryptpad.selectTemplate('code', info.realtime, Cryptget); + if (isNew && appType) { + Cryptpad.selectTemplate(appType, info.realtime, Cryptget); } }); }; diff --git a/www/common/toolbar.js b/www/common/toolbar.js index e64639bc8..6920c0b0d 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -558,7 +558,8 @@ define([ $userButton.click(function (e) { e.preventDefault(); e.stopPropagation(); - Cryptpad.getLastName(function (lastName) { + Cryptpad.getLastName(function (err, lastName) { + if (err) { return void console.error("Cannot get last name", err); } Cryptpad.prompt(Messages.changeNamePrompt, lastName || '', function (newName) { if (newName === null && typeof(lastName) === "string") { return; } if (newName === null) { newName = ''; } diff --git a/www/common/toolbar2.js b/www/common/toolbar2.js index 49ab10ad3..9d7d43aa9 100644 --- a/www/common/toolbar2.js +++ b/www/common/toolbar2.js @@ -102,7 +102,7 @@ define([ var checkSynchronizing = function (toolbar, config) { if (!toolbar.state) { return; } var userList = config.userList.list.users; - var userNetfluxId = config.userList.userNetfluxId; // TODO + var userNetfluxId = config.userList.userNetfluxId; var meIdx = userList.indexOf(userNetfluxId); if (meIdx === -1) { toolbar.state.text(Messages.synchronizing); @@ -113,7 +113,7 @@ define([ var getOtherUsers = function(config) { var userList = config.userList.list.users; var userData = config.userList.data; - var userNetfluxId = config.userList.userNetfluxId; // TODO + var userNetfluxId = config.userList.userNetfluxId; var i = 0; // duplicates counter var list = []; @@ -260,8 +260,6 @@ define([ $block.attr('id', 'userButtons'); toolbar.$leftside.prepend($block); - initUserList(toolbar, config); - return $block; }; @@ -596,7 +594,7 @@ define([ 'class': 'synced fa fa-check', title: Messages.synced }).appendTo($spin); - toolbar.$userAdmin.prepend($spin); // TODO: put at the corretc position in userAdmin + toolbar.$userAdmin.prepend($spin); if (config.realtime) { config.realtime.onPatch(ks(toolbar, config)); config.realtime.onMessage(ks(toolbar, config, true)); @@ -677,7 +675,8 @@ define([ $userButton.click(function (e) { e.preventDefault(); e.stopPropagation(); - Cryptpad.getLastName(function (lastName) { + Cryptpad.getLastName(function (err, lastName) { + if (err) { return void console.error("Cannot get last name", err); } Cryptpad.prompt(Messages.changeNamePrompt, lastName || '', function (newName) { if (newName === null && typeof(lastName) === "string") { return; } if (newName === null) { newName = ''; } @@ -844,7 +843,7 @@ define([ tb['userlist'] = createUserList; tb['share'] = createShare; tb['fileshare'] = createFileShare; - tb['title'] = createTitle; // TODO: New one, add it to the displayed in main.js ? + tb['title'] = createTitle; tb['lag'] = createLag; tb['spinner'] = createSpinner; tb['state'] = createState; @@ -866,6 +865,7 @@ define([ }; addElement(config.displayed, {}, true); + initUserList(toolbar, config); toolbar['linkToMain'] = createLinkToMain(toolbar, config); diff --git a/www/drive/main.js b/www/drive/main.js index 66d040d54..3f2af6720 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -6,7 +6,7 @@ define([ 'json.sortify', '/common/cryptpad-common.js', '/common/userObject.js', - '/common/toolbar.js', + '/common/toolbar2.js', '/customize/application_config.js', '/common/cryptget.js', '/common/mergeDrive.js' @@ -2583,21 +2583,26 @@ define([ var userList = APP.userList = info.userList; var config = { displayed: ['useradmin', 'spinner', 'lag', 'state', 'limit'], + userList: { + list: userList, + userNetfluxId: info.myID + }, + common: Cryptpad, readOnly: readOnly, ifrw: window, - common: Cryptpad, - hideShare: true + realtime: info.realtime, + network: info.network, + $container: APP.$bar }; - APP.toolbar = info.realtime.toolbar = Toolbar.create(APP.$bar, info.myID, info.realtime, info.getLag, userList, config); + var toolbar = APP.toolbar = Toolbar.create(config); - var $bar = APP.$bar; - var $rightside = $bar.find('.' + Toolbar.constants.rightside); - var $leftside = $bar.find('.' + Toolbar.constants.leftside); - var $userBlock = $bar.find('.' + Toolbar.constants.userAdmin); - APP.$displayName = $bar.find('.' + Toolbar.constants.username); + var $rightside = toolbar.$rightside; + var $leftside = toolbar.$leftside; + var $userBlock = toolbar.$userAdmin; + APP.$displayName = APP.$bar.find('.' + Toolbar.constants.username); if (APP.homePageIframe) { - var $linkToMain = $bar.find('.cryptpad-link a'); + var $linkToMain = toolbar.linkToMain; $linkToMain.attr('href', '#'); $linkToMain.attr('title', ''); $linkToMain.css('cursor', 'default'); @@ -2669,7 +2674,7 @@ define([ // Called when the history is loaded and the UI displayed setHistory(true); }; - histConfig.$toolbar = $bar; + histConfig.$toolbar = APP.$bar; histConfig.href = window.location.origin + window.location.pathname + APP.hash; var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); $rightside.append($hist); diff --git a/www/pad/main.js b/www/pad/main.js index 9ae4e4549..5e679f05f 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -3,7 +3,7 @@ define([ '/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/hyperjson/hyperjson.js', - '/common/toolbar.js', + '/common/toolbar2.js', '/common/cursor.js', '/bower_components/chainpad-json-validator/json-ot.js', '/common/TypingTests.js', @@ -277,51 +277,7 @@ define([ }; var initializing = true; - var userData = module.userData = {}; // List of pretty names for all users (mapped with their ID) - var userList; // List of users still connected to the channel (server IDs) - var addToUserData = function(data) { - var users = module.users; - 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); - } - }; - - var myData = {}; - var myUserName = ''; // My "pretty name" - var myID; // My server ID - - var setMyID = function(info) { - myID = info.myID || null; - }; - - var setName = module.setName = function (newName) { - if (typeof(newName) !== 'string') { return; } - var myUserNameTemp = newName.trim(); - if(myUserNameTemp.length > 32) { - myUserNameTemp = myUserNameTemp.substr(0, 32); - } - myUserName = myUserNameTemp; - myData[myID] = { - name: myUserName, - uid: Cryptpad.getUid(), - }; - addToUserData(myData); - Cryptpad.setAttribute('username', newName, function (err) { - if (err) { - console.error("Couldn't set username"); - return; - } - editor.fire('change'); - }); - }; + var UserList; var getHeadingText = function () { var text; @@ -359,7 +315,7 @@ define([ var hjson = Hyperjson.fromDOM(dom, isNotMagicLine, brFilter); hjson[3] = { metadata: { - users: userData, + users: UserList.userData, defaultTitle: defaultName } }; @@ -385,9 +341,6 @@ define([ validateKey: secret.keys.validateKey || undefined, readOnly: readOnly, - // method which allows us to get the id of the user - setMyID: setMyID, - // Pass in encrypt and decrypt methods crypto: Crypto.createEncryptor(secret.keys), @@ -449,7 +402,7 @@ define([ if (peerMetadata.metadata.users) { var userData = peerMetadata.metadata.users; // Update the local user data - addToUserData(userData); + UserList.addToUserData(userData); } if (peerMetadata.metadata.defaultTitle) { updateDefaultTitle(peerMetadata.metadata.defaultTitle); @@ -570,13 +523,11 @@ define([ }; realtimeOptions.onInit = function (info) { - userList = info.userList; + UserList = Cryptpad.createUserList(info, realtimeOptions.onLocal, Cryptget, Cryptpad); var configTb = { - displayed: ['useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], - userData: userData, - readOnly: readOnly, - ifrw: ifrw, + displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], + userList: UserList.getToolbarConfig(), share: { secret: secret, channel: info.channel @@ -586,13 +537,16 @@ define([ defaultName: defaultName, suggestName: suggestName }, - common: Cryptpad + common: Cryptpad, + readOnly: readOnly, + ifrw: ifrw, + realtime: info.realtime, + network: info.network, + $container: $bar }; - toolbar = info.realtime.toolbar = Toolbar.create($bar, info.myID, info.realtime, info.getLag, userList, configTb); + toolbar = info.realtime.toolbar = Toolbar.create(configTb); - var $rightside = $bar.find('.' + Toolbar.constants.rightside); - $bar.find('.' + Toolbar.constants.username); - module.$userNameButton = $($bar.find('.' + Toolbar.constants.changeUsername)); + var $rightside = toolbar.$rightside; var editHash; @@ -667,10 +621,6 @@ define([ /* add an import button */ var $import = Cryptpad.createButton('import', true, {}, importFile); $rightside.append($import); - - /* add a rename button */ - //var $setTitle = Cryptpad.createButton('rename', true, {suggestName: suggestName}, renameCb); - //$rightside.append($setTitle); } /* add a forget button */ @@ -683,8 +633,6 @@ define([ // set the hash if (!readOnly) { Cryptpad.replaceHash(editHash); } - - Cryptpad.onDisplayNameChanged(setName); }; // this should only ever get called once, when the chain syncs @@ -707,7 +655,6 @@ define([ }); } - module.users = info.userList.users; module.realtime = info.realtime; var shjson = module.realtime.getUserDoc(); @@ -743,35 +690,18 @@ define([ documentBody.innerHTML = Messages.initialState; } - Cryptpad.getLastName(function (err, lastName) { - console.log("Unlocking editor"); - setEditable(!readOnly); - initializing = false; - Cryptpad.removeLoadingScreen(emitResize); - - // Update the toolbar list: - // Add the current user in the metadata if he has edit rights - if (readOnly) { return; } - if (typeof(lastName) === 'string') { - setName(lastName); - } else { - myData[myID] = { - name: "", - uid: Cryptpad.getUid() - }; - addToUserData(myData); - realtimeOptions.onLocal(); - module.$userNameButton.click(); - } + Cryptpad.removeLoadingScreen(emitResize); + setEditable(!readOnly); + initializing = false; - editor.focus(); - if (newPad) { - Cryptpad.selectTemplate('pad', info.realtime, Cryptget); - cursor.setToEnd(); - } else { - cursor.setToStart(); - } - }); + if (readOnly) { return; } + UserList.getLastName(toolbar.$userNameButton, newPad); + editor.focus(); + if (newPad) { + cursor.setToEnd(); + } else { + cursor.setToStart(); + } }; realtimeOptions.onAbort = function () { diff --git a/www/poll/main.js b/www/poll/main.js index 0e6a22168..3881ddd87 100644 --- a/www/poll/main.js +++ b/www/poll/main.js @@ -7,7 +7,7 @@ define([ '/common/cryptget.js', '/bower_components/hyperjson/hyperjson.js', 'render.js', - '/common/toolbar.js', + '/common/toolbar2.js', '/common/visible.js', '/common/notify.js', '/bower_components/file-saver/FileSaver.min.js' @@ -442,47 +442,7 @@ define([ }); }; - var userData = APP.userData = {}; // List of pretty names for all users (mapped with their ID) - var userList; // List of users still connected to the channel (server IDs) - var addToUserData = function(data) { - var users = userList ? userList.users : undefined; - //var userData = APP.proxy.info.userData; - 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); - } - - APP.proxy.info.userData = userData; - }; - - var setName = APP.setName = function (newName) { - if (typeof(newName) !== 'string') { return; } - var myUserNameTemp = newName.trim(); - if(myUserNameTemp.length > 32) { - myUserNameTemp = myUserNameTemp.substr(0, 32); - } - var myUserName = myUserNameTemp; - var myID = APP.myID; - var myData = {}; - myData[myID] = { - name: myUserName, - uid: Cryptpad.getUid(), - }; - addToUserData(myData); - Cryptpad.setAttribute('username', newName, function (err) { - if (err) { - console.error("Couldn't set username"); - return; - } - }); - }; + var UserList; var updateTitle = function (newTitle) { if (newTitle === document.title) { return; } @@ -526,248 +486,229 @@ define([ // special UI elements var $description = $('#description').attr('placeholder', Messages.poll_descriptionHint || 'description'); - var ready = function (info, userid, readOnly) { - debug("READY"); - debug('userid: %s', userid); +var ready = function (info, userid, readOnly) { + debug("READY"); + debug('userid: %s', userid); - var proxy = APP.proxy; + var proxy = APP.proxy; - var isNew = false; - var userDoc = JSON.stringify(proxy); - if (userDoc === "" || userDoc === "{}") { isNew = true; } + var isNew = false; + var userDoc = JSON.stringify(proxy); + if (userDoc === "" || userDoc === "{}") { isNew = true; } - var uncommitted = APP.uncommitted = {}; - prepareProxy(proxy, copyObject(Render.Example)); - prepareProxy(uncommitted, copyObject(Render.Example)); - if (!readOnly && proxy.table.colsOrder.indexOf(userid) === -1 && - uncommitted.table.colsOrder.indexOf(userid) === -1) { - uncommitted.table.colsOrder.unshift(userid); - } + var uncommitted = APP.uncommitted = {}; + prepareProxy(proxy, copyObject(Render.Example)); + prepareProxy(uncommitted, copyObject(Render.Example)); + if (!readOnly && proxy.table.colsOrder.indexOf(userid) === -1 && + uncommitted.table.colsOrder.indexOf(userid) === -1) { + uncommitted.table.colsOrder.unshift(userid); + } - var displayedObj = mergeUncommitted(proxy, uncommitted, false); + var displayedObj = mergeUncommitted(proxy, uncommitted, false); - var colsOrder = sortColumns(displayedObj.table.colsOrder, userid); + var colsOrder = sortColumns(displayedObj.table.colsOrder, userid); - var $table = APP.$table = $(Render.asHTML(displayedObj, null, colsOrder, readOnly)); - APP.$createRow = $('#create-option').click(function () { - //console.error("BUTTON CLICKED! LOL"); - Render.createRow(proxy, function (empty, id) { - change(null, null, null, null, function() { - $('.edit[data-rt-id="' + id + '"]').click(); - }); + var $table = APP.$table = $(Render.asHTML(displayedObj, null, colsOrder, readOnly)); + APP.$createRow = $('#create-option').click(function () { + //console.error("BUTTON CLICKED! LOL"); + Render.createRow(proxy, function (empty, id) { + change(null, null, null, null, function() { + $('.edit[data-rt-id="' + id + '"]').click(); }); }); + }); - APP.$createCol = $('#create-user').click(function () { - Render.createColumn(proxy, function (empty, id) { - change(null, null, null, null, function() { - $('.edit[data-rt-id="' + id + '"]').click(); - }); + APP.$createCol = $('#create-user').click(function () { + Render.createColumn(proxy, function (empty, id) { + change(null, null, null, null, function() { + $('.edit[data-rt-id="' + id + '"]').click(); }); }); + }); - // Commit button - APP.$commit = $('#commit').click(function () { - var uncommittedCopy = JSON.parse(JSON.stringify(APP.uncommitted)); - APP.uncommitted = {}; - prepareProxy(APP.uncommitted, copyObject(Render.Example)); - mergeUncommitted(proxy, uncommittedCopy, true); - APP.$commit.hide(); - change(); - }); + // Commit button + APP.$commit = $('#commit').click(function () { + var uncommittedCopy = JSON.parse(JSON.stringify(APP.uncommitted)); + APP.uncommitted = {}; + prepareProxy(APP.uncommitted, copyObject(Render.Example)); + mergeUncommitted(proxy, uncommittedCopy, true); + APP.$commit.hide(); + change(); + }); - // #publish button is removed in readonly - APP.$publish = $('#publish') - .click(function () { - publish(true); - }); + // #publish button is removed in readonly + APP.$publish = $('#publish') + .click(function () { + publish(true); + }); - // #publish button is removed in readonly - APP.$admin = $('#admin') - .click(function () { - publish(false); - }); + // #publish button is removed in readonly + APP.$admin = $('#admin') + .click(function () { + publish(false); + }); - // Title - if (APP.proxy.info.defaultTitle) { - updateDefaultTitle(APP.proxy.info.defaultTitle); - } else { - APP.proxy.info.defaultTitle = defaultName; - } - if (Cryptpad.initialName && !APP.proxy.info.title) { - APP.proxy.info.title = Cryptpad.initialName; - updateTitle(Cryptpad.initialName); - } else { - updateTitle(APP.proxy.info.title || defaultName); - } + // Title + if (APP.proxy.info.defaultTitle) { + updateDefaultTitle(APP.proxy.info.defaultTitle); + } else { + APP.proxy.info.defaultTitle = defaultName; + } + if (Cryptpad.initialName && !APP.proxy.info.title) { + APP.proxy.info.title = Cryptpad.initialName; + updateTitle(Cryptpad.initialName); + } else { + updateTitle(APP.proxy.info.title || defaultName); + } - // Description - var resize = function () { - var lineCount = $description.val().split('\n').length; - $description.css('height', lineCount + 'rem'); - }; - $description.on('change keyup', function () { - var val = $description.val(); - proxy.info.description = val; - resize(); - }); + // Description + var resize = function () { + var lineCount = $description.val().split('\n').length; + $description.css('height', lineCount + 'rem'); + }; + $description.on('change keyup', function () { + var val = $description.val(); + proxy.info.description = val; resize(); - if (typeof(proxy.info.description) !== 'undefined') { - $description.val(proxy.info.description); - } + }); + resize(); + if (typeof(proxy.info.description) !== 'undefined') { + $description.val(proxy.info.description); + } - $('#tableScroll').html('').prepend($table); - updateDisplayedTable(); - - $table - .click(handleClick) - .on('keyup', function (e) { handleClick(e, true); }); - - proxy - .on('change', ['info'], function (o, n, p) { - if (p[1] === 'title') { - updateTitle(n); - notify(); - } else if (p[1] === "userData") { - addToUserData(APP.proxy.info.userData); - } else if (p[1] === 'description') { - var op = TextPatcher.diff(o, n); - var el = $description[0]; - - var selects = ['selectionStart', 'selectionEnd'].map(function (attr) { - return TextPatcher.transformCursor(el[attr], op); - }); - $description.val(n); - if (op) { - el.selectionStart = selects[0]; - el.selectionEnd = selects[1]; - } - notify(); + $('#tableScroll').html('').prepend($table); + updateDisplayedTable(); + + $table + .click(handleClick) + .on('keyup', function (e) { handleClick(e, true); }); + + proxy + .on('change', ['info'], function (o, n, p) { + if (p[1] === 'title') { + updateTitle(n); + notify(); + } else if (p[1] === "userData") { + UserList.addToUserData(APP.proxy.info.userData); + } else if (p[1] === 'description') { + var op = TextPatcher.diff(o, n); + var el = $description[0]; + + var selects = ['selectionStart', 'selectionEnd'].map(function (attr) { + return TextPatcher.transformCursor(el[attr], op); + }); + $description.val(n); + if (op) { + el.selectionStart = selects[0]; + el.selectionEnd = selects[1]; } + notify(); + } - debug("change: (%s, %s, [%s])", o, n, p.join(', ')); - }) - .on('change', ['table'], change) - .on('remove', [], change); + debug("change: (%s, %s, [%s])", o, n, p.join(', ')); + }) + .on('change', ['table'], change) + .on('remove', [], change); - addToUserData(APP.proxy.info.userData); + UserList.addToUserData(APP.proxy.info.userData); - if (Visible.isSupported()) { - Visible.onChange(function (yes) { - if (yes) { unnotify(); } - }); - } + if (Visible.isSupported()) { + Visible.onChange(function (yes) { + if (yes) { unnotify(); } + }); + } - Cryptpad.getLastName(function (err, lastName) { - APP.ready = true; + APP.ready = true; + if (!proxy.published) { + publish(false); + } else { + publish(true); + } + Cryptpad.removeLoadingScreen(); + + if (readOnly) { return; } + UserList.getLastName(APP.toolbar.$userNameButton, isNew); +}; + +var disconnect = function () { + //setEditable(false); // TODO + APP.toolbar.failed(); + Cryptpad.alert(Messages.common_connectionLost, undefined, true); +}; + +var reconnect = function (info) { + //setEditable(true); // TODO + APP.toolbar.reconnecting(info.myId); + Cryptpad.findOKButton().click(); +}; + +var create = function (info) { + APP.myID = info.myID; + + var editHash; + if (!readOnly) { + editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); + } - if (!proxy.published) { - publish(false); - } else { - publish(true); - } - Cryptpad.removeLoadingScreen(); - - // Update the toolbar list: - // Add the current user in the metadata if he has edit rights - if (readOnly) { return; } - if (typeof(lastName) === 'string') { - setName(lastName); - } else { - var myData = {}; - myData[info.myId] = { - name: "", - uid: Cryptpad.getUid(), - }; - addToUserData(myData); - APP.$userNameButton.click(); - } - if (isNew) { - Cryptpad.selectTemplate('poll', info.realtime, Cryptget); - } + if (APP.realtime !== info.realtime) { + APP.realtime = info.realtime; + APP.patchText = TextPatcher.create({ + realtime: info.realtime, + logging: true, }); - }; - - var disconnect = function () { - //setEditable(false); // TODO - APP.realtime.toolbar.failed(); - Cryptpad.alert(Messages.common_connectionLost, undefined, true); - }; + } - var reconnect = function (info) { - //setEditable(true); // TODO - APP.realtime.toolbar.reconnecting(info.myId); - Cryptpad.findOKButton().click(); + var onLocal = function () { + APP.proxy.info.userData = UserList.userData; + }; + UserList = Cryptpad.createUserList(info, onLocal, Cryptget, Cryptpad); + + var configTb = { + displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], + userList: UserList.getToolbarConfig(), + share: { + secret: secret, + channel: info.channel + }, + title: { + onRename: renameCb, + defaultName: defaultName, + suggestName: suggestName + }, + common: Cryptpad, + readOnly: readOnly, + ifrw: window, + realtime: info.realtime, + network: info.network, + $container: APP.$bar + }; + APP.toolbar = Toolbar.create(configTb); + + var $rightside = APP.toolbar.$rightside; + + /* add a forget button */ + var forgetCb = function (err) { + if (err) { return; } + disconnect(); }; + var $forgetPad = Cryptpad.createButton('forget', true, {}, forgetCb); + $rightside.append($forgetPad); - var create = function (info) { - APP.myID = info.myID; - - var editHash; - if (!readOnly) { - editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); - } - - if (APP.realtime !== info.realtime) { - APP.realtime = info.realtime; - APP.patchText = TextPatcher.create({ - realtime: info.realtime, - logging: true, - }); - } - - userList = APP.userList = info.userList; + // set the hash + if (!readOnly) { Cryptpad.replaceHash(editHash); } - var config = { - displayed: ['useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], - userData: userData, - readOnly: readOnly, - share: { - secret: secret, - channel: info.channel - }, - title: { - onRename: renameCb, - defaultName: defaultName, - suggestName: suggestName - }, - ifrw: window, - common: Cryptpad, + /* save as template */ + if (!Cryptpad.isTemplate(window.location.href)) { + var templateObj = { + rt: info.realtime, + Crypt: Cryptget, + getTitle: function () { return document.title; } }; - info.realtime.toolbar = Toolbar.create(APP.$bar, info.myID, info.realtime, info.getLag, userList, config); - - var $bar = APP.$bar; - var $rightside = $bar.find('.' + Toolbar.constants.rightside); - $bar.find('.' + Toolbar.constants.username); - $bar.find('.' + Toolbar.constants.editShare); - $bar.find('.' + Toolbar.constants.viewShare); - APP.$userNameButton = $($bar.find('.' + Toolbar.constants.changeUsername)); - - /* add a forget button */ - var forgetCb = function (err) { - if (err) { return; } - disconnect(); - }; - var $forgetPad = Cryptpad.createButton('forget', true, {}, forgetCb); - $rightside.append($forgetPad); - - // set the hash - if (!readOnly) { Cryptpad.replaceHash(editHash); } - - /* save as template */ - if (!Cryptpad.isTemplate(window.location.href)) { - var templateObj = { - rt: info.realtime, - Crypt: Cryptget, - getTitle: function () { return document.title; } - }; - var $templateButton = Cryptpad.createButton('template', true, templateObj); - $rightside.append($templateButton); - } - - Cryptpad.onDisplayNameChanged(setName); - }; + var $templateButton = Cryptpad.createButton('template', true, templateObj); + $rightside.append($templateButton); + } +}; // don't initialize until the store is ready. Cryptpad.ready(function () { diff --git a/www/slide/main.js b/www/slide/main.js index 31b91d768..2b229c14b 100644 --- a/www/slide/main.js +++ b/www/slide/main.js @@ -3,7 +3,7 @@ define([ '/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/textpatcher/TextPatcher.js', - '/common/toolbar.js', + '/common/toolbar2.js', 'json.sortify', '/bower_components/chainpad-json-validator/json-ot.js', '/common/cryptpad-common.js', @@ -167,37 +167,11 @@ define([ editor.setOption('readOnly', !bool); }; - var userData = module.userData = {}; // List of pretty name of all users (mapped with their server ID) - var userList; // List of users still connected to the channel (server IDs) - var addToUserData = function(data) { - var users = module.users; - 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); - } - }; + var UserList; var textColor; var backColor; - var myData = {}; - var myUserName = ''; // My "pretty name" - var myID; // My server ID - - var setMyID = function(info) { - myID = info.myID || null; - myUserName = myID; - }; - var config = { //initialState: Messages.codeInitialState, initialState: '{}', @@ -207,7 +181,6 @@ define([ validateKey: secret.keys.validateKey || undefined, readOnly: readOnly, crypto: Crypto.createEncryptor(secret.keys), - setMyID: setMyID, transformFunction: JsonOT.validate, network: Cryptpad.getNetwork() }; @@ -228,7 +201,7 @@ define([ var obj = { content: textValue, metadata: { - users: userData, + users: UserList.userData, defaultTitle: defaultName, slideOptions: slideOptions } @@ -264,28 +237,6 @@ define([ } }; - var setName = module.setName = function (newName) { - if (typeof(newName) !== 'string') { return; } - var myUserNameTemp = newName.trim(); - if(newName.trim().length > 32) { - myUserNameTemp = myUserNameTemp.substr(0, 32); - } - myUserName = myUserNameTemp; - myData[myID] = { - 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; - } - onLocal(); - }); - }; - var getHeadingText = function () { var lines = editor.getValue().split(/\n/); @@ -413,7 +364,7 @@ define([ if (json.metadata.users) { var userData = json.metadata.users; // Update the local user data - addToUserData(userData); + UserList.addToUserData(userData); } if (json.metadata.defaultTitle) { updateDefaultTitle(json.metadata.defaultTitle); @@ -508,13 +459,11 @@ define([ }; config.onInit = function (info) { - userList = info.userList; + UserList = Cryptpad.createUserList(info, config.onLocal, Cryptget, Cryptpad); var configTb = { - displayed: ['useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], - userData: userData, - readOnly: readOnly, - ifrw: ifrw, + displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], + userList: UserList.getToolbarConfig(), share: { secret: secret, channel: info.channel @@ -524,13 +473,16 @@ define([ defaultName: defaultName, suggestName: suggestName }, - common: Cryptpad + common: Cryptpad, + readOnly: readOnly, + ifrw: ifrw, + realtime: info.realtime, + network: info.network, + $container: $bar }; - toolbar = module.toolbar = Toolbar.create($bar, info.myID, info.realtime, info.getLag, info.userList, configTb); + toolbar = module.toolbar = Toolbar.create(configTb); - var $rightside = $bar.find('.' + Toolbar.constants.rightside); - $bar.find('.' + Toolbar.constants.username); - module.$userNameButton = $($bar.find('.' + Toolbar.constants.changeUsername)); + var $rightside = toolbar.$rightside; var editHash; @@ -733,8 +685,6 @@ define([ if (!window.location.hash || window.location.hash === '#') { Cryptpad.replaceHash(editHash); } - - Cryptpad.onDisplayNameChanged(setName); }; var unnotify = module.unnotify = function () { @@ -752,8 +702,6 @@ define([ }; config.onReady = function (info) { - module.users = info.userList.users; - if (module.realtime !== info.realtime) { var realtime = module.realtime = info.realtime; module.patchText = TextPatcher.create({ @@ -816,33 +764,11 @@ define([ Cryptpad.removeLoadingScreen(); setEditable(true); initializing = false; - //Cryptpad.log("Your document is ready"); onLocal(); // push local state to avoid parse errors later. - 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 he has edit rights - if (readOnly) { return; } - if (typeof(lastName) === 'string') { - setName(lastName); - } else { - myData[myID] = { - name: "", - uid: Cryptpad.getUid(), - }; - addToUserData(myData); - onLocal(); - module.$userNameButton.click(); - } - if (isNew) { - Cryptpad.selectTemplate('slide', info.realtime, Cryptget); - } - }); + + if (readOnly) { return; } + UserList.getLastName(toolbar.$userNameButton, isNew); }; var cursorToPos = function(cursor, oldText) { diff --git a/www/whiteboard/main.js b/www/whiteboard/main.js index d78419b41..a67229231 100644 --- a/www/whiteboard/main.js +++ b/www/whiteboard/main.js @@ -3,7 +3,7 @@ define([ '/api/config', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/chainpad-crypto/crypto.js', - '/common/toolbar.js', + '/common/toolbar2.js', '/bower_components/textpatcher/TextPatcher.amd.js', 'json.sortify', '/bower_components/chainpad-json-validator/json-ot.js', @@ -214,33 +214,7 @@ window.canvas = canvas; var $bar = $('#toolbar'); var parsedHash = Cryptpad.parsePadUrl(window.location.href); var defaultName = Cryptpad.getDefaultName(parsedHash); - var userData = module.userData = {}; // List of pretty name of all users (mapped with their server ID) - var userList; // List of users still connected to the channel (server IDs) - var addToUserData = function(data) { - var users = module.users; - 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); - } - }; - - var myData = {}; - var myUserName = ''; // My "pretty name" - var myID; // My server ID - - var setMyID = function(info) { - myID = info.myID || null; - myUserName = myID; - }; + var UserList; var config = module.config = { initialState: '{}', @@ -249,7 +223,6 @@ window.canvas = canvas; readOnly: readOnly, channel: secret.channel, crypto: Crypto.createEncryptor(secret.keys), - setMyID: setMyID, transformFunction: JsonOT.transform, }; @@ -331,29 +304,30 @@ window.canvas = canvas; }; config.onInit = function (info) { - userList = info.userList; - var config = { - displayed: ['useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], - userData: userData, - readOnly: readOnly, + UserList = Cryptpad.createUserList(info, config.onLocal, Cryptget, Cryptpad); + var configTb = { + displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], + userList: UserList.getToolbarConfig(), share: { secret: secret, channel: info.channel }, - ifrw: window, title: { onRename: renameCb, defaultName: defaultName, suggestName: suggestName }, - common: Cryptpad + common: Cryptpad, + readOnly: readOnly, + ifrw: window, + realtime: info.realtime, + network: info.network, + $container: $bar }; - if (readOnly) {delete config.changeNameID; } - toolbar = module.toolbar = Toolbar.create($bar, info.myID, info.realtime, info.getLag, userList, config); + toolbar = module.toolbar = Toolbar.create(configTb); - var $rightside = $bar.find('.' + Toolbar.constants.rightside); - module.$userNameButton = $($bar.find('.' + Toolbar.constants.changeUsername)); + var $rightside = toolbar.$rightside; /* save as template */ if (!Cryptpad.isTemplate(window.location.href)) { @@ -384,8 +358,6 @@ window.canvas = canvas; editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); } if (!readOnly) { Cryptpad.replaceHash(editHash); } - - Cryptpad.onDisplayNameChanged(module.setName); }; // used for debugging, feel free to remove @@ -431,7 +403,7 @@ window.canvas = canvas; if (json.metadata.users) { var userData = json.metadata.users; // Update the local user data - addToUserData(userData); + UserList.addToUserData(userData); } if (json.metadata.defaultTitle) { updateDefaultTitle(json.metadata.defaultTitle); @@ -486,7 +458,7 @@ window.canvas = canvas; var obj = { content: textValue, metadata: { - users: userData, + users: UserList.userData, palette: palette, defaultTitle: defaultName } @@ -508,28 +480,6 @@ window.canvas = canvas; module.patchText(content); }); - var setName = module.setName = function (newName) { - if (typeof(newName) !== 'string') { return; } - var myUserNameTemp = newName.trim(); - if(newName.trim().length > 32) { - myUserNameTemp = myUserNameTemp.substr(0, 32); - } - myUserName = myUserNameTemp; - myData[myID] = { - 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; - } - onLocal(); - }); - }; - config.onReady = function (info) { var realtime = module.realtime = info.realtime; module.patchText = TextPatcher.create({ @@ -550,30 +500,9 @@ window.canvas = canvas; } /* TODO: restore palette from metadata.palette */ - 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 he has edit rights - if (readOnly) { return; } - if (typeof(lastName) === 'string') { - setName(lastName); - } else { - myData[myID] = { - name: "", - uid: Cryptpad.getUid(), - }; - addToUserData(myData); - onLocal(); - module.$userNameButton.click(); - } - if (isNew) { - Cryptpad.selectTemplate('whiteboard', info.realtime, Cryptget); - } - }); + + if (readOnly) { return; } + UserList.getLastName(toolbar.$userNameButton, isNew); }; config.onAbort = function () { From 9206d895313228d296d4fe252932396d622efcda Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 5 May 2017 17:58:32 +0200 Subject: [PATCH 4/4] Fix lint errors --- www/common/common-userlist.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/www/common/common-userlist.js b/www/common/common-userlist.js index 46071779b..8ba7b31e1 100644 --- a/www/common/common-userlist.js +++ b/www/common/common-userlist.js @@ -1,6 +1,4 @@ -define([ - 'jquery', -], function ($) { +define(function () { var module = {}; module.create = function (info, onLocal, Cryptget, Cryptpad) {