api.js 6.7 KB

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