cryptpad/www/style/main.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-02-12 10:45:40 +00:00
define([
'/api/config?cb=' + Math.random().toString(16).substring(2),
2016-06-06 10:14:07 +00:00
'/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/textpatcher/TextPatcher.amd.js',
2016-06-21 13:17:09 +00:00
'/common/cryptpad-common.js',
2016-02-12 10:45:40 +00:00
'/bower_components/jquery/dist/jquery.min.js',
'/customize/pad.js'
2016-06-21 13:17:09 +00:00
], function (Config, Realtime, Crypto, TextPatcher, Cryptpad) {
// TODO consider adding support for less.js
2016-02-15 15:07:46 +00:00
var $ = window.jQuery;
2016-02-12 10:45:40 +00:00
2016-04-25 14:06:10 +00:00
var $style = $('style').first(),
$edit = $('#edit');
2016-04-22 16:54:24 +00:00
var module = window.APP = {};
2016-02-12 10:45:40 +00:00
2016-06-21 13:17:09 +00:00
var secret = Cryptpad.getSecrets();
2016-04-22 16:54:24 +00:00
var config = {
websocketURL: Config.websocketURL,
2016-06-21 13:17:09 +00:00
channel: secret.channel,
crypto: Crypto.createEncryptor(secret.key),
2016-04-22 16:54:24 +00:00
};
2016-02-12 10:45:40 +00:00
2016-04-22 16:54:24 +00:00
var userName = module.userName = config.userName = Crypto.rand64(8);
2016-02-12 10:45:40 +00:00
var lazyDraw = (function () {
var to,
delay = 500;
return function (content) {
2016-02-12 15:21:52 +00:00
if (to) { clearTimeout(to); }
2016-02-12 10:45:40 +00:00
to = setTimeout(function () {
$style.text(content);
},delay);
};
}());
2016-04-22 16:54:24 +00:00
var draw = function (content) { lazyDraw(content); };
var initializing = true;
var onInit = config.onInit = function (info) {
2016-06-21 13:17:09 +00:00
window.location.hash = info.channel + secret.key;
2016-04-22 16:54:24 +00:00
var realtime = module.realtime = info.realtime;
module.patchText = TextPatcher.create({
realtime: realtime,
logging: true,
});
$(window).on('hashchange', function() {
window.location.reload();
});
};
var onReady = config.onReady = function (info) {
var userDoc = module.realtime.getUserDoc();
draw(userDoc);
console.log("Ready");
initializing = false;
2016-02-12 10:45:40 +00:00
};
2016-04-22 16:54:24 +00:00
var onRemote = config.onRemote = function () {
draw(module.realtime.getUserDoc());
};
2016-02-12 10:45:40 +00:00
2016-04-22 16:54:24 +00:00
var onAbort = config.onAbort = function (info) {
// notify the user of the abort
window.alert("Network Connection Lost");
};
2016-04-22 16:54:24 +00:00
var onLocal = config.onLocal = function () {
// nope
};
2016-04-22 16:54:24 +00:00
$edit.attr('href', '/text/'+ window.location.hash);
2016-04-22 16:54:24 +00:00
var rt = Realtime.start(config);
2016-02-12 10:45:40 +00:00
});