No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

195 líneas
5.6 KiB

  1. /*
  2. * Author: Alex Gibson
  3. * https://github.com/alexgibson/notify.js
  4. * License: MIT license
  5. */
  6. (function(global, factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. // AMD environment
  9. define(function() {
  10. return factory(global, global.document);
  11. });
  12. } else if (typeof module !== 'undefined' && module.exports) {
  13. // CommonJS environment
  14. module.exports = factory(global, global.document);
  15. } else {
  16. // Browser environment
  17. global.Notify = factory(global, global.document);
  18. }
  19. } (typeof window !== 'undefined' ? window : this, function (w, d) {
  20. 'use strict';
  21. function isFunction (item) {
  22. return typeof item === 'function';
  23. }
  24. function Notify(title, options) {
  25. if (typeof title !== 'string') {
  26. throw new Error('Notify(): first arg (title) must be a string.');
  27. }
  28. this.title = title;
  29. this.options = {
  30. icon: '',
  31. body: '',
  32. tag: '',
  33. notifyShow: null,
  34. notifyClose: null,
  35. notifyClick: null,
  36. notifyError: null,
  37. timeout: null
  38. };
  39. this.permission = null;
  40. if (!Notify.isSupported) {
  41. return;
  42. }
  43. //User defined options for notification content
  44. if (typeof options === 'object') {
  45. for (var i in options) {
  46. if (options.hasOwnProperty(i)) {
  47. this.options[i] = options[i];
  48. }
  49. }
  50. //callback when notification is displayed
  51. if (isFunction(this.options.notifyShow)) {
  52. this.onShowCallback = this.options.notifyShow;
  53. }
  54. //callback when notification is closed
  55. if (isFunction(this.options.notifyClose)) {
  56. this.onCloseCallback = this.options.notifyClose;
  57. }
  58. //callback when notification is clicked
  59. if (isFunction(this.options.notifyClick)) {
  60. this.onClickCallback = this.options.notifyClick;
  61. }
  62. //callback when notification throws error
  63. if (isFunction(this.options.notifyError)) {
  64. this.onErrorCallback = this.options.notifyError;
  65. }
  66. }
  67. }
  68. // true if the browser supports HTML5 Notification
  69. Notify.isSupported = 'Notification' in w;
  70. // true if the permission is not granted
  71. Notify.needsPermission = !(Notify.isSupported && Notification.permission === 'granted');
  72. // returns current permission level ('granted', 'default', 'denied' or null)
  73. Notify.permissionLevel = (Notify.isSupported ? Notification.permission : null);
  74. // asks the user for permission to display notifications. Then calls the callback functions is supplied.
  75. Notify.requestPermission = function (onPermissionGrantedCallback, onPermissionDeniedCallback) {
  76. if (!Notify.isSupported) {
  77. return;
  78. }
  79. w.Notification.requestPermission(function (perm) {
  80. switch (perm) {
  81. case 'granted':
  82. Notify.needsPermission = false;
  83. if (isFunction(onPermissionGrantedCallback)) {
  84. onPermissionGrantedCallback();
  85. }
  86. break;
  87. case 'denied':
  88. if (isFunction(onPermissionDeniedCallback)) {
  89. onPermissionDeniedCallback();
  90. }
  91. break;
  92. }
  93. });
  94. };
  95. Notify.prototype.show = function () {
  96. if (!Notify.isSupported) {
  97. return;
  98. }
  99. this.myNotify = new Notification(this.title, {
  100. 'body': this.options.body,
  101. 'tag' : this.options.tag,
  102. 'icon' : this.options.icon
  103. });
  104. if (this.options.timeout && !isNaN(this.options.timeout)) {
  105. setTimeout(this.close.bind(this), this.options.timeout * 1000);
  106. }
  107. this.myNotify.addEventListener('show', this, false);
  108. this.myNotify.addEventListener('error', this, false);
  109. this.myNotify.addEventListener('close', this, false);
  110. this.myNotify.addEventListener('click', this, false);
  111. };
  112. Notify.prototype.onShowNotification = function (e) {
  113. if (this.onShowCallback) {
  114. this.onShowCallback(e);
  115. }
  116. };
  117. Notify.prototype.onCloseNotification = function (e) {
  118. if (this.onCloseCallback) {
  119. this.onCloseCallback(e);
  120. }
  121. this.destroy();
  122. };
  123. Notify.prototype.onClickNotification = function (e) {
  124. if (this.onClickCallback) {
  125. this.onClickCallback(e);
  126. }
  127. };
  128. Notify.prototype.onErrorNotification = function (e) {
  129. if (this.onErrorCallback) {
  130. this.onErrorCallback(e);
  131. }
  132. this.destroy();
  133. };
  134. Notify.prototype.destroy = function () {
  135. this.myNotify.removeEventListener('show', this, false);
  136. this.myNotify.removeEventListener('error', this, false);
  137. this.myNotify.removeEventListener('close', this, false);
  138. this.myNotify.removeEventListener('click', this, false);
  139. };
  140. Notify.prototype.close = function () {
  141. this.myNotify.close();
  142. };
  143. Notify.prototype.handleEvent = function (e) {
  144. switch (e.type) {
  145. case 'show':
  146. this.onShowNotification(e);
  147. break;
  148. case 'close':
  149. this.onCloseNotification(e);
  150. break;
  151. case 'click':
  152. this.onClickNotification(e);
  153. break;
  154. case 'error':
  155. this.onErrorNotification(e);
  156. break;
  157. }
  158. };
  159. return Notify;
  160. }));