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;
};
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

@ -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] = {

@ -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);

@ -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;

@ -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;

@ -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] = {

@ -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);

@ -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 () {

@ -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] = {

Loading…
Cancel
Save