code refactor, remove angular-notification, and add icon in notification

This commit is contained in:
MaysWind 2018-08-26 16:44:32 +08:00
parent 325e16e299
commit 6119b8e54e
5 changed files with 59 additions and 21 deletions

View file

@ -27,7 +27,6 @@
"angular-websocket": "^2.0.0", "angular-websocket": "^2.0.0",
"angular-utf8-base64": "^0.0.5", "angular-utf8-base64": "^0.0.5",
"angular-local-storage": "^0.7.1", "angular-local-storage": "^0.7.1",
"angular-notification": "git://github.com/neoziro/angular-notification#775ee861c1737b284588bcb878ba1f4e43c70c97",
"angular-ui-notification": "^0.3.6", "angular-ui-notification": "^0.3.6",
"angular-bittorrent-peerid": "^1.2.0", "angular-bittorrent-peerid": "^1.2.0",
"angular-busy": "^4.1.4", "angular-busy": "^4.1.4",

View file

@ -128,8 +128,8 @@
ariaNgSettingService.setBrowserNotification(value); ariaNgSettingService.setBrowserNotification(value);
if (value && !ariaNgNotificationService.hasBrowserPermission()) { if (value && !ariaNgNotificationService.hasBrowserPermission()) {
ariaNgNotificationService.requestBrowserPermission(function (permission) { ariaNgNotificationService.requestBrowserPermission(function (result) {
if (!ariaNgNotificationService.isPermissionGranted(permission)) { if (!result.granted) {
$scope.context.settings.browserNotification = false; $scope.context.settings.browserNotification = false;
ariaNgLocalizationService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.'); ariaNgLocalizationService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.');
} }

View file

@ -13,7 +13,6 @@
'ngWebSocket', 'ngWebSocket',
'utf8-base64', 'utf8-base64',
'LocalStorageModule', 'LocalStorageModule',
'notification',
'ui-notification', 'ui-notification',
'angularBittorrentPeerid', 'angularBittorrentPeerid',
'cgBusy', 'cgBusy',

View file

@ -54,7 +54,7 @@
ariaNgCommonService.confirm(title, text, type, callback, notClose, extendSettings); ariaNgCommonService.confirm(title, text, type, callback, notClose, extendSettings);
}, },
notifyViaBrowser: function (title, content) { notifyViaBrowser: function (title, content, options) {
if (title) { if (title) {
title = this.getLocalizedText(title); title = this.getLocalizedText(title);
} }
@ -63,7 +63,7 @@
content = this.getLocalizedText(content); content = this.getLocalizedText(content);
} }
return ariaNgNotificationService.notifyViaBrowser(title, content); return ariaNgNotificationService.notifyViaBrowser(title, content, options);
}, },
notifyInPage: function (title, content, options) { notifyInPage: function (title, content, options) {
if (!options) { if (!options) {

View file

@ -1,47 +1,87 @@
(function () { (function () {
'use strict'; 'use strict';
angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', 'Notification', 'ariaNgSettingService', function ($notification, Notification, ariaNgSettingService) { angular.module('ariaNg').factory('ariaNgNotificationService', ['$window', 'Notification', 'ariaNgSettingService', function ($window, Notification, ariaNgSettingService) {
var isSupportBrowserNotification = $notification.isSupported; var isSupportBrowserNotification = !!$window.Notification;
var isPermissionGranted = function (permission) { var isBrowserNotifactionGranted = function (permission) {
return permission === 'granted'; 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);
};
return { return {
isSupportBrowserNotification: function () { isSupportBrowserNotification: function () {
return isSupportBrowserNotification; return isSupportBrowserNotification;
}, },
isPermissionGranted: function (permission) {
return isPermissionGranted(permission);
},
hasBrowserPermission: function () { hasBrowserPermission: function () {
if (!isSupportBrowserNotification) { if (!isSupportBrowserNotification) {
return false; return false;
} }
return isPermissionGranted($notification.getPermission()); return isBrowserNotifactionGranted(getBrowserNotifactionPermission());
}, },
requestBrowserPermission: function (callback) { requestBrowserPermission: function (callback) {
if (!isSupportBrowserNotification) { if (!isSupportBrowserNotification) {
return; return;
} }
$notification.requestPermission().then(function (permission) { requestBrowserNotifactionPermission(function (result) {
if (!isPermissionGranted(permission)) { if (!result.granted) {
ariaNgSettingService.setBrowserNotification(false); ariaNgSettingService.setBrowserNotification(false);
} }
if (callback) { if (callback) {
callback(permission); callback(result);
} }
}); });
}, },
notifyViaBrowser: function (title, content) { notifyViaBrowser: function (title, content, options) {
if (!options) {
options = {};
}
options.body = content;
if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) { if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) {
$notification(title, { showBrowserNotifaction(title, options);
body: content
});
} }
}, },
notifyInPage: function (title, content, options) { notifyInPage: function (title, content, options) {