2017-03-21 11:15:18 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
const { notify } = require('./electron-notify.js');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* implementation for notifications interface,
|
|
|
|
* wrapper around electron-notify.
|
|
|
|
*/
|
|
|
|
class Notify {
|
|
|
|
constructor(title, options) {
|
|
|
|
this.emitter = new EventEmitter();
|
|
|
|
|
|
|
|
this._id = notify({
|
|
|
|
title: title,
|
|
|
|
text: options.body,
|
|
|
|
image: options.image,
|
|
|
|
flash: options.flash,
|
|
|
|
color: options.color,
|
|
|
|
onShowFunc: onShow.bind(this),
|
|
|
|
onClickFunc: onClick.bind(this),
|
|
|
|
onCloseFunc: onClose.bind(this)
|
|
|
|
});
|
|
|
|
|
|
|
|
function onShow(arg) {
|
|
|
|
if (arg.id === this._id) {
|
|
|
|
this.emitter.emit('show');
|
|
|
|
this._closeNotification = arg.closeNotification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClick(arg) {
|
|
|
|
if (arg.id === this._id) {
|
|
|
|
this.emitter.emit('click');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClose(arg) {
|
|
|
|
if (arg.id === this._id || arg.event === 'close-all') {
|
|
|
|
this.emitter.emit('close');
|
|
|
|
this.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
if (typeof this._closeNotification === 'function') {
|
|
|
|
this._closeNotification('close');
|
|
|
|
}
|
|
|
|
this.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
static get permission() {
|
|
|
|
return 'granted';
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener(event, cb) {
|
|
|
|
if (event && typeof cb === 'function') {
|
|
|
|
this.emitter.on(event, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListener(event, cb) {
|
|
|
|
if (event && typeof cb === 'function') {
|
|
|
|
this.emitter.removeEventListener(event, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// private stuff below here
|
|
|
|
//
|
2017-03-23 18:19:59 -05:00
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.emitter.removeAllListeners();
|
|
|
|
// allow live instance to be destroyed
|
|
|
|
this.emitter.emit('destroy');
|
|
|
|
}
|
|
|
|
|
2017-03-21 11:15:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Notify;
|