diff --git a/www/code/main.js b/www/code/main.js index 764ff6698..524726c8a 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -150,7 +150,10 @@ define([ var obj = {content: textValue}; // append the userlist to the hyperjson structure - obj.metadata = userList; + obj.metadata = { + users: userList, + title: document.title + }; // set mode too... obj.highlightMode = module.highlightMode; @@ -355,6 +358,7 @@ define([ return; } document.title = title; + onLocal(); }); }); }); @@ -461,13 +465,33 @@ define([ }); }; - var updateUserList = function(shjson) { + var updateTitle = function (newTitle) { + if (newTitle === document.title) { return; } + // Change the title now, and set it back to the old value if there is an error + var oldTitle = document.title; + document.title = newTitle; + Cryptpad.setPadTitle(newTitle, function (err, data) { + if (err) { + console.log("Couldn't set pad title"); + console.error(err); + document.title = oldTitle; + return; + } + }); + }; + + var updateMetadata = function(shjson) { // Extract the user list (metadata) from the hyperjson - var hjson = (shjson === "") ? "" : JSON.parse(shjson); - if(hjson && hjson.metadata) { - var userData = hjson.metadata; - // Update the local user data - addToUserList(userData); + var json = (shjson === "") ? "" : JSON.parse(shjson); + if (json && json.metadata) { + if (json.metadata.users) { + var userData = json.metadata.users; + // Update the local user data + addToUserList(userData); + } + if (json.metadata.title) { + updateTitle(json.metadata.title); + } } }; @@ -572,7 +596,7 @@ define([ var shjson = module.realtime.getUserDoc(); // Update the user list (metadata) from the hyperjson - updateUserList(shjson); + updateMetadata(shjson); var hjson = JSON.parse(shjson); var remoteDoc = hjson.content; @@ -607,7 +631,10 @@ define([ var localDoc = canonicalize($textarea.val()); var hjson2 = { content: localDoc, - metadata: userList, + metadata: { + users: userList, + title: document.title + }, highlightMode: highlightMode, }; var shjson2 = stringify(hjson2); diff --git a/www/pad/main.js b/www/pad/main.js index fac663e51..b2174a0ce 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -303,7 +303,12 @@ define([ var stringifyDOM = module.stringifyDOM = function (dom) { var hjson = Hyperjson.fromDOM(dom, isNotMagicLine, brFilter); - hjson[3] = {metadata: userList}; + hjson[3] = { + metadata: { + users: userList, + title: document.title + } + }; return stringify(hjson); }; @@ -343,15 +348,34 @@ define([ } }; - var updateUserList = function(shjson) { + var updateTitle = function (newTitle) { + if (newTitle === document.title) { return; } + // Change the title now, and set it back to the old value if there is an error + var oldTitle = document.title; + document.title = newTitle; + Cryptpad.setPadTitle(newTitle, function (err, data) { + if (err) { + console.log("Couldn't set pad title"); + console.error(err); + document.title = oldTitle; + return; + } + }); + }; + + var updateMetadata = function(shjson) { // Extract the user list (metadata) from the hyperjson var hjson = JSON.parse(shjson); - var peerUserList = hjson[3]; - if(peerUserList && peerUserList.metadata) { - var userData = peerUserList.metadata; - // Update the local user data - addToUserList(userData); - hjson.pop(); + var peerMetadata = hjson[3]; + if (peerMetadata && peerMetadata.metadata) { + if (peerMetadata.metadata.users) { + var userData = peerMetadata.metadata.users; + // Update the local user data + addToUserList(userData); + } + if (peerMetadata.metadata.title) { + updateTitle(peerMetadata.metadata.title); + } } }; @@ -378,7 +402,7 @@ define([ cursor.update(); // Update the user list (metadata) from the hyperjson - updateUserList(shjson); + updateMetadata(shjson); // build a dom from HJSON, diff, and patch the editor applyHjson(shjson); @@ -519,6 +543,7 @@ define([ return; } document.title = title; + editor.fire('change'); }); }); }); diff --git a/www/poll/main.js b/www/poll/main.js index 9636beb4a..f79d1c6ee 100644 --- a/www/poll/main.js +++ b/www/poll/main.js @@ -604,6 +604,21 @@ define([ break; } }) + .on('change', ['metadata'], function (o, n, p) { + var newTitle = n.title; + if (newTitle === document.title) { return; } + // Change the title now, and set it back to the old value if there is an error + var oldTitle = document.title; + document.title = newTitle; + Cryptpad.setPadTitle(newTitle, function (err, data) { + if (err) { + console.log("Couldn't set pad title"); + console.error(err); + document.title = oldTitle; + return; + } + }); + }) .on('remove', [], function (o, p, root) { //console.log("remove: (%s, [%s])", o, p.join(', ')); //console.log(p, o, p.length); @@ -711,6 +726,13 @@ define([ return; } document.title = title; + var proxy = module.rt.proxy; + if (proxy.metadata) { + proxy.metadata.title = title; + } + else { + proxy.metadata = {title: title}; + } }); }); }); @@ -838,7 +860,7 @@ define([ // don't initialize until the store is ready. Cryptpad.ready(function () { - var rt = module.rt = Listmap.create(config); + var rt = window.rt = module.rt = Listmap.create(config); rt.proxy.on('create', function (info) { var realtime = module.realtime = info.realtime; window.location.hash = Cryptpad.getHashFromKeys(info.channel, secret.key);