This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues/pull-requests.
AriaNg/src/scripts/core/root.js

471 lines
16 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
angular.module('ariaNg').run(['$window', '$rootScope', '$location', '$document', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', function ($window, $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
2021-02-10 18:05:51 +01:00
var setLightTheme = function () {
$rootScope.currentTheme = 'light';
angular.element('body').removeClass('theme-dark');
};
var setDarkTheme = function () {
$rootScope.currentTheme = 'dark';
angular.element('body').addClass('theme-dark');
};
var setThemeBySystemSettings = function () {
if (!ariaNgSettingService.isBrowserSupportMatchMedia()) {
setLightTheme();
return;
}
var matchPreferColorScheme = $window.matchMedia('(prefers-color-scheme: dark)');
ariaNgLogService.info(matchPreferColorScheme)
if (matchPreferColorScheme.matches) {
setDarkTheme();
} else {
setLightTheme();
}
};
var initTheme = function () {
if (ariaNgSettingService.getTheme() === 'system') {
setThemeBySystemSettings();
} else if (ariaNgSettingService.getTheme() === 'dark') {
setDarkTheme();
} else {
setLightTheme();
}
};
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');
2019-08-02 19:44:59 +02:00
ariaNgLocalizationService.notifyInPage('', 'You cannot use AriaNg because this browser does not meet the minimum requirements for data storage.', {
type: 'error',
delay: false
});
2019-08-02 19:44:59 +02:00
throw new Error('You cannot use AriaNg because this browser does not meet the minimum requirements for 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');
};
2021-02-10 18:05:51 +01:00
$rootScope.currentTheme = 'light';
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;
},
2019-04-07 08:23:56 +02:00
hasRetryableTask: function () {
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
if ($rootScope.isTaskRetryable(task)) {
return true;
}
}
return false;
},
2019-04-22 18:15:17 +02:00
hasCompletedTask: function () {
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
if (task.status === 'complete') {
return true;
}
}
return false;
},
2018-11-10 15:25:32 +01:00
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;
}
2019-04-07 08:23:56 +02:00
},
selectAllFailed: function () {
if (!this.list || !this.selected || this.list.length < 1) {
return;
}
if (!this.enableSelectAll) {
return;
}
var isAllFailedSelected = true;
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
if (!$rootScope.isTaskRetryable(task)) {
continue;
}
if (!this.selected[task.gid]) {
isAllFailedSelected = false;
}
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
if (!$rootScope.isTaskRetryable(task)) {
this.selected[task.gid] = false;
continue;
}
2019-04-22 18:15:17 +02:00
this.selected[task.gid] = !isAllFailedSelected;
}
},
selectAllCompleted: function () {
if (!this.list || !this.selected || this.list.length < 1) {
return;
}
if (!this.enableSelectAll) {
return;
}
var isAllFailedSelected = true;
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
if (task.status !== 'complete') {
continue;
}
if (!this.selected[task.gid]) {
isAllFailedSelected = false;
}
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!$rootScope.filterTask(task)) {
continue;
}
if (task.status !== 'complete') {
this.selected[task.gid] = false;
continue;
}
2019-04-07 08:23:56 +02:00
this.selected[task.gid] = !isAllFailedSelected;
}
2016-05-30 16:34:15 +02:00
}
};
$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);
};
2019-04-07 08:23:56 +02:00
$rootScope.isTaskRetryable = function (task) {
return task && task.status === 'error' && task.errorDescription && !task.bittorrent;
};
$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();
}
}
};
$rootScope.refreshPage = function () {
$window.location.reload();
};
2021-02-10 18:05:51 +01:00
$rootScope.setTheme = function (theme) {
if (theme === 'system') {
setThemeBySystemSettings();
} else if (theme === 'dark') {
setDarkTheme();
} else {
setLightTheme();
}
};
ariaNgSettingService.onApplicationCacheUpdated(function () {
ariaNgLocalizationService.notifyInPage('', 'Application cache has been updated, please reload the page for the changes to take effect.', {
delay: false,
type: 'info',
templateUrl: 'views/notification-reloadable.html'
});
});
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) {
ariaNgLocalizationService.notifyInPage('', 'is connected', {
type: 'success',
contentPrefix: event.rpcName + ' '
});
});
aria2TaskService.onConnectionSuccess(function () {
if ($rootScope.taskContext.rpcStatus !== 'Connected') {
$rootScope.taskContext.rpcStatus = 'Connected';
}
2017-03-19 10:39:38 +01:00
});
aria2TaskService.onConnectionFailed(function () {
if ($rootScope.taskContext.rpcStatus !== 'Disconnected') {
$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
2021-02-10 18:05:51 +01:00
if (ariaNgSettingService.isBrowserSupportMatchMedia()) {
var matchPreferColorScheme = $window.matchMedia('(prefers-color-scheme: dark)');
matchPreferColorScheme.addEventListener('change', function (e) {
if (ariaNgSettingService.getTheme() === 'system') {
if (e.matches) {
setDarkTheme();
} else {
setLightTheme();
}
}
});
}
initTheme();
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
}());