cryptpad/www/common/common-history.js

230 lines
8.2 KiB
JavaScript
Raw Normal View History

2017-04-14 16:36:36 +00:00
define([
'jquery',
2017-04-14 16:36:36 +00:00
'/bower_components/chainpad-json-validator/json-ot.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad/chainpad.dist.js',
], function ($, JsonOT, Crypto) {
2017-04-14 16:36:36 +00:00
var ChainPad = window.ChainPad;
var History = {};
2017-04-21 15:31:47 +00:00
var getStates = function (rt) {
var states = [];
var b = rt.getAuthBlock();
if (b) { states.unshift(b); }
2017-04-21 15:31:47 +00:00
while (b.getParent()) {
b = b.getParent();
states.unshift(b);
2017-04-21 15:31:47 +00:00
}
return states;
};
2017-04-28 16:23:41 +00:00
var loadHistory = function (config, common, cb) {
2017-04-14 16:36:36 +00:00
var network = common.getNetwork();
var hkn = network.historyKeeper;
2017-04-28 16:23:41 +00:00
var wcId = common.hrefToHexChannelId(config.href || window.location.href);
2017-04-14 16:36:36 +00:00
2017-04-28 16:23:41 +00:00
console.log(wcId);
2017-04-14 16:36:36 +00:00
var createRealtime = function(chan) {
return ChainPad.create({
userName: 'history',
initialState: '',
transformFunction: JsonOT.validate,
logLevel: 0,
2017-04-21 15:31:47 +00:00
noPrune: true
2017-04-14 16:36:36 +00:00
});
};
var realtime = createRealtime();
2017-04-28 16:23:41 +00:00
var hash = config.href ? common.parsePadUrl(config.href).hash : undefined;
var secret = common.getSecrets(hash);
2017-04-14 16:36:36 +00:00
var crypto = Crypto.createEncryptor(secret.keys);
var to = window.setTimeout(function () {
cb('[GET_FULL_HISTORY_TIMEOUT]');
2017-04-21 15:31:47 +00:00
}, 30000);
2017-04-14 16:36:36 +00:00
var parse = function (msg) {
try {
return JSON.parse(msg);
} catch (e) {
return null;
}
};
var onMsg = function (msg) {
var parsed = parse(msg);
if (parsed[0] === 'FULL_HISTORY_END') {
2017-04-21 15:31:47 +00:00
console.log('END');
2017-04-14 16:36:36 +00:00
window.clearTimeout(to);
cb(null, realtime);
return;
}
if (parsed[0] !== 'FULL_HISTORY') { return; }
2017-04-21 15:31:47 +00:00
msg = parsed[1][4];
if (msg) {
msg = msg.replace(/^cp\|/, '');
var decryptedMsg = crypto.decrypt(msg, secret.keys.validateKey);
realtime.message(decryptedMsg);
}
2017-04-14 16:36:36 +00:00
};
network.on('message', function (msg, sender) {
onMsg(msg);
});
2017-04-21 15:31:47 +00:00
network.sendto(hkn, JSON.stringify(['GET_FULL_HISTORY', wcId, secret.keys.validateKey]));
2017-04-14 16:36:36 +00:00
};
2017-04-21 15:31:47 +00:00
var create = 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;
2017-04-21 15:31:47 +00:00
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;
var Messages = common.Messages;
2017-04-14 16:36:36 +00:00
2017-04-21 15:31:47 +00:00
var realtime;
var states = [];
2017-04-21 15:31:47 +00:00
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');
2017-04-14 16:36:36 +00:00
var onUpdate;
2017-04-21 15:31:47 +00:00
var update = function () {
if (!realtime) { return []; }
states = getStates(realtime);
2017-04-14 16:36:36 +00:00
if (typeof onUpdate === "function") { onUpdate(); }
return states;
};
2017-04-21 15:31:47 +00:00
// Get the content of the selected version, and change the version number
var get = function (i) {
2017-04-14 16:36:36 +00:00
i = parseInt(i);
2017-04-21 15:31:47 +00:00
if (isNaN(i)) { return; }
if (i < 0) { i = 0; }
if (i > states.length - 1) { i = states.length - 1; }
var val = states[i].getContent().doc;
2017-04-14 16:36:36 +00:00
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'); }
2017-04-21 15:31:47 +00:00
return val || '';
2017-04-14 16:36:36 +00:00
};
2017-04-21 15:31:47 +00:00
var getNext = function (step) {
return typeof step === "number" ? get(c + step) : get(c + 1);
2017-04-14 16:36:36 +00:00
};
2017-04-21 15:31:47 +00:00
var getPrevious = function (step) {
return typeof step === "number" ? get(c - step) : get(c - 1);
2017-04-14 16:36:36 +00:00
};
2017-04-21 15:31:47 +00:00
// Create the history toolbar
var display = function () {
$hist.html('').show();
$left.hide();
$right.hide();
$cke.hide();
2017-04-21 15:31:47 +00:00
var $prev =$('<button>', {
2017-04-28 16:23:41 +00:00
'class': 'previous fa fa-step-backward buttonPrimary',
2017-04-21 15:31:47 +00:00
title: Messages.history_prev
}).appendTo($hist);
var $nav = $('<div>', {'class': 'goto'}).appendTo($hist);
2017-04-21 15:31:47 +00:00
var $next = $('<button>', {
2017-04-28 16:23:41 +00:00
'class': 'next fa fa-step-forward buttonPrimary',
2017-04-21 15:31:47 +00:00
title: Messages.history_next
}).appendTo($hist);
2017-04-14 16:36:36 +00:00
var $label = $('<label>').text(Messages.history_version).appendTo($nav);
2017-04-14 16:36:36 +00:00
var $cur = $('<input>', {
2017-04-21 15:31:47 +00:00
'class' : 'gotoInput',
2017-04-14 16:36:36 +00:00
'type' : 'number',
'min' : '1',
'max' : states.length
}).val(c + 1).appendTo($nav).mousedown(function (e) {
// stopPropagation because the event would be cancelled by the dropdown menus
e.stopPropagation();
});
2017-05-02 07:42:51 +00:00
var $label2 = $('<label>').text(' / '+ states.length).appendTo($nav);
2017-04-14 16:36:36 +00:00
$('<br>').appendTo($nav);
2017-04-21 15:31:47 +00:00
var $close = $('<button>', {
'class':'closeHistory',
title: Messages.history_closeTitle
}).text(Messages.history_close).appendTo($nav);
var $rev = $('<button>', {
2017-04-28 16:23:41 +00:00
'class':'revertHistory buttonSuccess',
title: Messages.history_restoreTitle
}).text(Messages.history_restore).appendTo($nav);
2017-04-14 16:36:36 +00:00
onUpdate = function () {
2017-04-21 15:31:47 +00:00
$cur.attr('max', states.length);
2017-04-14 16:36:36 +00:00
$cur.val(c+1);
2017-05-02 07:42:51 +00:00
$label2.text(' / ' + states.length);
2017-04-14 16:36:36 +00:00
};
2017-04-21 15:31:47 +00:00
var close = function () {
$hist.hide();
$left.show();
$right.show();
$cke.show();
2017-04-14 16:36:36 +00:00
};
2017-04-21 15:31:47 +00:00
// Buttons actions
$prev.click(function () { render(getPrevious()); });
$next.click(function () { render(getNext()); });
$cur.keydown(function (e) {
var p = function () { e.preventDefault(); };
if (e.which === 13) { p(); return render( get($cur.val() - 1) ); } // Enter
if ([37, 40].indexOf(e.which) >= 0) { p(); return render(getPrevious()); } // Left
if ([38, 39].indexOf(e.which) >= 0) { p(); return render(getNext()); } // Right
if (e.which === 33) { p(); return render(getNext(10)); } // PageUp
if (e.which === 34) { p(); return render(getPrevious(10)); } // PageUp
if (e.which === 27) { p(); $close.click(); }
}).focus();
$cur.on('change', function () {
render( get($cur.val() - 1) );
2017-04-14 16:36:36 +00:00
});
$close.click(function () {
2017-04-21 15:31:47 +00:00
states = [];
close();
2017-04-14 16:36:36 +00:00
onClose();
});
2017-04-21 15:31:47 +00:00
$rev.click(function () {
common.confirm(Messages.history_restorePrompt, function (yes) {
if (!yes) { return; }
close();
onRevert();
common.log(Messages.history_restoreDone);
});
});
// Display the latest content
2017-04-14 16:36:36 +00:00
render(get(c));
};
2017-04-21 15:31:47 +00:00
// Load all the history messages into a new chainpad object
2017-04-28 16:23:41 +00:00
loadHistory(config, common, function (err, newRt) {
History.loading = false;
2017-04-14 16:36:36 +00:00
if (err) { throw new Error(err); }
2017-04-21 15:31:47 +00:00
realtime = newRt;
update();
c = states.length - 1;
display();
onReady();
2017-04-14 16:36:36 +00:00
});
};
return History;
});