ariaNgNotificationService.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgNotificationService', ['$window', 'Notification', 'ariaNgLocalizationService', 'ariaNgSettingService', function ($window, Notification, ariaNgLocalizationService, ariaNgSettingService) {
  4. var isSupportBrowserNotification = !!$window.Notification;
  5. var isBrowserNotifactionGranted = function (permission) {
  6. return permission === 'granted';
  7. };
  8. var getBrowserNotifactionPermission = function () {
  9. if (!$window.Notification) {
  10. return null;
  11. }
  12. return $window.Notification.permission;
  13. };
  14. var requestBrowserNotifactionPermission = function (callback) {
  15. if (!$window.Notification) {
  16. return;
  17. }
  18. $window.Notification.requestPermission(function (permission) {
  19. if (callback) {
  20. callback({
  21. granted: isBrowserNotifactionGranted(permission),
  22. permission: permission
  23. });
  24. }
  25. });
  26. };
  27. var showBrowserNotifaction = function (title, options) {
  28. if (!$window.Notification) {
  29. return;
  30. }
  31. if (!isBrowserNotifactionGranted(getBrowserNotifactionPermission())) {
  32. return;
  33. }
  34. options = angular.extend({
  35. icon: 'tileicon.png'
  36. }, options);
  37. new $window.Notification(title, options);
  38. };
  39. var notifyViaBrowser = function (title, content, options) {
  40. if (!options) {
  41. options = {};
  42. }
  43. options.body = content;
  44. if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) {
  45. showBrowserNotifaction(title, options);
  46. }
  47. };
  48. var notifyInPage = function (title, content, options) {
  49. if (!options) {
  50. options = {};
  51. }
  52. if (!content) {
  53. options.message = title;
  54. } else {
  55. options.title = title;
  56. options.message = content;
  57. }
  58. if (!options.type || !Notification[options.type]) {
  59. options.type = 'primary';
  60. }
  61. if (!options.positionY) {
  62. options.positionY = 'bottom';
  63. }
  64. return Notification[options.type](options);
  65. };
  66. return {
  67. isSupportBrowserNotification: function () {
  68. return isSupportBrowserNotification;
  69. },
  70. hasBrowserPermission: function () {
  71. if (!isSupportBrowserNotification) {
  72. return false;
  73. }
  74. return isBrowserNotifactionGranted(getBrowserNotifactionPermission());
  75. },
  76. requestBrowserPermission: function (callback) {
  77. if (!isSupportBrowserNotification) {
  78. return;
  79. }
  80. requestBrowserNotifactionPermission(function (result) {
  81. if (!result.granted) {
  82. ariaNgSettingService.setBrowserNotification(false);
  83. }
  84. if (callback) {
  85. callback(result);
  86. }
  87. });
  88. },
  89. notifyViaBrowser: function (title, content, options) {
  90. if (title) {
  91. title = ariaNgLocalizationService.getLocalizedText(title);
  92. }
  93. if (content) {
  94. content = ariaNgLocalizationService.getLocalizedText(content);
  95. }
  96. return notifyViaBrowser(title, content, options);
  97. },
  98. notifyTaskComplete: function (task) {
  99. this.notifyViaBrowser('Download Completed', (task && task.taskName ? task.taskName : ''));
  100. },
  101. notifyBtTaskComplete: function (task) {
  102. this.notifyViaBrowser('BT Download Completed', (task && task.taskName ? task.taskName : ''));
  103. },
  104. notifyTaskError: function (task) {
  105. this.notifyViaBrowser('Download Error', (task && task.taskName ? task.taskName : ''));
  106. },
  107. notifyInPage: function (title, content, options) {
  108. if (!options) {
  109. options = {};
  110. }
  111. if (title) {
  112. title = ariaNgLocalizationService.getLocalizedText(title, options.titleParams);
  113. }
  114. if (content) {
  115. content = ariaNgLocalizationService.getLocalizedText(content, options.contentParams);
  116. if (options.contentPrefix) {
  117. content = options.contentPrefix + content;
  118. }
  119. }
  120. return notifyInPage(title, content, options);
  121. },
  122. clearNotificationInPage: function () {
  123. Notification.clearAll();
  124. }
  125. };
  126. }]);
  127. }());