ariaNgNotificationService.js 4.9 KB

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