github.com/resonatecoop/id@v1.1.0-43/frontend/src/plugins/notifications.js (about) 1 const Notifications = require('../components/notifications') 2 3 module.exports = () => { 4 return (state, emitter) => { 5 state.messages = state.messages || [] 6 7 emitter.on(state.events.DOMCONTENTLOADED, _ => { 8 emitter.on('notification:denied', () => { 9 emitter.emit('notify', { 10 type: 'warning', 11 timeout: 6000, 12 message: 'Notifications are blocked, you should modify your browser site settings' 13 }) 14 }) 15 16 emitter.on('notification:granted', () => { 17 emitter.emit('notify', { 18 type: 'success', 19 message: 'Notifications are enabled' 20 }) 21 }) 22 23 emitter.on('notify', (props) => { 24 const { message } = props 25 26 if (!state.notification.permission) { 27 const dialog = document.querySelector('dialog') 28 const name = dialog ? 'notifications' : 'notifications-dialog' 29 const notifications = state.cache(Notifications, name) 30 const host = props.host || (dialog || document.body) 31 32 if (notifications.element) { 33 notifications.add(props) 34 } else { 35 const el = notifications.render({ 36 size: dialog ? 'small' : 'default' 37 }) 38 host.insertBefore(el, host.firstChild) 39 notifications.add(props) 40 } 41 } else { 42 emitter.emit('notification:new', message) 43 } 44 }) 45 }) 46 } 47 }