Separate notification data from its element contruction

This commit is contained in:
ClemDee 2019-06-27 09:51:23 +02:00
parent 2d6e8754be
commit bea13317fe
2 changed files with 64 additions and 49 deletions

View File

@ -8,11 +8,24 @@ define([
var handlers = {}; var handlers = {};
var defaultDismiss = function (common, data) {
return function (e) {
e.preventDefault();
e.stopPropagation();
common.mailbox.dismiss(data, function (err) {
if (err) { return void console.error(err); }
});
};
};
// Friend request // Friend request
handlers['FRIEND_REQUEST'] = function (common, data, el) { handlers['FRIEND_REQUEST'] = function (common, data) {
var content = data.content; var content = data.content;
var msg = content.msg; var msg = content.msg;
content.handler = function () {
UIElements.displayFriendRequestModal(common, data);
};
// Check authenticity // Check authenticity
if (msg.author !== msg.content.curvePublic) { return; } if (msg.author !== msg.content.curvePublic) { return; }
@ -20,56 +33,58 @@ define([
common.addFriendRequest(data); common.addFriendRequest(data);
// Display the notification // Display the notification
$(el).find('.cp-notification-content p') content.getFormatText = function () {
.html(Messages._getKey('friendRequest_notification', [msg.content.displayName || Messages.anonymous])); return Messages._getKey('friendRequest_notification', [msg.content.displayName || Messages.anonymous]);
$(el).find('.cp-notification-content').addClass("cp-clickable") };
.click(function () {
UIElements.displayFriendRequestModal(common, data);
});
}; };
handlers['FRIEND_REQUEST_ACCEPTED'] = function (common, data, el) { handlers['FRIEND_REQUEST_ACCEPTED'] = function (common, data) {
var content = data.content; var content = data.content;
var msg = content.msg; var msg = content.msg;
$(el).find('.cp-notification-content p') content.dismissHandler = defaultDismiss(common, data);
.html(Messages._getKey('friendRequest_accepted', [msg.content.name || Messages.anonymous])); content.getFormatText = function () {
$(el).find('.cp-notification-dismiss').css('display', 'flex'); return Messages._getKey('friendRequest_accepted', [msg.content.name || Messages.anonymous]);
};
}; };
handlers['FRIEND_REQUEST_DECLINED'] = function (common, data, el) { handlers['FRIEND_REQUEST_DECLINED'] = function (common, data) {
var content = data.content; var content = data.content;
var msg = content.msg; var msg = content.msg;
$(el).find('.cp-notification-content p') content.dismissHandler = defaultDismiss(common, data);
.html(Messages._getKey('friendRequest_declined', [msg.content.name || Messages.anonymous])); content.getFormatText = function () {
$(el).find('.cp-notification-dismiss').css('display', 'flex'); return Messages._getKey('friendRequest_declined', [msg.content.name || Messages.anonymous]);
};
}; };
// Share pad // Share pad
handlers['SHARE_PAD'] = function (common, data, el) { handlers['SHARE_PAD'] = function (common, data) {
var content = data.content; var content = data.content;
var msg = content.msg; var msg = content.msg;
var type = Hash.parsePadUrl(msg.content.href).type; var type = Hash.parsePadUrl(msg.content.href).type;
var key = type === 'drive' ? 'notification_folderShared' : var key = type === 'drive' ? 'notification_folderShared' :
(type === 'file' ? 'notification_fileShared' : (type === 'file' ? 'notification_fileShared' :
'notification_padShared'); 'notification_padShared');
$(el).find('.cp-notification-content p') content.handler = function () {
.html(Messages._getKey(key, [msg.content.name || Messages.anonymous, msg.content.title])); common.openURL(msg.content.href);
$(el).find('.cp-notification-content').addClass("cp-clickable") };
.click(function () { content.dismissHandler = defaultDismiss(common, data);
common.openURL(msg.content.href); content.getFormatText = function () {
}); return Messages._getKey(key, [msg.content.name || Messages.anonymous, msg.content.title]);
$(el).find('.cp-notification-dismiss').css('display', 'flex'); };
}; };
return { return {
add: function (common, data, el) { add: function (common, data) {
var type = data.content.msg.type; var type = data.content.msg.type;
if (handlers[type]) { if (handlers[type]) {
handlers[type](common, data, el); handlers[type](common, data);
// add getters to access simply some informations
data.content.isClickable = typeof data.content.handler === "function";
data.content.isDismissible = typeof data.content.dismissHandler === "function";
} else { } else {
$(el).find('.cp-notification-dismiss').css('display', 'flex'); // $(el).find('.cp-notification-dismiss').css('display', 'flex'); // XXX
} }
}, },
remove: function (common, data) { remove: function (common, data) {

View File

@ -49,28 +49,28 @@ define([
}; };
var createElement = function (data) { var createElement = function (data) {
var notif; var notif;
var dismissIcon = h('span.fa.fa-times');
var dismiss = h('div.cp-notification-dismiss', {
title: Messages.notifications_dismiss
}, dismissIcon);
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) {
try {
notif.parentNode.removeChild(notif);
} catch (e) { console.error(e); }
}*/
});
});
notif = h('div.cp-notification', { notif = h('div.cp-notification', {
'data-hash': data.content.hash 'data-hash': data.content.hash
}, [ }, [h('div.cp-notification-content', h('p', formatData(data)))]);
h('div.cp-notification-content', h('p', formatData(data))),
dismiss if (data.content.getFormatText) {
]); $(notif).find('.cp-notification-content p').html(data.content.getFormatText());
}
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).css('display', 'flex'); // XXX
$(dismiss).addClass("cp-clickable")
.click(data.content.dismissHandler);
$(notif).append(dismiss);
}
return notif; return notif;
}; };
@ -92,8 +92,9 @@ define([
try { try {
var el; var el;
if (data.type === 'notifications') { if (data.type === 'notifications') {
Notifications.add(Common, data);
console.log(data);
el = createElement(data); el = createElement(data);
Notifications.add(Common, data, el);
} }
f(data, el); f(data, el);
} catch (e) { } catch (e) {
@ -123,7 +124,6 @@ define([
var onMessage = function (data) { var onMessage = function (data) {
// data = { type: 'type', content: {msg: 'msg', hash: 'hash'} } // data = { type: 'type', content: {msg: 'msg', hash: 'hash'} }
console.log(data.content);
pushMessage(data); pushMessage(data);
if (!history[data.type]) { history[data.type] = []; } if (!history[data.type]) { history[data.type] = []; }
history[data.type].push(data.content); history[data.type].push(data.content);