cryptpad/www/common/onlyoffice/history.js

383 lines
13 KiB
JavaScript
Raw Normal View History

2020-09-18 15:54:57 +00:00
define([
'jquery',
'/common/common-interface.js',
'/common/hyperscript.js',
2020-09-22 13:27:16 +00:00
], function ($, UI, h) {
2020-09-18 15:54:57 +00:00
//var ChainPad = window.ChainPad;
var History = {};
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;
var sframeChan = common.getSframeChannel();
2020-09-29 14:08:21 +00:00
History.readOnly = common.getMetadataMgr().getPrivateData().readOnly || !common.isLoggedIn();
2020-09-18 15:54:57 +00:00
2020-10-01 11:54:30 +00:00
if (!config.onlyoffice || !config.setHistory || !config.onCheckpoint || !config.onPatch || !config.makeSnapshot) {
2020-09-18 15:54:57 +00:00
throw new Error("Missing config element");
}
var cpIndex = -1;
2020-09-29 14:08:21 +00:00
var msgIndex = -1;
2020-09-18 15:54:57 +00:00
var ooMessages = {};
var loading = false;
var update = function () {};
2020-10-01 11:54:30 +00:00
var currentTime;
2020-09-18 15:54:57 +00:00
// Get an array of the checkpoint IDs sorted their patch index
var hashes = config.onlyoffice.hashes;
var sortedCp = Object.keys(hashes).map(Number).sort(function (a, b) {
return hashes[a].index - hashes[b].index;
});
var endWithCp = sortedCp.length &&
config.onlyoffice.lastHash === hashes[sortedCp[sortedCp.length - 1]].hash;
2020-09-22 08:56:57 +00:00
2020-09-18 15:54:57 +00:00
var fillOO = function (id, messages) {
if (!id) { return; }
if (ooMessages[id]) { return; }
ooMessages[id] = messages;
update();
};
2020-09-22 08:56:57 +00:00
if (endWithCp) { cpIndex = 0; }
2020-09-24 14:04:44 +00:00
var $version, $time, $share;
2020-09-22 13:27:16 +00:00
var $hist = $toolbar.find('.cp-toolbar-history');
2020-09-29 14:08:21 +00:00
$hist.addClass('cp-smallpatch');
$hist.addClass('cp-history-oo');
2020-09-22 13:27:16 +00:00
var $bottom = $toolbar.find('.cp-toolbar-bottom');
2020-09-24 14:04:44 +00:00
var getVersion = function () {
var major = sortedCp.length - cpIndex;
2020-09-29 14:08:21 +00:00
return major + '.' + (msgIndex+1);
2020-09-24 14:04:44 +00:00
};
var showVersion = function (initial) {
var v = getVersion();
if (initial) {
v = "Latest"; // XXX
}
$version.text("Version: " + v); // XXX
2020-09-29 14:08:21 +00:00
var $pos = $hist.find('.cp-history-timeline-pos');
var cps = sortedCp.length;
var id = sortedCp[cps - cpIndex -1] || -1;
if (!ooMessages[id]) { return; }
var msgs = ooMessages[id];
2020-09-29 14:08:21 +00:00
console.log(ooMessages);
var p = 100*((msgIndex+1) / (msgs.length));
$pos.css('margin-left', p+'%');
2020-10-01 11:54:30 +00:00
var time = currentTime = msgs[msgIndex] && msgs[msgIndex].time;
if (time) { $time.text(new Date(time).toLocaleString()); }
else { $time.text(''); }
};
2020-09-18 15:54:57 +00:00
// We want to load a checkpoint (or initial state)
var loadMoreOOHistory = function () {
if (!Array.isArray(sortedCp)) { return void console.error("Wrong type"); }
var cp = {};
// Get the checkpoint ID
var id = -1;
if (cpIndex < sortedCp.length) {
id = sortedCp[sortedCp.length - 1 - cpIndex];
cp = hashes[id];
}
var nextId = sortedCp[sortedCp.length - cpIndex];
// Get the history between "toHash" and "fromHash". This function is using
// "getOlderHistory", that's why we start from the more recent hash
// and we go back in time to an older hash
// We need to get all the patches between the current cp hash and the next cp hash
// Current cp or initial hash (invalid hash ==> initial hash)
var toHash = cp.hash || 'NONE';
// Next cp or last hash
var fromHash = nextId ? hashes[nextId].hash : config.onlyoffice.lastHash;
2020-09-29 14:08:21 +00:00
msgIndex = -1;
2020-09-21 12:51:13 +00:00
showVersion();
2020-09-18 15:54:57 +00:00
if (ooMessages[id]) {
// Cp already loaded: reload OO
loading = false;
return void config.onCheckpoint(cp);
}
sframeChan.query('Q_GET_HISTORY_RANGE', {
channel: config.onlyoffice.channel,
lastKnownHash: fromHash,
toHash: toHash,
}, function (err, data) {
if (err) { return void console.error(err); }
if (!Array.isArray(data.messages)) { return void console.error('Not an array!'); }
var messages = (data.messages || []).slice(1);
if (config.debug) { console.log(data.messages); }
fillOO(id, messages);
loading = false;
config.onCheckpoint(cp);
2020-09-24 14:04:44 +00:00
$share.show();
2020-09-18 15:54:57 +00:00
});
};
var onClose = function () { config.setHistory(false); };
var onRevert = function () {
config.onRevert();
};
config.setHistory(true);
var Messages = common.Messages;
$hist.html('').css('display', 'flex');
$bottom.hide();
UI.spinner($hist).get().show();
2020-09-29 14:08:21 +00:00
var $fastPrev, $fastNext, $next;
2020-09-18 15:54:57 +00:00
var getId = function () {
var cps = sortedCp.length;
return sortedCp[cps - cpIndex -1] || -1;
};
2020-09-18 15:54:57 +00:00
update = function () {
2020-09-29 14:08:21 +00:00
console.log($fastPrev, $next, $fastNext);
2020-09-18 15:54:57 +00:00
var cps = sortedCp.length;
2020-09-29 14:08:21 +00:00
console.log(cpIndex, msgIndex, cps);
2020-09-18 15:54:57 +00:00
$fastPrev.show();
$next.show();
$fastNext.show();
if (cpIndex >= cps) {
$fastPrev.hide();
}
if (cpIndex === 0) {
$fastNext.hide();
}
var id = getId();
2020-09-18 15:54:57 +00:00
var msgs = (ooMessages[id] || []).length;
2020-09-29 14:08:21 +00:00
if (msgIndex >= (msgs-1)) {
2020-09-18 15:54:57 +00:00
$next.hide();
}
};
var next = function () {
var id = getId();
2020-09-29 14:08:21 +00:00
if (!ooMessages[id]) { loading = false; return; }
2020-09-18 15:54:57 +00:00
var msgs = ooMessages[id];
2020-09-29 14:08:21 +00:00
msgIndex++;
2020-09-18 15:54:57 +00:00
var patch = msgs[msgIndex];
2020-09-29 14:08:21 +00:00
if (!patch) { loading = false; return; }
2020-09-18 15:54:57 +00:00
config.onPatch(patch);
2020-09-21 12:51:13 +00:00
showVersion();
setTimeout(function () {
$('iframe').blur();
2020-09-29 14:08:21 +00:00
loading = false;
}, 200);
2020-09-18 15:54:57 +00:00
};
2020-09-29 14:08:21 +00:00
2020-09-18 15:54:57 +00:00
// Create the history toolbar
var display = function () {
$hist.html('');
2020-09-29 14:08:21 +00:00
var fastPrev = h('button.cp-toolbar-history-previous', { title: Messages.history_prev }, [
h('i.fa.fa-fast-backward'),
]);
var fastNext = h('button.cp-toolbar-history-next', { title: Messages.history_next }, [
h('i.fa.fa-fast-forward'),
]);
var _next = h('button.cp-toolbar-history-next', { title: Messages.history_next }, [
h('i.fa.fa-step-forward')
]);
$fastPrev = $(fastPrev);
$fastNext = $(fastNext).hide();
$next = $(_next).hide();
2020-09-29 15:48:37 +00:00
var pos = h('span.cp-history-timeline-pos.fa.fa-caret-down');
2020-09-29 14:08:21 +00:00
var time = h('div.cp-history-timeline-time');
var version = h('div.cp-history-timeline-version');
$time = $(time);
$version = $(version);
var bar;
var timeline = h('div.cp-toolbar-history-timeline', [
h('div.cp-history-timeline-line', [
bar = h('span.cp-history-timeline-container')
]),
h('div.cp-history-timeline-actions', [
h('span.cp-history-timeline-prev', [
fastPrev,
]),
time,
version,
h('span.cp-history-timeline-next', [
_next,
fastNext
])
])
]);
var snapshot = h('button', {
title: Messages.snapshots_new,
}, [
h('i.fa.fa-camera')
]);
var share = h('button', { title: Messages.history_shareTitle }, [
h('i.fa.fa-shhare-alt'),
h('span', Messages.shareButton)
]);
var restore = h('button', {
title: Messages.history_restoreTitle,
}, [
h('i.fa.fa-check'),
h('span', Messages.history_restore)
]);
var close = h('button', { title: Messages.history_closeTitle }, [
h('i.fa.fa-times'),
h('span', Messages.history_close)
]);
var actions = h('div.cp-toolbar-history-actions', [
h('span.cp-history-actions-first', [
2020-10-01 11:54:30 +00:00
snapshot,
2020-09-29 14:08:21 +00:00
share
]),
h('span.cp-history-actions-last', [
restore,
close
])
]);
if (History.readOnly) {
2020-10-01 11:54:30 +00:00
snapshot.disabled = true;
2020-09-29 14:08:21 +00:00
restore.disabled = true;
}
$share = $(share);
$hist.append([timeline, actions]);
$(bar).append(pos);
2020-09-18 15:54:57 +00:00
var onKeyDown, onKeyUp;
2020-09-29 14:08:21 +00:00
var closeUI = function () {
2020-09-18 15:54:57 +00:00
$hist.hide();
$bottom.show();
$(window).trigger('resize');
$(window).off('keydown', onKeyDown);
$(window).off('keyup', onKeyUp);
};
// Push one patch
$next.click(function () {
if (loading) { return; }
loading = true;
next();
update();
});
// Go to previous checkpoint
$fastNext.click(function () {
if (loading) { return; }
loading = true;
cpIndex--;
loadMoreOOHistory();
update();
});
// Go to next checkpoint
2020-09-29 14:08:21 +00:00
$fastPrev.click(function () {
2020-09-18 15:54:57 +00:00
if (loading) { return; }
loading = true;
2020-09-29 14:08:21 +00:00
if (msgIndex === -1) {
2020-09-21 12:51:13 +00:00
cpIndex++;
}
2020-09-18 15:54:57 +00:00
loadMoreOOHistory();
update();
});
onKeyDown = function (e) {
var p = function () { e.preventDefault(); };
if ([38, 39].indexOf(e.which) >= 0) { p(); return $next.click(); } // Right
if (e.which === 33) { p(); return $fastNext.click(); } // PageUp
if (e.which === 34) { p(); return $fastPrev.click(); } // PageUp
2020-09-29 14:08:21 +00:00
if (e.which === 27) { p(); return $(close).click(); }
2020-09-18 15:54:57 +00:00
};
onKeyUp = function (e) { e.stopPropagation(); };
$(window).on('keydown', onKeyDown).on('keyup', onKeyUp).focus();
2020-09-29 14:08:21 +00:00
// Versioned link
$share.click(function () {
common.getSframeChannel().event('EV_SHARE_OPEN', {
versionHash: getVersion()
});
});
2020-10-01 11:54:30 +00:00
Messages.snapshots_ooPickVersion = "You must select a version before creating a snapshot"; // XXX
$(snapshot).click(function () {
if (cpIndex === -1 && msgIndex === -1) { return void UI.warn(Messages.snapshots_ooPickVersion); }
var input = h('input', {
placeholder: Messages.snapshots_placeholder
});
var $input = $(input);
var content = h('div', [
h('h5', Messages.snapshots_new),
input
]);
var buttons = [{
className: 'cancel',
name: Messages.filePicker_close,
onClick: function () {},
keys: [27],
}, {
className: 'primary',
iconClass: '.fa.fa-camera',
name: Messages.snapshots_new,
onClick: function () {
var val = $input.val();
if (!val) { return true; }
config.makeSnapshot(val, function (err) {
if (err) { return; }
UI.log(Messages.saved);
}, {
hash: getVersion(),
time: currentTime || 0
});
},
keys: [13],
}];
UI.openCustomModal(UI.dialog.customModal(content, {buttons: buttons }));
setTimeout(function () {
$input.focus();
});
});
2020-09-29 14:08:21 +00:00
// Close & restore buttons
$(close).click(function () {
History.loading = false;
onClose();
closeUI();
});
$(restore).click(function () {
UI.confirm(Messages.history_restorePrompt, function (yes) {
if (!yes) { return; }
closeUI();
History.loading = false;
onRevert();
UI.log(Messages.history_restoreDone);
});
});
2020-09-18 15:54:57 +00:00
};
display();
2020-09-21 12:51:13 +00:00
2020-09-21 13:47:49 +00:00
showVersion(true);
2020-09-21 12:51:13 +00:00
2020-09-18 15:54:57 +00:00
//return void loadMoreOOHistory();
};
return History;
});