events.js 16 KB

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