angular-notification.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*! Angular notification v1.1.1 | (c) 2013 Greg Bergé | License MIT */
  2. angular
  3. .module('notification', [])
  4. .provider('$notification', $notificationProvider);
  5. /**
  6. * Notification provider.
  7. * Configure the default notification options.
  8. */
  9. function $notificationProvider() {
  10. var provider = this;
  11. /**
  12. * Expose Notification service.
  13. */
  14. this.$get = ['$window', '$rootScope', '$q', $notificationFactory];
  15. /**
  16. * Create a new Notification service.
  17. */
  18. function $notificationFactory($window, $rootScope, $q) {
  19. /**
  20. * Create a new Notification.
  21. *
  22. * @param {String} title
  23. * @param {Object} options
  24. */
  25. function NgNotification(title, options) {
  26. if (!$window.Notification) return false;
  27. options = options || {};
  28. var self = this;
  29. // Events cache.
  30. this._events = [];
  31. /**
  32. * Create the notification.
  33. */
  34. function createNotification() {
  35. // Extend options with default provider options.
  36. options = angular.extend({
  37. focusWindowOnClick: true
  38. }, provider.options || {}, options);
  39. // Create a base notification.
  40. try {
  41. self.baseNotification = new $window.Notification(title, options);
  42. } catch (error) {
  43. return;
  44. }
  45. // Close notification after specified delay.
  46. if (options.delay) setTimeout(angular.bind(self, self.close), options.delay);
  47. // Focus window on click.
  48. if (options.focusWindowOnClick)
  49. self.$on('click', function () {
  50. $window.focus();
  51. });
  52. // Re-bind events.
  53. self._events.forEach(function (args) {
  54. self.$on.apply(self, args);
  55. });
  56. // Reset events.
  57. self._events = [];
  58. }
  59. if ($window.Notification.permission === 'granted') {
  60. return createNotification();
  61. } else if ($window.Notification.permission !== 'denied') {
  62. NgNotification.requestPermission().then(createNotification);
  63. }
  64. }
  65. /**
  66. * Listen on event of a given type.
  67. * Supported events are:
  68. * - error
  69. * - show
  70. * - click
  71. * - close
  72. *
  73. * @param {String} name
  74. * @param {Function} listener
  75. */
  76. NgNotification.prototype.$on = function $on(name, listener) {
  77. var self = this;
  78. // If the notification is not ready, we cache the event.
  79. if (!this.baseNotification) return this._events.push([name, listener]);
  80. this.baseNotification.addEventListener(name, applyListener);
  81. function applyListener() {
  82. var args = arguments;
  83. $rootScope.$apply(function () {
  84. listener.apply(self, args);
  85. });
  86. }
  87. // Return the deregistration function.
  88. return function $off() {
  89. this.baseNotification.removeListener(event, applyListener);
  90. };
  91. };
  92. /**
  93. * Close the notification.
  94. */
  95. NgNotification.prototype.close = function close() {
  96. if (this.baseNotification) this.baseNotification.close();
  97. };
  98. /**
  99. * Static method to request permission.
  100. * It returns a promise
  101. */
  102. NgNotification.requestPermission = function () {
  103. return $q(function (resolve, reject) {
  104. if (!$window.Notification)
  105. return reject();
  106. $window.Notification.requestPermission(function (permission) {
  107. // Persist permission.
  108. $window.Notification.permission = $window.Notification.permission || permission;
  109. resolve($window.Notification.permission);
  110. });
  111. });
  112. };
  113. /**
  114. * Create a new notification.
  115. *
  116. * @param {string} title
  117. * @param {options} options
  118. */
  119. function $notification(title, options) {
  120. return new NgNotification(title, options);
  121. }
  122. // Expose requestPermission on $notification.
  123. $notification.requestPermission = NgNotification.requestPermission;
  124. return $notification;
  125. }
  126. /**
  127. * Define default options.
  128. *
  129. * @param {Object} options
  130. */
  131. this.setOptions = function setOptions(options) {
  132. this.options = options;
  133. };
  134. }