api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. $(document).ready(function() {
  2. function is_active(elem) {
  3. if ($(elem).data('submitted') == '1') {
  4. return true;
  5. } else {
  6. var parent_btn_grp = $(elem).parentsUntil(".btn-group").parent();
  7. if (parent_btn_grp.hasClass('btn-group')) {
  8. parent_btn_grp.replaceWith('<button class="btn btn-default btn-sm" disabled>' + lang_footer.loading + '</a>');
  9. }
  10. $(elem).text(lang_footer.loading);
  11. $(elem).attr('data-submitted', '1');
  12. function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
  13. $(document).on("keydown", disableF5);
  14. return false;
  15. }
  16. }
  17. $.fn.serializeObject = function() {
  18. var o = {};
  19. var a = this.serializeArray();
  20. $.each(a, function() {
  21. if (o[this.name]) {
  22. if (!o[this.name].push) {
  23. o[this.name] = [o[this.name]];
  24. }
  25. o[this.name].push(this.value || '');
  26. } else {
  27. o[this.name] = this.value || '';
  28. }
  29. });
  30. return o;
  31. };
  32. // Collect values of input fields with name "multi_select" and same data-id to js array multi_data[data-id]
  33. var multi_data = [];
  34. $(document).on('change', 'input[name=multi_select]:checkbox', function() {
  35. if ($(this).is(':checked') && $(this).data('id')) {
  36. var id = $(this).data('id');
  37. if (typeof multi_data[id] == "undefined") {
  38. multi_data[id] = [];
  39. }
  40. multi_data[id].push($(this).val());
  41. }
  42. else {
  43. var id = $(this).data('id');
  44. multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
  45. }
  46. });
  47. // Select checkbox by click on parent tr
  48. $(document).on('click', 'tbody>tr', function(e) {
  49. if(e.target.tagName.toLowerCase() === 'button') {
  50. e.stopPropagation();
  51. }
  52. else if(e.target.tagName.toLowerCase() === 'a') {
  53. e.stopPropagation();
  54. }
  55. else if (e.target.type == "checkbox") {
  56. e.stopPropagation();
  57. }
  58. else {
  59. var checkbox = $(this).find(':checkbox');
  60. checkbox.trigger('click');
  61. }
  62. });
  63. // Select or deselect all checkboxes with same data-id
  64. $(document).on('click', '#toggle_multi_select_all', function(e) {
  65. e.preventDefault();
  66. id = $(this).data("id");
  67. var all_checkboxes = $("input[data-id=" + id + "]:enabled");
  68. all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
  69. });
  70. // General API edit actions
  71. $(document).on('click', "[data-action='edit_selected']", function(e) {
  72. e.preventDefault();
  73. var id = $(this).data('id');
  74. var api_url = $(this).data('api-url');
  75. var api_attr = $(this).data('api-attr');
  76. if (typeof $(this).data('api-reload-window') !== 'undefined') {
  77. api_reload_window = $(this).data('api-reload-window');
  78. } else {
  79. api_reload_window = true;
  80. }
  81. // If clicked element #edit_selected is in a form with the same data-id as the button,
  82. // we merge all input fields by {"name":"value"} into api-attr
  83. if ($(this).closest("form").data('id') == id) {
  84. var invalid = false;
  85. $(this).closest("form").find('select, textarea, input').each(function() {
  86. if ($(this).prop('required')) {
  87. if (!$(this).val() && $(this).prop('disabled') === false) {
  88. invalid = true;
  89. $(this).addClass('inputMissingAttr');
  90. } else {
  91. $(this).removeClass('inputMissingAttr');
  92. }
  93. }
  94. if ($(this).attr("max")) {
  95. if (Number($(this).val()) > Number($(this).attr("max"))) {
  96. invalid = true;
  97. $(this).addClass('inputMissingAttr');
  98. } else {
  99. if ($(this).attr("min")) {
  100. if (Number($(this).val()) < Number($(this).attr("min"))) {
  101. invalid = true;
  102. $(this).addClass('inputMissingAttr');
  103. } else {
  104. $(this).removeClass('inputMissingAttr');
  105. }
  106. }
  107. }
  108. }
  109. });
  110. if (!invalid) {
  111. var attr_to_merge = $(this).closest("form").serializeObject();
  112. var api_attr = $.extend(api_attr, attr_to_merge)
  113. } else {
  114. return false;
  115. }
  116. }
  117. // alert(JSON.stringify(api_attr));
  118. // If clicked element #edit_selected has data-item attribute, it is added to "items"
  119. if (typeof $(this).data('item') !== 'undefined') {
  120. var id = $(this).data('id');
  121. if (typeof multi_data[id] == "undefined") {
  122. multi_data[id] = [];
  123. }
  124. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  125. multi_data[id].push($(this).data('item'));
  126. }
  127. if (typeof multi_data[id] == "undefined") return;
  128. api_items = multi_data[id];
  129. for (var i in api_items) {
  130. api_items[i] = decodeURIComponent(api_items[i]);
  131. }
  132. // alert(JSON.stringify(api_attr));
  133. if (Object.keys(api_items).length !== 0) {
  134. if (is_active($(this))) { return false; }
  135. $.ajax({
  136. type: "POST",
  137. dataType: "json",
  138. data: {
  139. "items": JSON.stringify(api_items),
  140. "attr": JSON.stringify(api_attr),
  141. "csrf_token": csrf_token
  142. },
  143. url: '/api/v1/' + api_url,
  144. jsonp: false,
  145. complete: function(data) {
  146. var response = (data.responseText);
  147. if (typeof response !== 'undefined' && response.length !== 0) {
  148. response_obj = JSON.parse(response);
  149. }
  150. if (api_reload_window === true) {
  151. window.location = window.location.href.split("#")[0];
  152. }
  153. }
  154. });
  155. }
  156. });
  157. // General API add actions
  158. $(document).on('click', "[data-action='add_item']", function(e) {
  159. e.preventDefault();
  160. var id = $(this).data('id');
  161. var api_url = $(this).data('api-url');
  162. var api_attr = $(this).data('api-attr');
  163. if (typeof $(this).data('api-reload-window') !== 'undefined') {
  164. api_reload_window = $(this).data('api-reload-window');
  165. } else {
  166. api_reload_window = true;
  167. }
  168. // If clicked button is in a form with the same data-id as the button,
  169. // we merge all input fields by {"name":"value"} into api-attr
  170. if ($(this).closest("form").data('id') == id) {
  171. var invalid = false;
  172. $(this).closest("form").find('select, textarea, input').each(function() {
  173. if ($(this).prop('required')) {
  174. if (!$(this).val() && $(this).prop('disabled') === false) {
  175. invalid = true;
  176. $(this).addClass('inputMissingAttr');
  177. } else {
  178. $(this).removeClass('inputMissingAttr');
  179. }
  180. }
  181. if ($(this).attr("max")) {
  182. if (Number($(this).val()) > Number($(this).attr("max"))) {
  183. alert($(this).attr("max"))
  184. invalid = true;
  185. $(this).addClass('inputMissingAttr');
  186. } else {
  187. if ($(this).attr("min")) {
  188. if (Number($(this).val()) < Number($(this).attr("min"))) {
  189. invalid = true;
  190. $(this).addClass('inputMissingAttr');
  191. } else {
  192. $(this).removeClass('inputMissingAttr');
  193. }
  194. }
  195. }
  196. }
  197. });
  198. if (!invalid) {
  199. var attr_to_merge = $(this).closest("form").serializeObject();
  200. var api_attr = $.extend(api_attr, attr_to_merge)
  201. } else {
  202. return false;
  203. }
  204. }
  205. if (is_active($(this))) { return false; }
  206. // alert(JSON.stringify(api_attr));
  207. $.ajax({
  208. type: "POST",
  209. dataType: "json",
  210. data: {
  211. "attr": JSON.stringify(api_attr),
  212. "csrf_token": csrf_token
  213. },
  214. url: '/api/v1/' + api_url,
  215. jsonp: false,
  216. complete: function(data) {
  217. var response = (data.responseText);
  218. if (typeof response !== 'undefined' && response.length !== 0) {
  219. response_obj = JSON.parse(response);
  220. unset = true;
  221. $.each(response_obj, function(i, v) {
  222. if (v.type == "danger") {
  223. unset = false;
  224. }
  225. });
  226. if (unset === true) {
  227. unset = null;
  228. $('form').formcache('clear');
  229. $('form').formcache('destroy');
  230. var i = localStorage.length;
  231. while(i--) {
  232. var key = localStorage.key(i);
  233. if(/formcache/.test(key)) {
  234. localStorage.removeItem(key);
  235. }
  236. }
  237. }
  238. else {
  239. var add_modal = $('.modal.in').attr('id');
  240. localStorage.setItem("add_modal", add_modal);
  241. }
  242. }
  243. if (api_reload_window === true) {
  244. window.location = window.location.href.split("#")[0];
  245. }
  246. }
  247. });
  248. });
  249. // General API delete actions
  250. $(document).on('click', "[data-action='delete_selected']", function(e) {
  251. e.preventDefault();
  252. var id = $(this).data('id');
  253. // If clicked element #delete_selected has data-item attribute, it is added to "items"
  254. if (typeof $(this).data('item') !== 'undefined') {
  255. var id = $(this).data('id');
  256. if (typeof multi_data[id] == "undefined") {
  257. multi_data[id] = [];
  258. }
  259. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  260. multi_data[id].push($(this).data('item'));
  261. }
  262. if (typeof $(this).data('text') !== 'undefined') {
  263. $("#DeleteText").empty();
  264. $("#DeleteText").text($(this).data('text'));
  265. }
  266. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  267. data_array = multi_data[id];
  268. api_url = $(this).data('api-url');
  269. $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
  270. $("#ItemsToDelete").empty();
  271. for (var i in data_array) {
  272. data_array[i] = decodeURIComponent(data_array[i]);
  273. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  274. }
  275. })
  276. $('#ConfirmDeleteModal').modal({
  277. backdrop: 'static',
  278. keyboard: false
  279. })
  280. .one('click', '#IsConfirmed', function(e) {
  281. if (is_active($('#IsConfirmed'))) { return false; }
  282. $.ajax({
  283. type: "POST",
  284. dataType: "json",
  285. cache: false,
  286. data: {
  287. "items": JSON.stringify(data_array),
  288. "csrf_token": csrf_token
  289. },
  290. url: '/api/v1/' + api_url,
  291. jsonp: false,
  292. complete: function(data) {
  293. window.location = window.location.href.split("#")[0];
  294. }
  295. });
  296. })
  297. .one('click', '#isCanceled', function(e) {
  298. // Remove event handler to allow to close modal and restart dialog without multiple submits
  299. $('#ConfirmDeleteModal').off();
  300. $('#ConfirmDeleteModal').modal('hide');
  301. });
  302. });
  303. });