012-api.js 14 KB

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