012-api.js 13 KB

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