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/core/root.js

277 lines
9.4 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2018-08-12 14:26:26 +02:00
angular.module('ariaNg').run(['$rootScope', '$location', '$document', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', function ($rootScope, $location, $document, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService, aria2TaskService) {
2016-05-31 16:51:12 +02:00
var isUrlMatchUrl2 = function (url, url2) {
if (url === url2) {
return true;
}
var index = url2.indexOf(url);
if (index !== 0) {
return false;
}
var lastPart = url2.substring(url.length);
2016-08-01 16:49:16 +02:00
if (lastPart.indexOf('/') === 0) {
2016-05-31 16:51:12 +02:00
return true;
}
return false;
};
2016-05-13 18:09:12 +02:00
var initCheck = function () {
var browserFeatures = ariaNgSettingService.getBrowserFeatures();
if (!browserFeatures.localStroage) {
ariaNgLogService.warn('[root.initCheck] LocalStorage is not supported!');
}
if (!browserFeatures.cookies) {
ariaNgLogService.warn('[root.initCheck] Cookies is not supported!');
}
if (!ariaNgSettingService.isBrowserSupportStorage()) {
angular.element('body').prepend('<div class="disable-overlay"></div>');
angular.element('.main-sidebar').addClass('blur');
angular.element('.navbar').addClass('blur');
angular.element('.content-body').addClass('blur');
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.notifyInPage('', 'You cannot use AriaNg because this browser does not support data storage.', {
type: 'error',
delay: false
});
throw new Error('You cannot use AriaNg because this browser does not support data storage.');
}
};
2016-06-29 17:22:42 +02:00
var initNavbar = function () {
angular.element('section.sidebar > ul > li[data-href-match] > a').click(function () {
angular.element('section.sidebar > ul li').removeClass('active');
angular.element(this).parent().addClass('active');
});
angular.element('section.sidebar > ul > li.treeview > ul.treeview-menu > li[data-href-match] > a').click(function () {
angular.element('section.sidebar > ul li').removeClass('active');
angular.element(this).parent().addClass('active').parent().parent().addClass('active');
});
};
2016-05-13 18:09:12 +02:00
var setNavbarSelected = function (location) {
angular.element('section.sidebar > ul li').removeClass('active');
angular.element('section.sidebar > ul > li[data-href-match]').each(function (index, element) {
var match = angular.element(element).attr('data-href-match');
2016-05-13 18:09:12 +02:00
2016-05-31 16:51:12 +02:00
if (isUrlMatchUrl2(match, location)) {
2016-05-13 18:09:12 +02:00
angular.element(element).addClass('active');
}
});
angular.element('section.sidebar > ul > li.treeview > ul.treeview-menu > li[data-href-match]').each(function (index, element) {
var match = angular.element(element).attr('data-href-match');
2016-05-13 18:09:12 +02:00
2016-05-31 16:51:12 +02:00
if (isUrlMatchUrl2(match, location)) {
2016-05-13 18:09:12 +02:00
angular.element(element).addClass('active').parent().parent().addClass('active');
}
});
};
var showSidebar = function () {
angular.element('body').removeClass('sidebar-collapse').addClass('sidebar-open');
};
var hideSidebar = function () {
angular.element('body').addClass('sidebar-collapse').removeClass('sidebar-open');
};
var isSidebarShowInSmallScreen = function () {
return angular.element('body').hasClass('sidebar-open');
};
2016-05-30 16:34:15 +02:00
$rootScope.searchContext = {
text: ''
};
$rootScope.taskContext = {
2017-03-19 10:39:38 +01:00
rpcStatus: 'Connecting',
2016-05-30 16:34:15 +02:00
list: [],
selected: {},
2016-05-31 16:51:12 +02:00
enableSelectAll: false,
2016-05-30 16:34:15 +02:00
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;
},
2016-05-30 19:26:41 +02:00
getSelectedTasks: 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);
}
}
return result;
},
2018-11-10 15:25:32 +01:00
isAllSelected: function () {
2016-05-30 16:34:15 +02:00
var isAllSelected = true;
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
2016-05-30 16:34:15 +02:00
if (!this.selected[task.gid]) {
isAllSelected = false;
break;
}
}
2018-11-10 15:25:32 +01:00
return isAllSelected;
},
selectAll: function () {
if (!this.list || !this.selected || this.list.length < 1) {
return;
}
if (!this.enableSelectAll) {
return;
}
var isAllSelected = this.isAllSelected();
2016-05-30 16:34:15 +02:00
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
2016-05-30 16:34:15 +02:00
this.selected[task.gid] = !isAllSelected;
}
}
};
$rootScope.filterTask = function (task) {
if (!task || !angular.isString(task.taskName)) {
return false;
}
if (!$rootScope.searchContext || !$rootScope.searchContext.text) {
return true;
}
return (task.taskName.toLowerCase().indexOf($rootScope.searchContext.text.toLowerCase()) >= 0);
};
$rootScope.swipeActions = {
leftSwipe: function () {
if (isSidebarShowInSmallScreen()) {
hideSidebar();
return;
}
if (!this.extentLeftSwipe ||
(angular.isFunction(this.extentLeftSwipe) && !this.extentLeftSwipe())) {
hideSidebar();
}
},
rightSwipe: function () {
if (!this.extentRightSwipe ||
(angular.isFunction(this.extentRightSwipe) && !this.extentRightSwipe())) {
showSidebar();
}
}
};
ariaNgSettingService.onFirstAccess(function () {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.notifyInPage('', 'Tap to configure and get started with AriaNg.', {
delay: false,
onClose: function () {
$location.path('/settings/ariang');
}
});
});
aria2TaskService.onFirstSuccess(function (event) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.notifyInPage('', '{{name}} is connected', {
type: 'success',
contentParams: {
name: event.rpcName
}
});
});
2018-10-21 09:11:59 +02:00
aria2TaskService.onOperationSuccess(function () {
2017-03-19 10:39:38 +01:00
$rootScope.taskContext.rpcStatus = 'Connected';
});
2018-10-21 09:11:59 +02:00
aria2TaskService.onOperationError(function () {
2018-08-12 15:21:18 +02:00
$rootScope.taskContext.rpcStatus = 'Disconnected';
2017-03-19 10:39:38 +01:00
});
2016-06-28 18:18:17 +02:00
aria2TaskService.onTaskCompleted(function (event) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.notifyTaskComplete(event.task);
2016-06-28 18:18:17 +02:00
});
aria2TaskService.onBtTaskCompleted(function (event) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.notifyBtTaskComplete(event.task);
2016-06-28 18:18:17 +02:00
});
aria2TaskService.onTaskErrorOccur(function (event) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.notifyTaskError(event.task);
2016-06-28 18:18:17 +02:00
});
2016-05-23 15:57:58 +02:00
$rootScope.$on('$locationChangeStart', function (event) {
2018-08-12 10:32:04 +02:00
ariaNgCommonService.closeAllDialogs();
2016-06-01 18:02:10 +02:00
$rootScope.loadPromise = null;
delete $rootScope.swipeActions.extentLeftSwipe;
delete $rootScope.swipeActions.extentRightSwipe;
2016-05-30 16:34:15 +02:00
if (angular.isArray($rootScope.taskContext.list) && $rootScope.taskContext.list.length > 0) {
$rootScope.taskContext.list.length = 0;
}
2016-05-30 19:26:41 +02:00
if (angular.isObject($rootScope.taskContext.selected)) {
$rootScope.taskContext.selected = {};
}
2016-05-30 16:34:15 +02:00
2016-05-31 16:51:12 +02:00
$rootScope.taskContext.enableSelectAll = false;
2016-05-23 15:57:58 +02:00
});
2016-05-13 18:09:12 +02:00
$rootScope.$on('$routeChangeStart', function (event, next, current) {
var location = $location.path();
setNavbarSelected(location);
$document.unbind('keypress');
});
2016-06-29 17:22:42 +02:00
initCheck();
2016-06-29 17:22:42 +02:00
initNavbar();
2016-05-13 18:09:12 +02:00
}]);
2016-08-01 16:49:16 +02:00
}());