From f6e9e9878ed46f8c6bf4cc4c76794b1a93601d22 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Wed, 1 Jun 2016 23:31:48 +0800 Subject: [PATCH] refactor code --- app/index.html | 1 + app/scripts/controllers/settings-aria2.js | 36 +++--------- app/scripts/directives/setting.js | 72 +++++++++++++++++++++++ app/views/setting.html | 54 +++++++++++++++++ app/views/settings-aria2.html | 71 +--------------------- 5 files changed, 138 insertions(+), 96 deletions(-) create mode 100644 app/scripts/directives/setting.js create mode 100644 app/views/setting.html diff --git a/app/index.html b/app/index.html index 48a71d3..3beb1b9 100644 --- a/app/index.html +++ b/app/index.html @@ -276,6 +276,7 @@ + diff --git a/app/scripts/controllers/settings-aria2.js b/app/scripts/controllers/settings-aria2.js index d7517f3..08966ef 100644 --- a/app/scripts/controllers/settings-aria2.js +++ b/app/scripts/controllers/settings-aria2.js @@ -1,9 +1,8 @@ (function () { 'use strict'; - angular.module('ariaNg').controller('Aria2SettingsController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgConstants', 'ariaNgCommonService', 'aria2SettingService', function ($rootScope, $scope, $location, $timeout, ariaNgConstants, ariaNgCommonService, aria2SettingService) { + angular.module('ariaNg').controller('Aria2SettingsController', ['$rootScope', '$scope', '$location', 'ariaNgConstants', 'ariaNgCommonService', 'aria2SettingService', function ($rootScope, $scope, $location, ariaNgConstants, ariaNgCommonService, aria2SettingService) { var location = $location.path().substring($location.path().lastIndexOf('/') + 1); - var pendingSaveRequests = {}; var getAvailableOptions = function (type) { var keys = aria2SettingService.getAvailableOptionsKeys(type); @@ -19,33 +18,16 @@ $scope.optionStatus = {}; $scope.availableOptions = getAvailableOptions(location); - $scope.setGlobalOption = function (option, value, lazySave) { - if (!option || !option.key || option.readonly) { - return; - } + $scope.pendingGlobalOption = function (key, value) { + $scope.optionStatus[key] = 'pending'; + }; - var key = option.key; - var invoke = function () { - $scope.optionStatus[key] = 'saving'; + $scope.setGlobalOption = function (key, value) { + $scope.optionStatus[key] = 'saving'; - return aria2SettingService.setGlobalOption(key, value, function (result) { - $scope.optionStatus[key] = 'saved'; - }); - }; - - delete $scope.optionStatus[key]; - - if (lazySave) { - if (pendingSaveRequests[key]) { - $timeout.cancel(pendingSaveRequests[key]); - } - - pendingSaveRequests[key] = $timeout(function () { - invoke(); - }, ariaNgConstants.lazySaveTimeout); - } else { - invoke(); - } + return aria2SettingService.setGlobalOption(key, value, function (result) { + $scope.optionStatus[key] = 'saved'; + }); }; $rootScope.loadPromise = (function () { diff --git a/app/scripts/directives/setting.js b/app/scripts/directives/setting.js new file mode 100644 index 0000000..25b74bb --- /dev/null +++ b/app/scripts/directives/setting.js @@ -0,0 +1,72 @@ +(function () { + 'use strict'; + + angular.module("ariaNg").directive('ngSetting', ['$timeout', 'ariaNgConstants', function ($timeout, ariaNgConstants) { + return { + restrict: 'E', + templateUrl: "views/setting.html", + require: '?ngModel', + replace: true, + scope: { + option: '=', + ngModel: '=', + status: '=', + beforeChangeValue: '&', + onChangeValue: '&' + }, + link: function (scope, element, attrs, ngModel) { + var pendingSaveRequest = null; + var options = { + lazySaveTimeout: ariaNgConstants.lazySaveTimeout + }; + + angular.extend(options, attrs); + + scope.getTotalCount = function () { + if (!scope.optionValue && !angular.isString(scope.optionValue)) { + return 0; + } + + return scope.optionValue.split(scope.option.split).length; + }; + + scope.changeValue = function (optionValue, lazySave) { + scope.optionValue = optionValue; + + if (!scope.option || !scope.option.key || scope.option.readonly) { + return; + } + + var data = { + key: scope.option.key, + value: optionValue + }; + + if (scope.beforeChangeValue) { + scope.beforeChangeValue(data); + } + + if (pendingSaveRequest) { + $timeout.cancel(pendingSaveRequest); + } + + if (scope.onChangeValue) { + if (lazySave) { + pendingSaveRequest = $timeout(function () { + scope.onChangeValue(data); + }, options.lazySaveTimeout); + } else { + scope.onChangeValue(data); + } + } + }; + + scope.$watch(function () { + return ngModel.$viewValue; + }, function (value) { + scope.optionValue = value; + }); + } + }; + }]); +})(); diff --git a/app/views/setting.html b/app/views/setting.html new file mode 100644 index 0000000..226a836 --- /dev/null +++ b/app/views/setting.html @@ -0,0 +1,54 @@ +
+
+ + + + +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+ +
+
+
diff --git a/app/views/settings-aria2.html b/app/views/settings-aria2.html index 4926ffa..954b4b7 100644 --- a/app/views/settings-aria2.html +++ b/app/views/settings-aria2.html @@ -1,73 +1,6 @@
-
-
- - - - -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
- -
-
- -
- -
-
-
-
-
- -
- -
-
-
- -
-
-
+