012-api.js 13 KB

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