Created
June 27, 2013 07:51
-
-
Save crossai-2033/5874695 to your computer and use it in GitHub Desktop.
Notification本地通知类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Notification类 | |
(function () { | |
var Notification = window.Notification, | |
webkitNotify = window.webkitNotifications; | |
function DesktopNotify(title, options) { | |
this._title = title; | |
this._options = options; | |
this._instance = null; | |
this._events = { | |
"show": [], | |
"close": [], | |
"click": [], | |
"error": [] | |
} | |
var me = this; | |
this._router = function(ev) { | |
me._events[ev.type].forEach(function (fn) { | |
fn.call(me, ev); | |
}); | |
}; | |
options.autoShow && this.show(); | |
} | |
DesktopNotify.prototype = { | |
constructor: DesktopNotify, | |
show: function() { | |
var options = this._options; | |
if (Notification) { | |
this._instance = Notification && new Notification(this._title, options); | |
this._initEvents(); | |
} else { | |
this._instance = webkitNotify.createNotification(options.icon, this._title, options.body); | |
this._instance.dir = options.dir; | |
this._initEvents(); | |
this._instance.show(); | |
} | |
return this; | |
}, | |
_initEvents: function() { | |
var ins = this._instance, | |
events = this._events; | |
for (var type in events) { | |
ins.addEventListener(type, this._router, false); | |
} | |
}, | |
close: function() { | |
this._instance && this._instance.close(); | |
}, | |
on: function(type, fn) { | |
if (!this._events[type]) { | |
throw new Error("Unsupported events: " + type); | |
} | |
this._events[type].push(fn); | |
return this; | |
}, | |
off: function(type, fn) { | |
if (!this._events[type]) { | |
throw new Error("Unsupported events:" + type); | |
} | |
var events = this._events[type], | |
index; | |
if (!fn) { | |
this._events[type] = []; | |
} else { | |
index = events.indexOf(fn); | |
index > -1 && events.splice(index, 1); | |
} | |
} | |
} | |
DesktopNotify.show = function(title, body, icon) { | |
alert(Notification); | |
Notification && new Notification(title, { | |
body: body, | |
icon: icon | |
}) || (webkitNotify && new webkitNotify.createNotification(icon, title, body)).show(); | |
}; | |
DesktopNotify.isPermitted = function () { | |
return Notification.permission && Notification.permission === 'granted' || | |
webkitNotify.checkPermission() == 0 | |
} | |
DesktopNotify.requestPermission = function (cb) { | |
(Notification || webkitNotify).requestPermission(cb) | |
return this | |
} | |
DesktopNotify.isSupport = function () { | |
return 'Notification' in window || 'webkitNotifications' in window; | |
} | |
window.DesktopNotify = DesktopNotify; | |
})(); | |
document.onclick = function() { | |
if (DesktopNotify.isSupport()) { | |
DesktopNotify.requestPermission(function () { | |
DesktopNotify.show('提醒', '今天天气不错', 'http://0.gravatar.com/avatar/eba2d15b16971d6ea0800c8cc1801a1c?s=70'); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment