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