| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- (function () {
- 'use strict';
- angular.module('ariaNg').factory('ariaNgNotificationService', ['$window', 'Notification', 'ariaNgLocalizationService', 'ariaNgSettingService', function ($window, Notification, ariaNgLocalizationService, ariaNgSettingService) {
- var isSupportBrowserNotification = !!$window.Notification;
- var isBrowserNotifactionGranted = function (permission) {
- return permission === 'granted';
- };
- var getBrowserNotifactionPermission = function () {
- if (!$window.Notification) {
- return null;
- }
- return $window.Notification.permission;
- };
- var requestBrowserNotifactionPermission = function (callback) {
- if (!$window.Notification) {
- return;
- }
- $window.Notification.requestPermission(function (permission) {
- if (callback) {
- callback({
- granted: isBrowserNotifactionGranted(permission),
- permission: permission
- });
- }
- });
- };
- var showBrowserNotifaction = function (title, options) {
- if (!$window.Notification) {
- return;
- }
- if (!isBrowserNotifactionGranted(getBrowserNotifactionPermission())) {
- return;
- }
- options = angular.extend({
- icon: 'tileicon.png'
- }, options);
- new $window.Notification(title, options);
- };
- var notifyViaBrowser = function (title, content, options) {
- if (!options) {
- options = {};
- }
- options.body = content;
- if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) {
- showBrowserNotifaction(title, options);
- }
- };
- var notifyInPage = function (title, content, options) {
- if (!options) {
- options = {};
- }
- if (!content) {
- options.message = title;
- } else {
- options.title = title;
- options.message = content;
- }
- if (!options.type || !Notification[options.type]) {
- options.type = 'primary';
- }
- if (!options.positionY) {
- options.positionY = 'bottom';
- }
- return Notification[options.type](options);
- };
- return {
- isSupportBrowserNotification: function () {
- return isSupportBrowserNotification;
- },
- hasBrowserPermission: function () {
- if (!isSupportBrowserNotification) {
- return false;
- }
- return isBrowserNotifactionGranted(getBrowserNotifactionPermission());
- },
- requestBrowserPermission: function (callback) {
- if (!isSupportBrowserNotification) {
- return;
- }
- requestBrowserNotifactionPermission(function (result) {
- if (!result.granted) {
- ariaNgSettingService.setBrowserNotification(false);
- }
- if (callback) {
- callback(result);
- }
- });
- },
- notifyViaBrowser: function (title, content, options) {
- if (title) {
- title = ariaNgLocalizationService.getLocalizedText(title);
- }
- if (content) {
- content = ariaNgLocalizationService.getLocalizedText(content);
- }
- return notifyViaBrowser(title, content, options);
- },
- notifyTaskComplete: function (task) {
- this.notifyViaBrowser('Download Completed', (task && task.taskName ? task.taskName : ''));
- },
- notifyBtTaskComplete: function (task) {
- this.notifyViaBrowser('BT Download Completed', (task && task.taskName ? task.taskName : ''));
- },
- notifyTaskError: function (task) {
- this.notifyViaBrowser('Download Error', (task && task.taskName ? task.taskName : ''));
- },
- notifyInPage: function (title, content, options) {
- if (!options) {
- options = {};
- }
- if (title) {
- title = ariaNgLocalizationService.getLocalizedText(title, options.titleParams);
- }
- if (content) {
- content = ariaNgLocalizationService.getLocalizedText(content, options.contentParams);
- if (options.contentPrefix) {
- content = options.contentPrefix + content;
- }
- }
- return notifyInPage(title, content, options);
- },
- clearNotificationInPage: function () {
- Notification.clearAll();
- }
- };
- }]);
- }());
|