diff --git a/clientapp/app.js b/clientapp/app.js index baea192..84fd96b 100644 --- a/clientapp/app.js +++ b/clientapp/app.js @@ -12,7 +12,7 @@ var MainView = require('./views/main'); var Router = require('./router'); var Storage = require('./storage'); var xmppEventHandlers = require('./helpers/xmppEventHandlers'); -var notifier = require('./helpers/notifications'); +var Notify = require('notify.js'); module.exports = { @@ -20,7 +20,6 @@ module.exports = { var self = window.app = this; var config = localStorage.config; - self.notifier = notifier; if (!config) { console.log('missing config'); @@ -33,6 +32,7 @@ module.exports = { async.series([ function (cb) { + app.notifications = new Notify(); app.storage = new Storage(); app.storage.open(cb); }, diff --git a/clientapp/helpers/notifications.js b/clientapp/helpers/notifications.js deleted file mode 100644 index a0322b9..0000000 --- a/clientapp/helpers/notifications.js +++ /dev/null @@ -1,60 +0,0 @@ -// simple module for showing notifications using growl if in fluid app, -// webkit notifications if present and permission granted and using UI Kit -// as an in-browser fallback. #winning -/*global ui */ -/* Here's the api... pretty simple -{ - title: , - description: - sticky: , - callback: , - icon: -} -*/ -var templates = require('../templates'); - -exports.show = function (opts) { - var hideTimeout = 5000, - note; - - // set default icon - opts.icon || (opts.icon = '/images/applogo.png'); - - if (window.macgap) { - window.macgap.growl.notify(opts); - } else if (window.fluid) { - window.fluid.showGrowlNotification(opts); - } else if (window.webkitNotifications && window.webkitNotifications.checkPermission() === 0) { - note = window.webkitNotifications.createNotification(opts.icon, opts.title, opts.description); - note.show(); - if (!opts.sticky) { - setTimeout(function () { - note.cancel(); - }, hideTimeout); - } - if (opts.onclick) note.onclick = opts.onclick; - } else { - // build some HTML since we want to include an image - note = ui.notify(templates.misc.growlMessage(opts)).closable(); - if (opts.sticky) { - note.sticky(); - } else { - note.hide(hideTimeout); - } - if (opts.onclick) note.on('click', opts.onclick); - } -}; - -exports.shouldAskPermission = function () { - return (window.webkitNotifications && (window.webkitNotifications.checkPermission() !== 0) && (window.webkitNotifications.checkPermission() !== 2)) || false; -}; - -exports.askPermission = function (cb) { - if (!window.webkitNotifications) { - cb(false); - } else { - window.webkitNotifications.requestPermission(function () { - if (cb) cb(window.webkitNotifications.checkPermission() === 0); - }); - } -}; diff --git a/clientapp/models/contact.js b/clientapp/models/contact.js index 52c362b..1b60228 100644 --- a/clientapp/models/contact.js +++ b/clientapp/models/contact.js @@ -174,10 +174,10 @@ module.exports = HumanModel.define({ if (notify && (!this.activeContact || (this.activeContact && !app.state.focused)) && message.from.bare === this.jid) { this.unreadCount++; - app.notifier.show({ - title: this.displayName, - description: message.body, + app.notifications.create(this.displayName, { + body: message.body, icon: this.avatar, + tag: this.jid, onclick: _.bind(app.navigate, app, '/chat/' + this.jid) }); } diff --git a/clientapp/models/muc.js b/clientapp/models/muc.js index 3b684bf..1d98374 100644 --- a/clientapp/models/muc.js +++ b/clientapp/models/muc.js @@ -63,10 +63,10 @@ module.exports = HumanModel.define({ if (notify && (!this.activeContact || (this.activeContact && !app.hasFocus))) { this.unreadCount++; - app.notifier.show({ - title: this.displayName, - description: message.body, + app.notifications.create(this.displayName, { + body: message.body, icon: this.avatar, + tag: this.id, onclick: _.bind(app.navigate, app, '/chat/' + this.jid) }); } diff --git a/clientapp/models/state.js b/clientapp/models/state.js index 022936e..72ea61b 100644 --- a/clientapp/models/state.js +++ b/clientapp/models/state.js @@ -24,7 +24,7 @@ module.exports = HumanModel.define({ }, true); } - if (navigator.mozApps) { + if (window.navigator.mozApps) { this.installable = true; var req = navigator.mozApps.checkInstalled(window.location.origin + '/manifest.webapp'); req.onsuccess = function (e) { @@ -34,6 +34,8 @@ module.exports = HumanModel.define({ }; } + //this.allowAlerts = app.notifications.allowed(); + this.markActive(); }, session: { diff --git a/clientapp/pages/main.js b/clientapp/pages/main.js index 2005e90..1f94dd8 100644 --- a/clientapp/pages/main.js +++ b/clientapp/pages/main.js @@ -26,19 +26,18 @@ module.exports = BasePage.extend({ 'blur .status': 'handleStatusChange' }, initialize: function (spec) { - me.shouldAskForAlertsPermission = app.notifier.shouldAskPermission(); this.renderAndBind(); }, enableAlerts: function () { - app.notifier.askPermission(function () { - var shouldAsk = app.notifier.shouldAskPermission(); - if (!shouldAsk) { - app.notifier.show({ - title: 'Ok, sweet!', - description: "You'll now be notified of stuff that happens." - }); - } - }); + if (app.notifications.permissionNeeded()) { + app.notifications.requestPermission(function (perm) { + if (perm === 'granted') { + app.notifications.create('Ok, sweet!', { + body: "You'll now be notified of stuff that happens." + }); + } + }); + } }, installFirefox: function () { navigator.mozApps.install(window.location.origin + '/manifest.webapp'); diff --git a/package.json b/package.json index ac926f9..c45867d 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "templatizer": "0.1.2", "underscore": "1.5.1", "raf-component": "1.1.1", - "stanza.io": "2.3.1" + "stanza.io": "2.5.2", + "notify.js": "0.0.1" }, "devDependencies": { "precommit-hook": "0.3.6"