From 87d86384ac7e63b766e2aadd3f6fe8079718e707 Mon Sep 17 00:00:00 2001
From: yflory <yann.flory@xwiki.com>
Date: Tue, 25 Oct 2016 17:29:13 +0200
Subject: [PATCH] Fix broken elements in the new toolbar

---
 customize.dist/messages.js                 |   4 +-
 customize.dist/src/toolbar.less            |   7 +-
 customize.dist/toolbar.css                 |   5 +-
 customize.dist/translations/messages.fr.js |   1 +
 customize.dist/translations/messages.js    |   5 +-
 www/code/main.js                           | 136 ++++++++++-----------
 www/common/toolbar.js                      |  23 +++-
 www/pad/main.js                            |  22 ++--
 www/slide/main.js                          |  25 ++--
 9 files changed, 129 insertions(+), 99 deletions(-)

diff --git a/customize.dist/messages.js b/customize.dist/messages.js
index a1ff01d8e..f1062ada3 100644
--- a/customize.dist/messages.js
+++ b/customize.dist/messages.js
@@ -22,9 +22,11 @@ define(['/customize/languageSelector.js',
         messages = $.extend(true, {}, Default, map[language]);
     }
 
+    // messages_languages return the available translations and their name in an object :
+    // { "en": "English", "fr": "French", ... }
     messages._languages = {
         'en': Default._languageName
-    }
+    };
     for (var l in map) {
         messages._languages[l] = map[l]._languageName || l;
     }
