From c00158d23a3ee4119277d73834944c39d47705e1 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 10 May 2017 13:39:14 +0200 Subject: [PATCH] Move code to common-history --- www/code/main.js | 33 ++++++++------------------------- www/common/common-history.js | 32 ++++++++++++++++++++++++++------ www/drive/main.js | 34 ++++++++++------------------------ www/pad/main.js | 30 ++++++------------------------ www/slide/main.js | 35 +++++++++-------------------------- 5 files changed, 59 insertions(+), 105 deletions(-) diff --git a/www/code/main.js b/www/code/main.js index d8b9903f4..85cd26db3 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -150,34 +150,17 @@ define([ } /* add a history button */ - var histConfig = {}; - histConfig.onRender = function (val) { - if (typeof val === "undefined") { return; } - try { - var hjson = JSON.parse(val || '{}'); - var remoteDoc = hjson.content; + var histConfig = { + onLocal: config.onLocal(), + onRemote: config.onRemote(), + setHistory: setHistory, + applyVal: function (val) { + var remoteDoc = JSON.parse(val || '{}').content; editor.setValue(remoteDoc || ''); editor.save(); - } catch (e) { - // Probably a parse error - console.error(e); - } - }; - histConfig.onClose = function () { - // Close button clicked - setHistory(false, true); - }; - histConfig.onRevert = function () { - // Revert button clicked - setHistory(false, false); - config.onLocal(); - config.onRemote(); - }; - histConfig.onReady = function () { - // Called when the history is loaded and the UI displayed - setHistory(true); + }, + $toolbar: $bar }; - histConfig.$toolbar = $bar; var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); $rightside.append($hist); diff --git a/www/common/common-history.js b/www/common/common-history.js index 1cd0e6555..8597598c1 100644 --- a/www/common/common-history.js +++ b/www/common/common-history.js @@ -24,7 +24,6 @@ define([ var wcId = common.hrefToHexChannelId(config.href || window.location.href); - console.log(wcId); var createRealtime = function () { return ChainPad.create({ userName: 'history', @@ -80,11 +79,32 @@ define([ if (History.loading) { return void console.error("History is already being loaded..."); } History.loading = true; var $toolbar = config.$toolbar; - var noFunc = function () {}; - var render = config.onRender || noFunc; - var onClose = config.onClose || noFunc; - var onRevert = config.onRevert || noFunc; - var onReady = config.onReady || noFunc; + + if (!config.applyVal || !config.setHistory || !config.onLocal || !config.onRemote) { + throw new Error("Missing config element: applyVal, onLocal, onRemote, setHistory"); + } + + // config.setHistory(bool, bool) + // - bool1: history value + // - bool2: reset old content? + var render = function (val) { + if (typeof val === "undefined") { return; } + try { + config.applyVal(val); + } catch (e) { + // Probably a parse error + console.error(e); + } + }; + var onClose = function () { config.setHistory(false, true); }; + var onRevert = function () { + config.setHistory(false, false); + config.onLocal(); + config.onRemote(); + }; + var onReady = function () { + config.setHistory(true); + }; var Messages = common.Messages; diff --git a/www/drive/main.js b/www/drive/main.js index fe57a02c4..9e88fc5f0 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -2529,7 +2529,6 @@ define([ setEditable(!bool); if (!bool && update) { history.onLeaveHistory(); - //init(); //TODO real proxy here } }; @@ -2682,33 +2681,20 @@ define([ } /* add a history button */ - var histConfig = {}; - histConfig.onRender = function (val) { - if (typeof val === "undefined") { return; } - try { + var histConfig = { + onLocal: function () { + proxy.drive = history.currentObj.drive; + }, + onRemote: function () {}, + setHistory: setHistory, + applyVal: function (val) { var obj = JSON.parse(val || '{}'); history.currentObj = obj; history.onEnterHistory(obj); - } catch (e) { - // Probably a parse error - logError(e); - } - }; - histConfig.onClose = function () { - // Close button clicked - setHistory(false, true); - }; - histConfig.onRevert = function () { - // Revert button clicked - setHistory(false, false); - proxy.drive = history.currentObj.drive; - }; - histConfig.onReady = function () { - // Called when the history is loaded and the UI displayed - setHistory(true); + }, + $toolbar: APP.$bar, + href: window.location.origin + window.location.pathname + APP.hash }; - histConfig.$toolbar = APP.$bar; - histConfig.href = window.location.origin + window.location.pathname + APP.hash; var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); $rightside.append($hist); diff --git a/www/pad/main.js b/www/pad/main.js index 50e35f2dc..635ea0087 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -499,31 +499,13 @@ define([ } /* add a history button */ - var histConfig = {}; - histConfig.onRender = function (val) { - if (typeof val === "undefined") { return; } - try { - applyHjson(val || '["BODY",{},[]]'); - } catch (e) { - // Probably a parse error - console.error(e); - } - }; - histConfig.onClose = function () { - // Close button clicked - setHistory(false, true); - }; - histConfig.onRevert = function () { - // Revert button clicked - setHistory(false, false); - realtimeOptions.onLocal(); - realtimeOptions.onRemote(); - }; - histConfig.onReady = function () { - // Called when the history is loaded and the UI displayed - setHistory(true); + var histConfig = { + onLocal: realtimeOptions.onLocal(), + onRemote: realtimeOptions.onRemote(), + setHistory: setHistory, + applyVal: function (val) { applyHjson(val || '["BODY",{},[]]'); }, + $toolbar: $bar }; - histConfig.$toolbar = $bar; var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); $rightside.append($hist); diff --git a/www/slide/main.js b/www/slide/main.js index b3d1209fd..ad80f03d7 100644 --- a/www/slide/main.js +++ b/www/slide/main.js @@ -293,34 +293,17 @@ define([ } /* add a history button */ - var histConfig = {}; - histConfig.onRender = function (val) { - if (typeof val === "undefined") { return; } - try { - var hjson = JSON.parse(val || '{}'); - var remoteDoc = hjson.content; + var histConfig = { + onLocal: config.onLocal(), + onRemote: config.onRemote(), + setHistory: setHistory, + applyVal: function (val) { + var remoteDoc = JSON.parse(val || '{}').content; editor.setValue(remoteDoc || ''); editor.save(); - } catch (e) { - // Probably a parse error - console.error(e); - } - }; - histConfig.onClose = function () { - // Close button clicked - setHistory(false, true); - }; - histConfig.onRevert = function () { - // Revert button clicked - setHistory(false, false); - config.onLocal(); - config.onRemote(); - }; - histConfig.onReady = function () { - // Called when the history is loaded and the UI displayed - setHistory(true); - }; - histConfig.$toolbar = $bar; + }, + $toolbar: $bar + } var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); $rightside.append($hist);