cryptpad/www/common/sframe-common-mailbox.js

197 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-05-15 16:22:39 +00:00
define([
'jquery',
'/common/common-util.js',
'/common/common-interface.js',
'/common/common-ui-elements.js',
2019-05-22 16:03:52 +00:00
'/common/notifications.js',
2019-05-17 14:19:41 +00:00
'/common/hyperscript.js',
'/customize/messages.js',
], function ($, Util, UI, UIElements, Notifications, h, Messages) {
2019-05-15 16:22:39 +00:00
var Mailbox = {};
Mailbox.create = function (Common) {
2019-05-17 14:19:41 +00:00
var mailbox = Common.mailbox;
2019-05-15 16:22:39 +00:00
var sframeChan = Common.getSframeChannel();
var execCommand = function (cmd, data, cb) {
sframeChan.query('Q_MAILBOX_COMMAND', {
cmd: cmd,
data: data
}, function (err, obj) {
if (err) { return void cb({error: err}); }
cb(obj);
});
};
var history = {};
var removeFromHistory = function (type, hash) {
2019-05-27 12:03:15 +00:00
if (!history[type]) { return; }
2019-05-15 16:22:39 +00:00
history[type] = history[type].filter(function (obj) {
return obj.hash !== hash;
});
};
mailbox.sendTo = function (type, content, user) {
execCommand('SENDTO', {
type: type,
msg: content,
user: user
}, function (err, obj) {
if (err || (obj && obj.error)) { return void console.error(err || obj.error); }
});
2019-05-15 16:22:39 +00:00
};
// UI
2019-05-17 14:19:41 +00:00
var formatData = function (data) {
return JSON.stringify(data.content.msg.content);
};
var createElement = function (data) {
var notif;
var dismissIcon = h('span.fa.fa-times');
var dismiss = h('div.cp-notification-dismiss', {
title: Messages.notifications_dismiss
2019-06-07 13:50:00 +00:00
}, dismissIcon);
2019-05-17 14:19:41 +00:00
dismiss.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
mailbox.dismiss(data, function (err) {
if (err) { return void console.error(err); }
/*if (notif && notif.parentNode) {
2019-05-17 14:19:41 +00:00
try {
notif.parentNode.removeChild(notif);
} catch (e) { console.error(e); }
}*/
2019-05-17 14:19:41 +00:00
});
});
notif = h('div.cp-notification', {
'data-hash': data.content.hash
}, [
h('div.cp-notification-content', h('p', formatData(data))),
dismiss
2019-05-17 14:19:41 +00:00
]);
return notif;
};
2019-05-15 16:22:39 +00:00
var onViewedHandlers = [];
var onMessageHandlers = [];
onViewedHandlers.push(function (data) {
var hash = data.hash.replace(/"/g, '\\\"');
var $notif = $('.cp-notification[data-hash="'+hash+'"]');
if ($notif.length) {
$notif.remove();
}
});
2019-05-15 16:22:39 +00:00
// Call the onMessage handlers
2019-05-17 14:19:41 +00:00
var pushMessage = function (data, handler) {
var todo = function (f) {
2019-05-15 16:22:39 +00:00
try {
2019-06-24 10:15:34 +00:00
var el;
if (data.type === 'notifications') {
el = createElement(data);
Notifications.add(Common, data, el);
}
2019-05-17 14:19:41 +00:00
f(data, el);
2019-05-15 16:22:39 +00:00
} catch (e) {
console.error(e);
}
2019-05-17 14:19:41 +00:00
};
if (typeof (handler) === "function") {
return void todo(handler);
2019-05-15 16:22:39 +00:00
}
2019-05-17 14:19:41 +00:00
onMessageHandlers.forEach(todo);
2019-05-15 16:22:39 +00:00
};
var onViewed = function (data) {
// data = { type: 'type', hash: 'hash' }
onViewedHandlers.forEach(function (f) {
try {
f(data);
2019-06-24 10:15:34 +00:00
if (data.type === 'notifications') {
Notifications.remove(Common, data);
}
2019-05-15 16:22:39 +00:00
} catch (e) {
console.error(e);
}
});
removeFromHistory(data.type, data.hash);
};
var onMessage = function (data) {
// data = { type: 'type', content: {msg: 'msg', hash: 'hash'} }
console.log(data.content);
pushMessage(data);
if (!history[data.type]) { history[data.type] = []; }
history[data.type].push(data.content);
};
2019-05-17 14:19:41 +00:00
mailbox.dismiss = function (data, cb) {
var dataObj = {
hash: data.content.hash,
type: data.type
};
execCommand('DISMISS', dataObj, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
onViewed(dataObj);
cb();
});
};
var subscribed = false;
2019-05-17 14:19:41 +00:00
// Get all existing notifications + the new ones when they come
2019-06-24 10:15:34 +00:00
mailbox.subscribe = function (types, cfg) {
if (!subscribed) {
execCommand('SUBSCRIBE', null, function () {});
subscribed = true;
}
2019-05-17 14:19:41 +00:00
if (typeof(cfg.onViewed) === "function") {
2019-06-24 10:15:34 +00:00
onViewedHandlers.push(function (data) {
if (types.indexOf(data.type) === -1) { return; }
cfg.onViewed(data);
});
2019-05-17 14:19:41 +00:00
}
if (typeof(cfg.onMessage) === "function") {
2019-06-24 10:15:34 +00:00
onMessageHandlers.push(function (data, el) {
if (types.indexOf(data.type) === -1) { return; }
cfg.onMessage(data, el);
});
2019-05-17 14:19:41 +00:00
}
Object.keys(history).forEach(function (type) {
2019-06-24 10:15:34 +00:00
if (types.indexOf(type) === -1) { return; }
2019-05-17 14:19:41 +00:00
history[type].forEach(function (data) {
pushMessage({
type: type,
content: data
}, cfg.onMessage);
});
});
};
2019-05-15 16:22:39 +00:00
// CHANNEL WITH WORKER
sframeChan.on('EV_MAILBOX_EVENT', function (obj) {
// obj = { ev: 'type', data: obj }
var ev = obj.ev;
var data = obj.data;
if (ev === 'MESSAGE') {
return void onMessage(data);
}
if (ev === 'VIEWED') {
return void onViewed(data);
}
});
return mailbox;
};
return Mailbox;
});