Update display name in realtime across tabs and remember 'anonymous' name

pull/1/head
yflory 8 years ago
parent 7762ffcbff
commit e4dbe97d71

@ -128,15 +128,19 @@ define([
return ret; return ret;
}; };
var onReady = function (f, proxy, storageKey, exp) { var onReady = function (f, proxy, Cryptpad, exp) {
var fo = FO.init(proxy.drive, { var fo = FO.init(proxy.drive, {
storageKey: storageKey storageKey: Cryptpad.storageKey
}); });
//storeObj = proxy; //storeObj = proxy;
store = initStore(fo, proxy, exp); store = initStore(fo, proxy, exp);
if (typeof(f) === 'function') { if (typeof(f) === 'function') {
f(void 0, store); f(void 0, store);
} }
proxy.on('change', [Cryptpad.displayNameKey], function (o, n, p) {
if (typeof(n) !== "string") { return; }
Cryptpad.changeDisplayName(n);
});
}; };
var initialized = false; var initialized = false;
@ -200,12 +204,12 @@ define([
if (!drive[Cryptpad.storageKey] || !Cryptpad.isArray(drive[Cryptpad.storageKey])) { if (!drive[Cryptpad.storageKey] || !Cryptpad.isArray(drive[Cryptpad.storageKey])) {
Cryptpad.getLegacyPads(function (err, data) { Cryptpad.getLegacyPads(function (err, data) {
drive[Cryptpad.storageKey] = data; drive[Cryptpad.storageKey] = data;
onReady(f, rt.proxy, Cryptpad.storageKey, exp); onReady(f, rt.proxy, Cryptpad, exp);
}); });
return; return;
} }
// Drive already exist: return the existing drive, don't load data from legacy store // 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) { .on('disconnect', function (info) {
// We only manage errors during the loading screen here. Other websocket errors are handled by the apps // We only manage errors during the loading screen here. Other websocket errors are handled by the apps

@ -221,17 +221,10 @@ define([
console.error(err); console.error(err);
return; return;
} }
module.userName.lastName = myUserName;
onLocal(); onLocal();
}); });
}; };
var getLastName = function (cb) {
Cryptpad.getAttribute('username', function (err, userName) {
cb(err, userName || '');
});
};
var getHeadingText = function () { var getHeadingText = function () {
var lines = editor.getValue().split(/\n/); var lines = editor.getValue().split(/\n/);
@ -377,13 +370,6 @@ define([
var onInit = config.onInit = function (info) { var onInit = config.onInit = function (info) {
userList = info.userList; 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 = { var config = {
displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'],
userData: userData, userData: userData,
@ -394,10 +380,6 @@ define([
defaultName: defaultName, defaultName: defaultName,
suggestName: suggestName suggestName: suggestName
}, },
userName: {
setName: setName,
lastName: module.userName
},
common: Cryptpad common: Cryptpad
}; };
if (readOnly) {delete config.changeNameID; } if (readOnly) {delete config.changeNameID; }
@ -526,6 +508,8 @@ define([
// set the hash // set the hash
if (!readOnly) { Cryptpad.replaceHash(editHash); } if (!readOnly) { Cryptpad.replaceHash(editHash); }
Cryptpad.onDisplayNameChanged(setName);
}; };
var unnotify = module.unnotify = function () { var unnotify = module.unnotify = function () {
@ -592,7 +576,7 @@ define([
//Cryptpad.log("Your document is ready"); //Cryptpad.log("Your document is ready");
onLocal(); // push local state to avoid parse errors later. onLocal(); // push local state to avoid parse errors later.
getLastName(function (err, lastName) { Cryptpad.getLastName(function (err, lastName) {
if (err) { if (err) {
console.log("Could not get previous name"); console.log("Could not get previous name");
console.error(err); console.error(err);
@ -601,7 +585,7 @@ define([
// Update the toolbar list: // Update the toolbar list:
// Add the current user in the metadata if he has edit rights // Add the current user in the metadata if he has edit rights
if (readOnly) { return; } if (readOnly) { return; }
if (typeof(lastName) === 'string' && lastName.length) { if (typeof(lastName) === 'string') {
setName(lastName); setName(lastName);
} else { } else {
myData[myID] = { myData[myID] = {

@ -71,6 +71,7 @@ define([
var userHashKey = common.userHashKey = 'User_hash'; var userHashKey = common.userHashKey = 'User_hash';
var userNameKey = common.userNameKey = 'User_name'; var userNameKey = common.userNameKey = 'User_name';
var fileHashKey = common.fileHashKey = 'FS_hash'; var fileHashKey = common.fileHashKey = 'FS_hash';
var displayNameKey = common.displayNameKey = 'cryptpad.username';
var login = common.login = function (hash, name, cb) { var login = common.login = function (hash, name, cb) {
if (!hash) { throw new Error('expected a user hash'); } 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 // STORAGE
var forgetPad = common.forgetPad = function (href, cb) { var forgetPad = common.forgetPad = function (href, cb) {
var parsed = parsePadUrl(href); var parsed = parsePadUrl(href);

@ -375,11 +375,6 @@ define([
// User dropdown // User dropdown
if (config.displayed.indexOf('useradmin') !== -1) { 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 = { var userMenuCfg = {
displayNameCls: USERNAME_CLS, displayNameCls: USERNAME_CLS,
changeNameButtonCls: USERBUTTON_CLS, changeNameButtonCls: USERBUTTON_CLS,
@ -393,10 +388,22 @@ define([
$userAdmin.attr('id', 'userDropdown'); $userAdmin.attr('id', 'userDropdown');
$userContainer.append($userAdmin); $userContainer.append($userAdmin);
$userAdmin.find('a.' + USERBUTTON_CLS).click(function (e) { var $userButton = $userAdmin.find('a.' + USERBUTTON_CLS);
Cryptpad.prompt(Messages.changeNamePrompt, config.userName.lastName.lastName || '', function (newName) { var renameAlertOpened;
config.userName.setName(newName); $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();
}); });
} }

@ -192,18 +192,9 @@ define([
// TOOLBAR // 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 */ /* add a "change username" button */
if (!APP.readOnly) { if (!APP.readOnly) {
getLastName(function (err, lastName) { Cryptpad.getLastName(function (err, lastName) {
APP.userName.lastName = lastName;
APP.$displayName.text(lastName || Messages.anonymous); APP.$displayName.text(lastName || Messages.anonymous);
}); });
} else { } else {
@ -1852,12 +1843,11 @@ define([
logError("Couldn't set username", err); logError("Couldn't set username", err);
return; return;
} }
APP.userName.lastName = myUserName;
APP.$displayName.text(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) { var migrateAnonDrive = function (proxy, cb) {
if (sessionStorage.migrateAnonDrive) { if (sessionStorage.migrateAnonDrive) {
// Make sure we have an FS_hash and we don't use it, otherwise just stop the migration and cb // 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; var userList = APP.userList = info.userList;
APP.userName = {};
var config = { var config = {
displayed: ['useradmin', 'language', 'spinner', 'lag', 'state'], displayed: ['useradmin', 'language', 'spinner', 'lag', 'state'],
readOnly: readOnly, readOnly: readOnly,
ifrw: window, ifrw: window,
common: Cryptpad, common: Cryptpad,
userName: {
setName: setName,
lastName: APP.userName
},
hideShare: true hideShare: true
}; };
var toolbar = APP.toolbar = info.realtime.toolbar = Toolbar.create(APP.$bar, info.myID, info.realtime, info.getLag, userList, config); 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); $userBlock.append($backupButton);
} }
Cryptpad.onDisplayNameChanged(setName);
}; };
var onReady = function () { var onReady = function () {
module.files = proxy; module.files = proxy;

@ -302,12 +302,6 @@ define([
myID = info.myID || null; myID = info.myID || null;
}; };
var getLastName = function (cb) {
Cryptpad.getAttribute('username', function (err, userName) {
cb(err, userName || '');
});
};
var setName = module.setName = function (newName) { var setName = module.setName = function (newName) {
if (typeof(newName) !== 'string') { return; } if (typeof(newName) !== 'string') { return; }
var myUserNameTemp = Cryptpad.fixHTML(newName.trim()); var myUserNameTemp = Cryptpad.fixHTML(newName.trim());
@ -324,7 +318,6 @@ define([
console.error("Couldn't set username"); console.error("Couldn't set username");
return; return;
} }
module.userName.lastName = myUserName;
editor.fire('change'); editor.fire('change');
}); });
}; };
@ -580,13 +573,6 @@ define([
var onInit = realtimeOptions.onInit = function (info) { var onInit = realtimeOptions.onInit = function (info) {
userList = info.userList; 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 = { var config = {
displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'],
userData: userData, userData: userData,
@ -597,10 +583,6 @@ define([
defaultName: defaultName, defaultName: defaultName,
suggestName: suggestName suggestName: suggestName
}, },
userName: {
setName: setName,
lastName: module.userName
},
common: Cryptpad common: Cryptpad
}; };
if (readOnly) {delete config.changeNameID; } if (readOnly) {delete config.changeNameID; }
@ -619,7 +601,6 @@ define([
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
} }
// Expand / collapse the toolbar // Expand / collapse the toolbar
var $existingButton = $bar.find('#cke_1_toolbar_collapser'); var $existingButton = $bar.find('#cke_1_toolbar_collapser');
var $collapse = Cryptpad.createButton(null, true); var $collapse = Cryptpad.createButton(null, true);
@ -676,6 +657,8 @@ define([
// set the hash // set the hash
if (!readOnly) { Cryptpad.replaceHash(editHash); } if (!readOnly) { Cryptpad.replaceHash(editHash); }
Cryptpad.onDisplayNameChanged(setName);
}; };
// this should only ever get called once, when the chain syncs // 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"); console.log("Unlocking editor");
setEditable(true); setEditable(true);
initializing = false; initializing = false;
@ -720,7 +703,7 @@ define([
// Update the toolbar list: // Update the toolbar list:
// Add the current user in the metadata if he has edit rights // Add the current user in the metadata if he has edit rights
if (readOnly) { return; } if (readOnly) { return; }
if (typeof(lastName) === 'string' && lastName.length) { if (typeof(lastName) === 'string') {
setName(lastName); setName(lastName);
} else { } else {
myData[myID] = { myData[myID] = {

@ -436,13 +436,6 @@ define([
APP.proxy.info.userData = userData; 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) { var setName = APP.setName = function (newName) {
if (typeof(newName) !== 'string') { return; } if (typeof(newName) !== 'string') { return; }
var myUserNameTemp = Cryptpad.fixHTML(newName.trim()); var myUserNameTemp = Cryptpad.fixHTML(newName.trim());
@ -461,8 +454,6 @@ define([
console.error("Couldn't set username"); console.error("Couldn't set username");
return; return;
} }
APP.userName.lastName = myUserName;
//change();
}); });
}; };
@ -636,7 +627,7 @@ define([
} }
getLastName(function (err, lastName) { Cryptpad.getLastName(function (err, lastName) {
APP.ready = true; APP.ready = true;
if (!proxy.published) { if (!proxy.published) {
@ -680,13 +671,6 @@ define([
userList = APP.userList = info.userList; 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 = { var config = {
displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'],
userData: userData, userData: userData,
@ -696,10 +680,6 @@ define([
defaultName: defaultName, defaultName: defaultName,
suggestName: suggestName suggestName: suggestName
}, },
userName: {
setName: setName,
lastName: APP.userName
},
ifrw: window, ifrw: window,
common: Cryptpad, common: Cryptpad,
}; };
@ -734,6 +714,8 @@ define([
// set the hash // set the hash
if (!readOnly) { Cryptpad.replaceHash(editHash); } if (!readOnly) { Cryptpad.replaceHash(editHash); }
Cryptpad.onDisplayNameChanged(setName);
Cryptpad.getPadTitle(function (err, title) { Cryptpad.getPadTitle(function (err, title) {
if (err) { if (err) {
error(err); error(err);

@ -117,6 +117,7 @@ define([
} }
proxy.login_name = uname; proxy.login_name = uname;
proxy[Cryptpad.displayNameKey] = uname;
Cryptpad.whenRealtimeSyncs(result.realtime, function () { Cryptpad.whenRealtimeSyncs(result.realtime, function () {
Cryptpad.login(result.userHash, result.userName, function () { Cryptpad.login(result.userHash, result.userName, function () {

@ -264,17 +264,10 @@ define([
console.error(err); console.error(err);
return; return;
} }
module.userName.lastName = myUserName;
onLocal(); onLocal();
}); });
}; };
var getLastName = function (cb) {
Cryptpad.getAttribute('username', function (err, userName) {
cb(err, userName || '');
});
};
var getHeadingText = function () { var getHeadingText = function () {
var lines = editor.getValue().split(/\n/); var lines = editor.getValue().split(/\n/);
@ -418,13 +411,6 @@ define([
var onInit = config.onInit = function (info) { var onInit = config.onInit = function (info) {
userList = info.userList; 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 = { var config = {
displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'], displayed: ['useradmin', 'language', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad'],
userData: userData, userData: userData,
@ -435,10 +421,6 @@ define([
defaultName: defaultName, defaultName: defaultName,
suggestName: suggestName suggestName: suggestName
}, },
userName: {
setName: setName,
lastName: module.userName
},
common: Cryptpad common: Cryptpad
}; };
if (readOnly) {delete config.changeNameID; } if (readOnly) {delete config.changeNameID; }
@ -599,6 +581,8 @@ define([
if (!window.location.hash || window.location.hash === '#') { if (!window.location.hash || window.location.hash === '#') {
Cryptpad.replaceHash(editHash); Cryptpad.replaceHash(editHash);
} }
Cryptpad.onDisplayNameChanged(setName);
}; };
var unnotify = module.unnotify = function () { var unnotify = module.unnotify = function () {
@ -673,7 +657,7 @@ define([
//Cryptpad.log("Your document is ready"); //Cryptpad.log("Your document is ready");
onLocal(); // push local state to avoid parse errors later. onLocal(); // push local state to avoid parse errors later.
getLastName(function (err, lastName) { Cryptpad.getLastName(function (err, lastName) {
if (err) { if (err) {
console.log("Could not get previous name"); console.log("Could not get previous name");
console.error(err); console.error(err);
@ -682,7 +666,7 @@ define([
// Update the toolbar list: // Update the toolbar list:
// Add the current user in the metadata if he has edit rights // Add the current user in the metadata if he has edit rights
if (readOnly) { return; } if (readOnly) { return; }
if (typeof(lastName) === 'string' && lastName.length) { if (typeof(lastName) === 'string') {
setName(lastName); setName(lastName);
} else { } else {
myData[myID] = { myData[myID] = {

Loading…
Cancel
Save