diff --git a/customize.dist/src/toolbar.less b/customize.dist/src/toolbar.less
index a674db185..ab517124f 100644
--- a/customize.dist/src/toolbar.less
+++ b/customize.dist/src/toolbar.less
@@ -152,11 +152,14 @@
         a.cryptpad-logo {
             cursor: pointer;
             height: 3em;
-            border: 1px solid #aaa;
-            border-radius: @border-radius;
             padding: 0px 5px;
             text-decoration: none;
             color: inherit;
+            &:hover {
+                span {
+                    text-decoration: underline;
+                }
+            }
             img {
                 vertical-align: middle;
                 height: 3em;
diff --git a/customize.dist/toolbar.css b/customize.dist/toolbar.css
index 5bf1dc0bf..39b060844 100644
--- a/customize.dist/toolbar.css
+++ b/customize.dist/toolbar.css
@@ -154,12 +154,13 @@
 .cryptpad-toolbar-top .cryptpad-link a.cryptpad-logo {
   cursor: pointer;
   height: 3em;
-  border: 1px solid #aaa;
-  border-radius: 1px;
   padding: 0px 5px;
   text-decoration: none;
   color: inherit;
 }
+.cryptpad-toolbar-top .cryptpad-link a.cryptpad-logo:hover span {
+  text-decoration: underline;
+}
 .cryptpad-toolbar-top .cryptpad-link a.cryptpad-logo img {
   vertical-align: middle;
   height: 3em;
diff --git a/customize.dist/translations/messages.fr.js b/customize.dist/translations/messages.fr.js
index 49faa6f2e..299bcd5b3 100644
--- a/customize.dist/translations/messages.fr.js
+++ b/customize.dist/translations/messages.fr.js
@@ -53,6 +53,7 @@ define(function () {
     out.renameButtonTitle = 'Changer le titre utilisé par ce document dans la page d\'accueil de Cryptpad';
     out.renamePrompt = 'Quel titre souhaitez-vous utiliser pour ce document ?';
     out.renameConflict = 'Un autre document existe déjà avec le même titre';
+    out.clickToEdit = 'Cliquer pour modifier';
 
     out.forgetButton = 'OUBLIER';
     out.forgetButtonTitle = 'Enlever ce document de la liste en page d\'accueil';
diff --git a/customize.dist/translations/messages.js b/customize.dist/translations/messages.js
index 1821cde81..69cb4a4e7 100644
--- a/customize.dist/translations/messages.js
+++ b/customize.dist/translations/messages.js
@@ -53,6 +53,7 @@ define(function () {
     out.renameButtonTitle = 'Change the title under which this document is listed on your home page';
     out.renamePrompt = 'How would you like to title this pad?';
     out.renameConflict = 'Another pad already has that title';
+    out.clickToEdit = "Click to edit";
 
     out.forgetButton = 'FORGET';
     out.forgetButtonTitle = 'Remove this document from your home page listings';
@@ -81,8 +82,8 @@ define(function () {
     out.readonlyUrl = 'Read only document';
     out.copyReadOnly = "Copy URL to clipboard";
     out.openReadOnly = "Open in a new tab";
-    out.editing = "editing";
-    out.viewing = "viewing";
+    out.editing = "editor(s)";
+    out.viewing = "viewer(s)";
     out.editShare = "Share";
     out.editShareTitle = "Copy the edit URL to clipboard";
     out.viewShare = "Share view URL";
diff --git a/www/code/main.js b/www/code/main.js
index 77ecd84be..f2db83352 100644
--- a/www/code/main.js
+++ b/www/code/main.js
@@ -164,30 +164,30 @@ define([
 
             var initializing = true;
 
+            var stringifyInner = function (textValue) {
+                var obj = {
+                    content: textValue,
+                    metadata: {
+                        users: userList,
+                        defaultTitle: defaultName
+                    }
+                };
+                obj.metadata.title = document.title;
+                // set mode too...
+                obj.highlightMode = module.highlightMode;
+
+                // stringify the json and send it into chainpad
+                return stringify(obj);
+            };
+
             var onLocal = config.onLocal = function () {
                 if (initializing) { return; }
                 if (readOnly) { return; }
 
                 editor.save();
+
                 var textValue = canonicalize($textarea.val());
-                var obj = {content: textValue};
-
-                // append the userlist to the hyperjson structure
-                obj.metadata = {
-                    users: userList
-                };
-                if (!isDefaultTitle()) {
-                    obj.metadata.title = document.title;
-                }
-                else {
-                    obj.metadata.title = "";
-                }
-
-                // set mode too...
-                obj.highlightMode = module.highlightMode;
-
-                // stringify the json and send it into chainpad
-                var shjson = stringify(obj);
+                var shjson = stringifyInner(textValue);
 
                 module.patchText(shjson);
 
@@ -262,8 +262,8 @@ define([
             };
 
             var suggestName = function () {
-                if (Cryptpad.isDefaultName(parsedHash, document.title)) {
-                    return getHeadingText() || defaultName;
+                if (document.title === defaultName) {
+                    return getHeadingText() || "";
                 } else {
                     return document.title || getHeadingText() || defaultName;
                 }
@@ -317,7 +317,48 @@ define([
                 onLocal();
             };
 
-            var onInit = config.onInit = function (info) {
+            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.renamePad(newTitle, function (err, data) {
+                    if (err) {
+                        console.log("Couldn't set pad title");
+                        console.error(err);
+                        document.title = oldTitle;
+                        return;
+                    }
+                    document.title = data;
+                    $bar.find('.' + Toolbar.constants.title).find('span').text(data);
+                    $bar.find('.' + Toolbar.constants.title).find('input').val(data);
+                });
+            };
+
+            var updateDefaultTitle = function (defaultTitle) {
+                defaultName = defaultTitle;
+                $bar.find('.' + Toolbar.constants.title).find('input').attr("placeholder", defaultName);
+            };
+
+            var updateMetadata = function(shjson) {
+                // Extract the user list (metadata) from the hyperjson
+                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.defaultTitle) {
+                        updateDefaultTitle(json.metadata.defaultTitle);
+                    }
+                    if (typeof json.metadata.title !== "undefined") {
+                        updateTitle(json.metadata.title);
+                    }
+                }
+            };
+
+             var onInit = config.onInit = function (info) {
                 toolbarList = info.userList;
                 var config = {
                     userData: userList,
@@ -325,7 +366,8 @@ define([
                     ifrw: ifrw,
                     title: {
                         onRename: renameCb,
-                        defaultName: defaultName
+                        defaultName: defaultName,
+                        suggestName: suggestName
                     },
                     common: Cryptpad
                 };
@@ -466,39 +508,6 @@ define([
                 });
             };
 
-            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.renamePad(newTitle, function (err, data) {
-                    if (err) {
-                        console.log("Couldn't set pad title");
-                        console.error(err);
-                        document.title = oldTitle;
-                        return;
-                    }
-                    document.title = data;
-                    $bar.find('.' + Toolbar.constants.title).find('span').text(data);
-                    $bar.find('.' + Toolbar.constants.title).find('input').val(data);
-                });
-            };
-
-            var updateMetadata = function(shjson) {
-                // Extract the user list (metadata) from the hyperjson
-                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 (typeof json.metadata.title !== "undefined") {
-                        updateTitle(json.metadata.title);
-                    }
-                }
-            };
-
             var unnotify = module.unnotify = function () {
                 if (module.tabNotification &&
                     typeof(module.tabNotification.cancel) === 'function') {
@@ -645,21 +654,8 @@ define([
                 editor.scrollTo(scroll.left, scroll.top);
 
                 if (!readOnly) {
-                    var localDoc = canonicalize($textarea.val());
-                    var hjson2 = {
-                      content: localDoc,
-                      metadata: {
-                          users: userList
-                      },
-                      highlightMode: highlightMode,
-                    };
-                    if (!isDefaultTitle()) {
-                        hjson2.metadata.title = document.title;
-                    }
-                    else {
-                        hjson2.metadata.title = "";
-                    }
-                    var shjson2 = stringify(hjson2);
+                    var textValue = canonicalize($textarea.val());
+                    var shjson2 = stringifyInner(textValue);
                     if (shjson2 !== shjson) {
                         console.error("shjson2 !== shjson");
                         TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2));
diff --git a/www/common/toolbar.js b/www/common/toolbar.js
index 117c07dcc..e342a9241 100644
--- a/www/common/toolbar.js
+++ b/www/common/toolbar.js
@@ -313,14 +313,23 @@ define([
             alt: "Cryptpad",
             'class': "cryptofist"
         });
+
+        // We need to override the "a" tag action here because it is inside the iframe!
         var $aTagSmall = $('<a>', {
             href: "/",
             title: Messages.header_logoTitle,
             'class': "cryptpad-logo"
         }).append($imgTag);
-        $span = $('<span>').text('CryptPad');
+        var $span = $('<span>').text('CryptPad');
         var $aTagBig = $aTagSmall.clone().addClass('big').append($span);
         $aTagSmall.addClass('small');
+        var onClick = function (e) {
+            e.preventDefault();
+            window.location = "/";
+        };
+
+        $aTagBig.click(onClick);
+        $aTagSmall.click(onClick);
 
         $linkContainer.append($aTagSmall).append($aTagBig);
     };
@@ -360,13 +369,14 @@ define([
         config = config || {};
         var callback = config.onRename;
         var placeholder = config.defaultName;
+        var suggestName = config.suggestName;
 
         var $titleContainer = $('<span>', {
             id: 'toolbarTitle',
             'class': TITLE_CLS
         }).appendTo($container);
         var $text = $('<span>', {
-            title: "CLick to edit" //TODO translate
+            title: Messages.clickToEdit
         }).appendTo($titleContainer);
         if (readOnly === 1 || typeof(Cryptpad) === "undefined") { return; }
         var $input = $('<input>', {
@@ -383,7 +393,10 @@ define([
         $input.on('keyup', function (e) {
             if (e.which === 13) {
                 var name = $input.val().trim();
-                Cryptpad.renamePad($input.val(), function (err, newtitle) {
+                if (name === "") {
+                    name = $input.attr('placeholder');
+                }
+                Cryptpad.renamePad(name, function (err, newtitle) {
                     if (err) { return; }
                     $text.text(newtitle);
                     callback(null, newtitle);
@@ -398,7 +411,8 @@ define([
         });
         $text.on('click', function () {
             $text.hide();
-            $input.val($text.text());
+            var inputVal = suggestName() || "";
+            $input.val(inputVal);
             $input.show();
             $input.focus();
         });
@@ -444,6 +458,7 @@ define([
             if (config.ifrw.$('iframe').length) {
                 var innerIfrw = config.ifrw.$('iframe').each(function (i, el) {
                     $(el.contentWindow).on('click', removeDropdowns);
+                    $(el.contentWindow).on('click', cancelEditTitle);
                 });
             }
         }
diff --git a/www/pad/main.js b/www/pad/main.js
index 0ca608b21..bfe74e7b6 100644
--- a/www/pad/main.js
+++ b/www/pad/main.js
@@ -321,8 +321,8 @@ define([
             };
 
             var suggestName = function () {
-                if (Cryptpad.isDefaultName(parsedHash, document.title)) {
-                    return getHeadingText() || defaultName;
+                if (document.title === defaultName) {
+                    return getHeadingText() || "";
                 } else {
                     return document.title || getHeadingText() || defaultName;
                 }
@@ -346,13 +346,10 @@ define([
                 hjson[3] = {
                     metadata: {
                         users: userList,
+                        defaultTitle: defaultName
                     }
                 };
-                if (!isDefaultTitle()) {
-                    hjson[3].metadata.title = document.title;
-                } else {
-                    hjson[3].metadata.title = "";
-                }
+                hjson[3].metadata.title = document.title;
                 return stringify(hjson);
             };
 
@@ -411,6 +408,11 @@ define([
                 });
             };
 
+            var updateDefaultTitle = function (defaultTitle) {
+                defaultName = defaultTitle;
+                $bar.find('.' + Toolbar.constants.title).find('input').attr("placeholder", defaultName);
+            };
+
             var updateMetadata = function(shjson) {
                 // Extract the user list (metadata) from the hyperjson
                 var hjson = JSON.parse(shjson);
@@ -421,6 +423,9 @@ define([
                         // Update the local user data
                         addToUserList(userData);
                     }
+                    if (peerMetadata.metadata.defaultTitle) {
+                        updateDefaultTitle(peerMetadata.metadata.defaultTitle);
+                    }
                     if (typeof peerMetadata.metadata.title !== "undefined") {
                         updateTitle(peerMetadata.metadata.title);
                     }
@@ -532,7 +537,8 @@ define([
                     ifrw: ifrw,
                     title: {
                         onRename: renameCb,
-                        defaultName: defaultName
+                        defaultName: defaultName,
+                        suggestName: suggestName
                     },
                     common: Cryptpad
                 };
diff --git a/www/slide/main.js b/www/slide/main.js
index 3e48d745a..31a09a1a6 100644
--- a/www/slide/main.js
+++ b/www/slide/main.js
@@ -208,15 +208,11 @@ define([
                 var obj = {
                     content: textValue,
                     metadata: {
-                        users: userList
+                        users: userList,
+                        defaultTitle: defaultName
                     }
                 };
-                if (!isDefaultTitle()) {
-                    obj.metadata.title = APP.title;
-                }
-                else {
-                    obj.metadata.title = "";
-                }
+                obj.metadata.title = APP.title;
                 if (textColor) {
                     obj.metadata.color = textColor;
                 }
@@ -292,8 +288,8 @@ define([
             };
 
             var suggestName = function () {
-                if (Cryptpad.isDefaultName(parsedHash, APP.title)) {
-                    return getHeadingText() || defaultName;
+                if (APP.title === defaultName) {
+                    return getHeadingText() || "";
                 } else {
                     return APP.title || getHeadingText() || defaultName;
                 }
@@ -376,6 +372,11 @@ define([
                 }
             };
 
+            var updateDefaultTitle = function (defaultTitle) {
+                defaultName = defaultTitle;
+                $bar.find('.' + Toolbar.constants.title).find('input').attr("placeholder", defaultName);
+            };
+
             var updateMetadata = function(shjson) {
                 // Extract the user list (metadata) from the hyperjson
                 var json = (shjson === "") ? "" : JSON.parse(shjson);
@@ -385,6 +386,9 @@ define([
                         // Update the local user data
                         addToUserList(userData);
                     }
+                    if (json.metadata.defaultTitle) {
+                        updateDefaultTitle(json.metadata.defaultTitle);
+                    }
                     if (typeof json.metadata.title !== "undefined") {
                         updateTitle(json.metadata.title);
                     }
@@ -407,7 +411,8 @@ define([
                     ifrw: ifrw,
                     title: {
                         onRename: renameCb,
-                        defaultName: defaultName
+                        defaultName: defaultName,
+                        suggestName: suggestName
                     },
                     common: Cryptpad
                 };