From 96ccb943979e28ef201ef9196440f2fe3889e9bc Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 10 May 2017 14:44:01 +0200 Subject: [PATCH] Add the share button when a file upload is complete --- www/common/toolbar2.js | 5 +-- www/file/main.js | 75 ++++++++++++++---------------------------- 2 files changed, 28 insertions(+), 52 deletions(-) diff --git a/www/common/toolbar2.js b/www/common/toolbar2.js index 9d7d43aa9..49e2376e8 100644 --- a/www/common/toolbar2.js +++ b/www/common/toolbar2.js @@ -367,7 +367,7 @@ define([ return "Loading share button"; }; - var createFileShare = function () { + var createFileShare = function (toolbar) { if (!window.location.hash) { throw new Error("Unable to display the share button: hash required in the URL"); } @@ -380,6 +380,7 @@ define([ if (success) { Cryptpad.log(Messages.shareSuccess); } }); + toolbar.$leftside.append($button); return $button; }; @@ -852,7 +853,7 @@ define([ tb['useradmin'] = createUserAdmin; - var addElement = function (arr, additionnalCfg, init) { + var addElement = toolbar.addElement = function (arr, additionnalCfg, init) { if (typeof additionnalCfg === "object") { $.extend(true, config, additionnalCfg); } arr.forEach(function (el) { if (typeof el !== "string" || !el.trim()) { return; } diff --git a/www/file/main.js b/www/file/main.js index 223fcc41e..a8e7da517 100644 --- a/www/file/main.js +++ b/www/file/main.js @@ -2,7 +2,7 @@ define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', - '/common/toolbar.js', + '/common/toolbar2.js', '/common/cryptpad-common.js', '/common/visible.js', '/common/notify.js', @@ -24,6 +24,8 @@ define([ Cryptpad.addLoadingScreen(); + var Title; + var myFile; var myDataType; @@ -68,17 +70,13 @@ define([ var uri = ['', 'blob', id.slice(0,2), id].join('/'); console.log("encrypted blob is now available as %s", uri); - // TODO use cryptpad-common utilities - window.location.hash = [ - '', - 2, - Cryptpad.hexToBase64(id).replace(/\//g, '-'), - Nacl.util.encodeBase64(key).replace(/\//g, '-'), - '' - ].join('/'); + var b64Key = Nacl.util.encodeBase64(key); + window.location.hash = Cryptpad.getFileHashFromKeys(id, ib64key); $form.hide(); + APP.toolbar.addElement(['fileshare'], {}); + // check if the uploaded file can be decrypted var newU8 = FileCrypto.joinChunks(chunks); FileCrypto.decrypt(newU8, key, function (e, res) { @@ -88,7 +86,8 @@ define([ myDataType = res.metadata.type; var defaultName = Cryptpad.getDefaultName(Cryptpad.parsePadUrl(window.location.href)); - APP.updateTitle(title || defaultName); + Title.updateTitle(title || defaultName); + APP.toolbar.title.show(); Cryptpad.alert("successfully uploaded: " + title); }); }); @@ -133,9 +132,6 @@ define([ uploadMode = true; } - var parsed = Cryptpad.parsePadUrl(window.location.href); - var defaultName = Cryptpad.getDefaultName(parsed); - var getTitle = function () { var pad = Cryptpad.getRelativeHref(window.location.href); var fo = Cryptpad.getStore().getProxy().fo; @@ -143,27 +139,6 @@ define([ return data ? data.title : undefined; }; - var updateTitle = APP.updateTitle = function (newTitle) { - Cryptpad.renamePad(newTitle, function (err, data) { - if (err) { - console.log("Couldn't set pad title"); - console.error(err); - return; - } - document.title = newTitle; - $bar.find('.' + Toolbar.constants.title).find('span.title').text(data); - $bar.find('.' + Toolbar.constants.title).find('input').val(data); - }); - }; - - var suggestName = function () { - return document.title || getTitle() || ''; - }; - - var renameCb = function (err, title) { - document.title = title; - }; - var exportFile = function () { var suggestion = document.title; Cryptpad.prompt(Messages.exportPrompt, @@ -174,33 +149,33 @@ define([ }); }; - var displayed = ['useradmin', 'newpad', 'limit']; + Title = Cryptpad.createTitle({}, function(){}, Cryptpad); + + var displayed = ['title', 'useradmin', 'newpad', 'limit']; if (secret && hexFileName) { - displayed.push('share'); + displayed.push('fileshare'); } var configTb = { displayed: displayed, ifrw: ifrw, common: Cryptpad, - title: { - onRename: renameCb, - defaultName: defaultName, - suggestName: suggestName - }, - share: { - secret: secret, - channel: hexFileName - }, - hideDisplayName: true + title: Title.getTitleConfig(), + hideDisplayName: true, + $container: $bar }; - Toolbar.create($bar, null, null, null, null, configTb); - var $rightside = $bar.find('.' + Toolbar.constants.rightside); + var toolbar = APP.toolbar = Toolbar.create(configTb); + + Title.setToolbar(toolbar); + + if (uploadMode) { toolbar.title.hide(); } + + var $rightside = toolbar.$rightside; var $export = Cryptpad.createButton('export', true, {}, exportFile); $rightside.append($export); - updateTitle(Cryptpad.initialName || getTitle() || defaultName); + Title.updateTitle(Cryptpad.initialName || getTitle() || Title.defaultTitle); if (!uploadMode) { var src = Cryptpad.getBlobPathFromHex(hexFileName); @@ -219,7 +194,7 @@ define([ var title = document.title = data.metadata.name; myFile = data.content; myDataType = data.metadata.type; - updateTitle(title || defaultName); + Title.updateTitle(title || Title.defaultTitle); Cryptpad.removeLoadingScreen(); }); });