From 8d020833afcdc30554a6a4c3b106a2cc5a7f49e5 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 21 Aug 2017 18:21:25 +0200 Subject: [PATCH] Add missing file --- www/common/sframe-common-history.js | 224 ++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 www/common/sframe-common-history.js diff --git a/www/common/sframe-common-history.js b/www/common/sframe-common-history.js new file mode 100644 index 000000000..530691645 --- /dev/null +++ b/www/common/sframe-common-history.js @@ -0,0 +1,224 @@ +define([ + 'jquery', + '/bower_components/chainpad-json-validator/json-ot.js', + '/bower_components/chainpad-crypto/crypto.js', + '/bower_components/chainpad/chainpad.dist.js', +], function ($, JsonOT, Crypto) { + var ChainPad = window.ChainPad; + var History = {}; + + var getStates = function (rt) { + var states = []; + var b = rt.getAuthBlock(); + if (b) { states.unshift(b); } + while (b.getParent()) { + b = b.getParent(); + states.unshift(b); + } + return states; + }; + + var loadHistory = function (config, common, cb) { + var createRealtime = function () { + return ChainPad.create({ + userName: 'history', + initialState: '', + transformFunction: JsonOT.validate, + logLevel: 0, + noPrune: true + }); + }; + var realtime = createRealtime(); + + History.readOnly = common.getMetadataMgr().getPrivateData().readOnly; + + var to = window.setTimeout(function () { + cb('[GET_FULL_HISTORY_TIMEOUT]'); + }, 30000); + + common.getFullHistory(realtime, function () { + window.clearTimeout(to); + cb(null, realtime); + }); + }; + + History.create = function (common, config) { + if (!config.$toolbar) { return void console.error("config.$toolbar is undefined");} + if (History.loading) { return void console.error("History is already being loaded..."); } + History.loading = true; + var $toolbar = config.$toolbar; + + 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; + var Cryptpad = common.getCryptpadCommon(); + + var realtime; + + var states = []; + var c = states.length - 1; + + var $hist = $toolbar.find('.cryptpad-toolbar-history'); + var $left = $toolbar.find('.cryptpad-toolbar-leftside'); + var $right = $toolbar.find('.cryptpad-toolbar-rightside'); + var $cke = $toolbar.find('.cke_toolbox_main'); + + $hist.html('').show(); + $left.hide(); + $right.hide(); + $cke.hide(); + + Cryptpad.spinner($hist).get().show(); + + var onUpdate; + + var update = function () { + if (!realtime) { return []; } + states = getStates(realtime); + if (typeof onUpdate === "function") { onUpdate(); } + return states; + }; + + // Get the content of the selected version, and change the version number + var get = function (i) { + i = parseInt(i); + if (isNaN(i)) { return; } + if (i < 0) { i = 0; } + if (i > states.length - 1) { i = states.length - 1; } + var val = states[i].getContent().doc; + c = i; + if (typeof onUpdate === "function") { onUpdate(); } + $hist.find('.next, .previous').css('visibility', ''); + if (c === states.length - 1) { $hist.find('.next').css('visibility', 'hidden'); } + if (c === 0) { $hist.find('.previous').css('visibility', 'hidden'); } + return val || ''; + }; + + var getNext = function (step) { + return typeof step === "number" ? get(c + step) : get(c + 1); + }; + var getPrevious = function (step) { + return typeof step === "number" ? get(c - step) : get(c - 1); + }; + + // Create the history toolbar + var display = function () { + $hist.html(''); + var $prev =$('