api.js 8.5 KB

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