From 63710e1cccba59dc84dfa89b2fe01fb1b1204a1b Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 9 May 2017 12:45:18 +0200 Subject: [PATCH] Move the code related to the title in another file --- www/code/main.js | 82 ++++++++--------------------------- www/common/common-title.js | 82 +++++++++++++++++++++++++++++++++++ www/common/cryptpad-common.js | 6 ++- 3 files changed, 106 insertions(+), 64 deletions(-) create mode 100644 www/common/common-title.js diff --git a/www/code/main.js b/www/code/main.js index c382b38e4..a21c67273 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -48,8 +48,6 @@ define([ var $textarea = $pad.contents().find('#editor1'); var $bar = $('#pad-iframe')[0].contentWindow.$('#cme_toolbox'); - var parsedHash = Cryptpad.parsePadUrl(window.location.href); - var defaultName = Cryptpad.getDefaultName(parsedHash); var isHistoryMode = false; @@ -120,6 +118,7 @@ define([ editor.setOption('readOnly', !bool); }; + var Title; var UserList; var config = { @@ -144,11 +143,6 @@ define([ } }; -/* var isDefaultTitle = function () { - var parsed = Cryptpad.parsePadUrl(window.location.href); - return Cryptpad.isDefaultName(parsed, document.title); - };*/ - var initializing = true; var stringifyInner = function (textValue) { @@ -156,11 +150,11 @@ define([ content: textValue, metadata: { users: UserList.userData, - defaultTitle: defaultName + defaultTitle: Title.defaultTitle } }; if (!initializing) { - obj.metadata.title = document.title; + obj.metadata.title = Title.title; } // set mode too... obj.highlightMode = module.highlightMode; @@ -226,20 +220,12 @@ define([ return text.trim(); }; - var suggestName = function (fallback) { - if (document.title === defaultName) { - return getHeadingText() || fallback || ""; - } else { - return document.title || getHeadingText() || defaultName; - } - }; - var exportText = module.exportText = function () { var text = editor.getValue(); var ext = Modes.extensionOf(module.highlightMode); - var title = Cryptpad.fixFileName(suggestName('cryptpad')) + (ext || '.txt'); + var title = Cryptpad.fixFileName(Title.suggestTitle('cryptpad')) + (ext || '.txt'); Cryptpad.prompt(Messages.exportPrompt, title, function (filename) { if (filename === null) { return; } @@ -276,35 +262,6 @@ define([ onLocal(); }; - var renameCb = function (err, title) { - if (err) { return; } - document.title = title; - onLocal(); - }; - - 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.title').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); @@ -316,21 +273,27 @@ define([ UserList.addToUserData(userData); } if (json.metadata.defaultTitle) { - updateDefaultTitle(json.metadata.defaultTitle); + Title.updateDefaultTitle(json.metadata.defaultTitle); } if (typeof json.metadata.title !== "undefined") { - updateTitle(json.metadata.title || defaultName); + Title.updateTitle(json.metadata.title || Title.defaultTitle); titleUpdated = true; } } if (!titleUpdated) { - updateTitle(defaultName); + Title.updateTitle(Title.defaultTitle); } }; config.onInit = function (info) { UserList = Cryptpad.createUserList(info, config.onLocal, Cryptget, Cryptpad); + var titleCfg = { + $bar: $bar, + getHeadingText: getHeadingText + }; + Title = Cryptpad.createTitle(titleCfg, config.onLocal, Cryptpad); + var configTb = { displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'], userList: UserList.getToolbarConfig(), @@ -338,11 +301,7 @@ define([ secret: secret, channel: info.channel }, - title: { - onRename: renameCb, - defaultName: defaultName, - suggestName: suggestName - }, + title: Title.getTitleConfig(), common: Cryptpad, readOnly: readOnly, ifrw: ifrw, @@ -352,6 +311,8 @@ define([ }; toolbar = module.toolbar = Toolbar.create(configTb); + Title.setToolbar(toolbar); + var $rightside = toolbar.$rightside; var editHash; @@ -396,7 +357,7 @@ define([ var templateObj = { rt: info.realtime, Crypt: Cryptget, - getTitle: function () { return document.title; } + getTitle: Title.getTitle }; var $templateButton = Cryptpad.createButton('template', true, templateObj); $rightside.append($templateButton); @@ -410,10 +371,6 @@ define([ /* add an import button */ var $import = Cryptpad.createButton('import', true, {}, importText); $rightside.append($import); - - /* add a rename button */ - //var $setTitle = Cryptpad.createButton('rename', true, {suggestName: suggestName}, renameCb); - //$rightside.append($setTitle); } /* add a forget button */ @@ -559,9 +516,8 @@ define([ editor.setValue(newDoc); } - if (Cryptpad.initialName && document.title === defaultName) { - updateTitle(Cryptpad.initialName); - onLocal(); + if (Cryptpad.initialName && Title.isDefaultTitle()) { + Title.updateTitle(Cryptpad.initialName); } if (Visible.isSupported()) { diff --git a/www/common/common-title.js b/www/common/common-title.js new file mode 100644 index 000000000..7ceb2741e --- /dev/null +++ b/www/common/common-title.js @@ -0,0 +1,82 @@ +define(function () { + var module = {}; + + module.create = function (cfg, onLocal, Cryptpad) { + var exp = {}; + + var parsed = exp.parsedHref = Cryptpad.parsePadUrl(window.location.href); + exp.defaultTitle = Cryptpad.getDefaultName(parsed); + + exp.title = document.title; // TOOD slides + + var getHeadingText = cfg.getHeadingText || function () { return; }; + var updateLocalTitle = function (newTitle) { + exp.title = newTitle; + if (typeof cfg.updateLocalTitle === "function") { + cfg.updateLocalTitle(newTitle); + } else { + document.title = newTitle; + } + }; + + var $title; + exp.setToolbar = function (toolbar) { + $title = toolbar && toolbar.title; + } + + exp.getTitle = function () { return exp.title; }; + var isDefaultTitle = exp.isDefaultTitle = function (){return exp.title === exp.defaultTitle;}; + + var suggestTitle = exp.suggestTitle = function (fallback) { + if (isDefaultTitle()) { + return getHeadingText() || fallback || ""; + } else { + return exp.title || getHeadingText() || exp.defaultTitle; + } + }; + + var renameCb = function (err, newTitle) { + if (err) { return; } + updateLocalTitle(newTitle); + console.log('here'); + onLocal(); + }; + + exp.updateTitle = function (newTitle) { + if (newTitle === exp.title) { return; } + // Change the title now, and set it back to the old value if there is an error + var oldTitle = exp.title; + Cryptpad.renamePad(newTitle, function (err, data) { + if (err) { + console.log("Couldn't set pad title"); + console.error(err); + updateLocalTitle(oldTitle); + return; + } + updateLocalTitle(data); + if (!$title) { return; } + $title.find('span.title').text(data); + $title.find('input').val(data); + }); + }; + + exp.updateDefaultTitle = function (newDefaultTitle) { + exp.defaultTitle = newDefaultTitle; + if (!$title) { return; } + $title.find('input').attr("placeholder", exp.defaultTitle); + }; + + exp.getTitleConfig = function () { + return { + onRename: renameCb, + suggestName: suggestTitle, + defaultName: exp.defaultTitle + } + }; + + return exp; + }; + + return module; +}); + diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index b58209029..29d3e0ddb 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -8,11 +8,12 @@ define([ '/common/common-interface.js', '/common/common-history.js', '/common/common-userlist.js', + '/common/common-title.js', '/common/clipboard.js', '/common/pinpad.js', '/customize/application_config.js' -], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Clipboard, Pinpad, AppConfig) { +], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Clipboard, Pinpad, AppConfig) { /* This file exposes functionality which is specific to Cryptpad, but not to any particular pad type. This includes functions for committing metadata @@ -88,6 +89,9 @@ define([ // Userlist common.createUserList = UserList.create; + // Title + common.createTitle = Title.create; + // History common.getHistory = function (config) { return History.create(common, config); };