This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/services/ariaNgNotificationService.js
2018-08-12 20:26:26 +08:00

71 lines
2.4 KiB
JavaScript

(function () {
'use strict';
angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', 'Notification', 'ariaNgSettingService', function ($notification, Notification, ariaNgSettingService) {
var isSupportBrowserNotification = $notification.isSupported;
var isPermissionGranted = function (permission) {
return permission === 'granted';
};
return {
isSupportBrowserNotification: function () {
return isSupportBrowserNotification;
},
isPermissionGranted: function (permission) {
return isPermissionGranted(permission);
},
hasBrowserPermission: function () {
if (!isSupportBrowserNotification) {
return false;
}
return isPermissionGranted($notification.getPermission());
},
requestBrowserPermission: function (callback) {
if (!isSupportBrowserNotification) {
return;
}
$notification.requestPermission().then(function (permission) {
if (!isPermissionGranted(permission)) {
ariaNgSettingService.setBrowserNotification(false);
}
if (callback) {
callback(permission);
}
});
},
notifyViaBrowser: function (title, content) {
if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) {
$notification(title, {
body: content
});
}
},
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';
}
return Notification[options.type](options);
},
clearNotificationInPage: function () {
Notification.clearAll();
}
};
}]);
}());