blob: c657c1344037990c503ef334799962021773a63f [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001$(document).ready(function() {
2 mass_action = false;
3 function validateEmail(email) {
4 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
5 return re.test(email);
6 }
7 function validateRegex(e){var t=e.split("/"),n=e,r="";t.length>1&&(n=t[1],r=t[2]);try{return new RegExp(n,r),!0}catch(e){return!1}}
8 function is_active(elem) {
9 if ($(elem).data('submitted') == '1') {
10 return true;
11 } else {
12 var parent_btn_grp = $(elem).parentsUntil(".btn-group").parent();
13 if (parent_btn_grp.hasClass('btn-group')) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010014 parent_btn_grp.replaceWith('<button class="btn btn-secondary btn-sm" disabled>' + lang_footer.loading + '</a>');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010015 }
16 $(elem).text(lang_footer.loading);
17 $(elem).attr('data-submitted', '1');
18 function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
19 $(document).on("keydown", disableF5);
20 return false;
21 }
22 }
23 $.fn.serializeObject = function() {
24 var o = {};
25 var a = this.serializeArray();
26 $.each(a, function() {
27 if (o[this.name]) {
28 if (!o[this.name].push) {
29 o[this.name] = [o[this.name]];
30 }
31 o[this.name].push(this.value || '');
32 } else {
33 o[this.name] = this.value || '';
34 }
35 });
36 return o;
37 };
38 // Collect values of input fields with name "multi_select" and same data-id to js array multi_data[data-id]
39 var multi_data = [];
40 $(document).on('change', 'input[name=multi_select]:checkbox', function(e) {
41 if(mass_action === true) {
42 multi_data = [];
43 mass_action = false;
44 }
45 if ($(this).is(':checked') && $(this).data('id')) {
46 var id = $(this).data('id');
47 if (typeof multi_data[id] == "undefined") {
48 multi_data[id] = [];
49 }
50 multi_data[id].push($(this).val());
51 }
52 else {
53 var id = $(this).data('id');
54 if (typeof multi_data[id] !== "undefined") {
55 multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
56 }
57 }
58 });
59
60 // Select checkbox by click on parent tr
61 $(document).on('click', 'tbody>tr', function(e) {
62 if(e.target.tagName.toLowerCase() === 'button') {
63 e.stopPropagation();
64 }
65 else if(e.target.tagName.toLowerCase() === 'a') {
66 e.stopPropagation();
67 }
68 else if (e.target.type == "checkbox") {
69 e.stopPropagation();
70 }
71 else {
72 var checkbox = $(this).find(':checkbox');
73 checkbox.trigger('click');
74 }
75 });
76
77 // Select or deselect all checkboxes with same data-id
78 $(document).on('click', '#toggle_multi_select_all', function(e) {
79 mass_action = true
80 e.preventDefault();
81 id = $(this).data("id");
82 var all_checkboxes = $("input[data-id=" + id + "]:enabled");
83 all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
84 });
85
86 // General API edit actions
87 $(document).on('click', "[data-action='edit_selected']", function(e) {
88 e.preventDefault();
89 var id = $(this).data('id');
90 var api_url = $(this).data('api-url');
91 var api_attr = $(this).data('api-attr');
92 if (typeof $(this).data('api-reload-window') !== 'undefined') {
93 api_reload_window = $(this).data('api-reload-window');
94 } else {
95 api_reload_window = true;
96 }
97 if (typeof $(this).data('api-reload-location') !== 'undefined') {
98 api_reload_location = $(this).data('api-reload-location');
99 } else {
100 api_reload_location = '#';
101 }
102 // If clicked element #edit_selected is in a form with the same data-id as the button,
103 // we merge all input fields by {"name":"value"} into api-attr
104 if ($(this).closest("form").data('id') == id) {
105 var invalid = false;
106 $(this).closest("form").find('select, textarea, input').each(function() {
107 if ($(this).prop('required')) {
108 if (!$(this).val() && $(this).prop('disabled') === false) {
109 invalid = true;
110 if ($(this).is("select")) {
111 $(this).selectpicker('setStyle', 'btn-input-missing', 'add');
112 }
113 $(this).addClass('inputMissingAttr');
114 } else {
115 if ($(this).is("select")) {
116 $(this).selectpicker('setStyle', 'btn-input-missing', 'remove');
117 }
118 $(this).removeClass('inputMissingAttr');
119 }
120 }
121 if ($(this).val() && $(this).attr("type") == 'email') {
122 if (!validateEmail($(this).val())) {
123 invalid = true;
124 $(this).addClass('inputMissingAttr');
125 } else {
126 $(this).removeClass('inputMissingAttr');
127 }
128 }
129 if ($(this).attr("max")) {
130 if (Number($(this).val()) > Number($(this).attr("max"))) {
131 invalid = true;
132 $(this).addClass('inputMissingAttr');
133 } else {
134 if ($(this).attr("min")) {
135 if (Number($(this).val()) < Number($(this).attr("min"))) {
136 invalid = true;
137 $(this).addClass('inputMissingAttr');
138 } else {
139 $(this).removeClass('inputMissingAttr');
140 }
141 }
142 }
143 }
144 if ($(this).val() && $(this).attr("regex")) {
145 var regex_content = $(this).val();
146 $(this).removeClass('inputMissingAttr');
147 if(!validateRegex(regex_content)) {
148 invalid = true;
149 $(this).addClass('inputMissingAttr');
150 }
151 if(!regex_content.startsWith('/') || !/\/[ims]?$/.test(regex_content)){
152 invalid = true;
153 $(this).addClass('inputMissingAttr');
154 }
155 }
156 });
157 if (!invalid) {
158 var attr_to_merge = $(this).closest("form").serializeObject();
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100159 // parse possible JSON Strings
160 for (var [key, value] of Object.entries(attr_to_merge)) {
161 try {
162 attr_to_merge[key] = JSON.parse(attr_to_merge[key]);
163 } catch {}
164 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100165 var api_attr = $.extend(api_attr, attr_to_merge)
166 } else {
167 return false;
168 }
169 }
170 // alert(JSON.stringify(api_attr));
171 // If clicked element #edit_selected has data-item attribute, it is added to "items"
172 if (typeof $(this).data('item') !== 'undefined') {
173 var id = $(this).data('id');
174 if (typeof multi_data[id] == "undefined") {
175 multi_data[id] = [];
176 }
177 multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
178 multi_data[id].push($(this).data('item'));
179 }
180 if (typeof multi_data[id] == "undefined") return;
181 api_items = multi_data[id];
182 for (var i in api_items) {
183 api_items[i] = decodeURIComponent(api_items[i]);
184 }
185 // alert(JSON.stringify(api_attr));
186 if (Object.keys(api_items).length !== 0) {
187 if (is_active($(this))) { return false; }
188 $.ajax({
189 type: "POST",
190 dataType: "json",
191 data: {
192 "items": JSON.stringify(api_items),
193 "attr": JSON.stringify(api_attr),
194 "csrf_token": csrf_token
195 },
196 url: '/api/v1/' + api_url,
197 jsonp: false,
198 complete: function(data) {
199 var response = (data.responseText);
200 if (typeof response !== 'undefined' && response.length !== 0) {
201 response_obj = JSON.parse(response);
202 }
203 if (api_reload_window === true) {
204 if (api_reload_location != '#') {
205 window.location.replace(api_reload_location)
206 } else {
207 window.location = window.location.href.split("#")[0];
208 }
209 }
210 }
211 });
212 }
213 });
214
215 // General API add actions
216 $(document).on('click', "[data-action='add_item']", function(e) {
217 e.preventDefault();
218 var id = $(this).data('id');
219 var api_url = $(this).data('api-url');
220 var api_attr = $(this).data('api-attr');
221 if (typeof $(this).data('api-reload-window') !== 'undefined') {
222 api_reload_window = $(this).data('api-reload-window');
223 } else {
224 api_reload_window = true;
225 }
226 // If clicked button is in a form with the same data-id as the button,
227 // we merge all input fields by {"name":"value"} into api-attr
228 if ($(this).closest("form").data('id') == id) {
229 var invalid = false;
230 $(this).closest("form").find('select, textarea, input').each(function() {
231 if ($(this).prop('required')) {
232 if (!$(this).val() && $(this).prop('disabled') === false) {
233 invalid = true;
234 if ($(this).is("select")) {
235 $(this).selectpicker('setStyle', 'btn-input-missing', 'add');
236 }
237 $(this).addClass('inputMissingAttr');
238 } else {
239 if ($(this).is("select")) {
240 $(this).selectpicker('setStyle', 'btn-input-missing', 'remove');
241 }
242 $(this).removeClass('inputMissingAttr');
243 }
244 }
245 if ($(this).attr("type") == 'email') {
246 var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
247 if (!emailReg.test($(this).val())) {
248 invalid = true;
249 $(this).addClass('inputMissingAttr');
250 } else {
251 $(this).removeClass('inputMissingAttr');
252 }
253 }
254 if ($(this).attr("max")) {
255 if (Number($(this).val()) > Number($(this).attr("max"))) {
256 invalid = true;
257 $(this).addClass('inputMissingAttr');
258 } else {
259 if ($(this).attr("min")) {
260 if (Number($(this).val()) < Number($(this).attr("min"))) {
261 invalid = true;
262 $(this).addClass('inputMissingAttr');
263 } else {
264 $(this).removeClass('inputMissingAttr');
265 }
266 }
267 }
268 }
269 });
270 if (!invalid) {
271 var attr_to_merge = $(this).closest("form").serializeObject();
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100272 // parse possible JSON Strings
273 for (var [key, value] of Object.entries(attr_to_merge)) {
274 try {
275 attr_to_merge[key] = JSON.parse(attr_to_merge[key]);
276 } catch {}
277 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100278 var api_attr = $.extend(api_attr, attr_to_merge)
279 } else {
280 return false;
281 }
282 }
283 if (is_active($(this))) { return false; }
284 // alert(JSON.stringify(api_attr));
285 $.ajax({
286 type: "POST",
287 dataType: "json",
288 data: {
289 "attr": JSON.stringify(api_attr),
290 "csrf_token": csrf_token
291 },
292 url: '/api/v1/' + api_url,
293 jsonp: false,
294 complete: function(data) {
295 var response = (data.responseText);
296 if (typeof response !== 'undefined' && response.length !== 0) {
297 response_obj = JSON.parse(response);
298 unset = true;
299 $.each(response_obj, function(i, v) {
300 if (v.type == "danger") {
301 unset = false;
302 }
303 });
304 if (unset === true) {
305 unset = null;
306 // Keep form data for sync jobs
307 if (id != "add_syncjob") {
308 $('form').formcache('clear');
309 $('form').formcache('destroy');
310 var i = localStorage.length;
311 while(i--) {
312 var key = localStorage.key(i);
313 if(/formcache/.test(key)) {
314 localStorage.removeItem(key);
315 }
316 }
317 }
318 }
319 else {
320 var add_modal = $('.modal.in').attr('id');
321 localStorage.setItem("add_modal", add_modal);
322 }
323 }
324 if (api_reload_window === true) {
325 window.location = window.location.href.split("#")[0];
326 }
327 }
328 });
329 });
330
331 // General API delete actions
332 $(document).on('click', "[data-action='delete_selected']", function(e) {
333 e.preventDefault();
334 var id = $(this).data('id');
335 // If clicked element #delete_selected has data-item attribute, it is added to "items"
336 if (typeof $(this).data('item') !== 'undefined') {
337 var id = $(this).data('id');
338 if (typeof multi_data[id] == "undefined") {
339 multi_data[id] = [];
340 }
341 multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
342 multi_data[id].push($(this).data('item'));
343 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100344
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100345 if (typeof $(this).data('text') !== 'undefined') {
346 $("#DeleteText").empty();
347 $("#DeleteText").text($(this).data('text'));
348 }
349 if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
350 data_array = multi_data[id];
351 api_url = $(this).data('api-url');
352 $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
353 $("#ItemsToDelete").empty();
354 for (var i in data_array) {
355 data_array[i] = decodeURIComponent(data_array[i]);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100356 $("#ItemsToDelete").append("<li>" + escapeHtml(data_array[i]) + "</li>");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100357 }
358 })
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100359 $('#ConfirmDeleteModal').modal('show')
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100360 .one('click', '#IsConfirmed', function(e) {
361 if (is_active($('#IsConfirmed'))) { return false; }
362 $.ajax({
363 type: "POST",
364 dataType: "json",
365 cache: false,
366 data: {
367 "items": JSON.stringify(data_array),
368 "csrf_token": csrf_token
369 },
370 url: '/api/v1/' + api_url,
371 jsonp: false,
372 complete: function(data) {
373 window.location = window.location.href.split("#")[0];
374 }
375 });
376 })
377 .one('click', '#isCanceled', function(e) {
378 // Remove event handler to allow to close modal and restart dialog without multiple submits
379 $('#ConfirmDeleteModal').off();
380 $('#ConfirmDeleteModal').modal('hide');
381 });
382 });
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100383
384 // toggle jquery datatables child rows
385 $('button[data-datatables-expand], a[data-datatables-expand]').on('click', function (e) {
386 e.preventDefault();
387 var tableId = e.target.getAttribute("data-datatables-expand");
388 var table = $("#" + tableId).DataTable();
389 table.rows(':not(.parent)').nodes().to$().find('td:first-child').trigger('click');
390 });
391 $('button[data-datatables-collapse], a[data-datatables-collapse]').on('click', function (e) {
392 e.preventDefault();
393 var tableId = e.target.getAttribute("data-datatables-collapse");
394 var table = $("#" + tableId).DataTable();
395 table.rows('.parent').nodes().to$().find('td:first-child').trigger('click');
396 });
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100397});