79 lines
2.3 KiB
JavaScript
79 lines
2.3 KiB
JavaScript
var AmiNotify = function() {
|
|
var enabled = false;
|
|
|
|
var enable = function(callback) {
|
|
if(typeof callback !== 'function')
|
|
callback = function() {};
|
|
|
|
if(enabled) {
|
|
callback(true);
|
|
return;
|
|
}
|
|
|
|
if('Notification' in window) {
|
|
if(Notification.permission === 'granted') {
|
|
callback(enabled = true);
|
|
return;
|
|
}
|
|
|
|
if(Notification.permission !== 'denied') {
|
|
Notification.requestPermission().then(function(perm) {
|
|
callback(enabled = perm === 'granted');
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
|
|
callback(false);
|
|
};
|
|
|
|
var disable = function() {
|
|
enabled = false;
|
|
};
|
|
|
|
return {
|
|
enable: enable,
|
|
disable: disable,
|
|
display: function(info) {
|
|
if(!enabled)
|
|
return;
|
|
|
|
var title = info.title;
|
|
if(typeof title !== 'string')
|
|
throw 'missing title property';
|
|
|
|
var opts = {};
|
|
|
|
if(typeof info.text === 'string')
|
|
opts.body = info.text;
|
|
if(typeof info.data !== 'undefined')
|
|
opts.data = info.data;
|
|
if(typeof info.icon === 'string')
|
|
opts.icon = info.icon;
|
|
if(typeof info.tag === 'string')
|
|
opts.tag = info.tag;
|
|
if(typeof info.dir === 'string')
|
|
opts.dir = info.dir;
|
|
|
|
var notif = new Notification(title, opts);
|
|
|
|
if(typeof info.onclick === 'function')
|
|
notif.addEventListener('click', function(ev) {
|
|
info.onclick.call(notif, ev);
|
|
});
|
|
if(typeof info.onshow === 'function')
|
|
notif.addEventListener('show', function(ev) {
|
|
info.onshow.call(notif, ev);
|
|
});
|
|
if(typeof info.onerror === 'function')
|
|
notif.addEventListener('error', function(ev) {
|
|
info.onerror.call(notif, ev);
|
|
});
|
|
if(typeof info.onclose === 'function')
|
|
notif.addEventListener('close', function(ev) {
|
|
info.onclose.call(notif, ev);
|
|
});
|
|
},
|
|
};
|
|
};
|