diff --git a/customize.dist/fsStore.js b/customize.dist/fsStore.js index 026d62622..858c1d145 100644 --- a/customize.dist/fsStore.js +++ b/customize.dist/fsStore.js @@ -128,15 +128,19 @@ define([ return ret; }; - var onReady = function (f, proxy, storageKey, exp) { + var onReady = function (f, proxy, Cryptpad, exp) { var fo = FO.init(proxy.drive, { - storageKey: storageKey + storageKey: Cryptpad.storageKey }); //storeObj = proxy; store = initStore(fo, proxy, exp); if (typeof(f) === 'function') { f(void 0, store); } + proxy.on('change', [Cryptpad.displayNameKey], function (o, n, p) { + if (typeof(n) !== "string") { return; } + Cryptpad.changeDisplayName(n); + }); }; var initialized = false; @@ -200,12 +204,12 @@ define([ if (!drive[Cryptpad.storageKey] || !Cryptpad.isArray(drive[Cryptpad.storageKey])) { Cryptpad.getLegacyPads(function (err, data) { drive[Cryptpad.storageKey] = data; - onReady(f, rt.proxy, Cryptpad.storageKey, exp); + onReady(f, rt.proxy, Cryptpad, exp); }); return; } // Drive already exist: return the existing drive, don't load data from legacy store - onReady(f, rt.proxy, Cryptpad.storageKey, exp); + onReady(f, rt.proxy, Cryptpad, exp); }) .on('disconnect', function (info) { // We only manage errors during the loading screen here. Other websocket errors are handled by the apps diff --git a/www/code/main.js b/www/code/main.js index 83f5c92d0..e9dd9d8e4 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -221,17 +221,10 @@ define([ console.error(err); return; } - module.userName.lastName = myUserName; onLocal(); }); }; - var getLastName = function (cb) { - Cryptpad.getAttribute('username', function (err, userName) { - cb(err, userName || ''); - }); - }; - var getHeadingText = function () { var lines = editor.getValue().split(/\n/); @@ -377,13 +370,6 @@ define([ var onInit = config.onInit = function (info) { userList = info.userList; - module.userName = {}; - // The lastName is stored in an object passed to the toolbar so that when the user clicks on - // the "change display name" button, the prompt already knows his current name - getLastName(function (err, lastName) { - module.userName.lastName = lastName; - }); - var config = { displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], userData: userData, @@ -394,10 +380,6 @@ define([ defaultName: defaultName, suggestName: suggestName }, - userName: { - setName: setName, - lastName: module.userName - }, common: Cryptpad }; if (readOnly) {delete config.changeNameID; } @@ -526,6 +508,8 @@ define([ // set the hash if (!readOnly) { Cryptpad.replaceHash(editHash); } + + Cryptpad.onDisplayNameChanged(setName); }; var unnotify = module.unnotify = function () { @@ -592,7 +576,7 @@ define([ //Cryptpad.log("Your document is ready"); onLocal(); // push local state to avoid parse errors later. - getLastName(function (err, lastName) { + Cryptpad.getLastName(function (err, lastName) { if (err) { console.log("Could not get previous name"); console.error(err); @@ -601,7 +585,7 @@ define([ // Update the toolbar list: // Add the current user in the metadata if he has edit rights if (readOnly) { return; } - if (typeof(lastName) === 'string' && lastName.length) { + if (typeof(lastName) === 'string') { setName(lastName); } else { myData[myID] = { diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index d25142ae3..1097e4e55 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -71,6 +71,7 @@ define([ var userHashKey = common.userHashKey = 'User_hash'; var userNameKey = common.userNameKey = 'User_name'; var fileHashKey = common.fileHashKey = 'FS_hash'; + var displayNameKey = common.displayNameKey = 'cryptpad.username'; var login = common.login = function (hash, name, cb) { if (!hash) { throw new Error('expected a user hash'); } @@ -534,6 +535,25 @@ define([ }); }; + // STORAGE: Display Name + var getLastName = common.getLastName = function (cb) { + common.getAttribute('username', function (err, userName) { + cb(err, userName); + }); + }; + var _onDisplayNameChanged = []; + var onDisplayNameChanged = common.onDisplayNameChanged = function (h) { + if (typeof(h) !== "function") { return; } + if (_onDisplayNameChanged.indexOf(h) !== -1) { return; } + _onDisplayNameChanged.push(h); + }; + var changeDisplayName = common.changeDisplayName = function (newName) { + _onDisplayNameChanged.forEach(function (h) { + h(newName); + }); + }; + + // STORAGE var forgetPad = common.forgetPad = function (href, cb) { var parsed = parsePadUrl(href); diff --git a/www/common/toolbar.js b/www/common/toolbar.js index f2c285836..c1c70a1bb 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -375,11 +375,6 @@ define([ // User dropdown if (config.displayed.indexOf('useradmin') !== -1) { - if (!config.userName || !config.userName.setName || !config.userName.lastName) { - throw new Error("You must provide a `userName` object containing `setName` (function) " + - "and `lastName` (object) if you want to display the user admin menu."); - } - var userMenuCfg = { displayNameCls: USERNAME_CLS, changeNameButtonCls: USERBUTTON_CLS, @@ -393,11 +388,23 @@ define([ $userAdmin.attr('id', 'userDropdown'); $userContainer.append($userAdmin); - $userAdmin.find('a.' + USERBUTTON_CLS).click(function (e) { - Cryptpad.prompt(Messages.changeNamePrompt, config.userName.lastName.lastName || '', function (newName) { - config.userName.setName(newName); + var $userButton = $userAdmin.find('a.' + USERBUTTON_CLS); + var renameAlertOpened; + $userButton.click(function (e) { + e.preventDefault(); + e.stopPropagation(); + Cryptpad.getLastName(function (lastName) { + Cryptpad.prompt(Messages.changeNamePrompt, lastName || '', function (newName) { + if (newName === null && typeof(lastName) === "string") { return; } + if (newName === null) { newName = ''; } + Cryptpad.changeDisplayName(newName); + //config.userName.setName(newName); TODO + }); }); }); + Cryptpad.onDisplayNameChanged(function (newName) { + Cryptpad.findCancelButton().click(); + }); } return $userContainer; diff --git a/www/drive/main.js b/www/drive/main.js index c1e2a3021..18f4fe4f2 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -192,18 +192,9 @@ define([ // TOOLBAR - var getLastName = function (cb) { - Cryptpad.getAttribute('username', function (err, userName) { - cb(err, userName || ''); - }); - }; - - // Store the object sent for the "change username" button so that we can update the field value correctly - var userNameButtonObject = APP.userName = {}; /* add a "change username" button */ if (!APP.readOnly) { - getLastName(function (err, lastName) { - APP.userName.lastName = lastName; + Cryptpad.getLastName(function (err, lastName) { APP.$displayName.text(lastName || Messages.anonymous); }); } else { @@ -1852,12 +1843,11 @@ define([ logError("Couldn't set username", err); return; } - APP.userName.lastName = myUserName; APP.$displayName.text(myUserName); }); }; - // TODO: move that function and use a more generic API + // TODO: move that function and use a more generic API? var migrateAnonDrive = function (proxy, cb) { if (sessionStorage.migrateAnonDrive) { // Make sure we have an FS_hash and we don't use it, otherwise just stop the migration and cb @@ -1933,16 +1923,11 @@ define([ }); var userList = APP.userList = info.userList; - APP.userName = {}; var config = { displayed: ['useradmin', 'language', 'spinner', 'lag', 'state'], readOnly: readOnly, ifrw: window, common: Cryptpad, - userName: { - setName: setName, - lastName: APP.userName - }, hideShare: true }; var toolbar = APP.toolbar = info.realtime.toolbar = Toolbar.create(APP.$bar, info.myID, info.realtime, info.getLag, userList, config); @@ -1975,6 +1960,7 @@ define([ $userBlock.append($backupButton); } + Cryptpad.onDisplayNameChanged(setName); }; var onReady = function () { module.files = proxy; diff --git a/www/pad/main.js b/www/pad/main.js index 58f51d583..15a5abb47 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -302,12 +302,6 @@ define([ myID = info.myID || null; }; - var getLastName = function (cb) { - Cryptpad.getAttribute('username', function (err, userName) { - cb(err, userName || ''); - }); - }; - var setName = module.setName = function (newName) { if (typeof(newName) !== 'string') { return; } var myUserNameTemp = Cryptpad.fixHTML(newName.trim()); @@ -324,7 +318,6 @@ define([ console.error("Couldn't set username"); return; } - module.userName.lastName = myUserName; editor.fire('change'); }); }; @@ -580,13 +573,6 @@ define([ var onInit = realtimeOptions.onInit = function (info) { userList = info.userList; - module.userName = {}; - // The lastName is stored in an object passed to the toolbar so that when the user clicks on - // the "change display name" button, the prompt already knows his current name - getLastName(function (err, lastName) { - module.userName.lastName = lastName; - }); - var config = { displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], userData: userData, @@ -597,10 +583,6 @@ define([ defaultName: defaultName, suggestName: suggestName }, - userName: { - setName: setName, - lastName: module.userName - }, common: Cryptpad }; if (readOnly) {delete config.changeNameID; } @@ -619,7 +601,6 @@ define([ editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); } - // Expand / collapse the toolbar var $existingButton = $bar.find('#cke_1_toolbar_collapser'); var $collapse = Cryptpad.createButton(null, true); @@ -676,6 +657,8 @@ define([ // set the hash if (!readOnly) { Cryptpad.replaceHash(editHash); } + + Cryptpad.onDisplayNameChanged(setName); }; // this should only ever get called once, when the chain syncs @@ -711,7 +694,7 @@ define([ }); } - getLastName(function (err, lastName) { + Cryptpad.getLastName(function (err, lastName) { console.log("Unlocking editor"); setEditable(true); initializing = false; @@ -720,7 +703,7 @@ define([ // Update the toolbar list: // Add the current user in the metadata if he has edit rights if (readOnly) { return; } - if (typeof(lastName) === 'string' && lastName.length) { + if (typeof(lastName) === 'string') { setName(lastName); } else { myData[myID] = { diff --git a/www/poll/main.js b/www/poll/main.js index abb5f302d..85ebec555 100644 --- a/www/poll/main.js +++ b/www/poll/main.js @@ -436,13 +436,6 @@ define([ APP.proxy.info.userData = userData; }; - //var myData = {}; - var getLastName = function (cb) { - Cryptpad.getAttribute('username', function (err, userName) { - cb(err, userName || ''); - }); - }; - var setName = APP.setName = function (newName) { if (typeof(newName) !== 'string') { return; } var myUserNameTemp = Cryptpad.fixHTML(newName.trim()); @@ -461,8 +454,6 @@ define([ console.error("Couldn't set username"); return; } - APP.userName.lastName = myUserName; - //change(); }); }; @@ -636,7 +627,7 @@ define([ } - getLastName(function (err, lastName) { + Cryptpad.getLastName(function (err, lastName) { APP.ready = true; if (!proxy.published) { @@ -680,13 +671,6 @@ define([ userList = APP.userList = info.userList; - APP.userName = {}; - // The lastName is stored in an object passed to the toolbar so that when the user clicks on - // the "change display name" button, the prompt already knows his current name - getLastName(function (err, lastName) { - APP.userName.lastName = lastName; - }); - var config = { displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], userData: userData, @@ -696,10 +680,6 @@ define([ defaultName: defaultName, suggestName: suggestName }, - userName: { - setName: setName, - lastName: APP.userName - }, ifrw: window, common: Cryptpad, }; @@ -734,6 +714,8 @@ define([ // set the hash if (!readOnly) { Cryptpad.replaceHash(editHash); } + Cryptpad.onDisplayNameChanged(setName); + Cryptpad.getPadTitle(function (err, title) { if (err) { error(err); diff --git a/www/register/main.js b/www/register/main.js index b6277d13f..27086b338 100644 --- a/www/register/main.js +++ b/www/register/main.js @@ -117,6 +117,7 @@ define([ } proxy.login_name = uname; + proxy[Cryptpad.displayNameKey] = uname; Cryptpad.whenRealtimeSyncs(result.realtime, function () { Cryptpad.login(result.userHash, result.userName, function () { diff --git a/www/slide/main.js b/www/slide/main.js index 7c75a42ce..f9e9bd8c2 100644 --- a/www/slide/main.js +++ b/www/slide/main.js @@ -264,17 +264,10 @@ define([ console.error(err); return; } - module.userName.lastName = myUserName; onLocal(); }); }; - var getLastName = function (cb) { - Cryptpad.getAttribute('username', function (err, userName) { - cb(err, userName || ''); - }); - }; - var getHeadingText = function () { var lines = editor.getValue().split(/\n/); @@ -418,13 +411,6 @@ define([ var onInit = config.onInit = function (info) { userList = info.userList; - module.userName = {}; - // The lastName is stored in an object passed to the toolbar so that when the user clicks on - // the "change display name" button, the prompt already knows his current name - getLastName(function (err, lastName) { - module.userName.lastName = lastName; - }); - var config = { displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], userData: userData, @@ -435,10 +421,6 @@ define([ defaultName: defaultName, suggestName: suggestName }, - userName: { - setName: setName, - lastName: module.userName - }, common: Cryptpad }; if (readOnly) {delete config.changeNameID; } @@ -599,6 +581,8 @@ define([ if (!window.location.hash || window.location.hash === '#') { Cryptpad.replaceHash(editHash); } + + Cryptpad.onDisplayNameChanged(setName); }; var unnotify = module.unnotify = function () { @@ -673,7 +657,7 @@ define([ //Cryptpad.log("Your document is ready"); onLocal(); // push local state to avoid parse errors later. - getLastName(function (err, lastName) { + Cryptpad.getLastName(function (err, lastName) { if (err) { console.log("Could not get previous name"); console.error(err); @@ -682,7 +666,7 @@ define([ // Update the toolbar list: // Add the current user in the metadata if he has edit rights if (readOnly) { return; } - if (typeof(lastName) === 'string' && lastName.length) { + if (typeof(lastName) === 'string') { setName(lastName); } else { myData[myID] = {