cryptpad/www/common/sframe-common.js

326 lines
11 KiB
JavaScript
Raw Normal View History

2017-08-17 22:28:54 +08:00
define([
2017-08-23 17:04:44 +08:00
'jquery',
2017-08-17 22:56:18 +08:00
'/bower_components/nthen/index.js',
2017-08-21 21:20:38 +08:00
'/customize/messages.js',
2017-08-17 22:28:54 +08:00
'/common/sframe-chainpad-netflux-inner.js',
2017-08-18 00:28:05 +08:00
'/common/sframe-channel.js',
'/common/sframe-common-title.js',
'/common/sframe-common-interface.js',
2017-08-21 23:40:21 +08:00
'/common/sframe-common-history.js',
'/common/sframe-common-file.js',
2017-09-08 00:56:58 +08:00
'/common/sframe-common-codemirror.js',
'/common/metadata-manager.js',
2017-08-18 00:28:05 +08:00
2017-08-22 00:30:51 +08:00
'/customize/application_config.js',
'/common/cryptpad-common.js',
'/common/common-realtime.js',
'/common/common-util.js'
2017-09-11 21:52:14 +08:00
], function (
$,
nThen,
Messages,
CpNfInner,
SFrameChannel,
Title,
UI,
History,
File,
CodeMirror,
2017-09-08 00:56:58 +08:00
MetadataMgr,
2017-09-11 21:52:14 +08:00
AppConfig,
Cryptpad,
CommonRealtime,
Util
) {
2017-08-17 22:28:54 +08:00
// Chainpad Netflux Inner
2017-08-17 22:56:18 +08:00
var funcs = {};
var ctx = {};
2017-08-21 23:40:21 +08:00
funcs.Messages = Messages;
var evRealtimeSynced = Util.mkEvent(true);
2017-08-17 22:56:18 +08:00
funcs.startRealtime = function (options) {
if (ctx.cpNfInner) { return ctx.cpNfInner; }
options.sframeChan = ctx.sframeChan;
options.metadataMgr = ctx.metadataMgr;
2017-08-17 22:56:18 +08:00
ctx.cpNfInner = CpNfInner.start(options);
ctx.cpNfInner.metadataMgr.onChangeLazy(options.onLocal);
ctx.cpNfInner.whenRealtimeSyncs(function () { evRealtimeSynced.fire(); });
2017-08-17 22:56:18 +08:00
return ctx.cpNfInner;
};
2017-09-07 00:26:10 +08:00
funcs.getMetadataMgr = function () { return ctx.metadataMgr; };
funcs.getCryptpadCommon = function () { return Cryptpad; };
funcs.getSframeChannel = function () { return ctx.sframeChan; };
funcs.getAppConfig = function () { return AppConfig; };
2017-08-21 23:40:21 +08:00
2017-09-08 00:56:58 +08:00
funcs.isLoggedIn = function () {
2017-09-13 00:40:11 +08:00
return ctx.metadataMgr.getPrivateData().accountName;
2017-08-17 22:28:54 +08:00
};
2017-09-07 00:26:10 +08:00
// MISC
// Call the selected function with 'funcs' as a (new) first parameter
var callWithCommon = function (f) {
return function () {
[].unshift.call(arguments, funcs);
return f.apply(null, arguments);
};
};
// UI
2017-09-07 00:26:10 +08:00
funcs.createUserAdminMenu = callWithCommon(UI.createUserAdminMenu);
funcs.initFilePicker = callWithCommon(UI.initFilePicker);
funcs.openFilePicker = callWithCommon(UI.openFilePicker);
funcs.openTemplatePicker = callWithCommon(UI.openTemplatePicker);
funcs.displayAvatar = callWithCommon(UI.displayAvatar);
funcs.createButton = callWithCommon(UI.createButton);
2017-09-21 23:56:24 +08:00
funcs.createUsageBar = callWithCommon(UI.createUsageBar);
2017-08-21 23:40:21 +08:00
// History
2017-09-07 00:26:10 +08:00
funcs.getHistory = callWithCommon(History.create);
2017-08-21 23:40:21 +08:00
2017-08-18 00:28:05 +08:00
// Title module
2017-09-07 00:26:10 +08:00
funcs.createTitle = callWithCommon(Title.create);
// Files
funcs.uploadFile = callWithCommon(File.uploadFile);
funcs.createFileManager = callWithCommon(File.create);
2017-09-13 00:40:11 +08:00
funcs.getMediatagScript = function () {
var origin = ctx.metadataMgr.getPrivateData().origin;
return '<script src="' + origin + '/common/media-tag-nacl.min.js"></script>';
};
funcs.getMediatagFromHref = function (href) {
var parsed = Cryptpad.parsePadUrl(href);
var secret = Cryptpad.getSecrets('file', parsed.hash);
var data = ctx.metadataMgr.getPrivateData();
if (secret.keys && secret.channel) {
var cryptKey = secret.keys && secret.keys.fileKeyStr;
var hexFileName = Cryptpad.base64ToHex(secret.channel);
var origin = data.fileHost || data.origin;
var src = origin + Cryptpad.getBlobPathFromHex(hexFileName);
return '<media-tag src="' + src + '" data-crypto-key="cryptpad:' + cryptKey + '">' +
'</media-tag>';
}
return;
};
2017-09-13 22:19:26 +08:00
funcs.getFileSize = function (href, cb) {
var channelId = Cryptpad.hrefToHexChannelId(href);
funcs.sendAnonRpcMsg("GET_FILE_SIZE", channelId, function (data) {
if (!data) { return void cb("No response"); }
if (data.error) { return void cb(data.error); }
if (data.response && data.response.length && typeof(data.response[0]) === 'number') {
return void cb(void 0, data.response[0]);
} else {
cb('INVALID_RESPONSE');
}
});
};
2017-08-18 00:28:05 +08:00
2017-09-08 00:56:58 +08:00
// CodeMirror
funcs.initCodeMirrorApp = callWithCommon(CodeMirror.create);
2017-08-18 00:28:05 +08:00
// Window
funcs.logout = function (cb) {
2017-09-07 00:26:10 +08:00
cb = cb || $.noop;
ctx.sframeChan.query('Q_LOGOUT', null, cb);
};
2017-08-29 17:20:02 +08:00
funcs.notify = function () {
ctx.sframeChan.event('EV_NOTIFY');
};
funcs.setTabTitle = function (newTitle) {
ctx.sframeChan.event('EV_SET_TAB_TITLE', newTitle);
};
2017-08-29 17:20:02 +08:00
funcs.setLoginRedirect = function (cb) {
2017-09-07 00:26:10 +08:00
cb = cb || $.noop;
ctx.sframeChan.query('Q_SET_LOGIN_REDIRECT', null, cb);
};
2017-09-08 00:56:58 +08:00
funcs.isPresentUrl = function (cb) {
ctx.sframeChan.query('Q_PRESENT_URL_GET_VALUE', null, cb);
};
funcs.setPresentUrl = function (value) {
ctx.sframeChan.event('EV_PRESENT_URL_SET_VALUE', value);
};
// Store
2017-08-21 18:01:38 +08:00
funcs.sendAnonRpcMsg = function (msg, content, cb) {
ctx.sframeChan.query('Q_ANON_RPC_MESSAGE', {
msg: msg,
content: content
}, function (err, data) {
if (cb) { cb(data); }
});
};
2017-09-21 23:56:24 +08:00
funcs.getPinUsage = function (cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_PIN_GET_USAGE', null, function (err, data) {
cb(err || data.error, data.data);
});
};
2017-08-21 18:01:38 +08:00
2017-08-21 18:24:33 +08:00
funcs.isOverPinLimit = function (cb) {
ctx.sframeChan.query('Q_GET_PIN_LIMIT_STATUS', null, function (err, data) {
cb(data.error, data.overLimit, data.limits);
});
};
2017-08-21 23:40:21 +08:00
funcs.getFullHistory = function (realtime, cb) {
ctx.sframeChan.on('EV_RT_HIST_MESSAGE', function (content) {
realtime.message(content);
});
ctx.sframeChan.query('Q_GET_FULL_HISTORY', null, cb);
};
2017-08-31 00:47:50 +08:00
funcs.getPadAttribute = function (key, cb) {
ctx.sframeChan.query('Q_GET_PAD_ATTRIBUTE', {
key: key
}, function (err, res) {
cb (err || res.error, res.data);
});
};
funcs.setPadAttribute = function (key, value, cb) {
ctx.sframeChan.query('Q_SET_PAD_ATTRIBUTE', {
key: key,
value: value
}, cb);
};
funcs.getAttribute = function (key, cb) {
ctx.sframeChan.query('Q_GET_ATTRIBUTE', {
key: key
}, function (err, res) {
cb (err || res.error, res.data);
});
};
funcs.setAttribute = function (key, value, cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_SET_ATTRIBUTE', {
key: key,
value: value
}, cb);
};
2017-09-07 00:26:10 +08:00
funcs.isStrongestStored = function () {
var data = ctx.metadataMgr.getPrivateData();
2017-09-13 00:40:11 +08:00
if (data.availableHashes.fileHash) { return true; }
2017-09-07 00:26:10 +08:00
return !data.readOnly || !data.availableHashes.editHash;
2017-08-31 00:47:50 +08:00
};
2017-09-08 00:56:58 +08:00
funcs.setDisplayName = function (name, cb) {
cb = cb || $.noop;
ctx.sframeChan.query('Q_SETTINGS_SET_DISPLAY_NAME', name, cb);
};
// Friends
var pendingFriends = [];
funcs.getPendingFriends = function () {
return pendingFriends.slice();
};
funcs.sendFriendRequest = function (netfluxId) {
ctx.sframeChan.query('Q_SEND_FRIEND_REQUEST', netfluxId, $.noop);
pendingFriends.push(netfluxId);
};
// Feedback
2017-08-23 00:18:58 +08:00
funcs.feedback = function (action, force) {
if (force !== true) {
if (!action) { return; }
try {
if (!ctx.metadataMgr.getPrivateData().feedbackAllowed) { return; }
} catch (e) { return void console.error(e); }
}
var randomToken = Math.random().toString(16).replace(/0./, '');
2017-08-23 17:04:44 +08:00
//var origin = ctx.metadataMgr.getPrivateData().origin;
2017-08-23 00:18:58 +08:00
var href = /*origin +*/ '/common/feedback.html?' + action + '=' + randomToken;
$.ajax({
type: "HEAD",
url: href,
});
};
2017-09-07 00:26:10 +08:00
funcs.prepareFeedback = function (key) {
2017-08-23 00:18:58 +08:00
if (typeof(key) !== 'string') { return $.noop; }
2017-08-21 21:20:38 +08:00
2017-08-23 00:18:58 +08:00
var type = ctx.metadataMgr.getMetadata().type;
return function () {
funcs.feedback((key + (type? '_' + type: '')).toUpperCase());
};
};
2017-08-21 21:20:38 +08:00
2017-09-07 00:26:10 +08:00
// RESTRICTED
// Filepicker app
2017-09-05 17:35:15 +08:00
funcs.getFilesList = function (types, cb) {
ctx.sframeChan.query('Q_GET_FILES_LIST', types, function (err, data) {
cb(err || data.error, data.data);
});
};
2017-08-19 00:43:04 +08:00
/* funcs.storeLinkToClipboard = function (readOnly, cb) {
ctx.sframeChan.query('Q_STORE_LINK_TO_CLIPBOARD', readOnly, function (err) {
if (cb) { cb(err); }
});
2017-09-07 00:26:10 +08:00
}; */
2017-08-30 23:57:10 +08:00
funcs.gotoURL = function (url) { ctx.sframeChan.event('EV_GOTO_URL', url); };
funcs.whenRealtimeSyncs = evRealtimeSynced.reg;
2017-08-17 22:56:18 +08:00
Object.freeze(funcs);
return { create: function (cb) {
nThen(function (waitFor) {
2017-09-06 16:56:27 +08:00
SFrameChannel.create(window.parent, waitFor(function (sfc) { ctx.sframeChan = sfc; }), true);
2017-08-17 22:56:18 +08:00
// CpNfInner.start() should be here....
2017-08-17 23:34:08 +08:00
}).nThen(function () {
ctx.metadataMgr = MetadataMgr.create(ctx.sframeChan);
ctx.sframeChan.whenReg('EV_CACHE_PUT', function () {
if (Object.keys(window.cryptpadCache.updated).length) {
ctx.sframeChan.event('EV_CACHE_PUT', window.cryptpadCache.updated);
}
window.cryptpadCache._put = window.cryptpadCache.put;
window.cryptpadCache.put = function (k, v, cb) {
window.cryptpadCache._put(k, v, cb);
var x = {};
x[k] = v;
ctx.sframeChan.event('EV_CACHE_PUT', x);
};
});
2017-09-21 23:56:24 +08:00
ctx.sframeChan.whenReg('EV_LOCALSTORE_PUT', function () {
if (Object.keys(window.cryptpadStore.updated).length) {
ctx.sframeChan.event('EV_LOCALSTORE_PUT', window.cryptpadStore.updated);
}
window.cryptpadStore._put = window.cryptpadStore.put;
window.cryptpadStore.put = function (k, v, cb) {
window.cryptpadStore._put(k, v, cb);
var x = {};
x[k] = v;
ctx.sframeChan.event('EV_LOCALSTORE_PUT', x);
};
});
UI.addTooltips();
ctx.sframeChan.on('EV_RT_CONNECT', function () { CommonRealtime.setConnectionState(true); });
ctx.sframeChan.on('EV_RT_DISCONNECT', function () { CommonRealtime.setConnectionState(false); });
ctx.sframeChan.on('Q_INCOMING_FRIEND_REQUEST', function (confirmMsg, cb) {
Cryptpad.confirm(confirmMsg, cb, null, true);
});
ctx.sframeChan.on('EV_FRIEND_REQUEST', function (data) {
var i = pendingFriends.indexOf(data.sender);
if (i !== -1) { pendingFriends.splice(i, 1); }
Cryptpad.log(data.logText);
});
ctx.sframeChan.ready();
2017-08-17 22:56:18 +08:00
cb(funcs);
});
} };
2017-08-17 22:28:54 +08:00
});