api.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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" an 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.type == "checkbox") {
  35. e.stopPropagation();
  36. } else {
  37. var checkbox = $(this).find(':checkbox');
  38. checkbox.trigger('click');
  39. }
  40. });
  41. // Select or deselect all checkboxes with same data-id
  42. $(document).on('click', '#toggle_multi_select_all', function(e) {
  43. e.preventDefault();
  44. id = $(this).data("id");
  45. var all_checkboxes = $("input[data-id=" + id + "]:enabled");
  46. all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
  47. });
  48. // General API edit actions
  49. $(document).on('click', '#edit_selected', function(e) {
  50. e.preventDefault();
  51. var id = $(this).data('id');
  52. var api_url = $(this).data('api-url');
  53. var api_attr = $(this).data('api-attr');
  54. // If clicked element #edit_selected is in a form with the same data-id as the button,
  55. // we merge all input fields by {"name":"value"} into api-attr
  56. if ($(this).closest("form").data('id') == id) {
  57. var attr_to_merge = $(this).closest("form").serializeObject();
  58. var api_attr = $.extend(api_attr, attr_to_merge)
  59. }
  60. // If clicked element #edit_selected has data-item attribute, it is added to "items"
  61. if (typeof $(this).data('item') !== 'undefined') {
  62. var id = $(this).data('id');
  63. if (typeof multi_data[id] == "undefined") {
  64. multi_data[id] = [];
  65. }
  66. multi_data[id].push($(this).data('item'));
  67. }
  68. if (typeof multi_data[id] == "undefined") return;
  69. api_items = multi_data[id];
  70. if (Object.keys(api_items).length !== 0) {
  71. $.ajax({
  72. type: "POST",
  73. dataType: "json",
  74. data: {
  75. "items": JSON.stringify(api_items),
  76. "attr": JSON.stringify(api_attr),
  77. "csrf_token": csrf_token
  78. },
  79. url: '/api/v1/' + api_url,
  80. jsonp: false,
  81. complete: function(data) {
  82. // var reponse = (JSON.parse(data.responseText));
  83. // console.log(reponse.type);
  84. // console.log(reponse.msg);
  85. window.location = window.location.href.split("#")[0];
  86. }
  87. });
  88. }
  89. });
  90. // General API add actions
  91. $(document).on('click', '#add_item', function(e) {
  92. e.preventDefault();
  93. var id = $(this).data('id');
  94. var api_url = $(this).data('api-url');
  95. var api_attr = $(this).data('api-attr');
  96. // If clicked button is in a form with the same data-id as the button,
  97. // we merge all input fields by {"name":"value"} into api-attr
  98. if ($(this).closest("form").data('id') == id) {
  99. var req_empty = false;
  100. $(this).closest("form").find('select, textarea, input').each(function() {
  101. if ($(this).prop('required')) {
  102. if (!$(this).val()) {
  103. req_empty = true;
  104. $(this).addClass('inputMissingAttr');
  105. } else {
  106. $(this).removeClass('inputMissingAttr');
  107. }
  108. }
  109. });
  110. if (!req_empty) {
  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. $.ajax({
  118. type: "POST",
  119. dataType: "json",
  120. data: {
  121. "attr": JSON.stringify(api_attr),
  122. "csrf_token": csrf_token
  123. },
  124. url: '/api/v1/' + api_url,
  125. jsonp: false,
  126. complete: function(data) {
  127. // var reponse = (JSON.parse(data.responseText));
  128. // console.log(reponse.type);
  129. // console.log(reponse.msg);
  130. window.location = window.location.href.split("#")[0];
  131. }
  132. });
  133. });
  134. // General API delete actions
  135. $(document).on('click', '#delete_selected', function(e) {
  136. e.preventDefault();
  137. var id = $(this).data('id');
  138. // If clicked element #delete_selected has data-item attribute, it is added to "items"
  139. if (typeof $(this).data('item') !== 'undefined') {
  140. var id = $(this).data('id');
  141. if (typeof multi_data[id] == "undefined") {
  142. multi_data[id] = [];
  143. }
  144. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  145. multi_data[id].push($(this).data('item'));
  146. }
  147. if (typeof $(this).data('text') !== 'undefined') {
  148. $("#DeleteText").empty();
  149. $("#DeleteText").text($(this).data('text'));
  150. }
  151. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  152. data_array = multi_data[id];
  153. api_url = $(this).data('api-url');
  154. $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
  155. $("#ItemsToDelete").empty();
  156. for (var i in data_array) {
  157. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  158. }
  159. })
  160. $('#ConfirmDeleteModal').modal({
  161. backdrop: 'static',
  162. keyboard: false
  163. })
  164. .one('click', '#IsConfirmed', function(e) {
  165. $.ajax({
  166. type: "POST",
  167. dataType: "json",
  168. cache: false,
  169. data: {
  170. "items": JSON.stringify(data_array),
  171. "csrf_token": csrf_token
  172. },
  173. url: '/api/v1/' + api_url,
  174. jsonp: false,
  175. complete: function(data) {
  176. window.location = window.location.href.split("#")[0];
  177. }
  178. });
  179. })
  180. .one('click', '#isCanceled', function(e) {
  181. // Remove event handler to allow to close modal and restart dialog without multiple submits
  182. $('#ConfirmDeleteModal').off();
  183. $('#ConfirmDeleteModal').modal('hide');
  184. });
  185. });
  186. });