diff options
author | Daniel Narvaez <dwnarvaez@gmail.com> | 2013-02-06 14:30:36 (GMT) |
---|---|---|
committer | Daniel Narvaez <dwnarvaez@gmail.com> | 2013-02-06 14:30:36 (GMT) |
commit | f5bac2f1e1a51b83d215a07f9d5d87db337873f3 (patch) | |
tree | 15c5f594b1e00c6272552cc5544a1bc757713e34 /shared/js/notification_helper.js | |
parent | c301ead6bb9c201801400964427b517dafb6a03c (diff) |
Get the build to work
Diffstat (limited to 'shared/js/notification_helper.js')
-rw-r--r-- | shared/js/notification_helper.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/shared/js/notification_helper.js b/shared/js/notification_helper.js new file mode 100644 index 0000000..384fbfc --- /dev/null +++ b/shared/js/notification_helper.js @@ -0,0 +1,68 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ + +'use strict'; + +/** + * Keeping a reference on all active notifications to avoid weird GC issues. + * See https://bugzilla.mozilla.org/show_bug.cgi?id=755402 + */ + +var NotificationHelper = { + _referencesArray: [], + + getIconURI: function nc_getIconURI(app, entryPoint) { + var icons = app.manifest.icons; + + if (entryPoint) { + icons = app.manifest.entry_points[entryPoint].icons; + } + + if (!icons) + return null; + + var sizes = Object.keys(icons).map(function parse(str) { + return parseInt(str, 10); + }); + sizes.sort(function(x, y) { return y - x; }); + + var HVGA = document.documentElement.clientWidth < 480; + var index = sizes[HVGA ? sizes.length - 1 : 0]; + return app.installOrigin + icons[index]; + }, + + send: function nc_send(title, body, icon, clickCB, closeCB) { + if (!('mozNotification' in navigator)) + return; + + var notification = navigator.mozNotification.createNotification(title, + body, icon); + + notification.onclick = (function() { + if (clickCB) + clickCB(); + + this._forget(notification); + }).bind(this); + + notification.onclose = (function() { + if (closeCB) + closeCB(); + + this._forget(notification); + }).bind(this); + + notification.show(); + this._keep(notification); + }, + + _keep: function nc_keep(notification) { + this._referencesArray.push(notification); + }, + _forget: function nc_forget(notification) { + this._referencesArray.splice( + this._referencesArray.indexOf(notification), 1 + ); + } +}; + |