From 5abcf35e567c1bedbe83c9884a2533c87919e385 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 26 Jun 2016 22:52:06 +0800 Subject: [PATCH] support browser notification --- bower.json | 3 +- src/index.html | 4 +- src/langs/zh_CN.json | 4 ++ src/scripts/config/constants.js | 1 + src/scripts/config/defaultLanguage.js | 4 ++ src/scripts/controllers/main.js | 6 ++- src/scripts/controllers/settings-ariang.js | 21 ++++++-- src/scripts/core/app.js | 1 + .../services/ariaNgNotificationService.js | 49 +++++++++++++++++++ src/scripts/services/ariaNgSettingService.js | 6 +++ src/views/settings-ariang.html | 11 +++++ 11 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/scripts/services/ariaNgNotificationService.js diff --git a/bower.json b/bower.json index 2e89092..536e8c3 100644 --- a/bower.json +++ b/bower.json @@ -44,9 +44,10 @@ "angular-websocket": "^1.1.0", "angular-utf8-base64": "^0.0.5", "angular-local-storage": "^0.2.7", + "angular-notification": "775ee861c1737b284588bcb878ba1f4e43c70c97", "angular-busy": "^4.1.3", "angular-promise-buttons": "^0.1.14", "angular-dragula": "^1.2.7", - "ngSweetAlert": "https://github.com/oitozero/ngSweetAlert.git#8df6c30b0996f09cb4cf5e90a41115a6c09fa852" + "ngSweetAlert": "8df6c30b0996f09cb4cf5e90a41115a6c09fa852" } } diff --git a/src/index.html b/src/index.html index a8e9d37..8911eda 100644 --- a/src/index.html +++ b/src/index.html @@ -264,6 +264,7 @@ + @@ -306,8 +307,9 @@ - + + diff --git a/src/langs/zh_CN.json b/src/langs/zh_CN.json index 66a57d7..22c62d3 100644 --- a/src/langs/zh_CN.json +++ b/src/langs/zh_CN.json @@ -5,6 +5,8 @@ "OK": "确定", "Confirm": "确认", "Cancel": "取消", + "True": "是", + "False": "否", "New": "新建", "Start": "开始下载任务", "Pause": "暂停下载任务", @@ -101,6 +103,7 @@ "More Options": "更多选项", "Language": "语言", "Page Title": "页面标题", + "Enable Browser Notification": "启用浏览器通知", "Aria2 RPC Host": "Aria2 RPC 主机", "Aria2 RPC Port": "Aria2 RPC 端口", "Aria2 RPC Protocol": "Aria2 RPC 协议", @@ -108,6 +111,7 @@ "Global Stat Refresh Interval": "全局状态刷新间隔", "Download Task Refresh Interval": "下载任务刷新间隔", "Supported Placeholder: AriaNg Title ${title}, Downloading Count ${downloading}, Waiting Count ${waiting}, Stopped Count ${stopped}, Download Speed ${downspeed}, Upload Speed ${upspeed}.": "支持的占位符: AriaNg 标题 ${title}, 正在下载数量 ${downloading}, 正在等待数量 ${waiting}, 已停止数量 ${stopped}, 下载速度 ${downspeed}, 上传速度: ${upspeed}.", + "You have disabled notification in your browser. You should change your browser's settings before you enable this function.": "您已经在浏览器中禁用通知功能. 如需使用此功能, 请修改您浏览器的设置.", "Aria2 Version": "Aria2 版本", "Enabled Features": "已启用的功能", "Functions": "方法", diff --git a/src/scripts/config/constants.js b/src/scripts/config/constants.js index 7a279f3..61f9d0c 100644 --- a/src/scripts/config/constants.js +++ b/src/scripts/config/constants.js @@ -109,6 +109,7 @@ }).constant('ariaNgDefaultOptions', { language: 'en', title: '${downspeed}, ${upspeed} - ${title}', + browserNotification: false, rpcHost: '', rpcPort: '6800', protocol: 'http', diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index e862e20..fe30372 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -9,6 +9,8 @@ 'OK': 'OK', 'Confirm': 'Confirm', 'Cancel': 'Cancel', + 'True': 'True', + 'False': 'False', 'New': 'New', 'Start': 'Start', 'Pause': 'Pause', @@ -105,6 +107,7 @@ 'More Options': 'More Options', 'Language': 'Language', 'Page Title': 'Page Title', + 'Enable Browser Notification': 'Enable Browser Notification', 'Aria2 RPC Host': 'Aria2 RPC Host', 'Aria2 RPC Port': 'Aria2 RPC Port', 'Aria2 RPC Protocol': 'Aria2 RPC Protocol', @@ -112,6 +115,7 @@ 'Global Stat Refresh Interval': 'Global Stat Refresh Interval', 'Download Task Refresh Interval': 'Download Task Refresh Interval', 'Supported Placeholder: AriaNg Title ${title}, Downloading Count ${downloading}, Waiting Count ${waiting}, Stopped Count ${stopped}, Download Speed ${downspeed}, Upload Speed ${upspeed}.': 'Supported Placeholder: AriaNg Title ${title}, Downloading Count ${downloading}, Waiting Count ${waiting}, Stopped Count ${stopped}, Download Speed ${downspeed}, Upload Speed ${upspeed}.', + 'You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.': 'You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.', 'Aria2 Version': 'Aria2 Version', 'Enabled Features': 'Enabled Features', 'Functions': 'Functions', diff --git a/src/scripts/controllers/main.js b/src/scripts/controllers/main.js index 0dc862d..191b166 100644 --- a/src/scripts/controllers/main.js +++ b/src/scripts/controllers/main.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$location', '$document', '$interval', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgMonitorService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $location, $document, $interval, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, ariaNgMonitorService, aria2TaskService, aria2SettingService) { + angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$location', '$document', '$interval', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgNotificationService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $location, $document, $interval, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, ariaNgMonitorService, ariaNgNotificationService, aria2TaskService, aria2SettingService) { var globalStatRefreshPromise = null; var refreshGlobalStat = function (silent) { @@ -24,6 +24,10 @@ } }, silent); }; + + if (ariaNgSettingService.getBrowserNotification()) { + ariaNgNotificationService.requestBrowserPermission(); + } $scope.globalStatusContext = { data: ariaNgMonitorService.getGlobalStatsData() diff --git a/src/scripts/controllers/settings-ariang.js b/src/scripts/controllers/settings-ariang.js index be8aefa..9d0739e 100644 --- a/src/scripts/controllers/settings-ariang.js +++ b/src/scripts/controllers/settings-ariang.js @@ -1,15 +1,30 @@ (function () { 'use strict'; - angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$timeout', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgSettingService', function ($rootScope, $scope, $timeout, ariaNgLanguages, ariaNgCommonService, ariaNgSettingService) { + angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$timeout', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgNotificationService', function ($rootScope, $scope, $timeout, ariaNgLanguages, ariaNgCommonService, ariaNgSettingService, ariaNgNotificationService) { $scope.context = { languages: ariaNgLanguages, availableTime: ariaNgCommonService.getTimeOptions([1000, 2000, 3000, 5000, 10000, 30000, 60000], true), - settings: ariaNgSettingService.getAllOptions() + trueFalseOptions: [{name: 'True', value: true}, {name: 'False', value: false}], + settings: ariaNgSettingService.getAllOptions(), + supportBrowserNotification: ariaNgNotificationService.isSupportBrowserNotification() }; - + $scope.settingService = ariaNgSettingService; + $scope.setEnableBrowserNotification = function (value) { + ariaNgSettingService.setBrowserNotification(value); + + if (value && !ariaNgNotificationService.hasBrowserPermission()) { + ariaNgNotificationService.requestBrowserPermission(function (permission) { + if (!ariaNgNotificationService.isPermissionGranted(permission)) { + $scope.context.settings.browserNotification = false; + ariaNgCommonService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.'); + } + }); + } + }; + $rootScope.loadPromise = $timeout(function () { ;//Do Nothing }, 100); diff --git a/src/scripts/core/app.js b/src/scripts/core/app.js index 9fb0cd5..2cce7e2 100644 --- a/src/scripts/core/app.js +++ b/src/scripts/core/app.js @@ -13,6 +13,7 @@ 'ngWebSocket', 'ab-base64', 'LocalStorageModule', + 'notification', 'cgBusy', 'angularPromiseButtons', 'oitozero.ngSweetAlert', diff --git a/src/scripts/services/ariaNgNotificationService.js b/src/scripts/services/ariaNgNotificationService.js new file mode 100644 index 0000000..0162c6a --- /dev/null +++ b/src/scripts/services/ariaNgNotificationService.js @@ -0,0 +1,49 @@ +(function () { + 'use strict'; + + angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', 'ariaNgSettingService', function ($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); + } + }); + }, + notify: function (title, content) { + if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) { + $notification(title, { + body: content + }); + } + } + } + }]); +})(); diff --git a/src/scripts/services/ariaNgSettingService.js b/src/scripts/services/ariaNgSettingService.js index 94282cf..59ce717 100644 --- a/src/scripts/services/ariaNgSettingService.js +++ b/src/scripts/services/ariaNgSettingService.js @@ -89,6 +89,12 @@ setTitle: function (value) { setOption('title', value); }, + getBrowserNotification: function () { + return getOption('browserNotification'); + }, + setBrowserNotification: function (value) { + setOption('browserNotification', value); + }, getJsonRpcUrl: function () { var protocol = getOption('protocol'); var rpcHost = getOption('rpcHost'); diff --git a/src/views/settings-ariang.html b/src/views/settings-ariang.html index de4e246..48a98e8 100644 --- a/src/views/settings-ariang.html +++ b/src/views/settings-ariang.html @@ -21,6 +21,17 @@ +
+
+ Enable Browser Notification +
+
+ +
+
Aria2 RPC Host