blob: 1fe595c69a333b4736fd123e4552a9c3b15537e9 [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-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 Benkard3c18a082022-12-27 17:32:58 +010014 parent_btn_grp.replaceWith('<button class="btn btn-secondary btn-sm" disabled>' + lang_footer.loading + '</a>');
Matthias Andreas Benkard4edeb9f2021-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 Benkard3c18a082022-12-27 17:32:58 +0100159 // parse possible JSON Strings
160 for (var [key, value] of Object.entries(attr_to_merge)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200161 if (typeof value === "string" && /^[\[\{"].*[\}\]"]$/.test(value.trim())) {
162 try {
163 attr_to_merge[key] = JSON.parse(attr_to_merge[key]);
164 } catch {}
165 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100166 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100167 var api_attr = $.extend(api_attr, attr_to_merge)
168 } else {
169 return false;
170 }
171 }
172 // alert(JSON.stringify(api_attr));
173 // If clicked element #edit_selected has data-item attribute, it is added to "items"
174 if (typeof $(this).data('item') !== 'undefined') {
175 var id = $(this).data('id');
176 if (typeof multi_data[id] == "undefined") {
177 multi_data[id] = [];
178 }
179 multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
180 multi_data[id].push($(this).data('item'));
181 }
182 if (typeof multi_data[id] == "undefined") return;
183 api_items = multi_data[id];
184 for (var i in api_items) {
185 api_items[i] = decodeURIComponent(api_items[i]);
186 }
187 // alert(JSON.stringify(api_attr));
188 if (Object.keys(api_items).length !== 0) {
189 if (is_active($(this))) { return false; }
190 $.ajax({
191 type: "POST",
192 dataType: "json",
193 data: {
194 "items": JSON.stringify(api_items),
195 "attr": JSON.stringify(api_attr),
196 "csrf_token": csrf_token
197 },
198 url: '/api/v1/' + api_url,
199 jsonp: false,
200 complete: function(data) {
201 var response = (data.responseText);
202 if (typeof response !== 'undefined' && response.length !== 0) {
203 response_obj = JSON.parse(response);
204 }
205 if (api_reload_window === true) {
206 if (api_reload_location != '#') {
207 window.location.replace(api_reload_location)
208 } else {
209 window.location = window.location.href.split("#")[0];
210 }
211 }
212 }
213 });
214 }
215 });
216
217 // General API add actions
218 $(document).on('click', "[data-action='add_item']", function(e) {
219 e.preventDefault();
220 var id = $(this).data('id');
221 var api_url = $(this).data('api-url');
222 var api_attr = $(this).data('api-attr');
223 if (typeof $(this).data('api-reload-window') !== 'undefined') {
224 api_reload_window = $(this).data('api-reload-window');
225 } else {
226 api_reload_window = true;
227 }
228 // If clicked button is in a form with the same data-id as the button,
229 // we merge all input fields by {"name":"value"} into api-attr
230 if ($(this).closest("form").data('id') == id) {
231 var invalid = false;
232 $(this).closest("form").find('select, textarea, input').each(function() {
233 if ($(this).prop('required')) {
234 if (!$(this).val() && $(this).prop('disabled') === false) {
235 invalid = true;
236 if ($(this).is("select")) {
237 $(this).selectpicker('setStyle', 'btn-input-missing', 'add');
238 }
239 $(this).addClass('inputMissingAttr');
240 } else {
241 if ($(this).is("select")) {
242 $(this).selectpicker('setStyle', 'btn-input-missing', 'remove');
243 }
244 $(this).removeClass('inputMissingAttr');
245 }
246 }
247 if ($(this).attr("type") == 'email') {
248 var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
249 if (!emailReg.test($(this).val())) {
250 invalid = true;
251 $(this).addClass('inputMissingAttr');
252 } else {
253 $(this).removeClass('inputMissingAttr');
254 }
255 }
256 if ($(this).attr("max")) {
257 if (Number($(this).val()) > Number($(this).attr("max"))) {
258 invalid = true;
259 $(this).addClass('inputMissingAttr');
260 } else {
261 if ($(this).attr("min")) {
262 if (Number($(this).val()) < Number($(this).attr("min"))) {
263 invalid = true;
264 $(this).addClass('inputMissingAttr');
265 } else {
266 $(this).removeClass('inputMissingAttr');
267 }
268 }
269 }
270 }
271 });
272 if (!invalid) {
273 var attr_to_merge = $(this).closest("form").serializeObject();
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100274 // parse possible JSON Strings
275 for (var [key, value] of Object.entries(attr_to_merge)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200276 if (typeof value === "string" && /^[\[\{"].*[\}\]"]$/.test(value.trim())) {
277 try {
278 attr_to_merge[key] = JSON.parse(attr_to_merge[key]);
279 } catch {}
280 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100281 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100282 var api_attr = $.extend(api_attr, attr_to_merge)
283 } else {
284 return false;
285 }
286 }
287 if (is_active($(this))) { return false; }
288 // alert(JSON.stringify(api_attr));
289 $.ajax({
290 type: "POST",
291 dataType: "json",
292 data: {
293 "attr": JSON.stringify(api_attr),
294 "csrf_token": csrf_token
295 },
296 url: '/api/v1/' + api_url,
297 jsonp: false,
298 complete: function(data) {
299 var response = (data.responseText);
300 if (typeof response !== 'undefined' && response.length !== 0) {
301 response_obj = JSON.parse(response);
302 unset = true;
303 $.each(response_obj, function(i, v) {
304 if (v.type == "danger") {
305 unset = false;
306 }
307 });
308 if (unset === true) {
309 unset = null;
310 // Keep form data for sync jobs
311 if (id != "add_syncjob") {
312 $('form').formcache('clear');
313 $('form').formcache('destroy');
314 var i = localStorage.length;
315 while(i--) {
316 var key = localStorage.key(i);
317 if(/formcache/.test(key)) {
318 localStorage.removeItem(key);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200319 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100320 }
321 }
322 }
323 else {
324 var add_modal = $('.modal.in').attr('id');
325 localStorage.setItem("add_modal", add_modal);
326 }
327 }
328 if (api_reload_window === true) {
329 window.location = window.location.href.split("#")[0];
330 }
331 }
332 });
333 });
334
335 // General API delete actions
336 $(document).on('click', "[data-action='delete_selected']", function(e) {
337 e.preventDefault();
338 var id = $(this).data('id');
339 // If clicked element #delete_selected has data-item attribute, it is added to "items"
340 if (typeof $(this).data('item') !== 'undefined') {
341 var id = $(this).data('id');
342 if (typeof multi_data[id] == "undefined") {
343 multi_data[id] = [];
344 }
345 multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
346 multi_data[id].push($(this).data('item'));
347 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100348
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100349 if (typeof $(this).data('text') !== 'undefined') {
350 $("#DeleteText").empty();
351 $("#DeleteText").text($(this).data('text'));
352 }
353 if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
354 data_array = multi_data[id];
355 api_url = $(this).data('api-url');
356 $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
357 $("#ItemsToDelete").empty();
358 for (var i in data_array) {
359 data_array[i] = decodeURIComponent(data_array[i]);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100360 $("#ItemsToDelete").append("<li>" + escapeHtml(data_array[i]) + "</li>");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100361 }
362 })
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100363 $('#ConfirmDeleteModal').modal('show')
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100364 .one('click', '#IsConfirmed', function(e) {
365 if (is_active($('#IsConfirmed'))) { return false; }
366 $.ajax({
367 type: "POST",
368 dataType: "json",
369 cache: false,
370 data: {
371 "items": JSON.stringify(data_array),
372 "csrf_token": csrf_token
373 },
374 url: '/api/v1/' + api_url,
375 jsonp: false,
376 complete: function(data) {
377 window.location = window.location.href.split("#")[0];
378 }
379 });
380 })
381 .one('click', '#isCanceled', function(e) {
382 // Remove event handler to allow to close modal and restart dialog without multiple submits
383 $('#ConfirmDeleteModal').off();
384 $('#ConfirmDeleteModal').modal('hide');
385 });
386 });
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100387
388 // toggle jquery datatables child rows
389 $('button[data-datatables-expand], a[data-datatables-expand]').on('click', function (e) {
390 e.preventDefault();
391 var tableId = e.target.getAttribute("data-datatables-expand");
392 var table = $("#" + tableId).DataTable();
393 table.rows(':not(.parent)').nodes().to$().find('td:first-child').trigger('click');
394 });
395 $('button[data-datatables-collapse], a[data-datatables-collapse]').on('click', function (e) {
396 e.preventDefault();
397 var tableId = e.target.getAttribute("data-datatables-collapse");
398 var table = $("#" + tableId).DataTable();
399 table.rows('.parent').nodes().to$().find('td:first-child').trigger('click');
400 });
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100401});