api.js 5.9 KB

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