api.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. $(document).ready(function() {
  2. // Collect values of input fields with name "multi_select" an same data-id to js array multi_data[data-id]
  3. var multi_data = [];
  4. $(document).on('change', 'input[name=multi_select]:checkbox', function() {
  5. if ($(this).is(':checked') && $(this).data('id')) {
  6. var id = $(this).data('id');
  7. if (typeof multi_data[id] == "undefined") {
  8. multi_data[id] = [];
  9. }
  10. multi_data[id].push($(this).val());
  11. }
  12. else {
  13. var id = $(this).data('id');
  14. multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
  15. }
  16. });
  17. // Select checkbox by click on parent tr
  18. $(document).on('click', 'tbody>tr', function(e) {
  19. if (e.target.type == "checkbox") {
  20. e.stopPropagation();
  21. } else {
  22. var checkbox = $(this).find(':checkbox');
  23. checkbox.trigger('click');
  24. }
  25. });
  26. // Select or deselect all checkboxes with same data-id
  27. $(document).on('click', '#toggle_multi_select_all', function(e) {
  28. e.preventDefault();
  29. id = $(this).data("id");
  30. multi_data[id] = [];
  31. var all_checkboxes = $("input[data-id=" + id + "]:enabled");
  32. all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
  33. });
  34. // General API edit actions
  35. $(document).on('click', '#edit_selected', function(e) {
  36. e.preventDefault();
  37. var id = $(this).data('id');
  38. var api_url = $(this).data('api-url');
  39. var api_attr = $(this).data('api-attr');
  40. // If clicked element #edit_selected is in a form with the same data-id as the button,
  41. // we merge all input fields by {"name":"value"} into api-attr
  42. if ($(this).closest("form").data('id') == id) {
  43. var attr_to_merge = {};
  44. $.each($(this).closest("form").serializeArray(), function(i, field) {
  45. attr_to_merge[field.name] = field.value;
  46. });
  47. var api_attr = $.extend(api_attr, attr_to_merge)
  48. }
  49. // If clicked element #edit_selected has data-item attribute, it is added to "items"
  50. if (typeof $(this).data('item') !== 'undefined') {
  51. var id = $(this).data('id');
  52. if (typeof multi_data[id] == "undefined") {
  53. multi_data[id] = [];
  54. }
  55. multi_data[id].push($(this).data('item'));
  56. }
  57. if (typeof multi_data[id] == "undefined") return;
  58. api_items = multi_data[id];
  59. if (Object.keys(api_items).length !== 0) {
  60. $.ajax({
  61. type: "POST",
  62. dataType: "json",
  63. data: { "items": JSON.stringify(api_items), "attr": JSON.stringify(api_attr), "csrf_token": csrf_token },
  64. url: '/api/v1/' + api_url,
  65. jsonp: false,
  66. complete: function (data) {
  67. // var reponse = (JSON.parse(data.responseText));
  68. // console.log(reponse.type);
  69. // console.log(reponse.msg);
  70. window.location = window.location.href.split("#")[0];
  71. }
  72. });
  73. }
  74. });
  75. // General API add actions
  76. $(document).on('click', '#add_item', function(e) {
  77. e.preventDefault();
  78. var id = $(this).data('id');
  79. var api_url = $(this).data('api-url');
  80. var api_attr = $(this).data('api-attr');
  81. // If clicked button is in a form with the same data-id as the button,
  82. // we merge all input fields by {"name":"value"} into api-attr
  83. if ($(this).closest("form").data('id') == id) {
  84. var attr_to_merge = {};
  85. $.each($(this).closest("form").serializeArray(), function(i, field) {
  86. attr_to_merge[field.name] = field.value;
  87. });
  88. var api_attr = $.extend(api_attr, attr_to_merge)
  89. }
  90. $.ajax({
  91. type: "POST",
  92. dataType: "json",
  93. data: { "attr": JSON.stringify(api_attr), "csrf_token": csrf_token },
  94. url: '/api/v1/' + api_url,
  95. jsonp: false,
  96. complete: function (data) {
  97. // var reponse = (JSON.parse(data.responseText));
  98. // console.log(reponse.type);
  99. // console.log(reponse.msg);
  100. window.location = window.location.href.split("#")[0];
  101. }
  102. });
  103. });
  104. // General API delete actions
  105. $(document).on('click', '#delete_selected', function(e) {
  106. e.preventDefault();
  107. var id = $(this).data('id');
  108. // If clicked element #delete_selected has data-item attribute, it is added to "items"
  109. if (typeof $(this).data('item') !== 'undefined') {
  110. var id = $(this).data('id');
  111. if (typeof multi_data[id] == "undefined") {
  112. multi_data[id] = [];
  113. }
  114. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]),1);
  115. multi_data[id].push($(this).data('item'));
  116. }
  117. if (typeof $(this).data('text') !== 'undefined') {
  118. $("#DeleteText").empty();
  119. $("#DeleteText").text($(this).data('text'));
  120. }
  121. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  122. data_array = multi_data[id];
  123. api_url = $(this).data('api-url');
  124. $(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
  125. $("#ItemsToDelete").empty();
  126. for (var i in data_array) {
  127. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  128. }
  129. })
  130. $('#ConfirmDeleteModal').modal({
  131. backdrop: 'static',
  132. keyboard: false
  133. })
  134. .one('click', '#IsConfirmed', function(e) {
  135. $.ajax({
  136. type: "POST",
  137. dataType: "json",
  138. data: { "items": JSON.stringify(data_array), "csrf_token": csrf_token },
  139. url: '/api/v1/' + api_url,
  140. jsonp: false,
  141. complete: function (data) {
  142. window.location = window.location.href.split("#")[0];
  143. }
  144. });
  145. })
  146. .one('click', '#isCanceled', function(e) {
  147. $('#ConfirmDeleteModal').modal('hide');
  148. });;
  149. });
  150. });