api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. $(document).ready(function() {
  2. function is_active(elem) {
  3. if ($(elem).data('submitted') == '1') {
  4. return true;
  5. } else {
  6. var parent_btn_grp = $(elem).parentsUntil(".btn-group").parent();
  7. if (parent_btn_grp.hasClass('btn-group')) {
  8. parent_btn_grp.replaceWith('<button class="btn btn-default btn-sm" disabled>' + lang_footer.loading + '</a>');
  9. }
  10. $(elem).text(lang_footer.loading);
  11. $(elem).attr('data-submitted', '1');
  12. function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
  13. $(document).on("keydown", disableF5);
  14. return false;
  15. }
  16. }
  17. $.fn.serializeObject = function() {
  18. var o = {};
  19. var a = this.serializeArray();
  20. $.each(a, function() {
  21. if (o[this.name]) {
  22. if (!o[this.name].push) {
  23. o[this.name] = [o[this.name]];
  24. }
  25. o[this.name].push(this.value || '');
  26. } else {
  27. o[this.name] = this.value || '';
  28. }
  29. });
  30. return o;
  31. };
  32. // Collect values of input fields with name "multi_select" and same data-id to js array multi_data[data-id]
  33. var multi_data = [];
  34. $(document).on('change', 'input[name=multi_select]:checkbox', function() {
  35. if ($(this).is(':checked') && $(this).data('id')) {
  36. var id = $(this).data('id');
  37. if (typeof multi_data[id] == "undefined") {
  38. multi_data[id] = [];
  39. }
  40. multi_data[id].push($(this).val());
  41. }
  42. else {
  43. var id = $(this).data('id');
  44. multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
  45. }
  46. });
  47. // Select checkbox by click on parent tr
  48. $(document).on('click', 'tbody>tr', function(e) {
  49. if(e.target.tagName.toLowerCase() === 'button') {
  50. e.stopPropagation();
  51. }
  52. else if(e.target.tagName.toLowerCase() === 'a') {
  53. e.stopPropagation();
  54. }
  55. else if (e.target.type == "checkbox") {
  56. e.stopPropagation();
  57. }
  58. else {
  59. var checkbox = $(this).find(':checkbox');
  60. checkbox.trigger('click');
  61. }
  62. });
  63. // Select or deselect all checkboxes with same data-id
  64. $(document).on('click', '#toggle_multi_select_all', function(e) {
  65. e.preventDefault();
  66. id = $(this).data("id");
  67. var all_checkboxes = $("input[data-id=" + id + "]:enabled");
  68. all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
  69. });
  70. // General API edit actions
  71. $(document).on('click', "[data-action='edit_selected']", function(e) {
  72. e.preventDefault();
  73. var id = $(this).data('id');
  74. var api_url = $(this).data('api-url');
  75. var api_attr = $(this).data('api-attr');
  76. if (typeof $(this).data('api-reload-window') !== 'undefined') {
  77. api_reload_window = $(this).data('api-reload-window');
  78. } else {
  79. api_reload_window = true;
  80. }
  81. if (typeof $(this).data('api-reload-location') !== 'undefined') {
  82. api_reload_location = $(this).data('api-reload-location');
  83. } else {
  84. api_reload_location = '#';
  85. }
  86. // If clicked element #edit_selected is in a form with the same data-id as the button,
  87. // we merge all input fields by {"name":"value"} into api-attr
  88. if ($(this).closest("form").data('id') == id) {
  89. var invalid = false;
  90. $(this).closest("form").find('select, textarea, input').each(function() {
  91. if ($(this).prop('required')) {
  92. if (!$(this).val() && $(this).prop('disabled') === false) {
  93. invalid = true;
  94. $(this).addClass('inputMissingAttr');
  95. } else {
  96. $(this).removeClass('inputMissingAttr');
  97. }
  98. }
  99. if ($(this).attr("max")) {
  100. if (Number($(this).val()) > Number($(this).attr("max"))) {
  101. invalid = true;
  102. $(this).addClass('inputMissingAttr');
  103. } else {
  104. if ($(this).attr("min")) {
  105. if (Number($(this).val()) < Number($(this).attr("min"))) {
  106. invalid = true;
  107. $(this).addClass('inputMissingAttr');
  108. } else {
  109. $(this).removeClass('inputMissingAttr');
  110. }
  111. }
  112. }
  113. }
  114. });
  115. if (!invalid) {
  116. var attr_to_merge = $(this).closest("form").serializeObject();
  117. var api_attr = $.extend(api_attr, attr_to_merge)
  118. } else {
  119. return false;
  120. }
  121. }
  122. // alert(JSON.stringify(api_attr));
  123. // If clicked element #edit_selected has data-item attribute, it is added to "items"
  124. if (typeof $(this).data('item') !== 'undefined') {
  125. var id = $(this).data('id');
  126. if (typeof multi_data[id] == "undefined") {
  127. multi_data[id] = [];
  128. }
  129. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  130. multi_data[id].push($(this).data('item'));
  131. }
  132. if (typeof multi_data[id] == "undefined") return;
  133. api_items = multi_data[id];
  134. for (var i in api_items) {
  135. api_items[i] = decodeURIComponent(api_items[i]);
  136. }
  137. // alert(JSON.stringify(api_attr));
  138. if (Object.keys(api_items).length !== 0) {
  139. if (is_active($(this))) { return false; }
  140. $.ajax({
  141. type: "POST",
  142. dataType: "json",
  143. data: {
  144. "items": JSON.stringify(api_items),
  145. "attr": JSON.stringify(api_attr),
  146. "csrf_token": csrf_token
  147. },
  148. url: '/api/v1/' + api_url,
  149. jsonp: false,
  150. complete: function(data) {
  151. var response = (data.responseText);
  152. if (typeof response !== 'undefined' && response.length !== 0) {
  153. response_obj = JSON.parse(response);
  154. }
  155. if (api_reload_window === true) {
  156. if (api_reload_location != '#') {
  157. window.location.replace(api_reload_location)
  158. } else {
  159. window.location = window.location.href.split("#")[0];
  160. }
  161. }
  162. }
  163. });
  164. }
  165. });
  166. // General API add actions
  167. $(document).on('click', "[data-action='add_item']", function(e) {
  168. e.preventDefault();
  169. var id = $(this).data('id');
  170. var api_url = $(this).data('api-url');
  171. var api_attr = $(this).data('api-attr');
  172. if (typeof $(this).data('api-reload-window') !== 'undefined') {
  173. api_reload_window = $(this).data('api-reload-window');
  174. } else {
  175. api_reload_window = true;
  176. }
  177. // If clicked button is in a form with the same data-id as the button,
  178. // we merge all input fields by {"name":"value"} into api-attr
  179. if ($(this).closest("form").data('id') == id) {
  180. var invalid = false;
  181. $(this).closest("form").find('select, textarea, input').each(function() {
  182. if ($(this).prop('required')) {
  183. if (!$(this).val() && $(this).prop('disabled') === false) {
  184. invalid = true;
  185. $(this).addClass('inputMissingAttr');
  186. } else {
  187. $(this).removeClass('inputMissingAttr');
  188. }
  189. }
  190. if ($(this).attr("max")) {
  191. if (Number($(this).val()) > Number($(this).attr("max"))) {
  192. invalid = true;
  193. $(this).addClass('inputMissingAttr');
  194. } else {
  195. if ($(this).attr("min")) {
  196. if (Number($(this).val()) < Number($(this).attr("min"))) {
  197. invalid = true;
  198. $(this).addClass('inputMissingAttr');
  199. } else {
  200. $(this).removeClass('inputMissingAttr');
  201. }
  202. }
  203. }
  204. }
  205. });
  206. if (!invalid) {
  207. var attr_to_merge = $(this).closest("form").serializeObject();
  208. var api_attr = $.extend(api_attr, attr_to_merge)
  209. } else {
  210. return false;
  211. }
  212. }
  213. if (is_active($(this))) { return false; }
  214. // alert(JSON.stringify(api_attr));
  215. $.ajax({
  216. type: "POST",
  217. dataType: "json",
  218. data: {
  219. "attr": JSON.stringify(api_attr),
  220. "csrf_token": csrf_token
  221. },
  222. url: '/api/v1/' + api_url,
  223. jsonp: false,
  224. complete: function(data) {
  225. var response = (data.responseText);
  226. if (typeof response !== 'undefined' && response.length !== 0) {
  227. response_obj = JSON.parse(response);
  228. unset = true;
  229. $.each(response_obj, function(i, v) {
  230. if (v.type == "danger") {
  231. unset = false;
  232. }
  233. });
  234. if (unset === true) {
  235. unset = null;
  236. $('form').formcache('clear');
  237. $('form').formcache('destroy');
  238. var i = localStorage.length;
  239. while(i--) {
  240. var key = localStorage.key(i);
  241. if(/formcache/.test(key)) {
  242. localStorage.removeItem(key);
  243. }
  244. }
  245. }
  246. else {
  247. var add_modal = $('.modal.in').attr('id');
  248. localStorage.setItem("add_modal", add_modal);
  249. }
  250. }
  251. if (api_reload_window === true) {
  252. window.location = window.location.href.split("#")[0];
  253. }
  254. }
  255. });
  256. });
  257. // General API delete actions
  258. $(document).on('click', "[data-action='delete_selected']", function(e) {
  259. e.preventDefault();
  260. var id = $(this).data('id');
  261. // If clicked element #delete_selected has data-item attribute, it is added to "items"
  262. if (typeof $(this).data('item') !== 'undefined') {
  263. var id = $(this).data('id');
  264. if (typeof multi_data[id] == "undefined") {
  265. multi_data[id] = [];
  266. }
  267. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  268. multi_data[id].push($(this).data('item'));
  269. }
  270. if (typeof $(this).data('text') !== 'undefined') {
  271. $("#DeleteText").empty();
  272. $("#DeleteText").text($(this).data('text'));
  273. }
  274. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  275. data_array = multi_data[id];
  276. api_url = $(this).data('api-url');
  277. $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
  278. $("#ItemsToDelete").empty();
  279. for (var i in data_array) {
  280. data_array[i] = decodeURIComponent(data_array[i]);
  281. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  282. }
  283. })
  284. $('#ConfirmDeleteModal').modal({
  285. backdrop: 'static',
  286. keyboard: false
  287. })
  288. .one('click', '#IsConfirmed', function(e) {
  289. if (is_active($('#IsConfirmed'))) { return false; }
  290. $.ajax({
  291. type: "POST",
  292. dataType: "json",
  293. cache: false,
  294. data: {
  295. "items": JSON.stringify(data_array),
  296. "csrf_token": csrf_token
  297. },
  298. url: '/api/v1/' + api_url,
  299. jsonp: false,
  300. complete: function(data) {
  301. window.location = window.location.href.split("#")[0];
  302. }
  303. });
  304. })
  305. .one('click', '#isCanceled', function(e) {
  306. // Remove event handler to allow to close modal and restart dialog without multiple submits
  307. $('#ConfirmDeleteModal').off();
  308. $('#ConfirmDeleteModal').modal('hide');
  309. });
  310. });
  311. });