root.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').run(['$window', '$rootScope', '$location', '$document', '$timeout', 'ariaNgCommonService', 'ariaNgKeyboardService', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgLocalizationService', 'ariaNgSettingService', 'aria2TaskService', 'ariaNgNativeElectronService', function ($window, $rootScope, $location, $document, $timeout, ariaNgCommonService, ariaNgKeyboardService, ariaNgNotificationService, ariaNgLogService, ariaNgLocalizationService, ariaNgSettingService, aria2TaskService, ariaNgNativeElectronService) {
  4. var autoRefreshAfterPageLoad = false;
  5. var isAnyTextboxOrTextareaFocus = function () {
  6. return angular.element('input[type="text"],textarea').is(':focus');
  7. };
  8. var isUrlMatchUrl2 = function (url, url2) {
  9. if (url === url2) {
  10. return true;
  11. }
  12. var index = url2.indexOf(url);
  13. if (index !== 0) {
  14. return false;
  15. }
  16. var lastPart = url2.substring(url.length);
  17. if (lastPart.indexOf('/') === 0) {
  18. return true;
  19. }
  20. return false;
  21. };
  22. var setLightTheme = function () {
  23. $rootScope.currentTheme = 'light';
  24. angular.element('body').removeClass('theme-dark');
  25. };
  26. var setDarkTheme = function () {
  27. $rootScope.currentTheme = 'dark';
  28. angular.element('body').addClass('theme-dark');
  29. };
  30. var setThemeBySystemSettings = function () {
  31. if (!ariaNgSettingService.isBrowserSupportDarkMode()) {
  32. setLightTheme();
  33. return;
  34. }
  35. var matchPreferColorScheme = $window.matchMedia('(prefers-color-scheme: dark)');
  36. ariaNgLogService.info('[root.setThemeBySystemSettings] system uses ' + (matchPreferColorScheme.matches ? 'dark' : 'light') + ' theme');
  37. if (matchPreferColorScheme.matches) {
  38. setDarkTheme();
  39. } else {
  40. setLightTheme();
  41. }
  42. };
  43. var initTheme = function () {
  44. if (ariaNgSettingService.getTheme() === 'system') {
  45. setThemeBySystemSettings();
  46. } else if (ariaNgSettingService.getTheme() === 'dark') {
  47. setDarkTheme();
  48. } else {
  49. setLightTheme();
  50. }
  51. };
  52. var initCheck = function () {
  53. var browserFeatures = ariaNgSettingService.getBrowserFeatures();
  54. if (!browserFeatures.localStroage) {
  55. ariaNgLogService.warn('[root.initCheck] LocalStorage is not supported!');
  56. }
  57. if (!browserFeatures.cookies) {
  58. ariaNgLogService.warn('[root.initCheck] Cookies is not supported!');
  59. }
  60. if (!ariaNgSettingService.isBrowserSupportStorage()) {
  61. angular.element('body').prepend('<div class="disable-overlay"></div>');
  62. angular.element('.main-sidebar').addClass('blur');
  63. angular.element('.navbar').addClass('blur');
  64. angular.element('.content-body').addClass('blur');
  65. ariaNgNotificationService.notifyInPage('', 'You cannot use AriaNg because this browser does not meet the minimum requirements for data storage.', {
  66. type: 'error',
  67. delay: false
  68. });
  69. throw new Error('You cannot use AriaNg because this browser does not meet the minimum requirements for data storage.');
  70. }
  71. };
  72. var initNavbar = function () {
  73. angular.element('section.sidebar > ul > li[data-href-match] > a').click(function () {
  74. angular.element('section.sidebar > ul li').removeClass('active');
  75. angular.element(this).parent().addClass('active');
  76. });
  77. angular.element('section.sidebar > ul > li.treeview > ul.treeview-menu > li[data-href-match] > a').click(function () {
  78. angular.element('section.sidebar > ul li').removeClass('active');
  79. angular.element(this).parent().addClass('active').parent().parent().addClass('active');
  80. });
  81. };
  82. var setNavbarSelected = function (location) {
  83. angular.element('section.sidebar > ul li').removeClass('active');
  84. angular.element('section.sidebar > ul > li[data-href-match]').each(function (index, element) {
  85. var match = angular.element(element).attr('data-href-match');
  86. if (isUrlMatchUrl2(match, location)) {
  87. angular.element(element).addClass('active');
  88. }
  89. });
  90. angular.element('section.sidebar > ul > li.treeview > ul.treeview-menu > li[data-href-match]').each(function (index, element) {
  91. var match = angular.element(element).attr('data-href-match');
  92. if (isUrlMatchUrl2(match, location)) {
  93. angular.element(element).addClass('active').parent().parent().addClass('active');
  94. }
  95. });
  96. };
  97. var initContentWrapper = function () {
  98. //copy from AdminLTE app.js
  99. var defaultNavbarWithAppTitleHeight = 74; // defined in "min-height" of ".custom-app-title .main-header .navbar" in app-title.css
  100. var defaultNavbarHeight = 50; // defined in "min-height" of ".main-header .navbar" in AdminLTE.css
  101. var defaultFooterHeight = 1 + 15 + 15 + 17; // defined in "border-top" of ".main-footer" in AdminLTE.css, "padding" of ".main-footer" in AdminLTE.css and "line-height" of ".skin-aria-ng .main-footer > .navbar > .navbar-toolbar > .nav > li > a" in default.css;
  102. var windowHeight = $(window).height();
  103. var headerHeight = $('.main-header').outerHeight() || (ariaNgNativeElectronService.useCustomAppTitle() ? defaultNavbarWithAppTitleHeight : defaultNavbarHeight);
  104. var footerHeight = $('.main-footer').outerHeight() || defaultFooterHeight;
  105. var neg = headerHeight + footerHeight;
  106. $('.content-wrapper').css('min-height', windowHeight - footerHeight);
  107. $('.content-body').css('height', windowHeight - neg);
  108. };
  109. var initFileDragSupport = function () {
  110. var getDropFile = function (e) {
  111. if (!e || !e.dataTransfer) {
  112. return null;
  113. }
  114. if (e.dataTransfer.items && e.dataTransfer.items[0] && e.dataTransfer.items[0].kind === 'file') {
  115. return e.dataTransfer.items[0].getAsFile();
  116. } else if (e.dataTransfer.files && e.dataTransfer.files[0]) {
  117. return e.dataTransfer.files[0];
  118. } else {
  119. return null;
  120. }
  121. };
  122. var getDropText = function (e) {
  123. if (!e || !e.dataTransfer) {
  124. return null;
  125. }
  126. return e.dataTransfer.getData('text');
  127. };
  128. var dropzone = angular.element('#dropzone');
  129. var dropzoneFileZone = angular.element('#dropzone-filezone');
  130. angular.element($window).on('dragenter', function (e) {
  131. dropzone.show();
  132. e.preventDefault();
  133. });
  134. dropzoneFileZone.on('drag dragstart dragend dragover dragenter dragleave drop', function(e) {
  135. e.preventDefault();
  136. e.stopPropagation();
  137. }).on('dragleave dragend drop', function() {
  138. dropzone.hide();
  139. }).on('drop', function(e) {
  140. var file = getDropFile(e.originalEvent);
  141. if (file) {
  142. ariaNgNativeElectronService.notifyMainProcessorNewDropFile({
  143. filePath: file.path,
  144. location: $location.url()
  145. });
  146. return;
  147. }
  148. var text = getDropText(e.originalEvent);
  149. if (text) {
  150. ariaNgNativeElectronService.notifyMainProcessorNewDropText({
  151. text: text,
  152. location: $location.url()
  153. });
  154. }
  155. });
  156. };
  157. var showSidebar = function () {
  158. angular.element('body').removeClass('sidebar-collapse').addClass('sidebar-open');
  159. };
  160. var hideSidebar = function () {
  161. angular.element('body').addClass('sidebar-collapse').removeClass('sidebar-open');
  162. };
  163. var isSidebarShowInSmallScreen = function () {
  164. return angular.element('body').hasClass('sidebar-open');
  165. };
  166. var toggleMaximizeButton = function () {
  167. angular.element('#native-title-maximize-icon').addClass('fa-window-maximize').removeClass('fa-window-restore');
  168. angular.element('#native-title-maximize-btn').attr('title', ariaNgLocalizationService.getLocalizedText('Maximize'));
  169. };
  170. var toggleRestoreButton = function () {
  171. angular.element('#native-title-maximize-icon').addClass('fa-window-restore').removeClass('fa-window-maximize');
  172. angular.element('#native-title-maximize-btn').attr('title', ariaNgLocalizationService.getLocalizedText('Restore Down'));
  173. };
  174. $rootScope.currentTheme = 'light';
  175. $rootScope.searchContext = {
  176. text: '',
  177. setSearchBoxFocused: function () {
  178. angular.element('#search-box').focus();
  179. }
  180. };
  181. $rootScope.taskContext = {
  182. rpcStatus: 'Connecting',
  183. list: [],
  184. selected: {},
  185. enableSelectAll: false,
  186. getSelectedTaskIds: function () {
  187. var result = [];
  188. if (!this.list || !this.selected || this.list.length < 1) {
  189. return result;
  190. }
  191. for (var i = 0; i < this.list.length; i++) {
  192. var task = this.list[i];
  193. if (this.selected[task.gid]) {
  194. result.push(task.gid);
  195. }
  196. }
  197. return result;
  198. },
  199. getSelectedTasks: function () {
  200. var result = [];
  201. if (!this.list || !this.selected || this.list.length < 1) {
  202. return result;
  203. }
  204. for (var i = 0; i < this.list.length; i++) {
  205. var task = this.list[i];
  206. if (this.selected[task.gid]) {
  207. result.push(task);
  208. }
  209. }
  210. return result;
  211. },
  212. isAllSelected: function () {
  213. var isAllSelected = true;
  214. for (var i = 0; i < this.list.length; i++) {
  215. var task = this.list[i];
  216. if (!$rootScope.filterTask(task)) {
  217. continue;
  218. }
  219. if (!this.selected[task.gid]) {
  220. isAllSelected = false;
  221. break;
  222. }
  223. }
  224. return isAllSelected;
  225. },
  226. hasRetryableTask: function () {
  227. for (var i = 0; i < this.list.length; i++) {
  228. var task = this.list[i];
  229. if (!$rootScope.filterTask(task)) {
  230. continue;
  231. }
  232. if ($rootScope.isTaskRetryable(task)) {
  233. return true;
  234. }
  235. }
  236. return false;
  237. },
  238. hasCompletedTask: function () {
  239. for (var i = 0; i < this.list.length; i++) {
  240. var task = this.list[i];
  241. if (!$rootScope.filterTask(task)) {
  242. continue;
  243. }
  244. if (task.status === 'complete') {
  245. return true;
  246. }
  247. }
  248. return false;
  249. },
  250. selectAll: function () {
  251. if (!this.list || !this.selected || this.list.length < 1) {
  252. return;
  253. }
  254. if (!this.enableSelectAll) {
  255. return;
  256. }
  257. var isAllSelected = this.isAllSelected();
  258. for (var i = 0; i < this.list.length; i++) {
  259. var task = this.list[i];
  260. if (!$rootScope.filterTask(task)) {
  261. continue;
  262. }
  263. this.selected[task.gid] = !isAllSelected;
  264. }
  265. },
  266. selectAllFailed: function () {
  267. if (!this.list || !this.selected || this.list.length < 1) {
  268. return;
  269. }
  270. if (!this.enableSelectAll) {
  271. return;
  272. }
  273. var isAllFailedSelected = true;
  274. for (var i = 0; i < this.list.length; i++) {
  275. var task = this.list[i];
  276. if (!$rootScope.filterTask(task)) {
  277. continue;
  278. }
  279. if (!$rootScope.isTaskRetryable(task)) {
  280. continue;
  281. }
  282. if (!this.selected[task.gid]) {
  283. isAllFailedSelected = false;
  284. }
  285. }
  286. for (var i = 0; i < this.list.length; i++) {
  287. var task = this.list[i];
  288. if (!$rootScope.filterTask(task)) {
  289. continue;
  290. }
  291. if (!$rootScope.isTaskRetryable(task)) {
  292. this.selected[task.gid] = false;
  293. continue;
  294. }
  295. this.selected[task.gid] = !isAllFailedSelected;
  296. }
  297. },
  298. selectAllCompleted: function () {
  299. if (!this.list || !this.selected || this.list.length < 1) {
  300. return;
  301. }
  302. if (!this.enableSelectAll) {
  303. return;
  304. }
  305. var isAllFailedSelected = true;
  306. for (var i = 0; i < this.list.length; i++) {
  307. var task = this.list[i];
  308. if (!$rootScope.filterTask(task)) {
  309. continue;
  310. }
  311. if (task.status !== 'complete') {
  312. continue;
  313. }
  314. if (!this.selected[task.gid]) {
  315. isAllFailedSelected = false;
  316. }
  317. }
  318. for (var i = 0; i < this.list.length; i++) {
  319. var task = this.list[i];
  320. if (!$rootScope.filterTask(task)) {
  321. continue;
  322. }
  323. if (task.status !== 'complete') {
  324. this.selected[task.gid] = false;
  325. continue;
  326. }
  327. this.selected[task.gid] = !isAllFailedSelected;
  328. }
  329. }
  330. };
  331. $rootScope.filterTask = function (task) {
  332. if (!task || !angular.isString(task.taskName)) {
  333. return false;
  334. }
  335. if (!$rootScope.searchContext || !$rootScope.searchContext.text) {
  336. return true;
  337. }
  338. return (task.taskName.toLowerCase().indexOf($rootScope.searchContext.text.toLowerCase()) >= 0);
  339. };
  340. $rootScope.isTaskRetryable = function (task) {
  341. return task && task.status === 'error' && task.errorDescription && !task.bittorrent;
  342. };
  343. $rootScope.keydownActions = {
  344. find: function (event) {
  345. if (event.preventDefault) {
  346. event.preventDefault();
  347. }
  348. $rootScope.searchContext.setSearchBoxFocused();
  349. return false;
  350. }
  351. };
  352. $rootScope.swipeActions = {
  353. leftSwipe: function () {
  354. if (!ariaNgSettingService.getSwipeGesture()) {
  355. return;
  356. }
  357. if (isSidebarShowInSmallScreen()) {
  358. hideSidebar();
  359. return;
  360. }
  361. if (!this.extendLeftSwipe ||
  362. (angular.isFunction(this.extendLeftSwipe) && !this.extendLeftSwipe())) {
  363. hideSidebar();
  364. }
  365. },
  366. rightSwipe: function () {
  367. if (!ariaNgSettingService.getSwipeGesture()) {
  368. return;
  369. }
  370. if (!this.extendRightSwipe ||
  371. (angular.isFunction(this.extendRightSwipe) && !this.extendRightSwipe())) {
  372. showSidebar();
  373. }
  374. }
  375. };
  376. $rootScope.refreshPage = function () {
  377. $window.location.reload();
  378. };
  379. $rootScope.setAutoRefreshAfterPageLoad = function () {
  380. autoRefreshAfterPageLoad = true;
  381. };
  382. $rootScope.setTheme = function (theme) {
  383. if (theme === 'system') {
  384. setThemeBySystemSettings();
  385. } else if (theme === 'dark') {
  386. setDarkTheme();
  387. } else {
  388. setLightTheme();
  389. }
  390. };
  391. $rootScope.useCustomAppTitle = ariaNgNativeElectronService.useCustomAppTitle();
  392. ariaNgNativeElectronService.getWindowMaximizedAsync(function (maximized) {
  393. if (maximized) {
  394. toggleRestoreButton();
  395. } else {
  396. toggleMaximizeButton();
  397. }
  398. });
  399. $window.addEventListener('contextmenu', function (event) {
  400. event.preventDefault();
  401. event.stopPropagation();
  402. var context = {};
  403. if (angular.isFunction($window.getSelection)) {
  404. var selection = $window.getSelection().toString();
  405. context.selected = !!selection && selection.length > 0;
  406. }
  407. if (event.target.nodeName.match(/^(input|textarea)$/i) || event.target.isContentEditable) {
  408. ariaNgNativeElectronService.showTextboxContextMenu(context);
  409. }
  410. });
  411. $window.addEventListener('keydown', function (event) {
  412. if (!ariaNgSettingService.getKeyboardShortcuts()) {
  413. return;
  414. }
  415. var isTextboxOrTextareaFocus = isAnyTextboxOrTextareaFocus();
  416. if (ariaNgKeyboardService.isCtrlAPressed(event) && !isTextboxOrTextareaFocus) {
  417. if (angular.isFunction($rootScope.keydownActions.selectAll)) {
  418. return $rootScope.keydownActions.selectAll(event);
  419. }
  420. } else if (ariaNgKeyboardService.isCtrlFPressed(event)) {
  421. if (angular.isFunction($rootScope.keydownActions.find)) {
  422. return $rootScope.keydownActions.find(event);
  423. }
  424. } else if (ariaNgKeyboardService.isDeletePressed(event) && !isTextboxOrTextareaFocus) {
  425. if (angular.isFunction($rootScope.keydownActions.delete)) {
  426. return $rootScope.keydownActions.delete(event);
  427. }
  428. }
  429. }, true);
  430. ariaNgNativeElectronService.onMainWindowMaximize(function () {
  431. toggleRestoreButton();
  432. });
  433. ariaNgNativeElectronService.onMainWindowUnmaximize(function () {
  434. toggleMaximizeButton();
  435. });
  436. ariaNgNativeElectronService.onMainProcessNavigateTo(function (event, routeUrl) {
  437. $location.path(routeUrl);
  438. });
  439. ariaNgNativeElectronService.onMainProcessShowError(function (event, message) {
  440. ariaNgCommonService.showError(message);
  441. });
  442. ariaNgSettingService.setDebugMode(ariaNgNativeElectronService.isDevMode());
  443. ariaNgSettingService.onFirstAccess(function () {
  444. ariaNgNotificationService.notifyInPage('', 'Tap to configure and get started with AriaNg.', {
  445. delay: false,
  446. onClose: function () {
  447. $location.path('/settings/ariang');
  448. }
  449. });
  450. });
  451. aria2TaskService.onFirstSuccess(function (event) {
  452. ariaNgNotificationService.notifyInPage('', 'is connected', {
  453. type: 'success',
  454. contentPrefix: event.rpcName + ' '
  455. });
  456. });
  457. aria2TaskService.onConnectionSuccess(function () {
  458. $timeout(function () {
  459. if ($rootScope.taskContext.rpcStatus !== 'Connected') {
  460. $rootScope.taskContext.rpcStatus = 'Connected';
  461. }
  462. });
  463. });
  464. aria2TaskService.onConnectionFailed(function () {
  465. $timeout(function () {
  466. if ($rootScope.taskContext.rpcStatus !== 'Disconnected') {
  467. $rootScope.taskContext.rpcStatus = 'Disconnected';
  468. }
  469. });
  470. });
  471. aria2TaskService.onConnectionReconnecting(function () {
  472. $timeout(function () {
  473. if ($rootScope.taskContext.rpcStatus !== 'Reconnecting') {
  474. $rootScope.taskContext.rpcStatus = 'Reconnecting';
  475. }
  476. });
  477. });
  478. aria2TaskService.onConnectionWaitingToReconnect(function () {
  479. $timeout(function () {
  480. if ($rootScope.taskContext.rpcStatus !== 'Waiting to reconnect') {
  481. $rootScope.taskContext.rpcStatus = 'Waiting to reconnect';
  482. }
  483. });
  484. });
  485. aria2TaskService.onTaskCompleted(function (event) {
  486. ariaNgNotificationService.notifyTaskComplete(event.task);
  487. });
  488. aria2TaskService.onBtTaskCompleted(function (event) {
  489. ariaNgNotificationService.notifyBtTaskComplete(event.task);
  490. });
  491. aria2TaskService.onTaskErrorOccur(function (event) {
  492. ariaNgNotificationService.notifyTaskError(event.task);
  493. });
  494. $rootScope.$on('$locationChangeStart', function (event) {
  495. ariaNgCommonService.closeAllDialogs();
  496. $rootScope.loadPromise = null;
  497. delete $rootScope.keydownActions.selectAll;
  498. delete $rootScope.keydownActions.delete;
  499. delete $rootScope.swipeActions.extendLeftSwipe;
  500. delete $rootScope.swipeActions.extendRightSwipe;
  501. if (angular.isArray($rootScope.taskContext.list) && $rootScope.taskContext.list.length > 0) {
  502. $rootScope.taskContext.list.length = 0;
  503. }
  504. if (angular.isObject($rootScope.taskContext.selected)) {
  505. $rootScope.taskContext.selected = {};
  506. }
  507. $rootScope.taskContext.enableSelectAll = false;
  508. });
  509. $rootScope.$on('$routeChangeStart', function (event, next, current) {
  510. var location = $location.path();
  511. setNavbarSelected(location);
  512. $document.unbind('keypress');
  513. });
  514. $rootScope.$on('$viewContentLoaded', function () {
  515. ariaNgNativeElectronService.notifyMainProcessViewLoaded($location.path());
  516. });
  517. $rootScope.$on('$translateChangeSuccess', function(event, current, previous) {
  518. ariaNgNativeElectronService.setMainWindowLanguage();
  519. });
  520. if (ariaNgSettingService.isBrowserSupportDarkMode()) {
  521. var matchPreferColorScheme = $window.matchMedia('(prefers-color-scheme: dark)');
  522. matchPreferColorScheme.addEventListener('change', function (e) {
  523. ariaNgLogService.info('[root] system switches to ' + (e.matches ? 'dark' : 'light') + ' theme');
  524. if (ariaNgSettingService.getTheme() === 'system') {
  525. if (e.matches) {
  526. setDarkTheme();
  527. } else {
  528. setLightTheme();
  529. }
  530. }
  531. });
  532. }
  533. $rootScope.$on('$locationChangeSuccess', function (event, newUrl) {
  534. if (autoRefreshAfterPageLoad) {
  535. $window.location.reload();
  536. }
  537. });
  538. initTheme();
  539. initCheck();
  540. initNavbar();
  541. initContentWrapper();
  542. initFileDragSupport();
  543. }]);
  544. }());