From fab3d67b7500516fb46f051c4f5d48eb1e745c40 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 17 Aug 2017 18:28:05 +0200 Subject: [PATCH] Enable title in pad2 --- www/common/sframe-common-title.js | 94 +++++++++++++++++++++++++++++++ www/common/sframe-common.js | 17 +++++- www/common/toolbar3.js | 6 +- www/pad2/main.js | 66 ++++++++++++++++------ 4 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 www/common/sframe-common-title.js diff --git a/www/common/sframe-common-title.js b/www/common/sframe-common-title.js new file mode 100644 index 000000000..d3c343210 --- /dev/null +++ b/www/common/sframe-common-title.js @@ -0,0 +1,94 @@ +define(['jquery'], function ($) { + var module = {}; + + module.create = function (cfg, onLocal, Common, metadataMgr) { + var exp = {}; + + exp.defaultTitle = Common.getDefaultTitle(); + + exp.title = document.title; // TOOD slides + + cfg = cfg || {}; + + var getHeadingText = cfg.getHeadingText || function () { return; }; + + var updateLocalTitle = function (newTitle) { + console.log(newTitle); + exp.title = newTitle; + onLocal(); + 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); + onLocal(); + }; + + // update title: href is optional; if not specified, we use window.location.href + exp.updateTitle = function (newTitle, cb) { + cb = cb || $.noop; + 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; + Common.setPadTitleInDrive(newTitle, function (err, data) { + if (err) { + console.log("Couldn't set pad title"); + console.error(err); + updateLocalTitle(oldTitle); + return void cb(err); + } + updateLocalTitle(data); + cb(null, data); + if (!$title) { return; } + $title.find('span.title').text(data); + $title.find('input').val(data); + }); + }; + + // TODO not needed? + /*exp.updateDefaultTitle = function (newDefaultTitle) { + exp.defaultTitle = newDefaultTitle; + if (!$title) { return; } + $title.find('input').attr("placeholder", exp.defaultTitle); + };*/ + + metadataMgr.onChange(function () { + var md = metadataMgr.getMetadata(); + exp.updateTitle(md.title || md.defaultTitle); + }); + + exp.getTitleConfig = function () { + return { + onRename: renameCb, + suggestName: suggestTitle, + defaultName: exp.defaultTitle + }; + }; + + return exp; + }; + + return module; +}); + diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 75f563beb..47bceb120 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -1,8 +1,10 @@ define([ '/bower_components/nthen/index.js', '/common/sframe-chainpad-netflux-inner.js', - '/common/sframe-channel.js' -], function (nThen, CpNfInner, SFrameChannel) { + '/common/sframe-channel.js', + '/common/sframe-common-title.js' + +], function (nThen, CpNfInner, SFrameChannel, Title) { // Chainpad Netflux Inner var funcs = {}; @@ -22,10 +24,19 @@ define([ funcs.setPadTitleInDrive = function (title, cb) { ctx.sframeChan.query('Q_SET_PAD_TITLE_IN_DRIVE', title, function (err) { - if (cb) { cb(err); } + if (cb) { cb(err, title); } }); }; + // Title module + funcs.createTitle = Title.create; + + funcs.getDefaultTitle = function () { + if (!ctx.cpNfInner) { throw new Error("cpNfInner is not ready!"); } + return ctx.cpNfInner.metadataMgr.getMetadata().defaultTitle; + }; + + Object.freeze(funcs); return { create: function (cb) { nThen(function (waitFor) { diff --git a/www/common/toolbar3.js b/www/common/toolbar3.js index 20bec12e9..97720dc67 100644 --- a/www/common/toolbar3.js +++ b/www/common/toolbar3.js @@ -5,6 +5,7 @@ define([ ], function ($, Config, ApiConfig) { var Messages = {}; var Cryptpad; + var Common; var Bar = { constants: {}, @@ -567,7 +568,9 @@ define([ if (name === "") { name = $input.attr('placeholder'); } - Cryptpad.renamePad(name, null, function (err, newtitle) { + console.log('here'); + Common.setPadTitleInDrive(name, function (err, newtitle) { + console.log('here'); if (err) { return console.error(err); } $text.text(newtitle); callback(null, newtitle); @@ -957,6 +960,7 @@ define([ Bar.create = function (cfg) { var config = cfg || {}; Cryptpad = config.common; + Common = config.sfCommon; Messages = Cryptpad.Messages; config.readOnly = (typeof config.readOnly !== "undefined") ? (config.readOnly ? 1 : 0) : -1; config.displayed = config.displayed || []; diff --git a/www/pad2/main.js b/www/pad2/main.js index 6dc173da1..5779f002e 100644 --- a/www/pad2/main.js +++ b/www/pad2/main.js @@ -2,7 +2,6 @@ console.log('one'); define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', - '/common/sframe-chainpad-netflux-inner.js', '/bower_components/hyperjson/hyperjson.js', '/common/toolbar3.js', '/common/cursor.js', @@ -22,7 +21,7 @@ define([ 'css!/bower_components/components-font-awesome/css/font-awesome.min.css', 'less!/customize/src/less/cryptpad.less', 'less!/customize/src/less/toolbar.less' -], function ($, Crypto, CpNfInner, Hyperjson, +], function ($, Crypto, Hyperjson, Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad, Cryptget, Links, nThen, SFCommon) { var saveAs = window.saveAs; var Messages = Cryptpad.Messages; @@ -305,7 +304,7 @@ define([ var initializing = true; - //var Title; + var Title; //var UserList; //var Metadata; @@ -350,7 +349,7 @@ define([ } };*/ if (!initializing) { - //TODO hjson[3].metadata.title = Title.title; + hjson[3].metadata.title = Title.title; } else if (Cryptpad.initialName && !hjson[3].metadata.title) { hjson[3].metadata.title = Cryptpad.initialName; } @@ -470,20 +469,51 @@ define([ realtimeOptions.onInit = function (info) { console.log('onInit'); var titleCfg = { getHeadingText: getHeadingText }; - Title = Cryptpad.createTitle(titleCfg, realtimeOptions.onLocal, Cryptpad); + Title = common.createTitle(titleCfg, realtimeOptions.onLocal, common, cpNfInner.metadataMgr); var configTb = { - displayed: ['userlist', 'useradmin'], - //title: Title.getTitleConfig(), + displayed: ['userlist', 'title'], + title: Title.getTitleConfig(), userList: cpNfInner.metadataMgr, readOnly: readOnly, ifrw: window, realtime: info.realtime, common: Cryptpad, + sfCommon: common, $container: $bar, $contentContainer: $('#cke_1_contents'), }; toolbar = info.realtime.toolbar = Toolbar.create(configTb); + Title.setToolbar(toolbar); + + $bar.find('#cke_1_toolbar_collapser').hide(); + if (!readOnly) { + // Expand / collapse the toolbar + var $collapse = Cryptpad.createButton(null, true); + $collapse.removeClass('fa-question'); + var updateIcon = function () { + $collapse.removeClass('fa-caret-down').removeClass('fa-caret-up'); + var isCollapsed = !$bar.find('.cke_toolbox_main').is(':visible'); + if (isCollapsed) { + if (!initializing) { Cryptpad.feedback('HIDETOOLBAR_PAD'); } + $collapse.addClass('fa-caret-down'); + } + else { + if (!initializing) { Cryptpad.feedback('SHOWTOOLBAR_PAD'); } + $collapse.addClass('fa-caret-up'); + } + }; + updateIcon(); + $collapse.click(function () { + $(window).trigger('resize'); + $('.cke_toolbox_main').toggle(); + $(window).trigger('cryptpad-ck-toolbar'); + updateIcon(); + }); + $rightside.append($collapse); + } + // TODO + return; // TODO UserList not needed anymore? @@ -491,12 +521,14 @@ define([ // --> select username on first visit //UserList = Cryptpad.createUserList(info, realtimeOptions.onLocal, Cryptget, Cryptpad); - var titleCfg = { getHeadingText: getHeadingText }; - Title = Cryptpad.createTitle(titleCfg, realtimeOptions.onLocal, Cryptpad); + //var titleCfg = { getHeadingText: getHeadingText }; + //Title = Cryptpad.createTitle(titleCfg, realtimeOptions.onLocal, Cryptpad); - Metadata = Cryptpad.createMetadata(UserList, Title, null, Cryptpad); + // Metadata not needed anymore? + // Title and defaultTitle handled by metadataMgr.onChange in sframe-common-title + //Metadata = Cryptpad.createMetadata(UserList, Title, null, Cryptpad); - var configTb = { + /*var configTb = { displayed: [ 'title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit', 'upgrade'], userList: UserList.getToolbarConfig(), @@ -514,7 +546,7 @@ define([ $contentContainer: $('#cke_1_contents'), }; toolbar = info.realtime.toolbar = Toolbar.create(configTb); - +*/ var src = 'less!/customize/src/less/toolbar.less'; require([ src @@ -525,7 +557,7 @@ define([ .appendTo($html.find('head')); }); - Title.setToolbar(toolbar); + //Title.setToolbar(toolbar); var $rightside = toolbar.$rightside; var $drawer = toolbar.$drawer; @@ -536,8 +568,8 @@ define([ editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); } - $bar.find('#cke_1_toolbar_collapser').hide(); - if (!readOnly) { + //$bar.find('#cke_1_toolbar_collapser').hide(); + /*if (!readOnly) { // Expand / collapse the toolbar var $collapse = Cryptpad.createButton(null, true); $collapse.removeClass('fa-question'); @@ -561,7 +593,7 @@ define([ updateIcon(); }); $rightside.append($collapse); - } + }*/ /* add a history button */ var histConfig = { @@ -657,7 +689,7 @@ define([ } } } else { - //Title.updateTitle(Cryptpad.initialName || Title.defaultTitle); + Title.updateTitle(Cryptpad.initialName || Title.defaultTitle); documentBody.innerHTML = Messages.initialState; }