define([ 'jquery', '/common/common-util.js', '/common/common-hash.js', '/common/common-interface.js', '/common/common-ui-elements.js', '/common/notifications.js', '/common/hyperscript.js', '/customize/messages.js', ], function ($, Util, Hash, UI, UIElements, Notifications, h, Messages) { var Mailbox = {}; Mailbox.create = function (Common) { var mailbox = Common.mailbox; 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) { if (!history[type]) { return; } history[type] = history[type].filter(function (obj) { return obj.hash !== hash; }); }; mailbox.sendTo = function (type, content, user, cb) { cb = cb || function () {}; execCommand('SENDTO', { type: type, msg: content, user: user }, function (obj) { cb(obj && obj.error, obj); if (obj && obj.error) { return void console.error(obj.error); } }); }; // UI var formatData = function (data) { return JSON.stringify(data.content.msg.content); }; var createElement = mailbox.createElement = function (data) { var notif; var avatar; var userData = Util.find(data, ['content', 'msg', 'content', 'user']); if (Util.find(data, ['content', 'msg', 'type']) === 'BROADCAST_DELETE') { return; } if (data.type === 'broadcast') { avatar = h('i.fa.fa-bullhorn.cp-broadcast'); if (/^LOCAL\|/.test(data.content.hash)) { $(avatar).addClass('preview'); } } else if (data.type === 'reminders') { avatar = h('i.fa.fa-calendar.cp-broadcast.preview'); } else if (userData && typeof(userData) === "object" && userData.profile) { avatar = h('span.cp-avatar'); Common.displayAvatar($(avatar), userData.avatar, userData.displayName || userData.name); $(avatar).click(function (e) { e.stopPropagation(); Common.openURL(Hash.hashToHref(userData.profile, 'profile')); }); } var order = -Math.floor((Util.find(data, ['content', 'msg', 'ctime']) || 0) / 1000); notif = h('div.cp-notification', { style: 'order:'+order+';', 'data-hash': data.content.hash }, [ avatar, h('div.cp-notification-content', h('p', formatData(data))) ]); if (typeof(data.content.getFormatText) === "function") { $(notif).find('.cp-notification-content p').html(data.content.getFormatText()); if (data.content.autorefresh) { var it = setInterval(function () { if (!data.content.autorefresh) { clearInterval(it); return; } $(notif).find('.cp-notification-content p').html(data.content.getFormatText()); }, 60000); } } if (data.content.isClickable) { $(notif).find('.cp-notification-content').addClass("cp-clickable") .click(data.content.handler); } if (data.content.isDismissible) { var dismissIcon = h('span.fa.fa-times'); var dismiss = h('div.cp-notification-dismiss', { title: Messages.notifications_dismiss }, dismissIcon); $(dismiss).addClass("cp-clickable") .click(data.content.dismissHandler); $(notif).append(dismiss); } return notif; }; var onViewedHandlers = []; var onMessageHandlers = []; onViewedHandlers.push(function (data) { var hash = data.hash.replace(/"/g, '\\\"'); var $notif = $('.cp-notification[data-hash="'+hash+'"]:not(.cp-app-notification-archived)'); if ($notif.length) { $notif.remove(); } }); // Call the onMessage handlers var isNotification = function (type) { return type === "notifications" || /^team-/.test(type) || type === "broadcast" || type === "reminders"; }; var pushMessage = function (data, handler) { var todo = function (f) { try { var el; if (isNotification(data.type)) { Notifications.add(Common, data); el = createElement(data); } f(data, el); } catch (e) { console.error(e); } }; if (typeof (handler) === "function") { return void todo(handler); } onMessageHandlers.forEach(todo); }; var onViewed = function (data) { // data = { type: 'type', hash: 'hash' } onViewedHandlers.forEach(function (f) { try { f(data); if (isNotification(data.type)) { Notifications.remove(Common, data); } } catch (e) { console.error(e); } }); removeFromHistory(data.type, data.hash); }; var onMessage = mailbox.onMessage = function (data, cb) { // data = { type: 'type', content: {msg: 'msg', hash: 'hash'} } pushMessage(data); if (data.content && typeof (data.content.getFormatText) === "function") { var text = $('