Add load notification from history

This commit is contained in:
ClemDee 2019-06-28 16:06:44 +02:00
parent 833d70825e
commit c96be7bf89
4 changed files with 74 additions and 17 deletions

View file

@ -83,8 +83,8 @@ define([
if (handlers[type]) {
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";
data.content.isClickable = typeof data.content.handler === "function" && data.content.archived !== true;
data.content.isDismissible = typeof data.content.dismissHandler === "function" && data.content.archived !== true;
} else {
// $(el).find('.cp-notification-dismiss').css('display', 'flex'); // XXX
}

View file

@ -204,6 +204,15 @@ define([
});
};
};
mailbox.getNotificationsHistory = function (type, count, lastKnownHash, cb) {
mailbox.getMoreHistory(type, count, lastKnownHash, function (err, messages) {
messages.forEach(function (data) {
data.content.archived = true;
Notifications.add(Common, data);
});
cb(err, messages);
});
};
// CHANNEL WITH WORKER

View file

@ -86,6 +86,9 @@
display: block;
}
}
&.cp-app-notification-archived {
background-color: #f1f1f1;
}
&:not(:first-child) {
border-top: 1px solid #ccc;
}
@ -121,9 +124,7 @@
align-items: center;
width: 100%;
padding: 0.5rem;
margin-top: 1rem;
border: 1px solid #ccc;
border-radius: 3px;
color: #333;
}

View file

@ -46,6 +46,35 @@ define([
var create = {};
var formatData = function (data) {
return JSON.stringify(data.content.msg.content);
};
var createElement = function (data) {
var notif;
notif = h('div.cp-notification', {
'data-hash': data.content.hash
}, [h('div.cp-notification-content', h('p', formatData(data)))]);
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).addClass("cp-clickable").click(data.content.dismissHandler);
$(notif).append(dismiss);
}
return notif;
};
// create the list of notifications
// show only notifs with type in filterTypes array. If filterTypes empty, don't filter.
var makeNotificationList = function (key, filterTypes) {
@ -69,29 +98,47 @@ define([
]),
]);
// add notification
function addNotification (data, el) {
// if the type of notification correspond
if (filterTypes.length === 0 || filterTypes.indexOf(data.content.msg.type) !== -1) {
notifsData.push(data);
$(notifsList).prepend(el);
}
}
function addArchivedNotification (data) {
if (data.content.archived) {
notifsData.push(data);
var el = createElement(data);
$(el).addClass("cp-app-notification-archived");
$(notifsList).prepend(el);
}
}
$div.append(notifsPanel);
if (key === "archived") {
var loadmore;
var lastKnownHash;
$(dismissAll).remove();
loadmore = h("div.cp-app-notification-loadmore.cp-clickable", Messages.loadMore || "Load more ...");
$div.append(loadmore);
$(loadmore).click(function () {
common.mailbox.getNotificationsHistory('notifications', 5, lastKnownHash, function (err, messages) {
console.log(messages);
messages.forEach(function (data) {
data.content.archived = true;
addArchivedNotification(data);
});
lastKnownHash = messages[0].content.hash;
});
});
notifsList.before(loadmore);
$(loadmore).click();
}
common.mailbox.subscribe(["notifications"], {
onMessage: function (data, el) {
console.log(data);
if (el) {
// if the type of notification correspond
if (filterTypes.length === 0 || filterTypes.indexOf(data.content.msg.type) !== -1) {
var dismissHandler = data.content.dismissHandler;
data.content.dismissHandler = function () {
$(el).addClass("dismissed");
dismissHandler();
};
notifsData.push(data);
$(notifsList).prepend(el);
}
}
addNotification(data, el);
},
onViewed: function () {}
});