2017-03-31 03:33:19 +02:00
|
|
|
const extension = require('extensionizer')
|
2017-10-26 03:05:52 +02:00
|
|
|
const height = 620
|
2017-03-31 03:33:19 +02:00
|
|
|
const width = 360
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationManager {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Public
|
|
|
|
//
|
|
|
|
|
2018-03-11 18:17:08 +01:00
|
|
|
showPopup () {
|
2017-04-01 03:41:51 +02:00
|
|
|
this._getPopup((err, popup) => {
|
2017-03-31 03:33:19 +02:00
|
|
|
if (err) throw err
|
|
|
|
|
2018-03-11 18:17:08 +01:00
|
|
|
// Bring focus to chrome popup
|
2017-03-31 03:33:19 +02:00
|
|
|
if (popup) {
|
2018-03-11 18:17:08 +01:00
|
|
|
// bring focus to existing chrome popup
|
2017-03-31 03:33:19 +02:00
|
|
|
extension.windows.update(popup.id, { focused: true })
|
|
|
|
} else {
|
2018-03-11 18:17:08 +01:00
|
|
|
// create new notification popup
|
2017-03-31 03:33:19 +02:00
|
|
|
extension.windows.create({
|
|
|
|
url: 'notification.html',
|
|
|
|
type: 'popup',
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
closePopup () {
|
2018-03-11 18:17:08 +01:00
|
|
|
// closes notification popup
|
2017-04-01 03:41:51 +02:00
|
|
|
this._getPopup((err, popup) => {
|
2017-03-31 03:33:19 +02:00
|
|
|
if (err) throw err
|
|
|
|
if (!popup) return
|
|
|
|
extension.windows.remove(popup.id, console.error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
|
2017-04-01 03:41:51 +02:00
|
|
|
_getPopup (cb) {
|
|
|
|
this._getWindows((err, windows) => {
|
|
|
|
if (err) throw err
|
|
|
|
cb(null, this._getPopupIn(windows))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:33:19 +02:00
|
|
|
_getWindows (cb) {
|
|
|
|
// Ignore in test environment
|
|
|
|
if (!extension.windows) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
|
|
|
|
extension.windows.getAll({}, (windows) => {
|
|
|
|
cb(null, windows)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_getPopupIn (windows) {
|
|
|
|
return windows ? windows.find((win) => {
|
2018-03-11 18:17:08 +01:00
|
|
|
// Returns notification popup
|
2018-03-07 23:19:27 +01:00
|
|
|
return (win && win.type === 'popup')
|
2017-03-31 03:33:19 +02:00
|
|
|
}) : null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-27 06:05:45 +02:00
|
|
|
module.exports = NotificationManager
|