2
0

api.js 7.3 KB

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