init.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. // Replace .no-js class with .js
  2. document.documentElement.className = document.documentElement.className.replace('no-js', 'js');
  3. $(document).ready(function () {
  4. if ($('.body-login')[0]) {
  5. $('input').first().focus();
  6. }
  7. $('.submenu-select-dropdown').each(function () {
  8. $(this).wrap("<span class='submenu-select-wrapper'></span>");
  9. $(this).after("<span class='holder'></span>");
  10. });
  11. $('.submenu-select-dropdown')
  12. .change(function () {
  13. var selectedOption = $(this).find(':selected').text();
  14. $(this).next('.holder').text(selectedOption);
  15. })
  16. .trigger('change');
  17. $('.js-to-top').on('click', function () {
  18. $('html, body').animate({ scrollTop: 0 }, 'normal');
  19. });
  20. $('.button').on('click', function (evt) {
  21. var action = $(this).data('action');
  22. var id = $(this).data('id');
  23. if (action == 'submit' && document.getElementById(id)) {
  24. evt.preventDefault();
  25. $(document.getElementById(id)).submit();
  26. }
  27. });
  28. var isMobile = false; //initiate as false
  29. // device detection
  30. /* eslint-disable no-useless-escape */
  31. if (
  32. /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(
  33. navigator.userAgent
  34. ) ||
  35. /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(
  36. navigator.userAgent.substr(0, 4)
  37. )
  38. ) {
  39. isMobile = true;
  40. $('body').addClass('mobile');
  41. }
  42. /* eslint-enable no-useless-escape */
  43. $(window).scroll(function () {
  44. set_sticky_class();
  45. });
  46. $('.toolbar-right .sort-by').click(function () {
  47. $('.context-menu.sort-order').toggle();
  48. });
  49. // SEARCH BOX
  50. $('.toolbar-search .js-search-input').hover(
  51. function () {
  52. clearTimeout(VE.tmp.search_display_interval);
  53. clearTimeout(VE.tmp.search_hover_interval);
  54. VE.tmp.search_display_interval = setTimeout(function () {
  55. $('.js-search-input').addClass('activated');
  56. }, 150);
  57. },
  58. function () {
  59. clearTimeout(VE.tmp.search_display_interval);
  60. clearTimeout(VE.tmp.search_hover_interval);
  61. VE.tmp.search_hover_interval = setTimeout(function () {
  62. if (!VE.tmp.search_activated && !$('.js-search-input').val().length) {
  63. $('.js-search-input').removeClass('activated');
  64. }
  65. }, 600);
  66. }
  67. );
  68. $('.js-search-input').focus(function () {
  69. VE.tmp.search_activated = 1;
  70. clearTimeout(VE.tmp.search_hover_interval);
  71. });
  72. $('.js-search-input').blur(function () {
  73. VE.tmp.search_activated = 0;
  74. clearTimeout(VE.tmp.search_hover_interval);
  75. VE.tmp.search_hover_interval = setTimeout(function () {
  76. if (!$('.js-search-input').val().length) {
  77. $('.js-search-input').removeClass('activated');
  78. }
  79. }, 600);
  80. });
  81. // TIMER
  82. if ($('.movement.left').length) {
  83. VE.helpers.refresh_timer.right = $('.movement.right');
  84. VE.helpers.refresh_timer.left = $('.movement.left');
  85. VE.helpers.refresh_timer.start();
  86. $('.pause').click(function () {
  87. VE.helpers.refresh_timer.stop();
  88. $('.pause').addClass('u-hidden');
  89. $('.play').removeClass('u-hidden');
  90. $('.refresh-timer').addClass('paused');
  91. });
  92. $('.play').click(function () {
  93. VE.helpers.refresh_timer.start();
  94. $('.pause').removeClass('u-hidden');
  95. $('.play').addClass('u-hidden');
  96. $('.refresh-timer').removeClass('paused');
  97. });
  98. }
  99. // SORTING
  100. $('#vstobjects input, #vstobjects select, #vstobjects textarea').change(function () {
  101. VE.tmp.form_changed = 1;
  102. });
  103. $('.sort-order span').click(function () {
  104. $('.context-menu.sort-order').toggle();
  105. if ($(this).hasClass('active')) return;
  106. $('.sort-order span').removeClass('active');
  107. $(this).addClass('active');
  108. VE.tmp.sort_par = $(this).parent('li').attr('entity');
  109. VE.tmp.sort_as_int = $(this).parent('li').attr('sort_as_int');
  110. VE.tmp.sort_direction = $(this).hasClass('up') * 1 || -1;
  111. $('.toolbar .sort-by b').html($(this).parent('li').find('.name').html());
  112. $('.toolbar .sort-by i').removeClass('fa-arrow-up-a-z fa-arrow-down-a-z');
  113. $(this).hasClass('up')
  114. ? $('.toolbar .sort-by i').addClass('fa-arrow-up-a-z')
  115. : $('.toolbar .sort-by i').addClass('fa-arrow-down-a-z');
  116. $('.units .l-unit')
  117. .sort(function (a, b) {
  118. if (VE.tmp.sort_as_int)
  119. return parseInt($(a).attr(VE.tmp.sort_par)) >= parseInt($(b).attr(VE.tmp.sort_par))
  120. ? VE.tmp.sort_direction
  121. : VE.tmp.sort_direction * -1;
  122. else
  123. return $(a).attr(VE.tmp.sort_par) <= $(b).attr(VE.tmp.sort_par)
  124. ? VE.tmp.sort_direction
  125. : VE.tmp.sort_direction * -1;
  126. })
  127. .appendTo('.l-center.units');
  128. });
  129. $('#objects').submit(function (e) {
  130. if (!e.originalEvent) {
  131. return;
  132. }
  133. e.preventDefault();
  134. $('.ch-toggle').each(function () {
  135. if ($(this).prop('checked')) {
  136. key = this.name;
  137. div = $('<input type="hidden" name="' + key + '" value="' + this.value + '">');
  138. $('#objects').append(div);
  139. }
  140. });
  141. $('#objects').submit();
  142. return false;
  143. });
  144. // Shortcuts
  145. shortcut.add(
  146. 'Ctrl+Enter',
  147. function () {
  148. $('form#vstobjects').submit();
  149. },
  150. {
  151. type: 'keydown',
  152. propagate: false,
  153. disable_in_input: false,
  154. target: document,
  155. }
  156. );
  157. shortcut.add(
  158. 'Ctrl+Backspace',
  159. function () {
  160. var redirect = $('a.button#btn-back').attr('href');
  161. if (VE.tmp.form_changed && redirect) {
  162. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', redirect);
  163. } else if ($('form#vstobjects .button.cancel')[0]) {
  164. location.href = $('form#vstobjects input.cancel')
  165. .attr('onclick')
  166. .replace("location.href='", '')
  167. .replace("'", '');
  168. } else if (redirect) {
  169. location.href = redirect;
  170. }
  171. },
  172. {
  173. type: 'keydown',
  174. propagate: false,
  175. disable_in_input: false,
  176. target: document,
  177. }
  178. );
  179. shortcut.add(
  180. 'f',
  181. function () {
  182. $('.js-search-input').addClass('activated').focus();
  183. },
  184. {
  185. type: 'keydown',
  186. propagate: false,
  187. disable_in_input: true,
  188. target: document,
  189. }
  190. );
  191. $(window).on('keypress', function (evt) {
  192. var tag = evt.target.tagName.toLowerCase();
  193. if (evt.charCode == 97 && tag != 'input' && tag != 'textarea' && tag != 'selectbox') {
  194. evt.preventDefault();
  195. if (!evt.ctrlKey && !evt.shiftKey) {
  196. if ($('.button#btn-create').length) {
  197. location.href = $('.button#btn-create').attr('href');
  198. }
  199. } else {
  200. if ($('.l-unit .ch-toggle:eq(0)').prop('checked')) {
  201. $('.l-unit').removeClass('selected');
  202. $('.l-unit .ch-toggle').prop('checked', false);
  203. } else {
  204. $('.l-unit').addClass('selected');
  205. $('.l-unit .ch-toggle').prop('checked', true);
  206. }
  207. }
  208. }
  209. });
  210. shortcut.add(
  211. '1',
  212. function () {
  213. var target = $('.main-menu .main-menu-item:nth-of-type(1) a');
  214. if (target.length != 1) {
  215. return;
  216. }
  217. if (VE.tmp.form_changed) {
  218. VE.helpers.createConfirmationDialog(
  219. $('.js-confirm-dialog-redirect'),
  220. '',
  221. target.attr('href')
  222. );
  223. } else {
  224. location.href = target.attr('href');
  225. }
  226. },
  227. {
  228. type: 'keydown',
  229. propagate: false,
  230. disable_in_input: true,
  231. target: document,
  232. }
  233. );
  234. shortcut.add(
  235. '2',
  236. function () {
  237. var target = $('.main-menu .main-menu-item:nth-of-type(2) a');
  238. if (target.length != 1) {
  239. return;
  240. }
  241. if (VE.tmp.form_changed) {
  242. VE.helpers.createConfirmationDialog(
  243. $('.js-confirm-dialog-redirect'),
  244. '',
  245. target.attr('href')
  246. );
  247. } else {
  248. location.href = target.attr('href');
  249. }
  250. },
  251. {
  252. type: 'keydown',
  253. propagate: false,
  254. disable_in_input: true,
  255. target: document,
  256. }
  257. );
  258. shortcut.add(
  259. '3',
  260. function () {
  261. var target = $('.main-menu .main-menu-item:nth-of-type(3) a');
  262. if (target.length != 1) {
  263. return;
  264. }
  265. if (VE.tmp.form_changed) {
  266. VE.helpers.createConfirmationDialog(
  267. $('.js-confirm-dialog-redirect'),
  268. '',
  269. target.attr('href')
  270. );
  271. } else {
  272. location.href = target.attr('href');
  273. }
  274. },
  275. {
  276. type: 'keydown',
  277. propagate: false,
  278. disable_in_input: true,
  279. target: document,
  280. }
  281. );
  282. shortcut.add(
  283. '4',
  284. function () {
  285. var target = $('.main-menu .main-menu-item:nth-of-type(4) a');
  286. if (target.length != 1) {
  287. return;
  288. }
  289. if (VE.tmp.form_changed) {
  290. VE.helpers.createConfirmationDialog(
  291. $('.js-confirm-dialog-redirect'),
  292. '',
  293. target.attr('href')
  294. );
  295. } else {
  296. location.href = target.attr('href');
  297. }
  298. },
  299. {
  300. type: 'keydown',
  301. propagate: false,
  302. disable_in_input: true,
  303. target: document,
  304. }
  305. );
  306. shortcut.add(
  307. '5',
  308. function () {
  309. var target = $('.main-menu .main-menu-item:nth-of-type(5) a');
  310. if (target.length != 1) {
  311. return;
  312. }
  313. if (VE.tmp.form_changed) {
  314. VE.helpers.createConfirmationDialog(
  315. $('.js-confirm-dialog-redirect'),
  316. '',
  317. target.attr('href')
  318. );
  319. } else {
  320. location.href = target.attr('href');
  321. }
  322. },
  323. {
  324. type: 'keydown',
  325. propagate: false,
  326. disable_in_input: true,
  327. target: document,
  328. }
  329. );
  330. shortcut.add(
  331. '6',
  332. function () {
  333. var target = $('.main-menu .main-menu-item:nth-of-type(6) a');
  334. if (target.length != 1) {
  335. return;
  336. }
  337. if (VE.tmp.form_changed) {
  338. VE.helpers.createConfirmationDialog(
  339. $('.js-confirm-dialog-redirect'),
  340. '',
  341. target.attr('href')
  342. );
  343. } else {
  344. location.href = target.attr('href');
  345. }
  346. },
  347. {
  348. type: 'keydown',
  349. propagate: false,
  350. disable_in_input: true,
  351. target: document,
  352. }
  353. );
  354. shortcut.add(
  355. '7',
  356. function () {
  357. var target = $('.main-menu .main-menu-item:nth-of-type(7) a');
  358. if (target.length != 1) {
  359. return;
  360. }
  361. if (VE.tmp.form_changed) {
  362. VE.helpers.createConfirmationDialog(
  363. $('.js-confirm-dialog-redirect'),
  364. '',
  365. target.attr('href')
  366. );
  367. } else {
  368. location.href = target.attr('href');
  369. }
  370. },
  371. {
  372. type: 'keydown',
  373. propagate: false,
  374. disable_in_input: true,
  375. target: document,
  376. }
  377. );
  378. shortcut.add(
  379. 'h',
  380. function () {
  381. var shortcutsDialog = document.querySelector('.shortcuts');
  382. if (shortcutsDialog.open) {
  383. shortcutsDialog.close();
  384. } else {
  385. shortcutsDialog.showModal();
  386. }
  387. },
  388. {
  389. type: 'keydown',
  390. propagate: false,
  391. disable_in_input: true,
  392. target: document,
  393. }
  394. );
  395. shortcut.add(
  396. 'Esc',
  397. function () {
  398. var shortcutsDialog = document.querySelector('.shortcuts');
  399. if (shortcutsDialog.open) {
  400. shortcutsDialog.close();
  401. }
  402. $('input, checkbox, textarea, select').blur();
  403. },
  404. {
  405. type: 'keydown',
  406. propagate: false,
  407. disable_in_input: false,
  408. target: document,
  409. }
  410. );
  411. shortcut.add(
  412. 'Left',
  413. function () {
  414. VE.navigation.move_focus_left();
  415. },
  416. {
  417. type: 'keydown',
  418. propagate: false,
  419. disable_in_input: true,
  420. target: document,
  421. }
  422. );
  423. shortcut.add(
  424. 'Right',
  425. function () {
  426. VE.navigation.move_focus_right();
  427. },
  428. {
  429. type: 'keydown',
  430. propagate: false,
  431. disable_in_input: true,
  432. target: document,
  433. }
  434. );
  435. shortcut.add(
  436. 'down',
  437. function () {
  438. VE.navigation.move_focus_down();
  439. },
  440. {
  441. type: 'keydown',
  442. propagate: false,
  443. disable_in_input: true,
  444. target: document,
  445. }
  446. );
  447. shortcut.add(
  448. 'up',
  449. function () {
  450. VE.navigation.move_focus_up();
  451. },
  452. {
  453. type: 'keydown',
  454. propagate: false,
  455. disable_in_input: true,
  456. target: document,
  457. }
  458. );
  459. shortcut.add(
  460. 'l',
  461. function () {
  462. var elm = $('.units.active .l-unit.focus .shortcut-l');
  463. if (elm.length) {
  464. VE.navigation.shortcut(elm);
  465. }
  466. },
  467. {
  468. type: 'keydown',
  469. propagate: false,
  470. disable_in_input: true,
  471. target: document,
  472. }
  473. );
  474. shortcut.add(
  475. 's',
  476. function () {
  477. var elm = $('.units.active .l-unit.focus .shortcut-s');
  478. if (elm.length) {
  479. VE.navigation.shortcut(elm);
  480. }
  481. },
  482. {
  483. type: 'keydown',
  484. propagate: false,
  485. disable_in_input: true,
  486. target: document,
  487. }
  488. );
  489. shortcut.add(
  490. 'w',
  491. function () {
  492. var elm = $('.units.active .l-unit.focus .shortcut-w');
  493. if (elm.length) {
  494. VE.navigation.shortcut(elm);
  495. }
  496. },
  497. {
  498. type: 'keydown',
  499. propagate: false,
  500. disable_in_input: true,
  501. target: document,
  502. }
  503. );
  504. shortcut.add(
  505. 'd',
  506. function () {
  507. var elm = $('.units.active .l-unit.focus .shortcut-d');
  508. if (elm.length) {
  509. VE.navigation.shortcut(elm);
  510. }
  511. },
  512. {
  513. type: 'keydown',
  514. propagate: false,
  515. disable_in_input: true,
  516. target: document,
  517. }
  518. );
  519. shortcut.add(
  520. 'r',
  521. function () {
  522. var elm = $('.units.active .l-unit.focus .shortcut-r');
  523. if (elm.length) {
  524. VE.navigation.shortcut(elm);
  525. }
  526. },
  527. {
  528. type: 'keydown',
  529. propagate: false,
  530. disable_in_input: true,
  531. target: document,
  532. }
  533. );
  534. shortcut.add(
  535. 'n',
  536. function () {
  537. var elm = $('.units.active .l-unit.focus .shortcut-n');
  538. if (elm.length) {
  539. VE.navigation.shortcut(elm);
  540. }
  541. },
  542. {
  543. type: 'keydown',
  544. propagate: false,
  545. disable_in_input: true,
  546. target: document,
  547. }
  548. );
  549. shortcut.add(
  550. 'u',
  551. function () {
  552. var elm = $('.units.active .l-unit.focus .shortcut-u');
  553. if (elm.length) {
  554. VE.navigation.shortcut(elm);
  555. }
  556. },
  557. {
  558. type: 'keydown',
  559. propagate: false,
  560. disable_in_input: true,
  561. target: document,
  562. }
  563. );
  564. shortcut.add(
  565. 'Delete',
  566. function () {
  567. var elm = $('.units.active .l-unit.focus .shortcut-delete');
  568. if (elm.length) {
  569. VE.navigation.shortcut(elm);
  570. }
  571. },
  572. {
  573. type: 'keydown',
  574. propagate: false,
  575. disable_in_input: true,
  576. target: document,
  577. }
  578. );
  579. shortcut.add(
  580. 'Enter',
  581. function (evt) {
  582. if (evt.target.tagName == 'INPUT' && evt.target.form.id == 'vstobjects') {
  583. $('form#vstobjects').submit();
  584. }
  585. if (VE.tmp.form_changed) {
  586. if (!$('.ui-dialog').is(':visible')) {
  587. VE.helpers.createConfirmationDialog(
  588. $('.js-confirm-dialog-redirect')[0],
  589. '',
  590. $(VE.navigation.state.menu_selector + '.focus a').attr('href')
  591. );
  592. } else {
  593. // if dialog is opened - submitting confirm box by "enter" shortcut
  594. $('.ui-dialog button.submit').click();
  595. }
  596. } else {
  597. if (!$('.ui-dialog').is(':visible')) {
  598. var elm = $('.units.active .l-unit.focus .shortcut-enter');
  599. if (elm.length) {
  600. VE.navigation.shortcut(elm);
  601. } else {
  602. VE.navigation.enter_focused();
  603. }
  604. } else {
  605. // if dialog is opened - submitting confirm box by "enter" shortcut
  606. $('.ui-dialog button.submit').click();
  607. }
  608. }
  609. },
  610. {
  611. type: 'keydown',
  612. propagate: true,
  613. disable_in_input: false,
  614. target: document,
  615. }
  616. );
  617. document.querySelector('.shortcuts-close').addEventListener('click', function () {
  618. var shortcutsDialog = document.querySelector('.shortcuts');
  619. if (shortcutsDialog.open) {
  620. shortcutsDialog.close();
  621. }
  622. });
  623. document.querySelector('.js-shortcuts').addEventListener('click', function () {
  624. event.preventDefault();
  625. var shortcutsDialog = document.querySelector('.shortcuts');
  626. if (shortcutsDialog.open) {
  627. shortcutsDialog.close();
  628. } else {
  629. shortcutsDialog.showModal();
  630. }
  631. });
  632. $(document).click(function (evt) {
  633. //close notification popup
  634. if (
  635. !$(evt.target).hasClass('js-notifications') &&
  636. $(evt.target).parents('ul.notification-container').length == 0
  637. ) {
  638. $('.notification-container').addClass('u-hidden');
  639. $('.js-notifications').removeClass('active');
  640. }
  641. });
  642. // focusing on the first input at form
  643. if (location.href.indexOf('lead=') == -1 && !$('.ui-dialog').is(':visible')) {
  644. $('#vstobjects .form-control:not([disabled]), #vstobjects .form-select:not([disabled])')
  645. .first()
  646. .focus();
  647. }
  648. $('.js-notifications').click(function (evt) {
  649. evt.preventDefault();
  650. if (!$('.js-notifications').hasClass('active')) {
  651. VE.notifications.get_list();
  652. $('.js-notifications').addClass('active');
  653. } else {
  654. $('.notification-container').addClass('u-hidden');
  655. $('.js-notifications').removeClass('active');
  656. }
  657. });
  658. $('.button').attr('title', 'ctrl+Enter');
  659. $('.button.cancel').attr('title', 'ctrl+Backspace');
  660. VE.core.register();
  661. if (location.href.search(/list/) != -1) {
  662. var shift_select_ref = $('body').finderSelect({
  663. children: '.l-unit',
  664. onFinish: function (evt) {
  665. if (
  666. $('.l-content').find('.l-unit.selected').length == $('.l-content').find('.l-unit').length
  667. ) {
  668. $('.toggle-all').addClass('clicked-on');
  669. }
  670. },
  671. toggleAllHook: function () {
  672. if ($('.l-unit').length == $('.ch-toggle:checked').length) {
  673. $('.l-unit.selected').removeClass('selected');
  674. $('.ch-toggle').prop('checked', false);
  675. $('#toggle-all').prop('checked', false);
  676. } else {
  677. $('.ch-toggle').prop('checked', true);
  678. $('#toggle-all').prop('checked', true);
  679. }
  680. },
  681. });
  682. $('table').on('mousedown', 'td', function (e) {
  683. if (e.ctrlKey) {
  684. e.preventDefault();
  685. }
  686. });
  687. }
  688. //
  689. $('form#objects').on('submit', function (evt) {
  690. $('.l-unit').find('.ch-toggle').prop('checked', false);
  691. $('.l-unit.selected').find('.ch-toggle').prop('checked', true);
  692. });
  693. // todo: maybe give the save button id?
  694. $('.button[data-id=vstobjects][data-action=submit]').on('click', function (ev) {
  695. let loadingAnimationEle = document.createElement('div');
  696. loadingAnimationEle.className = 'spinner';
  697. loadingAnimationEle.innerHTML =
  698. '<div class="spinner-inner"></div><div class="spinner-mask"></div> <div class="spinner-mask-two"></div>';
  699. // this both gives an indication that we've clicked and is loading, also prevents double-clicking/clicking-on-something-else while loading.
  700. $('.button[data-id=vstobjects][data-action=submit]').replaceWith(loadingAnimationEle);
  701. $('.button').replaceWith('');
  702. // workaround a render bug on Safari (loading icon doesn't render without this)
  703. ev.preventDefault();
  704. $('#vstobjects').submit();
  705. });
  706. });
  707. /**
  708. * generates a random string
  709. * using a cryptographically secure rng,
  710. * and ensuring it contains at least 1 lowercase, 1 uppercase, and 1 number.
  711. *
  712. * @param int length
  713. * @throws Error if length is too small to create a "sufficiently secure" string
  714. * @returns string
  715. */
  716. function randomString2(length = 16) {
  717. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  718. var secure_rng = function (min, max) {
  719. if (min < 0 || min > 0xffff) {
  720. throw new Error(
  721. 'minimum supported number is 0, this generator can only make numbers between 0-65535 inclusive.'
  722. );
  723. }
  724. if (max > 0xffff || max < 0) {
  725. throw new Error(
  726. 'max supported number is 65535, this generator can only make numbers between 0-65535 inclusive.'
  727. );
  728. }
  729. if (min > max) {
  730. throw new Error('dude min>max wtf');
  731. }
  732. // micro-optimization
  733. let randArr = max > 255 ? new Uint16Array(1) : new Uint8Array(1);
  734. let ret;
  735. let attempts = 0;
  736. for (;;) {
  737. crypto.getRandomValues(randArr);
  738. ret = randArr[0];
  739. if (ret >= min && ret <= max) {
  740. return ret;
  741. }
  742. ++attempts;
  743. if (attempts > 1000000) {
  744. // should basically never happen with max 0xFFFF/Uint16Array.
  745. throw new Error('tried a million times, something is wrong');
  746. }
  747. }
  748. };
  749. let attempts = 0;
  750. let minimumStrengthRegex = new RegExp(
  751. /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\d)[a-zA-Z\d]{8,}$/
  752. );
  753. let randmax = chars.length - 1;
  754. for (;;) {
  755. let ret = '';
  756. for (let i = 0; i < length; ++i) {
  757. ret += chars[secure_rng(0, randmax)];
  758. }
  759. if (minimumStrengthRegex.test(ret)) {
  760. return ret;
  761. }
  762. ++attempts;
  763. if (attempts > 1000000) {
  764. throw new Error('tried a million times, something is wrong');
  765. }
  766. }
  767. }