Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (win) {
- var PERMISSION_DEFAULT = "default",
- PERMISSION_GRANTED = "granted",
- PERMISSION_DENIED = "denied",
- PERMISSION = [PERMISSION_GRANTED, PERMISSION_DEFAULT, PERMISSION_DENIED],
- defaultSetting = {
- pageVisibility: false,
- autoClose: 0
- }, empty = {}, emptyString = "",
- isSupported = (function () {
- var isSupported = false;
- try {
- isSupported = !! (win.Notification || win.webkitNotifications || navigator.mozNotification || (win.external && win.external.msIsSiteMode() !== undefined));
- } catch (e) {}
- return isSupported;
- }()),
- ieVerification = Math.floor((Math.random() * 10) + 1),
- isFunction = function (value) {
- return (value && (value).constructor === Function);
- }, isString = function (value) {
- return (value && (value).constructor === String);
- }, isObject = function (value) {
- return (value && (value).constructor === Object);
- }, mixin = function (target, source) {
- var name, s;
- for (name in source) {
- s = source[name];
- if (!(name in target) || (target[name] !== s && (!(name in empty) || empty[name] !== s))) {
- target[name] = s;
- }
- }
- return target;
- }, noop = function () {}, settings = defaultSetting;
- function getNotification(title, options) {
- var notification;
- if (win.Notification) {
- notification = new win.Notification(title, {
- icon: isString(options.icon) ? options.icon : options.icon.x32,
- body: options.body || emptyString,
- tag: options.tag || emptyString
- });
- if (isFunction(options.onclick)) {
- notification.onclick = options.onclick;
- }
- } else if (win.webkitNotifications) {
- notification = win.webkitNotifications.createNotification(options.icon, title, options.body);
- if (isFunction(options.onclick)) {
- notification.onclick = options.onclick;
- }
- notification.show();
- } else if (navigator.mozNotification) {
- notification = navigator.mozNotification.createNotification(title, options.body, options.icon);
- if (isFunction(options.onclick)) {
- notification.onclick = options.onclick;
- }
- notification.show();
- } else if (win.external && win.external.msIsSiteMode()) {
- win.external.msSiteModeClearIconOverlay();
- win.external.msSiteModeSetIconOverlay((isString(options.icon) ? options.icon : options.icon.x16), title);
- win.external.msSiteModeActivate();
- notification = {
- "ieVerification": ieVerification + 1
- };
- }
- return notification;
- }
- function getWrapper(notification) {
- return {
- close: function () {
- if (notification) {
- if (notification.close) {
- notification.close();
- } else if (win.external && win.external.msIsSiteMode()) {
- if (notification.ieVerification === ieVerification) {
- win.external.msSiteModeClearIconOverlay();
- }
- }
- }
- }
- };
- }
- function requestPermission(callback) {
- if (!isSupported) {
- return;
- }
- var callbackFunction = isFunction(callback) ? callback : noop;
- if (win.webkitNotifications && win.webkitNotifications.checkPermission) {
- win.webkitNotifications.requestPermission(callbackFunction);
- } else if (win.Notification && win.Notification.requestPermission) {
- win.Notification.requestPermission(callbackFunction);
- }
- }
- function permissionLevel() {
- var permission;
- if (!isSupported) {
- return;
- }
- if (win.Notification && win.Notification.permissionLevel) {
- permission = win.Notification.permissionLevel();
- } else if (win.webkitNotifications && win.webkitNotifications.checkPermission) {
- permission = PERMISSION[win.webkitNotifications.checkPermission()];
- } else if (navigator.mozNotification) {
- permission = PERMISSION_GRANTED;
- } else if (win.Notification && win.Notification.permission) {
- permission = win.Notification.permission;
- } else if (win.external && (win.external.msIsSiteMode() !== undefined)) {
- permission = win.external.msIsSiteMode() ? PERMISSION_GRANTED : PERMISSION_DEFAULT;
- }
- return permission;
- }
- function config(params) {
- if (params && isObject(params)) {
- mixin(settings, params);
- }
- return settings;
- }
- function isDocumentHidden() {
- return settings.pageVisibility ? (document.hidden || document.msHidden || document.mozHidden || document.webkitHidden) : true;
- }
- function createNotification(title, options) {
- var notification, notificationWrapper;
- if (isSupported && isDocumentHidden() && isString(title) && (options && (isString(options.icon) || isObject(options.icon))) && (permissionLevel() === PERMISSION_GRANTED)) {
- notification = getNotification(title, options);
- }
- notificationWrapper = getWrapper(notification);
- if (settings.autoClose && notification && !notification.ieVerification && notification.addEventListener) {
- notification.addEventListener("show", function () {
- var notification = notificationWrapper;
- win.setTimeout(function () {
- notification.close();
- }, settings.autoClose);
- });
- }
- return notificationWrapper;
- }
- win.notify = {
- PERMISSION_DEFAULT: PERMISSION_DEFAULT,
- PERMISSION_GRANTED: PERMISSION_GRANTED,
- PERMISSION_DENIED: PERMISSION_DENIED,
- isSupported: isSupported,
- config: config,
- createNotification: createNotification,
- permissionLevel: permissionLevel,
- requestPermission: requestPermission
- };
- if (isFunction(Object.seal)) {
- Object.seal(win.notify);
- }
- }(window));
Advertisement
Add Comment
Please, Sign In to add comment