cryptpad/www/code/main.js

352 lines
12 KiB
JavaScript
Raw Normal View History

define([
'jquery',
2016-06-06 10:14:07 +00:00
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/textpatcher/TextPatcher.js',
2017-05-04 14:08:12 +00:00
'/common/toolbar2.js',
'json.sortify',
2016-06-06 10:14:07 +00:00
'/bower_components/chainpad-json-validator/json-ot.js',
2016-06-21 13:17:09 +00:00
'/common/cryptpad-common.js',
'/common/cryptget.js',
], function ($, Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Cryptget) {
var Messages = Cryptpad.Messages;
var module = window.APP = {
Cryptpad: Cryptpad,
};
2017-02-17 14:39:34 +00:00
$(function () {
Cryptpad.addLoadingScreen();
2017-02-17 14:39:34 +00:00
var ifrw = module.ifrw = $('#pad-iframe')[0].contentWindow;
var stringify = function (obj) {
return JSONSortify(obj);
};
var toolbar;
var editor;
2016-04-22 22:15:39 +00:00
2016-06-21 13:17:09 +00:00
var secret = Cryptpad.getSecrets();
2016-09-20 16:22:40 +00:00
var readOnly = secret.keys && !secret.keys.editKeyStr;
if (!secret.keys) {
secret.keys = secret.key;
}
2017-05-04 14:37:25 +00:00
var onConnectError = function () {
2016-12-21 17:33:21 +00:00
Cryptpad.errorLoadingScreen(Messages.websocketError);
};
var andThen = function (CMeditor) {
var CodeMirror = Cryptpad.createCodemirror(CMeditor, ifrw, Cryptpad);
editor = CodeMirror.editor;
2016-04-22 22:15:39 +00:00
var $bar = $('#pad-iframe')[0].contentWindow.$('#cme_toolbox');
2017-04-21 15:31:47 +00:00
var isHistoryMode = false;
var setEditable = module.setEditable = function (bool) {
if (readOnly && bool) { return; }
editor.setOption('readOnly', !bool);
};
var Title;
var UserList;
var Metadata;
2016-05-20 11:39:40 +00:00
var config = {
initialState: '{}',
2016-10-04 15:13:15 +00:00
websocketURL: Cryptpad.getWebsocketURL(),
2016-06-21 13:17:09 +00:00
channel: secret.channel,
// our public key
validateKey: secret.keys.validateKey || undefined,
readOnly: readOnly,
crypto: Crypto.createEncryptor(secret.keys),
2017-02-13 10:59:49 +00:00
network: Cryptpad.getNetwork(),
2017-02-09 16:20:13 +00:00
transformFunction: JsonOT.validate,
2016-05-20 11:39:40 +00:00
};
var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); };
2017-04-21 15:31:47 +00:00
var setHistory = function (bool, update) {
isHistoryMode = bool;
setEditable(!bool);
if (!bool && update) {
config.onRemote();
}
};
2016-05-20 11:39:40 +00:00
var initializing = true;
2016-10-25 15:29:13 +00:00
var stringifyInner = function (textValue) {
var obj = {
content: textValue,
metadata: {
users: UserList.userData,
defaultTitle: Title.defaultTitle
2016-10-25 15:29:13 +00:00
}
};
2017-01-12 14:15:10 +00:00
if (!initializing) {
obj.metadata.title = Title.title;
2017-01-12 14:15:10 +00:00
}
2016-10-25 15:29:13 +00:00
// set mode too...
obj.highlightMode = CodeMirror.highlightMode;
2016-10-25 15:29:13 +00:00
// stringify the json and send it into chainpad
return stringify(obj);
};
2016-05-20 11:39:40 +00:00
var onLocal = config.onLocal = function () {
if (initializing) { return; }
2017-04-21 15:31:47 +00:00
if (isHistoryMode) { return; }
2016-09-22 14:09:20 +00:00
if (readOnly) { return; }
2016-05-20 11:39:40 +00:00
editor.save();
var textValue = canonicalize(CodeMirror.$textarea.val());
2016-10-25 15:29:13 +00:00
var shjson = stringifyInner(textValue);
2016-05-20 11:39:40 +00:00
module.patchText(shjson);
if (module.realtime.getUserDoc() !== shjson) {
console.error("realtime.getUserDoc() !== shjson");
}
};
2016-10-25 15:29:13 +00:00
config.onInit = function (info) {
UserList = Cryptpad.createUserList(info, config.onLocal, Cryptget, Cryptpad);
var titleCfg = { getHeadingText: CodeMirror.getHeadingText };
Title = Cryptpad.createTitle(titleCfg, config.onLocal, Cryptpad);
Metadata = Cryptpad.createMetadata(UserList, Title);
2017-04-21 15:31:47 +00:00
var configTb = {
2017-05-04 14:08:12 +00:00
displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit'],
userList: UserList.getToolbarConfig(),
share: {
secret: secret,
channel: info.channel
},
title: Title.getTitleConfig(),
2017-05-04 14:08:12 +00:00
common: Cryptpad,
readOnly: readOnly,
ifrw: ifrw,
2017-05-04 14:08:12 +00:00
realtime: info.realtime,
network: info.network,
$container: $bar
};
2017-05-04 14:08:12 +00:00
toolbar = module.toolbar = Toolbar.create(configTb);
2016-06-29 10:00:12 +00:00
Title.setToolbar(toolbar);
CodeMirror.init(config.onLocal, Title, toolbar);
var $rightside = toolbar.$rightside;
2016-09-20 09:35:57 +00:00
var editHash;
if (!readOnly) {
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
}
2017-04-21 15:31:47 +00:00
/* add a history button */
2017-05-10 11:39:14 +00:00
var histConfig = {
onLocal: config.onLocal(),
onRemote: config.onRemote(),
setHistory: setHistory,
applyVal: function (val) {
var remoteDoc = JSON.parse(val || '{}').content;
2017-04-21 15:31:47 +00:00
editor.setValue(remoteDoc || '');
editor.save();
2017-05-10 11:39:14 +00:00
},
$toolbar: $bar
2017-04-21 15:31:47 +00:00
};
var $hist = Cryptpad.createButton('history', true, {histConfig: histConfig});
2017-04-14 16:36:36 +00:00
$rightside.append($hist);
/* save as template */
if (!Cryptpad.isTemplate(window.location.href)) {
var templateObj = {
rt: info.realtime,
Crypt: Cryptget,
getTitle: Title.getTitle
};
var $templateButton = Cryptpad.createButton('template', true, templateObj);
$rightside.append($templateButton);
}
/* add an export button */
var $export = Cryptpad.createButton('export', true, {}, CodeMirror.exportText);
$rightside.append($export);
if (!readOnly) {
/* add an import button */
var $import = Cryptpad.createButton('import', true, {}, CodeMirror.importText);
$rightside.append($import);
}
/* add a forget button */
2017-05-04 14:37:25 +00:00
var forgetCb = function (err) {
if (err) { return; }
2017-02-24 14:22:26 +00:00
setEditable(false);
};
var $forgetPad = Cryptpad.createButton('forget', true, {}, forgetCb);
$rightside.append($forgetPad);
2016-06-30 08:51:19 +00:00
if (!readOnly) {
CodeMirror.configureLanguage(CodeMirror.configureTheme);
}
else {
CodeMirror.configureTheme();
}
2016-06-29 09:51:53 +00:00
// set the hash
if (!readOnly) { Cryptpad.replaceHash(editHash); }
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onReady = function (info) {
if (module.realtime !== info.realtime) {
var realtime = module.realtime = info.realtime;
module.patchText = TextPatcher.create({
realtime: realtime,
//logging: true
});
}
var userDoc = module.realtime.getUserDoc();
var isNew = false;
if (userDoc === "" || userDoc === "{}") { isNew = true; }
var newDoc = "";
if(userDoc !== "") {
var hjson = JSON.parse(userDoc);
if (typeof (hjson) !== 'object' || Array.isArray(hjson)) {
var errorText = Messages.typeError;
Cryptpad.errorLoadingScreen(errorText);
throw new Error(errorText);
}
newDoc = hjson.content;
if (hjson.highlightMode) {
CodeMirror.setMode(hjson.highlightMode);
}
}
if (!CodeMirror.highlightMode) {
CodeMirror.setMode('javascript');
console.log("%s => %s", CodeMirror.highlightMode, CodeMirror.$language.val());
}
// Update the user list (metadata) from the hyperjson
Metadata.update(userDoc);
if (newDoc) {
editor.setValue(newDoc);
}
if (Cryptpad.initialName && Title.isDefaultTitle()) {
Title.updateTitle(Cryptpad.initialName);
2017-02-21 16:42:58 +00:00
}
2016-12-21 17:33:21 +00:00
Cryptpad.removeLoadingScreen();
setEditable(true);
2016-04-22 22:15:39 +00:00
initializing = false;
onLocal(); // push local state to avoid parse errors later.
if (readOnly) { return; }
UserList.getLastName(toolbar.$userNameButton, isNew);
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onRemote = function () {
2016-04-22 22:15:39 +00:00
if (initializing) { return; }
2017-04-21 15:31:47 +00:00
if (isHistoryMode) { return; }
var oldDoc = canonicalize(CodeMirror.$textarea.val());
var shjson = module.realtime.getUserDoc();
// Update the user list (metadata) from the hyperjson
Metadata.update(shjson);
var hjson = JSON.parse(shjson);
var remoteDoc = hjson.content;
var highlightMode = hjson.highlightMode;
if (highlightMode && highlightMode !== module.highlightMode) {
CodeMirror.setMode(highlightMode);
}
CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
if (!readOnly) {
var textValue = canonicalize(CodeMirror.$textarea.val());
2016-10-25 15:29:13 +00:00
var shjson2 = stringifyInner(textValue);
if (shjson2 !== shjson) {
console.error("shjson2 !== shjson");
TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2));
module.patchText(shjson2);
}
}
if (oldDoc !== remoteDoc) { Cryptpad.notify(); }
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onAbort = function () {
2016-04-22 22:15:39 +00:00
// inform of network disconnect
setEditable(false);
toolbar.failed();
2017-03-02 16:45:48 +00:00
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
2016-04-22 22:15:39 +00:00
};
2017-05-04 14:37:25 +00:00
config.onConnectionChange = function (info) {
setEditable(info.state);
toolbar.failed();
if (info.state) {
initializing = true;
toolbar.reconnecting(info.myId);
Cryptpad.findOKButton().click();
} else {
2017-03-02 16:45:48 +00:00
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
}
};
2017-05-04 14:37:25 +00:00
config.onError = onConnectError;
2016-12-08 15:01:46 +00:00
2017-05-04 14:37:25 +00:00
module.realtime = Realtime.start(config);
2016-04-22 22:15:39 +00:00
editor.on('change', onLocal);
Cryptpad.onLogout(function () { setEditable(false); });
};
var interval = 100;
var second = function (CM) {
2017-05-04 14:37:25 +00:00
Cryptpad.ready(function () {
andThen(CM);
2017-04-13 10:18:08 +00:00
Cryptpad.reportAppUsage();
});
Cryptpad.onError(function (info) {
if (info && info.type === "store") {
onConnectError();
}
});
};
var first = function () {
if (ifrw.CodeMirror) {
// it exists, call your continuation
second(ifrw.CodeMirror);
} else {
console.log("CodeMirror was not defined. Trying again in %sms", interval);
// try again in 'interval' ms
setTimeout(first, interval);
}
};
first();
});
});