diff --git a/www/common/notify.js b/www/common/notify.js new file mode 100644 index 000000000..1bc87e17e --- /dev/null +++ b/www/common/notify.js @@ -0,0 +1,100 @@ +(function () { + var Module = {}; + + var isSupported = Module.isSupported = function () { + return typeof(window.Notification) === 'function'; + }; + + var hasPermission = Module.hasPermission = function () { + return Notification.permission === 'granted'; + }; + + var getPermission = Module.getPermission = function (f) { + Notification.requestPermission(function (permission) { + if (permission === "granted") { f(true); } + else { f(false); } + }); + }; + + var create = Module.create = function (msg, title, icon) { + return new Notification(title,{ + // icon: icon, + body: msg, + }); + }; + + var system = Module.system = function (msg, title, icon) { + // Let's check if the browser supports notifications + if (!isSupported()) { console.log("Notifications are not supported"); } + + // Let's check whether notification permissions have already been granted + else if (hasPermission()) { + // If it's okay let's create a notification + return create(msg, title, icon); + } + + // Otherwise, we need to ask the user for permission + else if (Notification.permission !== 'denied') { + getPermission(function (state) { + if (state) { create(msg, title, icon); } + }); + } + }; + + var tab = Module.tab = function (msg, frequency, count) { + var original = document.title; + var key = '_pendingTabNotification'; + + var favicon = document.getElementById('favicon'); + if (favicon) { + var main = favicon.getAttribute('href'); + var alt = favicon.getAttribute('data-alt-favicon'); + } + + var cancel = function () { + // only run one tab notification at a time + if (Module[key]) { + window.clearInterval(Module[key]); + document.title = original; + if (favicon) { + favicon.setAttribute('href', main); + } + + return true; + } + return false; + }; + + cancel(); + + var step = function () { + document.title = (document.title === original) ? msg : original; + if (favicon) { + favicon.setAttribute('href', favicon.getAttribute('href') === main? alt : main); + } + + --count; + }; + + Module[key] = window.setInterval(function () { + if (count > 0) { return step(); } + cancel(); + }, frequency); + step(); + + return { + cancel: cancel, + original: original + }; + }; + + if (typeof(module) !== 'undefined' && module.exports) { + module.exports = Module; + } else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) { + define(function () { + return Module; + }); + } else { + window.Visible = Module; + } +}()); diff --git a/www/common/visible.js b/www/common/visible.js new file mode 100644 index 000000000..4f84ecdf1 --- /dev/null +++ b/www/common/visible.js @@ -0,0 +1,47 @@ +(function () { + // Set the name of the hidden property and the change event for visibility + var hidden, visibilityChange; + if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support + hidden = "hidden"; + visibilityChange = "visibilitychange"; + } else if (typeof document.mozHidden !== "undefined") { + hidden = "mozHidden"; + visibilityChange = "mozvisibilitychange"; + } else if (typeof document.msHidden !== "undefined") { + hidden = "msHidden"; + visibilityChange = "msvisibilitychange"; + } else if (typeof document.webkitHidden !== "undefined") { + hidden = "webkitHidden"; + visibilityChange = "webkitvisibilitychange"; + } + + var Visible = { + hidden: hidden, + visibilityChange: visibilityChange, + }; + + var isSupported = Visible.isSupported = function () { + return !(typeof(document.addEventListener) === "undefined" || + typeof document[hidden] === "undefined"); + }; + + var onChange = Visible.onChange = function (f) { + document.addEventListener(visibilityChange, function (ev) { + f(!document[hidden], ev); + }, false); + }; + + var currently = Visible.currently = function () { + return !document[hidden]; + }; + + if (typeof(module) !== 'undefined' && module.exports) { + module.exports = Visible; + } else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) { + define(function () { + return Visible; + }); + } else { + window.Visible = Visible; + } +}());