events.js 15 KB

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