events.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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('.js-confirm-dialog-suspend');
  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('.js-confirm-dialog-suspend');
  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('.js-confirm-dialog-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('.js-confirm-dialog-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('.js-confirm-dialog-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. var buttonGroup = $(this).closest(".ui-dialog").find('.ui-dialog-buttonset');
  133. buttonGroup.find('button:first').addClass('button submit')
  134. buttonGroup.find('button:last').addClass('button button-secondary cancel');
  135. }
  136. }
  137. var reference_copied = $(elm[0]).clone();
  138. config = $.extend(config, custom_config);
  139. $(reference_copied).dialog(config);
  140. }
  141. /*
  142. * Simple debug output
  143. */
  144. VE.helpers.warn = function(msg) {
  145. alert('WARNING: ' + msg);
  146. }
  147. VE.helpers.extendPasswordFields = function() {
  148. var references = ['.js-password-input'];
  149. $(document).ready(function() {
  150. $(references).each(function(i, ref) {
  151. VE.helpers.initAdditionalPasswordFieldElements(ref);
  152. });
  153. });
  154. }
  155. VE.helpers.initAdditionalPasswordFieldElements = function(ref) {
  156. var enabled = $.cookie('hide_passwords') == '1' ? true : false;
  157. if (enabled) {
  158. VE.helpers.hidePasswordFieldText(ref);
  159. }
  160. $(ref).prop('autocomplete', 'off');
  161. var enabled_html = enabled ? '' : 'show-passwords-enabled-action';
  162. var html = '<span class="toggle-password"><i class="toggle-psw-visibility-icon fas fa-eye-slash ' + enabled_html + '" onclick="VE.helpers.toggleHiddenPasswordText(\'' + ref + '\', this)"></i></span>';
  163. $(ref).after(html);
  164. }
  165. VE.helpers.hidePasswordFieldText = function(ref) {
  166. $.cookie('hide_passwords', '1', { expires: 365, path: '/' });
  167. $(ref).prop('type', 'password');
  168. }
  169. VE.helpers.revealPasswordFieldText = function(ref) {
  170. $.cookie('hide_passwords', '0', { expires: 365, path: '/' });
  171. $(ref).prop('type', 'text');
  172. }
  173. VE.helpers.toggleHiddenPasswordText = function(ref, triggering_elm) {
  174. $(triggering_elm).toggleClass('show-passwords-enabled-action');
  175. if ($(ref).prop('type') == 'text') {
  176. VE.helpers.hidePasswordFieldText(ref);
  177. }
  178. else {
  179. VE.helpers.revealPasswordFieldText(ref);
  180. }
  181. }
  182. var reloadTimer = 150;
  183. var reloadFunction = '';
  184. //$(document).ready(startTime);
  185. function startTime(){
  186. if ($(".spinner")[0]){
  187. reloadFunction = setInterval(updateInterval, 100);
  188. }
  189. }
  190. function updateInterval(){
  191. reloadTimer = reloadTimer -1;
  192. if(reloadTimer == 0){
  193. location.reload();
  194. }
  195. }
  196. function stopTimer(){
  197. if(reloadFunction){
  198. clearInterval(reloadFunction);
  199. reloadFunction = false;
  200. $('.spinner').addClass('paused');
  201. $('.pause-stop i').removeClass('fa-pause');
  202. $('.pause-stop i').addClass('fa-play');
  203. }else{
  204. reloadFunction = setInterval(updateInterval, 100);
  205. $('.spinner').removeClass('paused');
  206. $('.pause-stop i').removeClass('fa-play');
  207. $('.pause-stop i').addClass('fa-pause');
  208. }
  209. }
  210. VE.navigation.enter_focused = function() {
  211. if($('.units').hasClass('active')){
  212. location.href=($('.units.active .l-unit.focus .actions-panel__col.actions-panel__edit a').attr('href'));
  213. } else {
  214. if($(VE.navigation.state.menu_selector + '.focus a').attr('href')){
  215. location.href=($(VE.navigation.state.menu_selector + '.focus a').attr('href'));
  216. }
  217. }
  218. }
  219. VE.navigation.move_focus_left = function(){
  220. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  221. if(index == -1)
  222. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector)));
  223. if($('.units').hasClass('active')){
  224. $('.units').removeClass('active');
  225. if(VE.navigation.state.active_menu == 0){
  226. $('.l-menu').addClass('active');
  227. } else {
  228. $('.l-stat').addClass('active');
  229. }
  230. index++;
  231. }
  232. $(VE.navigation.state.menu_selector).removeClass('focus');
  233. if(index > 0){
  234. $($(VE.navigation.state.menu_selector)[index-1]).addClass('focus');
  235. } else {
  236. VE.navigation.switch_menu('last');
  237. }
  238. }
  239. VE.navigation.move_focus_right = function(){
  240. var max_index = $(VE.navigation.state.menu_selector).length-1;
  241. var index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_selector+'.focus')));
  242. if(index == -1)
  243. index = parseInt($(VE.navigation.state.menu_selector).index($(VE.navigation.state.menu_active_selector))) || 0;
  244. $(VE.navigation.state.menu_selector).removeClass('focus');
  245. if($('.units').hasClass('active')){
  246. $('.units').removeClass('active');
  247. if(VE.navigation.state.active_menu == 0){
  248. $('.l-menu').addClass('active');
  249. } else {
  250. $('.l-stat').addClass('active');
  251. }
  252. index--;
  253. }
  254. if(index < max_index){
  255. $($(VE.navigation.state.menu_selector)[index+1]).addClass('focus');
  256. } else {
  257. VE.navigation.switch_menu('first');
  258. }
  259. }
  260. VE.navigation.move_focus_down = function(){
  261. var max_index = $('.units .l-unit:not(.header)').length-1;
  262. var index = parseInt($('.units .l-unit').index($('.units .l-unit.focus')));
  263. if($('.l-menu').hasClass('active') || $('.l-stat').hasClass('active')){
  264. $('.l-menu').removeClass('active');
  265. $('.l-stat').removeClass('active');
  266. $('.units').addClass('active');
  267. index--;
  268. if(index == -2)
  269. index = -1;
  270. }
  271. if(index < max_index){
  272. $('.units .l-unit.focus').removeClass('focus');
  273. $($('.units .l-unit:not(.header)')[index+1]).addClass('focus');
  274. $('html, body').animate({
  275. scrollTop: $('.units .l-unit.focus').offset().top - 200
  276. }, 200);
  277. }
  278. }
  279. VE.navigation.move_focus_up = function(){
  280. var index = parseInt($('.units .l-unit:not(.header)').index($('.units .l-unit.focus')));
  281. if(index == -1)
  282. index = 0;
  283. if($('.l-menu').hasClass('active') || $('.l-stat').hasClass('active')){
  284. $('.l-menu').removeClass('active');
  285. $('.l-stat').removeClass('active');
  286. $('.units').addClass('active');
  287. index++;
  288. }
  289. if(index > 0){
  290. $('.units .l-unit.focus').removeClass('focus');
  291. $($('.units .l-unit:not(.header)')[index-1]).addClass('focus');
  292. $('html, body').animate({
  293. scrollTop: $('.units .l-unit.focus').offset().top - 200
  294. }, 200);
  295. }
  296. }
  297. VE.navigation.switch_menu = function(position){
  298. position = position || 'first'; // last
  299. if(VE.navigation.state.active_menu == 0){
  300. VE.navigation.state.active_menu = 1;
  301. VE.navigation.state.menu_selector = '.l-stat__col';
  302. VE.navigation.state.menu_active_selector = '.l-stat__col--active';
  303. $('.l-menu').removeClass('active');
  304. $('.l-stat').addClass('active');
  305. if(position == 'first'){
  306. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  307. } else {
  308. var max_index = $(VE.navigation.state.menu_selector).length-1;
  309. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  310. }
  311. } else {
  312. VE.navigation.state.active_menu = 0;
  313. VE.navigation.state.menu_selector = '.l-menu__item';
  314. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  315. $('.l-menu').addClass('active');
  316. $('.l-stat').removeClass('active');
  317. if(position == 'first'){
  318. $($(VE.navigation.state.menu_selector)[0]).addClass('focus');
  319. } else {
  320. var max_index = $(VE.navigation.state.menu_selector).length-1;
  321. $($(VE.navigation.state.menu_selector)[max_index]).addClass('focus');
  322. }
  323. }
  324. }
  325. VE.notifications.get_list = function(){
  326. /// TODO get notifications only once
  327. $.ajax({
  328. url: "/list/notifications/?ajax=1&token="+$('#token').attr('token'),
  329. dataType: "json"
  330. }).done(function(data) {
  331. var acc = [];
  332. $.each(data, function(i, elm){
  333. var tpl = Tpl.get('notification', 'WEB');
  334. if(elm.ACK == 'no')
  335. tpl.set(':UNSEEN', 'unseen');
  336. else
  337. tpl.set(':UNSEEN', '');
  338. tpl.set(':ID', elm.ID);
  339. tpl.set(':TYPE', elm.TYPE);
  340. tpl.set(':TOPIC', elm.TOPIC);
  341. tpl.set(':NOTICE', elm.NOTICE);
  342. tpl.set(':TIME', elm.TIME);
  343. tpl.set(':DATE', elm.DATE);
  344. acc.push(tpl.finalize());
  345. });
  346. if(!Object.keys(data).length){
  347. var tpl = Tpl.get('notification_empty', 'WEB');
  348. acc.push(tpl.finalize());
  349. }
  350. $('.notification-container').html(acc.done()).removeClass('u-hidden');
  351. $('.notification-container .mark-seen').click(function(event){
  352. // VE.notifications.mark_seen($(event.target).attr('id').replace("notification-", ""));
  353. VE.notifications.delete($(event.target).attr('id').replace("notification-", ""));
  354. });
  355. });
  356. }
  357. VE.notifications.delete = function(id){
  358. $('#notification-'+id).parents('li').hide();
  359. $.ajax({
  360. url: "/delete/notification/?delete=1&notification_id="+id+"&token="+$('#token').attr('token')
  361. });
  362. if($('.notification-container li:visible').length == 0) {
  363. $('.l-profile__notifications .status-icon').removeClass('status-icon');
  364. $('.l-profile__notifications').removeClass('updates').removeClass('active');
  365. }
  366. }
  367. VE.notifications.mark_seen = function(id){
  368. $('#notification-'+id).parents('li').removeClass('unseen');
  369. $.ajax({
  370. url: "/delete/notification/?notification_id="+id+"&token="+$('#token').attr('token')
  371. });
  372. if($('.notification-container .unseen').length == 0) {
  373. $('.l-profile__notifications .status-icon').removeClass('status-icon');
  374. $('.l-profile__notifications').removeClass('updates');
  375. }
  376. }
  377. VE.navigation.init = function(){
  378. if($('.l-menu__item.l-menu__item--active').length){
  379. // VE.navigation.switch_menu();
  380. VE.navigation.state.active_menu = 0;
  381. VE.navigation.state.menu_selector = '.l-menu__item';
  382. VE.navigation.state.menu_active_selector = '.l-menu__item--active';
  383. $('.l-menu').addClass('active');
  384. $('.l-stat').removeClass('active');
  385. } else {
  386. $('.l-stat').addClass('active');
  387. }
  388. }
  389. VE.navigation.shortcut = function(elm){
  390. var action = elm.attr('key-action');
  391. if(action == 'js'){
  392. var e = elm.find('.data-controls');
  393. VE.core.dispatch(true, e, 'click');
  394. }
  395. if(action == 'href') {
  396. location.href=elm.find('a').attr('href');
  397. }
  398. }
  399. VE.helpers.extendPasswordFields();