notifications: missing modules

This commit is contained in:
jean-emmanuel 2019-01-04 12:02:55 +01:00
parent 10b3b465f5
commit e69982dee9
2 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,83 @@
var html = require('nanohtml'),
keyboardJS = require('keyboardjs')
var DURATION = 3000
class Notifications {
constructor() {
this.container = DOM.get('#notifications')[0]
this.toasts = []
this.loop = null
keyboardJS.bind('k', ()=>{
this.add({
message: 'bla bla bla bla'
})
})
}
add(data) {
var toast = html`
<div class="toast ${data.class || ''}">
<i class="fa fa-fw fa-${data.icon || 'bell'}"></i>
<div class="content">
${data.message}
</div>
</div>
`
toast.date = Date.now()
this.container.appendChild(toast)
toast.style.height = toast.offsetHeight + 'px'
this.toasts.push(toast)
this.startLoop()
}
remove(toast) {
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()
}
startLoop() {
if (this.loop) return
this.loop = setInterval(()=>{
for (var i = this.toasts.length - 1; i > -1; i--) {
if (Date.now() - this.toasts[i].date > DURATION) {
this.remove(this.toasts[i])
}
}
},100)
}
stopLoop() {
if (!this.loop) return
clearInterval(this.loop)
this.loop = null
}
}
module.exports = new Notifications()