blob: d979a9b111a46b080690ceb0e27587cc6aa031d2 [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')) {
14 parent_btn_grp.replaceWith('<button class="btn btn-default btn-sm" disabled>' + lang_footer.loading + '</a>');
15 }
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();
159 var api_attr = $.extend(api_attr, attr_to_merge)
160 } else {
161 return false;
162 }
163 }
164 // alert(JSON.stringify(api_attr));
165 // If clicked element #edit_selected has data-item attribute, it is added to "items"
166 if (typeof $(this).data('item') !== 'undefined') {
167 var id = $(this).data('id');
168 if (typeof multi_data[id] == "undefined") {
169 multi_data[id] = [];
170 }
171 multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
172 multi_data[id].push($(this).data('item'));
173 }
174 if (typeof multi_data[id] == "undefined") return;
175 api_items = multi_data[id];
176 for (var i in api_items) {
177 api_items[i] = decodeURIComponent(api_items[i]);
178 }
179 // alert(JSON.stringify(api_attr));
180 if (Object.keys(api_items).length !== 0) {
181 if (is_active($(this))) { return false; }
182 $.ajax({
183 type: "POST",
184 dataType: "json",
185 data: {
186 "items": JSON.stringify(api_items),
187 "attr": JSON.stringify(api_attr),
188 "csrf_token": csrf_token
189 },
190 url: '/api/v1/' + api_url,
191 jsonp: false,
192 complete: function(data) {
193 var response = (data.responseText);
194 if (typeof response !== 'undefined' && response.length !== 0) {
195 response_obj = JSON.parse(response);
196 }
197 if (api_reload_window === true) {
198 if (api_reload_location != '#') {
199 window.location.replace(api_reload_location)
200 } else {
201 window.location = window.location.href.split("#")[0];
202 }
203 }
204 }
205 });
206 }
207 });
208
209 // General API add actions
210 $(document).on('click', "[data-action='add_item']", function(e) {
211 e.preventDefault();
212 var id = $(this).data('id');
213 var api_url = $(this).data('api-url');
214 var api_attr = $(this).data('api-attr');
215 if (typeof $(this).data('api-reload-window') !== 'undefined') {
216 api_reload_window = $(this).data('api-reload-window');
217 } else {
218 api_reload_window = true;
219 }
220 // If clicked button is in a form with the same data-id as the button,
221 // we merge all input fields by {"name":"value"} into api-attr
222 if ($(this).closest("form").data('id') == id) {
223 var invalid = false;
224 $(this).closest("form").find('select, textarea, input').each(function() {
225 if ($(this).prop('required')) {
226 if (!$(this).val() && $(this).prop('disabled') === false) {
227 invalid = true;
228 if ($(this).is("select")) {
229 $(this).selectpicker('setStyle', 'btn-input-missing', 'add');
230 }
231 $(this).addClass('inputMissingAttr');
232 } else {
233 if ($(this).is("select")) {
234 $(this).selectpicker('setStyle', 'btn-input-missing', 'remove');
235 }
236 $(this).removeClass('inputMissingAttr');
237 }
238 }
239 if ($(this).attr("type") == 'email') {
240 var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
241 if (!emailReg.test($(this).val())) {
242 invalid = true;
243 $(this).addClass('inputMissingAttr');
244 } else {
245 $(this).removeClass('inputMissingAttr');
246 }
247 }
248 if ($(this).attr("max")) {
249 if (Number($(this).val()) > Number($(this).attr("max"))) {
250 invalid = true;
251 $(this).addClass('inputMissingAttr');
252 } else {
253 if ($(this).attr("min")) {
254 if (Number($(this).val()) < Number($(this).attr("min"))) {
255 invalid = true;
256 $(this).addClass('inputMissingAttr');
257 } else {
258 $(this).removeClass('inputMissingAttr');
259 }
260 }
261 }
262 }
263 });
264 if (!invalid) {
265 var attr_to_merge = $(this).closest("form").serializeObject();
266 var api_attr = $.extend(api_attr, attr_to_merge)
267 } else {
268 return false;
269 }
270 }
271 if (is_active($(this))) { return false; }
272 // alert(JSON.stringify(api_attr));
273 $.ajax({
274 type: "POST",
275 dataType: "json",
276 data: {
277 "attr": JSON.stringify(api_attr),
278 "csrf_token": csrf_token
279 },
280 url: '/api/v1/' + api_url,
281 jsonp: false,
282 complete: function(data) {
283 var response = (data.responseText);
284 if (typeof response !== 'undefined' && response.length !== 0) {
285 response_obj = JSON.parse(response);
286 unset = true;
287 $.each(response_obj, function(i, v) {
288 if (v.type == "danger") {
289 unset = false;
290 }
291 });
292 if (unset === true) {
293 unset = null;
294 // Keep form data for sync jobs
295 if (id != "add_syncjob") {
296 $('form').formcache('clear');
297 $('form').formcache('destroy');
298 var i = localStorage.length;
299 while(i--) {
300 var key = localStorage.key(i);
301 if(/formcache/.test(key)) {
302 localStorage.removeItem(key);
303 }
304 }
305 }
306 }
307 else {
308 var add_modal = $('.modal.in').attr('id');
309 localStorage.setItem("add_modal", add_modal);
310 }
311 }
312 if (api_reload_window === true) {
313 window.location = window.location.href.split("#")[0];
314 }
315 }
316 });
317 });
318
319 // General API delete actions
320 $(document).on('click', "[data-action='delete_selected']", function(e) {
321 e.preventDefault();
322 var id = $(this).data('id');
323 // If clicked element #delete_selected has data-item attribute, it is added to "items"
324 if (typeof $(this).data('item') !== 'undefined') {
325 var id = $(this).data('id');
326 if (typeof multi_data[id] == "undefined") {
327 multi_data[id] = [];
328 }
329 multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
330 multi_data[id].push($(this).data('item'));
331 }
332 if (typeof $(this).data('text') !== 'undefined') {
333 $("#DeleteText").empty();
334 $("#DeleteText").text($(this).data('text'));
335 }
336 if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
337 data_array = multi_data[id];
338 api_url = $(this).data('api-url');
339 $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
340 $("#ItemsToDelete").empty();
341 for (var i in data_array) {
342 data_array[i] = decodeURIComponent(data_array[i]);
343 $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
344 }
345 })
346 $('#ConfirmDeleteModal').modal({
347 backdrop: 'static',
348 keyboard: false
349 })
350 .one('click', '#IsConfirmed', function(e) {
351 if (is_active($('#IsConfirmed'))) { return false; }
352 $.ajax({
353 type: "POST",
354 dataType: "json",
355 cache: false,
356 data: {
357 "items": JSON.stringify(data_array),
358 "csrf_token": csrf_token
359 },
360 url: '/api/v1/' + api_url,
361 jsonp: false,
362 complete: function(data) {
363 window.location = window.location.href.split("#")[0];
364 }
365 });
366 })
367 .one('click', '#isCanceled', function(e) {
368 // Remove event handler to allow to close modal and restart dialog without multiple submits
369 $('#ConfirmDeleteModal').off();
370 $('#ConfirmDeleteModal').modal('hide');
371 });
372 });
373});