Finalise uniquified toast stacking and deletion

pull/701/head
Vincent 6 years ago
parent 4d463c659c
commit 34ce386bdb

@ -802,43 +802,54 @@
appView.openConversation(groupId, {}); appView.openConversation(groupId, {});
}; };
window.toasts = []; window.toasts = {};
window.pushToast = (options) => { window.pushToast = (options) => {
// Toast ID can be used to prevent identical toasts appearing at once. // Setting toasts with the same ID can be used to prevent identical
// Eg, no two toasts with ID "messageDeletedAlert" can appear on the screen at once. // toasts from appearing at once (stacking).
// If you want to be able to display mutliple, don't use toast IDs. // If toast already exists, it will be reloaded (updated)
const params = {
title: options.title,
description: options.description ? options.description : '',
type: options.type ? options.type : '',
id: options.id ? options.id : '',
}
// Give all toasts an ID. User may define.
const toastID = options.id
? options.id
: Math.random().toString(36).substring(3);
let toastExists = false;
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
for (const toast of window.toasts){ for (const key in window.toasts) {
if ((!!options.id) && (toast.props.id === options.id)) { if ((!!options.id) && (key === options.id)) {
return; toastExists = true;
window.toasts[key].update(params);
break;
} }
} }
if (! toastExists){
// Make new Toast // Make new Toast
window.toasts.unshift( window.toasts[toastID] = new Whisper.SessionToastView({
new Whisper.SessionToastView({
el: window.$('#session-toast-container'), el: window.$('#session-toast-container'),
})
);
window.toasts[0].update({
title: options.title,
description: options.description ? options.description : '',
type: options.type ? options.type : '',
id: options.id ? options.id : '',
}); });
console.log(window.toasts[0].props.id); window.toasts[toastID].render();
window.toasts[toastID].update(params);
}
// Remove some toasts if too many exist // Remove some toasts if too many exist
const maxToasts = 6; const maxToasts = 6;
const numToasts = window.toasts.length; const numToasts = window.toasts.length;
if (numToasts > maxToasts){ if (numToasts > maxToasts){
window.toasts[4].fadeToast(); const finalToastID = window.toasts.keys[window.toasts.length];
window.toasts = window.toasts.slice(0, maxToasts - 1); window.toasts[finalToastID].fadeToast();
} }
return toastID;
} }
window.sendGroupInvitations = (serverInfo, pubkeys) => { window.sendGroupInvitations = (serverInfo, pubkeys) => {

@ -1377,6 +1377,7 @@
window.pushToast({ window.pushToast({
title: i18n('messageDeletionForbidden'), title: i18n('messageDeletionForbidden'),
type: 'error', type: 'error',
id: 'messageDeletionForbidden',
}); });
return; return;

@ -32,22 +32,38 @@
this.props.description = options.description ? options.description : ''; this.props.description = options.description ? options.description : '';
this.props.type = options.type ? options.type : ''; this.props.type = options.type ? options.type : '';
this.props.id = options.id ? options.id : ''; this.props.id = options.id ? options.id : '';
this.render();
setTimeout(this.fadeToast.bind(this), 4000); this.toastView.update(this.props);
this.showToast();
clearTimeout(this.timer);
this.timer = setTimeout(this.fadeToast.bind(this), 4000);
},
showToast() {
this.toastView.$el.show();
}, },
fadeToast() { fadeToast() {
this.toastView.$el.fadeOut(500, () => { this.toastView.$el.fadeOut(500, () => {
this.toastView.remove(); this.removeToast();
}); });
}, },
closeToast() { closeToast() {
this.toastView.$el.fadeOut(125, () => { this.toastView.$el.fadeOut(125, () => {
this.toastView.remove(); this.removeToast();
}); });
}, },
removeToast() {
this.toastView.remove();
if (this.props.id){
delete window.toasts[this.props.id];
}
},
}); });
})(); })();
Loading…
Cancel
Save