forked from andi34/photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
343 lines (284 loc) · 11.2 KB
/
admin.js
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
/* globals photoboothTools */
$(function () {
const shellCommand = function ($mode) {
const command = {
mode: $mode
};
photoboothTools.console.log('Run' + $mode);
jQuery
.post('../api/shellCommand.php', command)
.done(function (result) {
photoboothTools.console.log($mode, 'result: ', result);
})
.fail(function (xhr, status, result) {
photoboothTools.console.log($mode, 'result: ', result);
});
};
$('#reset-btn').on('click', function (e) {
e.preventDefault();
const msg = photoboothTools.getTranslation('really_delete');
const really = confirm(msg);
const data = {type: 'reset'};
const elem = $(this);
elem.addClass('saving');
if (really) {
$.ajax({
url: '../api/admin.php',
data: data,
dataType: 'json',
type: 'post',
success: function (resp) {
elem.removeClass('saving');
elem.addClass(resp);
setTimeout(function () {
elem.removeClass('error success');
window.location.reload();
}, 3000);
}
});
} else {
elem.removeClass('saving');
}
});
$('#save-btn').on('click', function (e) {
e.preventDefault();
const elem = $(this);
elem.addClass('saving');
const data = 'type=config&' + $('form').serialize();
$.ajax({
url: '../api/admin.php',
data: data,
dataType: 'json',
type: 'post',
success: function (resp) {
elem.removeClass('saving');
elem.addClass(resp);
setTimeout(function () {
elem.removeClass('error success');
if (resp === 'success') {
window.location.reload();
}
}, 2000);
}
});
});
$('#diskusage-btn').on('click', function (e) {
e.preventDefault();
location.assign('diskusage.php');
return false;
});
$('#updater-btn').on('click', function (e) {
e.preventDefault();
location.assign('../update.php');
return false;
});
$('#dependencies-btn').on('click', function (e) {
e.preventDefault();
location.assign('../dependencies.php');
return false;
});
$('#databaserebuild-btn').on('click', function (e) {
e.preventDefault();
const elem = $(this);
$('#databaserebuild-btn').children('.text').toggle();
elem.addClass('saving');
$.ajax({
url: '../api/rebuildImageDB.php',
success: function (resp) {
elem.removeClass('saving');
elem.addClass(resp);
setTimeout(function () {
elem.removeClass('error success');
$('#databaserebuild-btn').children('.text').toggle();
}, 2000);
}
});
});
$('#checkversion-btn').on('click', function (ev) {
ev.preventDefault();
const elem = $(this);
$('#checkversion-btn').children('.text').toggle();
elem.addClass('saving');
$.ajax({
url: '../api/checkVersion.php',
method: 'GET',
success: (data) => {
$('#checkVersion').empty();
photoboothTools.console.log('data', data);
if (!data.updateAvailable) {
$('#current_version_text').text(photoboothTools.getTranslation('using_latest_version'));
} else if (/^\d+\.\d+\.\d+$/u.test(data.availableVersion)) {
$('#current_version_text').text(photoboothTools.getTranslation('current_version'));
$('#current_version').text(data.currentVersion);
$('#available_version_text').text(photoboothTools.getTranslation('available_version'));
$('#available_version').text(data.availableVersion);
} else {
$('#current_version_text').text(photoboothTools.getTranslation('test_update_available'));
}
elem.removeClass('saving');
elem.addClass('success');
setTimeout(function () {
elem.removeClass('error success');
$('#checkversion-btn').children('.text').toggle();
}, 2000);
},
error: (jqXHR) => {
photoboothTools.console.log('Error checking Version: ', jqXHR.responseText);
elem.removeClass('saving');
elem.addClass('error');
setTimeout(function () {
elem.removeClass('error success');
$('#checkversion-btn').children('.text').toggle();
}, 2000);
}
});
});
$('#debugpanel-btn').on('click', function (ev) {
ev.preventDefault();
window.open('debugpanel.php');
return false;
});
$('#translate-btn').on('click', function (ev) {
ev.preventDefault();
window.open('https://crowdin.com/project/photobooth');
return false;
});
$('#reboot-btn').on('click', function (ev) {
ev.preventDefault();
shellCommand('reboot');
return false;
});
$('#shutdown-btn').on('click', function (ev) {
ev.preventDefault();
shellCommand('shutdown');
return false;
});
// Admin Panel active section at init
$('#nav-general').addClass('active');
/*
* Check if element is visible in current viewport on screen
* https://www.javascripttutorial.net/dom/css/check-if-an-element-is-visible-in-the-viewport
*/
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// Range slider - dynamically update value when being moved
$(document).on('input', '.configslider', function () {
document.querySelector('#' + this.name.replace('[', '\\[').replace(']', '\\]') + '-value span').innerHTML =
this.value;
});
/*
* Install waypoints for each section, to enable dynamic nav bar
* FIXME - trigger waypoints for settings at bottom of screen (those WPs never trigger automatically)
*/
$('.setting_section').waypoint({
handler: function (direction) {
$('.adminnavlistelement').removeClass('active');
$('#nav-' + this.element.id).addClass('active');
const contentpage = document.getElementById('adminsidebar');
const elemTarget = document.getElementById('nav-' + this.element.id);
if (!isInViewport(elemTarget)) {
const topPos = elemTarget.offsetTop;
let newPos = 0;
if (direction == 'down') {
newPos = topPos + elemTarget.offsetHeight - window.innerHeight;
} else {
newPos = topPos;
}
contentpage.scrollTop = newPos;
}
}
});
// Click on nav bar element scrolls settings content page
$('.adminnavlistelement').click(function (e) {
e.preventDefault();
// on small screens, hide navbar after click
if (window.matchMedia('screen and (max-width: 700px)').matches) {
$('div.adminsidebar').toggle();
}
const target = $(this).attr('href');
// scroll content page if we need to
const contentpage = document.getElementById('admincontentpage');
const elemTarget = document.getElementById(target.substring(1));
const totalPageHeight = contentpage.scrollHeight;
const scrollPoint = window.scrollY + window.innerHeight;
if (isInViewport(elemTarget) && scrollPoint >= totalPageHeight) {
$('.adminnavlistelement').removeClass('active');
$('#' + this.id).addClass('active');
return false;
}
$('html, body').animate(
{
// sroll element to 5em below top - and have to disable eslint rule because prettier removes unnecessary but clarifying parenthesis
// eslint-disable-next-line no-mixed-operators
scrollTop: $(target).offset().top - 5 * parseInt(config.ui.font_size, 10)
},
1000,
() => {
$('.adminnavlistelement').removeClass('active');
$('#' + this.id).addClass('active');
// scroll nav bar if needed
const cp = document.getElementById('adminsidebar');
const eT = document.getElementById(this.id);
if (!isInViewport(eT)) {
const viewportOffset = elemTarget.getBoundingClientRect();
let newPos = 0;
if (viewportOffset.top < 0) {
newPos = eT.offsetTop;
} else {
newPos = window.innerHeight - eT.offsetHeight;
}
cp.scrollTop = newPos;
}
return false;
}
);
return false;
});
// Localization of toggle button text
$('.toggle').click(function () {
if ($('input', this).is(':checked')) {
$('.toggleTextON', this).removeClass('hidden');
$('.toggleTextOFF', this).addClass('hidden');
} else {
$('.toggleTextOFF', this).removeClass('hidden');
$('.toggleTextON', this).addClass('hidden');
}
});
// activate selectize library for multi-selects
$('.multi-select').selectize({});
// menu toggle button for topnavbar (small screen sizes)
$('#admintopnavbarmenutoggle').on('click', function () {
$('.adminsidebar').toggle();
if (window.matchMedia('screen and (min-width: 701px)').matches) {
if ($('#adminsidebar').is(':visible')) {
$('#admincontentpage').css('margin-left', '200px');
} else {
$('#admincontentpage').css('margin-left', '0px');
}
}
});
// back button for topnavbar (small screen sizes)
$('#admintopnavbarback').on('click', function () {
location.assign('../');
});
// logout button for topnavbar (small screen sizes)
$('#admintopnavbarlogout').on('click', function () {
location.assign('../login/logout.php');
});
// check padding of settings content on window resize
window.addEventListener('resize', onWindowResize);
function onWindowResize() {
if (window.matchMedia('screen and (max-width: 700px)').matches) {
$('#admincontentpage').css('margin-left', '0px');
} else if ($('#adminsidebar').is(':visible')) {
$('#admincontentpage').css('margin-left', '200px');
}
}
});