refactor code

This commit is contained in:
MaysWind 2016-05-30 22:34:15 +08:00
parent 423d25b84e
commit 25c3bee90b
3 changed files with 64 additions and 56 deletions

View file

@ -1,7 +1,7 @@
(function () {
'use strict';
angular.module('ariaNg').controller('DownloadListController', ['$scope', '$window', '$location', '$interval', 'translateFilter', 'aria2RpcService', 'ariaNgSettingService', 'utils', function ($scope, $window, $location, $interval, translateFilter, aria2RpcService, ariaNgSettingService, utils) {
angular.module('ariaNg').controller('DownloadListController', ['$rootScope', '$scope', '$window', '$location', '$interval', 'translateFilter', 'aria2RpcService', 'ariaNgSettingService', 'utils', function ($rootScope, $scope, $window, $location, $interval, translateFilter, aria2RpcService, ariaNgSettingService, utils) {
var location = $location.path().substring(1);
var downloadTaskRefreshPromise = null;
var needRequestWholeInfo = true;
@ -40,9 +40,9 @@
return invokeMethod({
params: params,
callback: function (result) {
if (!utils.extendArray(result, $scope.taskContext.list, 'gid')) {
if (!utils.extendArray(result, $rootScope.taskContext.list, 'gid')) {
if (needRequestWholeInfo) {
$scope.taskContext.list = result;
$rootScope.taskContext.list = result;
needRequestWholeInfo = false;
} else {
needRequestWholeInfo = true;
@ -51,9 +51,9 @@
needRequestWholeInfo = false;
}
if ($scope.taskContext.list && $scope.taskContext.list.length > 0) {
for (var i = 0; i < $scope.taskContext.list.length; i++) {
utils.processDownloadTask($scope.taskContext.list[i]);
if ($rootScope.taskContext.list && $rootScope.taskContext.list.length > 0) {
for (var i = 0; i < $rootScope.taskContext.list.length; i++) {
utils.processDownloadTask($rootScope.taskContext.list[i]);
}
}
}
@ -68,11 +68,11 @@
return false;
}
if (!$scope.searchContext || !$scope.searchContext.text) {
if (!$rootScope.searchContext || !$rootScope.searchContext.text) {
return true;
}
return (task.taskName.toLowerCase().indexOf($scope.searchContext.text.toLowerCase()) >= 0);
return (task.taskName.toLowerCase().indexOf($rootScope.searchContext.text.toLowerCase()) >= 0);
};
$scope.getOrderType = function () {

View file

@ -1,7 +1,7 @@
(function () {
'use strict';
angular.module('ariaNg').controller('MainController', ['$scope', '$interval', 'aria2RpcService', 'ariaNgSettingService', 'utils', function ($scope, $interval, aria2RpcService, ariaNgSettingService, utils) {
angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$interval', 'aria2RpcService', 'ariaNgSettingService', 'utils', function ($rootScope, $scope, $interval, aria2RpcService, ariaNgSettingService, utils) {
var globalStatRefreshPromise = null;
var processStatResult = function (stat) {
@ -26,57 +26,12 @@
refreshGlobalStat();
$scope.searchContext = {
text: ''
};
$scope.taskContext = {
list: [],
selected: {}
};
$scope.isTaskSelected = function () {
var allTasks = $scope.taskContext.list;
if (!allTasks || allTasks.length < 1) {
return false;
}
var selectedTasks = $scope.taskContext.selected;
for (var i = 0; i < allTasks.length; i++) {
var task = allTasks[i];
if (selectedTasks[task.gid]) {
return true;
}
}
return false;
return $rootScope.taskContext.getSelectedTaskIds().length > 0;
};
$scope.selectAllTasks = function () {
var allTasks = $scope.taskContext.list;
if (!allTasks || allTasks.length < 1) {
return;
}
var selectedTasks = $scope.taskContext.selected;
var isAllSelected = true;
for (var i = 0; i < allTasks.length; i++) {
var task = allTasks[i];
if (!selectedTasks[task.gid]) {
isAllSelected = false;
break;
}
}
for (var i = 0; i < allTasks.length; i++) {
var task = allTasks[i];
selectedTasks[task.gid] = !isAllSelected;
}
$rootScope.taskContext.selectAll();
};
$scope.changeDisplayOrder = function (type, autoSetReverse) {

View file

@ -54,6 +54,53 @@
return angular.element('body').hasClass('sidebar-open');
};
$rootScope.searchContext = {
text: ''
};
$rootScope.taskContext = {
list: [],
selected: {},
getSelectedTaskIds: function () {
var result = [];
if (!this.list || !this.selected || this.list.length < 1) {
return result;
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (this.selected[task.gid]) {
result.push(task.gid);
}
}
return result;
},
selectAll: function () {
if (!this.list || !this.selected || this.list.length < 1) {
return result;
}
var isAllSelected = true;
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!this.selected[task.gid]) {
isAllSelected = false;
break;
}
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
this.selected[task.gid] = !isAllSelected;
}
}
};
$rootScope.swipeActions = {
leftSwipe: function () {
if (isSidebarShowInSmallScreen()) {
@ -78,6 +125,12 @@
delete $rootScope.swipeActions.extentLeftSwipe;
delete $rootScope.swipeActions.extentRightSwipe;
if (angular.isArray($rootScope.taskContext.list) && $rootScope.taskContext.list.length > 0) {
$rootScope.taskContext.list.length = 0;
}
console.log($rootScope.taskContext.list);
SweetAlert.close();
});