api.js 9.1 KB

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