From 28f3c53018bc611b0210df65595a4d8c7a95adb7 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 6 Jan 2019 11:33:57 +0100 Subject: [PATCH] notifications: ooify toasts and better server connection notifications --- src/client/app/ipc/index.js | 20 +++++--- src/client/app/locales/en.js | 3 +- src/client/app/locales/fr.js | 3 +- src/client/app/locales/ru.js | 5 +- src/client/app/ui/notifications.js | 79 +++++++++++++++++++++--------- 5 files changed, 77 insertions(+), 33 deletions(-) diff --git a/src/client/app/ipc/index.js b/src/client/app/ipc/index.js index f1295ce7..6a3f82f4 100644 --- a/src/client/app/ipc/index.js +++ b/src/client/app/ipc/index.js @@ -66,6 +66,12 @@ class Ipc extends EventEmitter { }, hearbeatTimeout) }, hearbeatInterval) + if (notifications) notifications.add({ + icon: 'wifi', + message: locales('server_connected'), + id: 'ipc_state' + }) + this.trigger('connect') this.flush() @@ -88,7 +94,13 @@ class Ipc extends EventEmitter { this.hearbeat = clearInterval(this.hearbeat) this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) this.dieTimeout = clearTimeout(this.dieTimeout) - + notifications.add({ + icon: 'wifi', + class: 'error', + message: locales('server_disconnected'), + id: 'ipc_state', + duration: Infinity + }) if (!this.reconnect) { this.reconnect = setInterval(()=>{ this.open() @@ -127,12 +139,6 @@ class Ipc extends EventEmitter { this.socket.send(packet) } else { this.queue.push(packet) - notifications.add({ - icon: 'wifi', - class: 'error', - message: locales('server_unreachable'), - id: 'server_unreachable' - }) } } diff --git a/src/client/app/locales/en.js b/src/client/app/locales/en.js index 0fb88864..52a43128 100644 --- a/src/client/app/locales/en.js +++ b/src/client/app/locales/en.js @@ -32,7 +32,8 @@ module.exports = { fs_addtohome: 'You must add this page to your home screen to launch it in fullscreen', // server - server_unreachable: 'The server is currently unreachable', + server_disconnected: 'The connection to the server has been interrupted', + server_connected: 'The connection to the server has been restored', // session session_browse: 'Browse...', diff --git a/src/client/app/locales/fr.js b/src/client/app/locales/fr.js index a7107b48..794d8c44 100644 --- a/src/client/app/locales/fr.js +++ b/src/client/app/locales/fr.js @@ -32,7 +32,8 @@ module.exports = { fs_addtohome: 'Vous devez ajouter cette page à votre écran d\'accueil pour pouvoir l\'ouvrir en plein écran.', // server - server_unreachable: 'La connexion avec le serveur a été interrompue', + server_disconnected: 'La connexion avec le serveur a été interrompue', + server_connected: 'La connexion avec le serveur a été rétablie', // session session_browse: 'Parcourir...', diff --git a/src/client/app/locales/ru.js b/src/client/app/locales/ru.js index 9ef05488..81d6f3ba 100644 --- a/src/client/app/locales/ru.js +++ b/src/client/app/locales/ru.js @@ -32,8 +32,9 @@ module.exports = { // @https://github.com/suhr fs_addtohome: 'Необходимо добавить эту страницу на главный экран чтобы запустить её в полноэкранном режиме', // server - server_unreachable: 'The server is currently unreachable', - + server_disconnected: 'The connection to the server has been interrupted', + server_connected: 'The connection to the server has been restored', + // session session_browse: 'Обзор...', session_new: 'Новая сессия', diff --git a/src/client/app/ui/notifications.js b/src/client/app/ui/notifications.js index 2e1bdce6..d8eb16ed 100644 --- a/src/client/app/ui/notifications.js +++ b/src/client/app/ui/notifications.js @@ -1,6 +1,55 @@ -var html = require('nanohtml') +var html = require('nanohtml'), + morph = require('nanomorph') -var DURATION = 3500 + +var DEFAULT_DURATION = 3500 + +class Toast { + + constructor(data) { + + this.id = data.id || Date.now() + this.update(data) + + } + + update(data) { + + this.expires = Date.now() + (data.duration || DEFAULT_DURATION) + + var html = html` +
+ +
+ ${data.message} +
+
+ ` + if (this.html) { + morph(this.html, html) + } else { + this.html = html + } + + } + + appendTo(el) { + + el.appendChild(this.html) + this.html.style.height = this.html.offsetHeight + 'px' + + } + + removeFrom(el) { + + this.html.classList.add('remove') + setTimeout(()=>{ + el.removeChild(this.html) + }, 200) + + } + +} class Notifications { @@ -18,23 +67,13 @@ class Notifications { if (data.id) { var match = this.toasts.filter(x => x.id === data.id) if (match.length) { - match[0].date = Date.now() - return + return match[0].update(data) } } - var toast = html` -
- -
- ${data.message} -
-
- ` - toast.date = Date.now() - if (data.id) toast.id = data.id - this.container.appendChild(toast) - toast.style.height = toast.offsetHeight + 'px' + var toast = new Toast(data) + + toast.appendTo(this.container) this.toasts.push(toast) this.startLoop() @@ -43,12 +82,8 @@ class Notifications { remove(toast) { + toast.removeFrom(this.container) this.toasts.splice(this.toasts.indexOf(toast), 1) - toast.classList.add('remove') - toast.classList.add('remove') - setTimeout(()=>{ - this.container.removeChild(toast) - }, 200) if (this.toasts.length === 0) this.stopLoop() @@ -60,7 +95,7 @@ class Notifications { this.loop = setInterval(()=>{ for (var i = this.toasts.length - 1; i > -1; i--) { - if (Date.now() - this.toasts[i].date > DURATION) { + if (Date.now() > this.toasts[i].expires) { this.remove(this.toasts[i]) } }