|
|
@ -13,6 +13,7 @@ define([
|
|
|
|
'/common/common-ui-elements.js',
|
|
|
|
'/common/common-ui-elements.js',
|
|
|
|
'/common/common-thumbnail.js',
|
|
|
|
'/common/common-thumbnail.js',
|
|
|
|
'/common/common-feedback.js',
|
|
|
|
'/common/common-feedback.js',
|
|
|
|
|
|
|
|
'/common/inner/snapshots.js',
|
|
|
|
'/customize/application_config.js',
|
|
|
|
'/customize/application_config.js',
|
|
|
|
'/bower_components/chainpad/chainpad.dist.js',
|
|
|
|
'/bower_components/chainpad/chainpad.dist.js',
|
|
|
|
'/common/test.js',
|
|
|
|
'/common/test.js',
|
|
|
@ -35,6 +36,7 @@ define([
|
|
|
|
UIElements,
|
|
|
|
UIElements,
|
|
|
|
Thumb,
|
|
|
|
Thumb,
|
|
|
|
Feedback,
|
|
|
|
Feedback,
|
|
|
|
|
|
|
|
Snapshots,
|
|
|
|
AppConfig,
|
|
|
|
AppConfig,
|
|
|
|
ChainPad,
|
|
|
|
ChainPad,
|
|
|
|
Test)
|
|
|
|
Test)
|
|
|
@ -43,6 +45,9 @@ define([
|
|
|
|
|
|
|
|
|
|
|
|
var UNINITIALIZED = 'UNINITIALIZED';
|
|
|
|
var UNINITIALIZED = 'UNINITIALIZED';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// History and snapshots mode shouldn't receive realtime data or push to chainpad
|
|
|
|
|
|
|
|
var unsyncMode = false;
|
|
|
|
|
|
|
|
|
|
|
|
var STATE = Object.freeze({
|
|
|
|
var STATE = Object.freeze({
|
|
|
|
DISCONNECTED: 'DISCONNECTED',
|
|
|
|
DISCONNECTED: 'DISCONNECTED',
|
|
|
|
FORGOTTEN: 'FORGOTTEN',
|
|
|
|
FORGOTTEN: 'FORGOTTEN',
|
|
|
@ -50,7 +55,6 @@ define([
|
|
|
|
INFINITE_SPINNER: 'INFINITE_SPINNER',
|
|
|
|
INFINITE_SPINNER: 'INFINITE_SPINNER',
|
|
|
|
ERROR: 'ERROR',
|
|
|
|
ERROR: 'ERROR',
|
|
|
|
INITIALIZING: 'INITIALIZING',
|
|
|
|
INITIALIZING: 'INITIALIZING',
|
|
|
|
HISTORY_MODE: 'HISTORY_MODE',
|
|
|
|
|
|
|
|
READY: 'READY'
|
|
|
|
READY: 'READY'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
@ -93,6 +97,7 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var onLocal;
|
|
|
|
var textContentGetter;
|
|
|
|
var textContentGetter;
|
|
|
|
var titleRecommender = function () { return false; };
|
|
|
|
var titleRecommender = function () { return false; };
|
|
|
|
var contentGetter = function () { return UNINITIALIZED; };
|
|
|
|
var contentGetter = function () { return UNINITIALIZED; };
|
|
|
@ -125,20 +130,38 @@ define([
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var makeSnapshot = function (title) {
|
|
|
|
|
|
|
|
var sframeChan = common.getSframeChannel();
|
|
|
|
|
|
|
|
sframeChan.query("Q_GET_LAST_HASH", null, function (err, obj) {
|
|
|
|
|
|
|
|
if (err || (obj && obj.error)) { return void UI.warn(Messages.error); }
|
|
|
|
|
|
|
|
var hash = obj.hash;
|
|
|
|
|
|
|
|
if (!hash) { return void UI.warn(Messages.error); }
|
|
|
|
|
|
|
|
var md = Util.clone(cpNfInner.metadataMgr.getMetadata());
|
|
|
|
|
|
|
|
var snapshots = md.snapshots = md.snapshots || {};
|
|
|
|
|
|
|
|
if (snapshots[hash]) { return void UI.warn(Messages.error); } // XXX EEXISTS
|
|
|
|
|
|
|
|
snapshots[hash] = {
|
|
|
|
|
|
|
|
title: title,
|
|
|
|
|
|
|
|
time: +new Date()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
cpNfInner.metadataMgr.updateMetadata(md);
|
|
|
|
|
|
|
|
onLocal();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var stateChange = function (newState, text) {
|
|
|
|
var stateChange = function (newState, text) {
|
|
|
|
var wasEditable = (state === STATE.READY);
|
|
|
|
var wasEditable = (state === STATE.READY && !unsyncMode);
|
|
|
|
if (state === STATE.DELETED || state === STATE.ERROR) { return; }
|
|
|
|
if (newState !== state) {
|
|
|
|
if (state === STATE.INFINITE_SPINNER && newState !== STATE.READY) { return; }
|
|
|
|
if (state === STATE.DELETED || state === STATE.ERROR) { return; }
|
|
|
|
if (newState === STATE.INFINITE_SPINNER || newState === STATE.DELETED) {
|
|
|
|
if (state === STATE.INFINITE_SPINNER && newState !== STATE.READY) { return; }
|
|
|
|
state = newState;
|
|
|
|
if (newState === STATE.INFINITE_SPINNER || newState === STATE.DELETED) {
|
|
|
|
} else if (newState === STATE.ERROR) {
|
|
|
|
state = newState;
|
|
|
|
state = newState;
|
|
|
|
} else if (newState === STATE.ERROR) {
|
|
|
|
} else if (state === STATE.DISCONNECTED && newState !== STATE.INITIALIZING) {
|
|
|
|
state = newState;
|
|
|
|
throw new Error("Cannot transition from DISCONNECTED to " + newState); // FIXME we are getting "DISCONNECTED to READY" on prod
|
|
|
|
} else if (state === STATE.DISCONNECTED && newState !== STATE.INITIALIZING) {
|
|
|
|
} else if (state !== STATE.READY && newState === STATE.HISTORY_MODE) {
|
|
|
|
throw new Error("Cannot transition from DISCONNECTED to " + newState); // FIXME we are getting "DISCONNECTED to READY" on prod
|
|
|
|
throw new Error("Cannot transition from " + state + " to " + newState);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
state = newState;
|
|
|
|
state = newState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (state) {
|
|
|
|
switch (state) {
|
|
|
|
case STATE.DISCONNECTED:
|
|
|
|
case STATE.DISCONNECTED:
|
|
|
@ -187,8 +210,9 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wasEditable !== (state === STATE.READY)) {
|
|
|
|
var isEditable = (state === STATE.READY && !unsyncMode);
|
|
|
|
evEditableStateChange.fire(state === STATE.READY);
|
|
|
|
if (wasEditable !== isEditable) {
|
|
|
|
|
|
|
|
evEditableStateChange.fire(isEditable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -205,8 +229,8 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var onLocal;
|
|
|
|
|
|
|
|
var onRemote = function () {
|
|
|
|
var onRemote = function () {
|
|
|
|
|
|
|
|
if (unsyncMode) { return; }
|
|
|
|
if (state !== STATE.READY) { return; }
|
|
|
|
if (state !== STATE.READY) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
var oldContent = normalize(contentGetter());
|
|
|
|
var oldContent = normalize(contentGetter());
|
|
|
@ -261,15 +285,47 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var setUnsyncMode = function (bool) {
|
|
|
|
|
|
|
|
if (unsyncMode === bool) { return; }
|
|
|
|
|
|
|
|
unsyncMode = bool;
|
|
|
|
|
|
|
|
evEditableStateChange.fire(state === STATE.READY && !unsyncMode);
|
|
|
|
|
|
|
|
stateChange(state);
|
|
|
|
|
|
|
|
};
|
|
|
|
var setHistoryMode = function (bool, update) {
|
|
|
|
var setHistoryMode = function (bool, update) {
|
|
|
|
cpNfInner.metadataMgr.setHistory(bool);
|
|
|
|
cpNfInner.metadataMgr.setHistory(bool);
|
|
|
|
toolbar.setHistory(bool);
|
|
|
|
toolbar.setHistory(bool);
|
|
|
|
stateChange((bool) ? STATE.HISTORY_MODE : STATE.READY);
|
|
|
|
setUnsyncMode(bool);
|
|
|
|
if (!bool && update) { onRemote(); }
|
|
|
|
if (!bool && update) { onRemote(); }
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
setTimeout(cpNfInner.metadataMgr.refresh);
|
|
|
|
setTimeout(cpNfInner.metadataMgr.refresh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var closeSnapshot = function (restore) {
|
|
|
|
|
|
|
|
setUnsyncMode(false);
|
|
|
|
|
|
|
|
if (restore) {
|
|
|
|
|
|
|
|
onLocal();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
onRemote();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var loadSnapshot = function (hash, data) {
|
|
|
|
|
|
|
|
setUnsyncMode(true);
|
|
|
|
|
|
|
|
Snapshots.create(common, {
|
|
|
|
|
|
|
|
$toolbar: $(toolbarContainer),
|
|
|
|
|
|
|
|
hash: hash,
|
|
|
|
|
|
|
|
data: data,
|
|
|
|
|
|
|
|
close: closeSnapshot,
|
|
|
|
|
|
|
|
applyVal: function (val) {
|
|
|
|
|
|
|
|
var newContent = JSON.parse(val);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
var meta = extractMetadata(newContent);
|
|
|
|
|
|
|
|
cpNfInner.metadataMgr.updateMetadata(meta);
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
contentUpdate(normalize(newContent) || ["BODY",{},[]], function (h) {
|
|
|
|
|
|
|
|
return h;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
var hasChanged = function (content) {
|
|
|
|
var hasChanged = function (content) {
|
|
|
@ -286,6 +342,7 @@ define([
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
onLocal = function (/*padChange*/) {
|
|
|
|
onLocal = function (/*padChange*/) {
|
|
|
|
|
|
|
|
if (unsyncMode) { return; }
|
|
|
|
if (state !== STATE.READY) { return; }
|
|
|
|
if (state !== STATE.READY) { return; }
|
|
|
|
if (readOnly) { return; }
|
|
|
|
if (readOnly) { return; }
|
|
|
|
|
|
|
|
|
|
|
@ -359,7 +416,7 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpNfInner.metadataMgr.updateMetadata(metadata);
|
|
|
|
cpNfInner.metadataMgr.updateMetadata(metadata);
|
|
|
|
newContent = normalize(newContent);
|
|
|
|
newContent = normalize(newContent);
|
|
|
|
if (state !== STATE.HISTORY_MODE) {
|
|
|
|
if (!unsyncMode) {
|
|
|
|
contentUpdate(newContent, waitFor);
|
|
|
|
contentUpdate(newContent, waitFor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -379,7 +436,7 @@ define([
|
|
|
|
evOnDefaultContentNeeded.fire();
|
|
|
|
evOnDefaultContentNeeded.fire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).nThen(function () {
|
|
|
|
}).nThen(function () {
|
|
|
|
if (state !== STATE.HISTORY_MODE) {
|
|
|
|
if (!unsyncMode) {
|
|
|
|
stateChange(STATE.READY);
|
|
|
|
stateChange(STATE.READY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
firstConnection = false;
|
|
|
|
firstConnection = false;
|
|
|
@ -419,7 +476,6 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var onConnectionChange = function (info) {
|
|
|
|
var onConnectionChange = function (info) {
|
|
|
|
if (state === STATE.HISTORY_MODE) { return; }
|
|
|
|
|
|
|
|
if (state === STATE.DELETED) { return; }
|
|
|
|
if (state === STATE.DELETED) { return; }
|
|
|
|
stateChange(info.state ? STATE.INITIALIZING : STATE.DISCONNECTED, info.permanent);
|
|
|
|
stateChange(info.state ? STATE.INITIALIZING : STATE.DISCONNECTED, info.permanent);
|
|
|
|
/*if (info.state) {
|
|
|
|
/*if (info.state) {
|
|
|
@ -716,6 +772,12 @@ define([
|
|
|
|
$hist.addClass('cp-hidden-if-readonly');
|
|
|
|
$hist.addClass('cp-hidden-if-readonly');
|
|
|
|
toolbar.$drawer.append($hist);
|
|
|
|
toolbar.$drawer.append($hist);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var $snapshot = common.createButton('snapshots', true, {
|
|
|
|
|
|
|
|
make: makeSnapshot,
|
|
|
|
|
|
|
|
load: loadSnapshot
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
toolbar.$drawer.append($snapshot);
|
|
|
|
|
|
|
|
|
|
|
|
var $copy = common.createButton('copy', true);
|
|
|
|
var $copy = common.createButton('copy', true);
|
|
|
|
toolbar.$drawer.append($copy);
|
|
|
|
toolbar.$drawer.append($copy);
|
|
|
|
|
|
|
|
|
|
|
|