events.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Init kinda namespace object
  2. var VE = {
  3. // Hestia Events object
  4. core: {}, // core functions
  5. navigation: {
  6. state: {
  7. active_menu: 1,
  8. menu_selector: '.main-menu-item',
  9. menu_active_selector: '.active',
  10. },
  11. }, // menu and element navigation functions
  12. notifications: {},
  13. callbacks: {
  14. // events callback functions
  15. click: {},
  16. mouseover: {},
  17. mouseout: {},
  18. keypress: {},
  19. },
  20. helpers: {}, // simple handy methods
  21. tmp: {
  22. sort_par: 'sort-name',
  23. sort_direction: -1,
  24. sort_as_int: 0,
  25. form_changed: 0,
  26. search_activated: 0,
  27. search_display_interval: 0,
  28. search_hover_interval: 0,
  29. },
  30. };
  31. /*
  32. * Main method that invokes further event processing
  33. * @param root is root HTML DOM element that. Pass HTML DOM Element or css selector
  34. * @param event_type (eg: click, mouseover etc..)
  35. */
  36. VE.core.register = function (root, event_type) {
  37. var root = !root ? 'body' : root; // if elm is not passed just bind events to body DOM Element
  38. var event_type = !event_type ? 'click' : event_type; // set event type to "click" by default
  39. $(root).bind(event_type, function (evt) {
  40. var elm = $(evt.target);
  41. VE.core.dispatch(evt, elm, event_type); // dispatch captured event
  42. });
  43. };
  44. /*
  45. * Dispatch event that was previously registered
  46. * @param evt related event object
  47. * @param elm that was catched
  48. * @param event_type (eg: click, mouseover etc..)
  49. */
  50. VE.core.dispatch = function (evt, elm, event_type) {
  51. if ('undefined' == typeof VE.callbacks[event_type]) {
  52. return VE.helpers.warn(
  53. 'There is no corresponding object that should contain event callbacks for "' +
  54. event_type +
  55. '" event type'
  56. );
  57. }
  58. // get class of element
  59. var classes = $(elm).attr('class');
  60. // if no classes are attached, then just stop any further processings
  61. if (!classes) {
  62. return; // no classes assigned
  63. }
  64. // split the classes and check if it related to function
  65. $(classes.split(/\s/)).each(function (i, key) {
  66. VE.callbacks[event_type][key] && VE.callbacks[event_type][key](evt, elm);
  67. });
  68. };
  69. //
  70. // CALLBACKS
  71. //
  72. /*
  73. * Suspend action
  74. */
  75. VE.callbacks.click.do_suspend = function (evt, elm) {
  76. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  77. var url = $('input[name="suspend_url"]', ref).val();
  78. var dialog_elm = ref.find('.js-confirm-dialog-suspend');
  79. VE.helpers.createConfirmationDialog(dialog_elm, $(elm).parent().attr('title'), url);
  80. };
  81. /*
  82. * Unsuspend action
  83. */
  84. VE.callbacks.click.do_unsuspend = function (evt, elm) {
  85. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  86. var url = $('input[name="unsuspend_url"]', ref).val();
  87. var dialog_elm = ref.find('.js-confirm-dialog-suspend');
  88. VE.helpers.createConfirmationDialog(dialog_elm, $(elm).parent().attr('title'), url);
  89. };
  90. /*
  91. * Delete action
  92. */
  93. VE.callbacks.click.do_delete = function (evt, elm) {
  94. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  95. var url = $('input[name="delete_url"]', ref).val();
  96. var dialog_elm = ref.find('.js-confirm-dialog-delete');
  97. VE.helpers.createConfirmationDialog(dialog_elm, $(elm).parent().attr('title'), url);
  98. };
  99. VE.callbacks.click.do_servicerestart = function (evt, elm) {
  100. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  101. var url = $('input[name="servicerestart_url"]', ref).val();
  102. var dialog_elm = ref.find('.js-confirm-dialog-servicerestart');
  103. VE.helpers.createConfirmationDialog(dialog_elm, $(elm).parent().attr('title'), url);
  104. };
  105. VE.callbacks.click.do_servicestop = function (evt, elm) {
  106. var ref = elm.hasClass('actions-panel') ? elm : elm.parents('.actions-panel');
  107. var url = $('input[name="servicestop_url"]', ref).val();
  108. var dialog_elm = ref.find('.js-confirm-dialog-servicestop');
  109. VE.helpers.createConfirmationDialog(dialog_elm, $(elm).parent().attr('title'), url);
  110. };
  111. /*
  112. * Create dialog box on the fly
  113. * @param elm Element which contains the dialog contents
  114. * @param dialog_title
  115. * @param confirmed_location_url URL that will be redirected to if user hit "OK"
  116. * @param custom_config Custom configuration parameters passed to dialog initialization (optional)
  117. */
  118. VE.helpers.createConfirmationDialog = function (
  119. elm,
  120. dialog_title,
  121. confirmed_location_url,
  122. custom_config
  123. ) {
  124. var custom_config = !custom_config ? {} : custom_config;
  125. var config = {
  126. modal: true,
  127. //autoOpen: true,
  128. resizable: false,
  129. width: 360,
  130. title: dialog_title,
  131. close: function () {
  132. $(this).dialog('destroy');
  133. },
  134. buttons: {
  135. OK: function (event, ui) {
  136. location.href = confirmed_location_url;
  137. },
  138. Cancel: function () {
  139. $(this).dialog('close');
  140. },
  141. },
  142. create: function () {
  143. var buttonGroup = $(this).closest('.ui-dialog').find('.ui-dialog-buttonset');
  144. buttonGroup.find('button:first').addClass('button submit');
  145. buttonGroup.find('button:last').addClass('button button-secondary cancel');
  146. },
  147. };
  148. var reference_copied = $(elm[0]).clone();
  149. config = $.extend(config, custom_config);
  150. $(reference_copied).dialog(config);
  151. };
  152. /*
  153. * Simple debug output
  154. */
  155. VE.helpers.warn = function (msg) {
  156. alert('WARNING: ' + msg);
  157. };
  158. VE.helpers.extendPasswordFields = function () {
  159. var references = ['.js-password-input'];
  160. $(document).ready(function () {
  161. $(references).each(function (i, ref) {
  162. VE.helpers.initAdditionalPasswordFieldElements(ref);
  163. });
  164. });
  165. };
  166. VE.helpers.initAdditionalPasswordFieldElements = function (ref) {
  167. var enabled = $.cookie('hide_passwords') == '1' ? true : false;
  168. if (enabled) {
  169. VE.helpers.hidePasswordFieldText(ref);
  170. }
  171. $(ref).prop('autocomplete', 'off');
  172. var enabled_html = enabled ? '' : 'show-passwords-enabled-action';
  173. var html =
  174. '<span class="toggle-password"><i class="toggle-psw-visibility-icon fas fa-eye-slash ' +
  175. enabled_html +
  176. '" onclick="VE.helpers.toggleHiddenPasswordText(\'' +
  177. ref +
  178. '\', this)"></i></span>';
  179. $(ref).after(html);
  180. };
  181. VE.helpers.hidePasswordFieldText = function (ref) {
  182. $.cookie('hide_passwords', '1', { expires: 365, path: '/' });
  183. $(ref).prop('type', 'password');
  184. };
  185. VE.helpers.revealPasswordFieldText = function (ref) {
  186. $.cookie('hide_passwords', '0', { expires: 365, path: '/' });
  187. $(ref).prop('type', 'text');
  188. };
  189. VE.helpers.toggleHiddenPasswordText = function (ref, triggering_elm) {
  190. $(triggering_elm).toggleClass('show-passwords-enabled-action');
  191. if ($(ref).prop('type') == 'text') {
  192. VE.helpers.hidePasswordFieldText(ref);
  193. } else {
  194. VE.helpers.revealPasswordFieldText(ref);
  195. }
  196. };
  197. var reloadTimer = 150;
  198. var reloadFunction = '';
  199. //$(document).ready(startTime);
  200. function startTime() {
  201. if ($('.spinner')[0]) {
  202. reloadFunction = setInterval(updateInterval, 100);
  203. }
  204. }
  205. function updateInterval() {
  206. reloadTimer = reloadTimer - 1;
  207. if (reloadTimer == 0) {
  208. location.reload();
  209. }
  210. }
  211. function stopTimer() {
  212. if (reloadFunction) {
  213. clearInterval(reloadFunction);
  214. reloadFunction = false;
  215. $('.spinner').addClass('paused');
  216. $('.pause-stop i').removeClass('fa-pause');
  217. $('.pause-stop i').addClass('fa-play');
  218. } else {
  219. reloadFunction = setInterval(updateInterval, 100);
  220. $('.spinner').removeClass('paused');
  221. $('.pause-stop i').removeClass('fa-play');
  222. $('.pause-stop i').addClass('fa-pause');
  223. }
  224. }
  225. VE.navigation.enter_focused = function () {
  226. if ($('.units').hasClass('active')) {
  227. location.href = $('.units.active .l-unit.focus .actions-panel__col.actions-panel__edit a').attr(
  228. 'href'
  229. );
  230. } else {
  231. if ($(VE.navigation.state.menu_selector + '.focus a').attr('href')) {
  232. location.href = $(VE.navigation.state.menu_selector + '.focus a').attr('href');
  233. }
  234. }
  235. };
  236. VE.navigation.move_focus_left = function () {
  237. var index = parseInt(
  238. $(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector + '.focus'))
  239. );
  240. if (index == -1)
  241. index = parseInt(
  242. $(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))
  243. );
  244. if ($('.units').hasClass('active')) {
  245. $('.units').removeClass('active');
  246. index++;
  247. }
  248. $(VE.navigation.state.menu_selector).removeClass('focus');
  249. if (index > 0) {
  250. $($(VE.navigation.state.menu_selector)[index - 1]).addClass('focus');
  251. } else {
  252. VE.navigation.switch_menu('last');
  253. }
  254. };
  255. VE.navigation.move_focus_right = function () {
  256. var max_index = $(VE.navigation.state.menu_selector).length - 1;
  257. var index = parseInt(
  258. $(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector + '.focus'))
  259. );
  260. if (index == -1)
  261. index =
  262. parseInt(
  263. $(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))
  264. ) || 0;
  265. $(VE.navigation.state.menu_selector).removeClass('focus');
  266. if ($('.units').hasClass('active')) {
  267. $('.units').removeClass('active');
  268. index--;
  269. }
  270. if (index < max_index) {
  271. $($(VE.navigation.state.menu_selector)[index + 1]).addClass('focus');
  272. } else {
  273. VE.navigation.switch_menu('first');
  274. }
  275. };
  276. VE.navigation.move_focus_down = function () {
  277. var max_index = $('.units .l-unit:not(.header)').length - 1;
  278. var index = parseInt($('.units .l-unit').index($('.units .l-unit.focus')));
  279. if (index < max_index) {
  280. $('.units .l-unit.focus').removeClass('focus');
  281. $($('.units .l-unit:not(.header)')[index + 1]).addClass('focus');
  282. $('html, body').animate(
  283. {
  284. scrollTop: $('.units .l-unit.focus').offset().top - 200,
  285. },
  286. 200
  287. );
  288. }
  289. };
  290. VE.navigation.move_focus_up = function () {
  291. var index = parseInt($('.units .l-unit:not(.header)').index($('.units .l-unit.focus')));
  292. if (index == -1) index = 0;
  293. if (index > 0) {
  294. $('.units .l-unit.focus').removeClass('focus');
  295. $($('.units .l-unit:not(.header)')[index - 1]).addClass('focus');
  296. $('html, body').animate(
  297. {
  298. scrollTop: $('.units .l-unit.focus').offset().top - 200,
  299. },
  300. 200
  301. );
  302. }
  303. };
  304. VE.navigation.switch_menu = function (position) {
  305. position = position || 'first'; // last
  306. if (VE.navigation.state.active_menu == 0) {
  307. VE.navigation.state.active_menu = 1;
  308. VE.navigation.state.menu_selector = '.main-menu-item';
  309. VE.navigation.state.menu_active_selector = '.active';
  310. if (position == 'first') {
  311. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  312. } else {
  313. var max_index = $(VE.navigation.state.menu_selector).length - 1;
  314. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  315. }
  316. }
  317. };
  318. VE.notifications.get_list = function () {
  319. /// TODO get notifications only once
  320. $.ajax({
  321. url: '/list/notifications/?ajax=1&token=' + $('#token').attr('token'),
  322. dataType: 'json',
  323. }).done(function (data) {
  324. var acc = [];
  325. $.each(data, function (i, elm) {
  326. var tpl = Tpl.get('notification', 'WEB');
  327. if (elm.ACK == 'no') tpl.set(':UNSEEN', 'unseen');
  328. else tpl.set(':UNSEEN', '');
  329. tpl.set(':ID', elm.ID);
  330. tpl.set(':TYPE', elm.TYPE);
  331. tpl.set(':TOPIC', elm.TOPIC);
  332. tpl.set(':NOTICE', elm.NOTICE);
  333. tpl.set(':TIME', elm.TIME);
  334. tpl.set(':DATE', elm.DATE);
  335. acc.push(tpl.finalize());
  336. });
  337. if (!Object.keys(data).length) {
  338. var tpl = Tpl.get('notification_empty', 'WEB');
  339. acc.push(tpl.finalize());
  340. }
  341. $('.notification-container').html(acc.done()).removeClass('u-hidden');
  342. $('.notification-container .mark-seen').click(function (event) {
  343. VE.notifications.delete($(event.target).attr('id').replace('notification-', ''));
  344. });
  345. });
  346. };
  347. VE.notifications.delete = function (id) {
  348. $('#notification-' + id)
  349. .parent('li')
  350. .hide();
  351. $.ajax({
  352. url:
  353. '/delete/notification/?delete=1&notification_id=' +
  354. id +
  355. '&token=' +
  356. $('#token').attr('token'),
  357. });
  358. if ($('.notification-container li:visible').length == 0) {
  359. $('.js-notifications .status-icon').removeClass('status-icon');
  360. $('.js-notifications').removeClass('updates').removeClass('active');
  361. }
  362. };
  363. VE.navigation.shortcut = function (elm) {
  364. var action = elm.attr('key-action');
  365. if (action == 'js') {
  366. var e = elm.find('.data-controls');
  367. VE.core.dispatch(true, e, 'click');
  368. }
  369. if (action == 'href') {
  370. location.href = elm.find('a').attr('href');
  371. }
  372. };
  373. VE.helpers.extendPasswordFields();