Move code to common-history

This commit is contained in:
yflory 2017-05-10 13:39:14 +02:00
parent 0f8ef2f516
commit c00158d23a
5 changed files with 59 additions and 105 deletions

View File

@ -150,34 +150,17 @@ define([
} }
/* add a history button */ /* add a history button */
var histConfig = {}; var histConfig = {
histConfig.onRender = function (val) { onLocal: config.onLocal(),
if (typeof val === "undefined") { return; } onRemote: config.onRemote(),
try { setHistory: setHistory,
var hjson = JSON.parse(val || '{}'); applyVal: function (val) {
var remoteDoc = hjson.content; var remoteDoc = JSON.parse(val || '{}').content;
editor.setValue(remoteDoc || ''); editor.setValue(remoteDoc || '');
editor.save(); editor.save();
} catch (e) { },
// Probably a parse error $toolbar: $bar
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;
var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig});
$rightside.append($hist); $rightside.append($hist);

View File

@ -24,7 +24,6 @@ define([
var wcId = common.hrefToHexChannelId(config.href || window.location.href); var wcId = common.hrefToHexChannelId(config.href || window.location.href);
console.log(wcId);
var createRealtime = function () { var createRealtime = function () {
return ChainPad.create({ return ChainPad.create({
userName: 'history', userName: 'history',
@ -80,11 +79,32 @@ define([
if (History.loading) { return void console.error("History is already being loaded..."); } if (History.loading) { return void console.error("History is already being loaded..."); }
History.loading = true; History.loading = true;
var $toolbar = config.$toolbar; var $toolbar = config.$toolbar;
var noFunc = function () {};
var render = config.onRender || noFunc; if (!config.applyVal || !config.setHistory || !config.onLocal || !config.onRemote) {
var onClose = config.onClose || noFunc; throw new Error("Missing config element: applyVal, onLocal, onRemote, setHistory");
var onRevert = config.onRevert || noFunc; }
var onReady = config.onReady || noFunc;
// 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 Messages = common.Messages;

View File

@ -2529,7 +2529,6 @@ define([
setEditable(!bool); setEditable(!bool);
if (!bool && update) { if (!bool && update) {
history.onLeaveHistory(); history.onLeaveHistory();
//init(); //TODO real proxy here
} }
}; };
@ -2682,33 +2681,20 @@ define([
} }
/* add a history button */ /* add a history button */
var histConfig = {}; var histConfig = {
histConfig.onRender = function (val) { onLocal: function () {
if (typeof val === "undefined") { return; } proxy.drive = history.currentObj.drive;
try { },
onRemote: function () {},
setHistory: setHistory,
applyVal: function (val) {
var obj = JSON.parse(val || '{}'); var obj = JSON.parse(val || '{}');
history.currentObj = obj; history.currentObj = obj;
history.onEnterHistory(obj); history.onEnterHistory(obj);
} catch (e) { },
// Probably a parse error $toolbar: APP.$bar,
logError(e); href: window.location.origin + window.location.pathname + APP.hash
}
}; };
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);
};
histConfig.$toolbar = APP.$bar;
histConfig.href = window.location.origin + window.location.pathname + APP.hash;
var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig});
$rightside.append($hist); $rightside.append($hist);

View File

@ -499,31 +499,13 @@ define([
} }
/* add a history button */ /* add a history button */
var histConfig = {}; var histConfig = {
histConfig.onRender = function (val) { onLocal: realtimeOptions.onLocal(),
if (typeof val === "undefined") { return; } onRemote: realtimeOptions.onRemote(),
try { setHistory: setHistory,
applyHjson(val || '["BODY",{},[]]'); applyVal: function (val) { applyHjson(val || '["BODY",{},[]]'); },
} catch (e) { $toolbar: $bar
// 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);
};
histConfig.$toolbar = $bar;
var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig});
$rightside.append($hist); $rightside.append($hist);

View File

@ -293,34 +293,17 @@ define([
} }
/* add a history button */ /* add a history button */
var histConfig = {}; var histConfig = {
histConfig.onRender = function (val) { onLocal: config.onLocal(),
if (typeof val === "undefined") { return; } onRemote: config.onRemote(),
try { setHistory: setHistory,
var hjson = JSON.parse(val || '{}'); applyVal: function (val) {
var remoteDoc = hjson.content; var remoteDoc = JSON.parse(val || '{}').content;
editor.setValue(remoteDoc || ''); editor.setValue(remoteDoc || '');
editor.save(); editor.save();
} catch (e) { },
// Probably a parse error $toolbar: $bar
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;
var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig}); var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig});
$rightside.append($hist); $rightside.append($hist);