api.js 7.5 KB

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