init.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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").change(function(){
  12. var selectedOption = $(this).find(":selected").text();
  13. $(this).next(".holder").text(selectedOption);
  14. }).trigger('change');
  15. $('.js-to-top').on('click', function() {
  16. $("html, body").animate({ scrollTop: 0 }, "normal");
  17. });
  18. $('.button').on('click',function(evt){
  19. var action = $(this).data('action');
  20. var id = $(this).data('id');
  21. if(action=='submit' && document.getElementById(id)){
  22. evt.preventDefault();
  23. $(document.getElementById(id)).submit();
  24. }
  25. });
  26. var isMobile = false; //initiate as false
  27. // device detection
  28. /* eslint-disable no-useless-escape */
  29. if(/(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(navigator.userAgent)
  30. || /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(navigator.userAgent.substr(0,4))){
  31. isMobile = true;
  32. $('body').addClass('mobile');
  33. }
  34. /* eslint-enable no-useless-escape */
  35. $(window).scroll(function(){set_sticky_class()});
  36. $('.toolbar-right .sort-by').click(function(){
  37. $('.context-menu.sort-order').toggle();
  38. });
  39. // SEARCH BOX
  40. $('.toolbar-search .js-search-input').hover(function(){
  41. clearTimeout(VE.tmp.search_display_interval);
  42. clearTimeout(VE.tmp.search_hover_interval);
  43. VE.tmp.search_display_interval = setTimeout(function(){$('.js-search-input').addClass('activated');}, 150);
  44. }, function(){
  45. clearTimeout(VE.tmp.search_display_interval);
  46. clearTimeout(VE.tmp.search_hover_interval);
  47. VE.tmp.search_hover_interval = setTimeout(function(){
  48. if(!VE.tmp.search_activated && !$(".js-search-input").val().length){
  49. $(".js-search-input").removeClass('activated');
  50. }
  51. }, 600);
  52. });
  53. $('.js-search-input').focus(function(){
  54. VE.tmp.search_activated = 1;
  55. clearTimeout(VE.tmp.search_hover_interval);
  56. });
  57. $('.js-search-input').blur(function(){
  58. VE.tmp.search_activated = 0;
  59. clearTimeout(VE.tmp.search_hover_interval);
  60. VE.tmp.search_hover_interval = setTimeout(function(){
  61. if(!$(".js-search-input").val().length){
  62. $(".js-search-input").removeClass('activated');
  63. }
  64. }, 600);
  65. });
  66. // TIMER
  67. if($('.movement.left').length){
  68. VE.helpers.refresh_timer.right = $('.movement.right');
  69. VE.helpers.refresh_timer.left = $('.movement.left');
  70. VE.helpers.refresh_timer.start();
  71. $('.pause').click(function(){
  72. VE.helpers.refresh_timer.stop();
  73. $('.pause').addClass('u-hidden');
  74. $('.play').removeClass('u-hidden');
  75. $('.refresh-timer').addClass('paused');
  76. });
  77. $('.play').click(function(){
  78. VE.helpers.refresh_timer.start();
  79. $('.pause').removeClass('u-hidden');
  80. $('.play').addClass('u-hidden');
  81. $('.refresh-timer').removeClass('paused');
  82. });
  83. }
  84. // SORTING
  85. $('#vstobjects input, #vstobjects select, #vstobjects textarea').change(function(){VE.tmp.form_changed=1});
  86. $('.sort-order span').click(function(){
  87. $('.context-menu.sort-order').toggle();
  88. if($(this).hasClass('active'))
  89. return;
  90. $('.sort-order span').removeClass('active');
  91. $(this).addClass('active');
  92. VE.tmp.sort_par = $(this).parent('li').attr('entity');
  93. VE.tmp.sort_as_int = $(this).parent('li').attr('sort_as_int');
  94. VE.tmp.sort_direction = $(this).hasClass('up')*1 || -1;
  95. $('.toolbar .sort-by b').html($(this).parent('li').find('.name').html());
  96. $('.toolbar .sort-by i').removeClass('fa-arrow-up-a-z fa-arrow-down-a-z');
  97. $(this).hasClass('up') ? $('.toolbar .sort-by i').addClass('fa-arrow-up-a-z') : $('.toolbar .sort-by i').addClass('fa-arrow-down-a-z');
  98. $('.units .l-unit').sort(function (a, b) {
  99. if(VE.tmp.sort_as_int)
  100. return parseInt($(a).attr(VE.tmp.sort_par)) >= parseInt($(b).attr(VE.tmp.sort_par)) ? VE.tmp.sort_direction : VE.tmp.sort_direction * -1;
  101. else
  102. return $(a).attr(VE.tmp.sort_par) <= $(b).attr(VE.tmp.sort_par) ? VE.tmp.sort_direction : VE.tmp.sort_direction * -1;
  103. }).appendTo(".l-center.units");
  104. });
  105. $('#objects').submit( function (e){
  106. if(!e.originalEvent){
  107. return;
  108. }
  109. e.preventDefault();
  110. $('.ch-toggle').each( function (){
  111. if($(this).prop('checked')){
  112. key=this.name;
  113. div=$('<input type="hidden" name="'+key+'" value="'+this.value+'">');
  114. $('#objects').append(div);
  115. }
  116. });
  117. $('#objects').submit();
  118. return false;
  119. });
  120. // Shortcuts
  121. shortcut.add("Ctrl+Enter", function(){
  122. $('form#vstobjects').submit();
  123. }, {
  124. 'type': 'keydown',
  125. 'propagate': false,
  126. 'disable_in_input': false,
  127. 'target': document
  128. }
  129. );
  130. shortcut.add("Ctrl+Backspace", function(){
  131. var redirect = $('a.button#btn-back').attr('href')
  132. if(VE.tmp.form_changed && redirect){
  133. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', redirect);
  134. } else if($('form#vstobjects .button.cancel')[0]){
  135. location.href=$('form#vstobjects input.cancel').attr('onclick').replace("location.href='", "").replace("'","");
  136. } else if(redirect){
  137. location.href = redirect;
  138. }
  139. }, {
  140. 'type': 'keydown',
  141. 'propagate': false,
  142. 'disable_in_input': false,
  143. 'target': document
  144. }
  145. );
  146. shortcut.add("f", function(){
  147. $('.js-search-input').addClass('activated').focus();
  148. }, {
  149. 'type': 'keydown',
  150. 'propagate': false,
  151. 'disable_in_input': true,
  152. 'target': document
  153. }
  154. );
  155. $(window).on('keypress', function(evt) {
  156. var tag = evt.target.tagName.toLowerCase();
  157. if (evt.charCode == 97 && tag != 'input' && tag != 'textarea' && tag != 'selectbox') {
  158. evt.preventDefault();
  159. if (!evt.ctrlKey && !evt.shiftKey) {
  160. if ($('.button#btn-create').length) {
  161. location.href=$('.button#btn-create').attr('href');
  162. }
  163. }
  164. else {
  165. if ($('.l-unit .ch-toggle:eq(0)').prop('checked')) {
  166. $('.l-unit').removeClass('selected');
  167. $('.l-unit .ch-toggle').prop('checked', false);
  168. }
  169. else {
  170. $('.l-unit').addClass('selected');
  171. $('.l-unit .ch-toggle').prop('checked', true);
  172. }
  173. }
  174. }
  175. });
  176. shortcut.add("1", function(){
  177. var target = $('.main-menu .main-menu-item:nth-of-type(1) a')
  178. if(target.length != 1){
  179. return;
  180. }
  181. if(VE.tmp.form_changed){
  182. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  183. } else {
  184. location.href=target.attr('href');
  185. }
  186. }, {
  187. 'type': 'keydown',
  188. 'propagate': false,
  189. 'disable_in_input': true,
  190. 'target': document
  191. }
  192. );
  193. shortcut.add("2", function(){
  194. var target = $('.main-menu .main-menu-item:nth-of-type(2) a')
  195. if(target.length != 1){
  196. return;
  197. }
  198. if(VE.tmp.form_changed){
  199. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  200. } else {
  201. location.href=target.attr('href');
  202. }
  203. }, {
  204. 'type': 'keydown',
  205. 'propagate': false,
  206. 'disable_in_input': true,
  207. 'target': document
  208. }
  209. );
  210. shortcut.add("3", function(){
  211. var target = $('.main-menu .main-menu-item:nth-of-type(3) a')
  212. if(target.length != 1){
  213. return;
  214. }
  215. if(VE.tmp.form_changed){
  216. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  217. } else {
  218. location.href=target.attr('href');
  219. }
  220. }, {
  221. 'type': 'keydown',
  222. 'propagate': false,
  223. 'disable_in_input': true,
  224. 'target': document
  225. }
  226. );
  227. shortcut.add("4", function(){
  228. var target = $('.main-menu .main-menu-item:nth-of-type(4) a')
  229. if(target.length != 1){
  230. return;
  231. }
  232. if(VE.tmp.form_changed){
  233. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  234. } else {
  235. location.href=target.attr('href');
  236. }
  237. }, {
  238. 'type': 'keydown',
  239. 'propagate': false,
  240. 'disable_in_input': true,
  241. 'target': document
  242. }
  243. );
  244. shortcut.add("5", function(){
  245. var target = $('.main-menu .main-menu-item:nth-of-type(5) a')
  246. if(target.length != 1){
  247. return;
  248. }
  249. if(VE.tmp.form_changed){
  250. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  251. } else {
  252. location.href=target.attr('href');
  253. }
  254. }, {
  255. 'type': 'keydown',
  256. 'propagate': false,
  257. 'disable_in_input': true,
  258. 'target': document
  259. }
  260. );
  261. shortcut.add("6", function(){
  262. var target = $('.main-menu .main-menu-item:nth-of-type(6) a')
  263. if(target.length != 1){
  264. return;
  265. }
  266. if(VE.tmp.form_changed){
  267. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  268. } else {
  269. location.href=target.attr('href');
  270. }
  271. }, {
  272. 'type': 'keydown',
  273. 'propagate': false,
  274. 'disable_in_input': true,
  275. 'target': document
  276. }
  277. );
  278. shortcut.add("7", function(){
  279. var target = $('.main-menu .main-menu-item:nth-of-type(7) a')
  280. if(target.length != 1){
  281. return;
  282. }
  283. if(VE.tmp.form_changed){
  284. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect'), '', target.attr('href'));
  285. } else {
  286. location.href=target.attr('href');
  287. }
  288. }, {
  289. 'type': 'keydown',
  290. 'propagate': false,
  291. 'disable_in_input': true,
  292. 'target': document
  293. }
  294. );
  295. shortcut.add("h", function(){
  296. var shortcutsDialog = document.querySelector('.shortcuts');
  297. if (shortcutsDialog.open) {
  298. shortcutsDialog.close();
  299. } else {
  300. shortcutsDialog.showModal();
  301. }
  302. }, {
  303. 'type': 'keydown',
  304. 'propagate': false,
  305. 'disable_in_input': true,
  306. 'target': document
  307. }
  308. );
  309. shortcut.add("Esc", function(){
  310. var shortcutsDialog = document.querySelector('.shortcuts');
  311. if (shortcutsDialog.open) {
  312. shortcutsDialog.close();
  313. }
  314. $('input, checkbox, textarea, select').blur();
  315. }, {
  316. 'type': 'keydown',
  317. 'propagate': false,
  318. 'disable_in_input': false,
  319. 'target': document
  320. }
  321. );
  322. shortcut.add("Left", function(){
  323. VE.navigation.move_focus_left();
  324. }, {
  325. 'type': 'keydown',
  326. 'propagate': false,
  327. 'disable_in_input': true,
  328. 'target': document
  329. }
  330. );
  331. shortcut.add("Right", function(){
  332. VE.navigation.move_focus_right();
  333. }, {
  334. 'type': 'keydown',
  335. 'propagate': false,
  336. 'disable_in_input': true,
  337. 'target': document
  338. }
  339. );
  340. shortcut.add("down", function(){
  341. VE.navigation.move_focus_down();
  342. }, {
  343. 'type': 'keydown',
  344. 'propagate': false,
  345. 'disable_in_input': true,
  346. 'target': document
  347. }
  348. );
  349. shortcut.add("up", function(){
  350. VE.navigation.move_focus_up();
  351. }, {
  352. 'type': 'keydown',
  353. 'propagate': false,
  354. 'disable_in_input': true,
  355. 'target': document
  356. }
  357. );
  358. shortcut.add("l", function(){
  359. var elm = $('.units.active .l-unit.focus .shortcut-l');
  360. if(elm.length){
  361. VE.navigation.shortcut(elm);
  362. }
  363. }, {
  364. 'type': 'keydown',
  365. 'propagate': false,
  366. 'disable_in_input': true,
  367. 'target': document
  368. }
  369. );
  370. shortcut.add("s", function(){
  371. var elm = $('.units.active .l-unit.focus .shortcut-s');
  372. if(elm.length){
  373. VE.navigation.shortcut(elm);
  374. }
  375. }, {
  376. 'type': 'keydown',
  377. 'propagate': false,
  378. 'disable_in_input': true,
  379. 'target': document
  380. }
  381. );
  382. shortcut.add("w", function(){
  383. var elm = $('.units.active .l-unit.focus .shortcut-w');
  384. if(elm.length){
  385. VE.navigation.shortcut(elm);
  386. }
  387. }, {
  388. 'type': 'keydown',
  389. 'propagate': false,
  390. 'disable_in_input': true,
  391. 'target': document
  392. }
  393. );
  394. shortcut.add("d", function(){
  395. var elm = $('.units.active .l-unit.focus .shortcut-d');
  396. if(elm.length){
  397. VE.navigation.shortcut(elm);
  398. }
  399. }, {
  400. 'type': 'keydown',
  401. 'propagate': false,
  402. 'disable_in_input': true,
  403. 'target': document
  404. }
  405. );
  406. shortcut.add("r", function(){
  407. var elm = $('.units.active .l-unit.focus .shortcut-r');
  408. if(elm.length){
  409. VE.navigation.shortcut(elm);
  410. }
  411. }, {
  412. 'type': 'keydown',
  413. 'propagate': false,
  414. 'disable_in_input': true,
  415. 'target': document
  416. }
  417. );
  418. shortcut.add("n", function(){
  419. var elm = $('.units.active .l-unit.focus .shortcut-n');
  420. if(elm.length){
  421. VE.navigation.shortcut(elm);
  422. }
  423. }, {
  424. 'type': 'keydown',
  425. 'propagate': false,
  426. 'disable_in_input': true,
  427. 'target': document
  428. }
  429. );
  430. shortcut.add("u", function(){
  431. var elm = $('.units.active .l-unit.focus .shortcut-u');
  432. if(elm.length){
  433. VE.navigation.shortcut(elm);
  434. }
  435. }, {
  436. 'type': 'keydown',
  437. 'propagate': false,
  438. 'disable_in_input': true,
  439. 'target': document
  440. }
  441. );
  442. shortcut.add("Delete", function(){
  443. var elm = $('.units.active .l-unit.focus .shortcut-delete');
  444. if(elm.length){
  445. VE.navigation.shortcut(elm);
  446. }
  447. }, {
  448. 'type': 'keydown',
  449. 'propagate': false,
  450. 'disable_in_input': true,
  451. 'target': document
  452. }
  453. );
  454. shortcut.add("Enter", function(evt){
  455. if (evt.target.tagName == 'INPUT' && evt.target.form.id == 'vstobjects'){
  456. $('form#vstobjects').submit();
  457. }
  458. if(VE.tmp.form_changed){
  459. if(!$('.ui-dialog').is(':visible')){
  460. VE.helpers.createConfirmationDialog($('.js-confirm-dialog-redirect')[0], '', $(VE.navigation.state.menu_selector + '.focus a').attr('href'));
  461. } else { // if dialog is opened - submitting confirm box by "enter" shortcut
  462. $('.ui-dialog button.submit').click();
  463. }
  464. } else {
  465. if(!$('.ui-dialog').is(':visible')){
  466. var elm = $('.units.active .l-unit.focus .shortcut-enter');
  467. if(elm.length){
  468. VE.navigation.shortcut(elm);
  469. } else {
  470. VE.navigation.enter_focused();
  471. }
  472. } else { // if dialog is opened - submitting confirm box by "enter" shortcut
  473. $('.ui-dialog button.submit').click();
  474. }
  475. }
  476. }, {
  477. 'type': 'keydown',
  478. 'propagate': true,
  479. 'disable_in_input': false,
  480. 'target': document
  481. }
  482. );
  483. document.querySelector('.shortcuts-close').addEventListener('click', function(){
  484. var shortcutsDialog = document.querySelector('.shortcuts');
  485. if (shortcutsDialog.open) {
  486. shortcutsDialog.close();
  487. }
  488. });
  489. document.querySelector('.js-shortcuts').addEventListener('click', function() {
  490. event.preventDefault();
  491. var shortcutsDialog = document.querySelector('.shortcuts');
  492. if (shortcutsDialog.open) {
  493. shortcutsDialog.close();
  494. } else {
  495. shortcutsDialog.showModal();
  496. }
  497. });
  498. $(document).click(function(evt){
  499. //close notification popup
  500. if(!$(evt.target).hasClass('js-notifications') && $(evt.target).parents('ul.notification-container').length == 0){
  501. $('.notification-container').addClass('u-hidden');
  502. $('.js-notifications').removeClass('active');
  503. }
  504. });
  505. // focusing on the first input at form
  506. if( location.href.indexOf('lead=') == -1 && !$('.ui-dialog').is(':visible') ){
  507. $('#vstobjects .form-control:not([disabled]), #vstobjects .form-select:not([disabled])').first().focus();
  508. }
  509. $('.js-notifications').click(function(evt){
  510. evt.preventDefault();
  511. if(!$('.js-notifications').hasClass('active')){
  512. VE.notifications.get_list();
  513. $('.js-notifications').addClass('active');
  514. } else {
  515. $('.notification-container').addClass('u-hidden');
  516. $('.js-notifications').removeClass('active');
  517. }
  518. });
  519. $('.button').attr('title','ctrl+Enter');
  520. $('.button.cancel').attr('title','ctrl+Backspace');
  521. VE.core.register();
  522. if (location.href.search(/list/) != -1) {
  523. var shift_select_ref = $('body').finderSelect({
  524. children: '.l-unit',
  525. 'onFinish': function(evt) {
  526. if ($('.l-content').find('.l-unit.selected').length == $('.l-content').find('.l-unit').length) {
  527. $('.toggle-all').addClass('clicked-on');
  528. }
  529. },
  530. 'toggleAllHook': function() {
  531. if ($('.l-unit').length == $('.ch-toggle:checked').length) {
  532. $('.l-unit.selected').removeClass('selected');
  533. $('.ch-toggle').prop('checked', false);
  534. $('#toggle-all').prop('checked', false);
  535. }
  536. else {
  537. $('.ch-toggle').prop('checked', true);
  538. $('#toggle-all').prop('checked', true);
  539. }
  540. }
  541. });
  542. $('table').on('mousedown', 'td', function(e) {
  543. if (e.ctrlKey) {
  544. e.preventDefault();
  545. }
  546. });
  547. }
  548. //
  549. $('form#objects').on('submit', function(evt) {
  550. $('.l-unit').find('.ch-toggle').prop('checked', false);
  551. $('.l-unit.selected').find('.ch-toggle').prop('checked', true);
  552. });
  553. // todo: maybe give the save button id?
  554. $(".button[data-id=vstobjects][data-action=submit]").on('click', function(ev){
  555. let loadingAnimationEle = document.createElement("div");
  556. loadingAnimationEle.className = "spinner";
  557. loadingAnimationEle.innerHTML = '<div class="spinner-inner"></div><div class="spinner-mask"></div> <div class="spinner-mask-two"></div>';
  558. // this both gives an indication that we've clicked and is loading, also prevents double-clicking/clicking-on-something-else while loading.
  559. $(".button[data-id=vstobjects][data-action=submit]").replaceWith(loadingAnimationEle);
  560. $(".button").replaceWith('');
  561. // workaround a render bug on Safari (loading icon doesn't render without this)
  562. ev.preventDefault();
  563. $('#vstobjects').submit();
  564. });
  565. });
  566. /**
  567. * generates a random string
  568. * using a cryptographically secure rng,
  569. * and ensuring it contains at least 1 lowercase, 1 uppercase, and 1 number.
  570. *
  571. * @param int length
  572. * @throws Error if length is too small to create a "sufficiently secure" string
  573. * @returns string
  574. */
  575. function randomString2(length = 16) {
  576. var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  577. var secure_rng = function (min, max) {
  578. if (min < 0 || min > 0xffff) {
  579. throw new Error(
  580. "minimum supported number is 0, this generator can only make numbers between 0-65535 inclusive."
  581. );
  582. }
  583. if (max > 0xffff || max < 0) {
  584. throw new Error(
  585. "max supported number is 65535, this generator can only make numbers between 0-65535 inclusive."
  586. );
  587. }
  588. if (min > max) {
  589. throw new Error("dude min>max wtf");
  590. }
  591. // micro-optimization
  592. let randArr = max > 255 ? new Uint16Array(1) : new Uint8Array(1);
  593. let ret;
  594. let attempts = 0;
  595. for (;;) {
  596. crypto.getRandomValues(randArr);
  597. ret = randArr[0];
  598. if (ret >= min && ret <= max) {
  599. return ret;
  600. }
  601. ++attempts;
  602. if (attempts > 1000000) {
  603. // should basically never happen with max 0xFFFF/Uint16Array.
  604. throw new Error("tried a million times, something is wrong");
  605. }
  606. }
  607. };
  608. let attempts = 0;
  609. let minimumStrengthRegex = new RegExp(
  610. /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\d)[a-zA-Z\d]{8,}$/
  611. );
  612. let randmax = chars.length - 1;
  613. for (;;) {
  614. let ret = "";
  615. for (let i = 0; i < length; ++i) {
  616. ret += chars[secure_rng(0, randmax)];
  617. }
  618. if (minimumStrengthRegex.test(ret)) {
  619. return ret;
  620. }
  621. ++attempts;
  622. if (attempts > 1000000) {
  623. throw new Error("tried a million times, something is wrong");
  624. }
  625. }
  626. }