Guest User

Untitled

a guest
Oct 16th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (win) {
  2.     var PERMISSION_DEFAULT = "default",
  3.         PERMISSION_GRANTED = "granted",
  4.         PERMISSION_DENIED = "denied",
  5.         PERMISSION = [PERMISSION_GRANTED, PERMISSION_DEFAULT, PERMISSION_DENIED],
  6.         defaultSetting = {
  7.             pageVisibility: false,
  8.             autoClose: 0
  9.         }, empty = {}, emptyString = "",
  10.         isSupported = (function () {
  11.             var isSupported = false;
  12.             try {
  13.                 isSupported = !! (win.Notification || win.webkitNotifications || navigator.mozNotification || (win.external && win.external.msIsSiteMode() !== undefined));
  14.             } catch (e) {}
  15.             return isSupported;
  16.         }()),
  17.         ieVerification = Math.floor((Math.random() * 10) + 1),
  18.         isFunction = function (value) {
  19.             return (value && (value).constructor === Function);
  20.         }, isString = function (value) {
  21.             return (value && (value).constructor === String);
  22.         }, isObject = function (value) {
  23.             return (value && (value).constructor === Object);
  24.         }, mixin = function (target, source) {
  25.             var name, s;
  26.             for (name in source) {
  27.                 s = source[name];
  28.                 if (!(name in target) || (target[name] !== s && (!(name in empty) || empty[name] !== s))) {
  29.                     target[name] = s;
  30.                 }
  31.             }
  32.             return target;
  33.         }, noop = function () {}, settings = defaultSetting;
  34.  
  35.     function getNotification(title, options) {
  36.         var notification;
  37.         if (win.Notification) {
  38.             notification = new win.Notification(title, {
  39.                 icon: isString(options.icon) ? options.icon : options.icon.x32,
  40.                 body: options.body || emptyString,
  41.                 tag: options.tag || emptyString
  42.             });
  43.             if (isFunction(options.onclick)) {
  44.                 notification.onclick = options.onclick;
  45.             }
  46.         } else if (win.webkitNotifications) {
  47.             notification = win.webkitNotifications.createNotification(options.icon, title, options.body);
  48.             if (isFunction(options.onclick)) {
  49.                 notification.onclick = options.onclick;
  50.             }
  51.             notification.show();
  52.         } else if (navigator.mozNotification) {
  53.             notification = navigator.mozNotification.createNotification(title, options.body, options.icon);
  54.             if (isFunction(options.onclick)) {
  55.                 notification.onclick = options.onclick;
  56.             }
  57.             notification.show();
  58.         } else if (win.external && win.external.msIsSiteMode()) {
  59.             win.external.msSiteModeClearIconOverlay();
  60.             win.external.msSiteModeSetIconOverlay((isString(options.icon) ? options.icon : options.icon.x16), title);
  61.             win.external.msSiteModeActivate();
  62.             notification = {
  63.                 "ieVerification": ieVerification + 1
  64.             };
  65.         }
  66.         return notification;
  67.     }
  68.  
  69.     function getWrapper(notification) {
  70.         return {
  71.             close: function () {
  72.                 if (notification) {
  73.                     if (notification.close) {
  74.                         notification.close();
  75.                     } else if (win.external && win.external.msIsSiteMode()) {
  76.                         if (notification.ieVerification === ieVerification) {
  77.                             win.external.msSiteModeClearIconOverlay();
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         };
  83.     }
  84.  
  85.     function requestPermission(callback) {
  86.         if (!isSupported) {
  87.             return;
  88.         }
  89.         var callbackFunction = isFunction(callback) ? callback : noop;
  90.         if (win.webkitNotifications && win.webkitNotifications.checkPermission) {
  91.             win.webkitNotifications.requestPermission(callbackFunction);
  92.         } else if (win.Notification && win.Notification.requestPermission) {
  93.             win.Notification.requestPermission(callbackFunction);
  94.         }
  95.     }
  96.  
  97.     function permissionLevel() {
  98.         var permission;
  99.         if (!isSupported) {
  100.             return;
  101.         }
  102.         if (win.Notification && win.Notification.permissionLevel) {
  103.             permission = win.Notification.permissionLevel();
  104.         } else if (win.webkitNotifications && win.webkitNotifications.checkPermission) {
  105.             permission = PERMISSION[win.webkitNotifications.checkPermission()];
  106.         } else if (navigator.mozNotification) {
  107.             permission = PERMISSION_GRANTED;
  108.         } else if (win.Notification && win.Notification.permission) {
  109.             permission = win.Notification.permission;
  110.         } else if (win.external && (win.external.msIsSiteMode() !== undefined)) {
  111.             permission = win.external.msIsSiteMode() ? PERMISSION_GRANTED : PERMISSION_DEFAULT;
  112.         }
  113.         return permission;
  114.     }
  115.  
  116.     function config(params) {
  117.         if (params && isObject(params)) {
  118.             mixin(settings, params);
  119.         }
  120.         return settings;
  121.     }
  122.  
  123.     function isDocumentHidden() {
  124.         return settings.pageVisibility ? (document.hidden || document.msHidden || document.mozHidden || document.webkitHidden) : true;
  125.     }
  126.  
  127.     function createNotification(title, options) {
  128.         var notification, notificationWrapper;
  129.         if (isSupported && isDocumentHidden() && isString(title) && (options && (isString(options.icon) || isObject(options.icon))) && (permissionLevel() === PERMISSION_GRANTED)) {
  130.             notification = getNotification(title, options);
  131.         }
  132.         notificationWrapper = getWrapper(notification);
  133.         if (settings.autoClose && notification && !notification.ieVerification && notification.addEventListener) {
  134.             notification.addEventListener("show", function () {
  135.                 var notification = notificationWrapper;
  136.                 win.setTimeout(function () {
  137.                     notification.close();
  138.                 }, settings.autoClose);
  139.             });
  140.         }
  141.         return notificationWrapper;
  142.     }
  143.     win.notify = {
  144.         PERMISSION_DEFAULT: PERMISSION_DEFAULT,
  145.         PERMISSION_GRANTED: PERMISSION_GRANTED,
  146.         PERMISSION_DENIED: PERMISSION_DENIED,
  147.         isSupported: isSupported,
  148.         config: config,
  149.         createNotification: createNotification,
  150.         permissionLevel: permissionLevel,
  151.         requestPermission: requestPermission
  152.     };
  153.     if (isFunction(Object.seal)) {
  154.         Object.seal(win.notify);
  155.     }
  156. }(window));
Advertisement
Add Comment
Please, Sign In to add comment