api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. alert($(this).attr("max"))
  193. invalid = true;
  194. $(this).addClass('inputMissingAttr');
  195. } else {
  196. if ($(this).attr("min")) {
  197. if (Number($(this).val()) < Number($(this).attr("min"))) {
  198. invalid = true;
  199. $(this).addClass('inputMissingAttr');
  200. } else {
  201. $(this).removeClass('inputMissingAttr');
  202. }
  203. }
  204. }
  205. }
  206. });
  207. if (!invalid) {
  208. var attr_to_merge = $(this).closest("form").serializeObject();
  209. var api_attr = $.extend(api_attr, attr_to_merge)
  210. } else {
  211. return false;
  212. }
  213. }
  214. if (is_active($(this))) { return false; }
  215. // alert(JSON.stringify(api_attr));
  216. $.ajax({
  217. type: "POST",
  218. dataType: "json",
  219. data: {
  220. "attr": JSON.stringify(api_attr),
  221. "csrf_token": csrf_token
  222. },
  223. url: '/api/v1/' + api_url,
  224. jsonp: false,
  225. complete: function(data) {
  226. var response = (data.responseText);
  227. if (typeof response !== 'undefined' && response.length !== 0) {
  228. response_obj = JSON.parse(response);
  229. unset = true;
  230. $.each(response_obj, function(i, v) {
  231. if (v.type == "danger") {
  232. unset = false;
  233. }
  234. });
  235. if (unset === true) {
  236. unset = null;
  237. $('form').formcache('clear');
  238. $('form').formcache('destroy');
  239. var i = localStorage.length;
  240. while(i--) {
  241. var key = localStorage.key(i);
  242. if(/formcache/.test(key)) {
  243. localStorage.removeItem(key);
  244. }
  245. }
  246. }
  247. else {
  248. var add_modal = $('.modal.in').attr('id');
  249. localStorage.setItem("add_modal", add_modal);
  250. }
  251. }
  252. if (api_reload_window === true) {
  253. window.location = window.location.href.split("#")[0];
  254. }
  255. }
  256. });
  257. });
  258. // General API delete actions
  259. $(document).on('click', "[data-action='delete_selected']", function(e) {
  260. e.preventDefault();
  261. var id = $(this).data('id');
  262. // If clicked element #delete_selected has data-item attribute, it is added to "items"
  263. if (typeof $(this).data('item') !== 'undefined') {
  264. var id = $(this).data('id');
  265. if (typeof multi_data[id] == "undefined") {
  266. multi_data[id] = [];
  267. }
  268. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  269. multi_data[id].push($(this).data('item'));
  270. }
  271. if (typeof $(this).data('text') !== 'undefined') {
  272. $("#DeleteText").empty();
  273. $("#DeleteText").text($(this).data('text'));
  274. }
  275. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  276. data_array = multi_data[id];
  277. api_url = $(this).data('api-url');
  278. $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
  279. $("#ItemsToDelete").empty();
  280. for (var i in data_array) {
  281. data_array[i] = decodeURIComponent(data_array[i]);
  282. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  283. }
  284. })
  285. $('#ConfirmDeleteModal').modal({
  286. backdrop: 'static',
  287. keyboard: false
  288. })
  289. .one('click', '#IsConfirmed', function(e) {
  290. if (is_active($('#IsConfirmed'))) { return false; }
  291. $.ajax({
  292. type: "POST",
  293. dataType: "json",
  294. cache: false,
  295. data: {
  296. "items": JSON.stringify(data_array),
  297. "csrf_token": csrf_token
  298. },
  299. url: '/api/v1/' + api_url,
  300. jsonp: false,
  301. complete: function(data) {
  302. window.location = window.location.href.split("#")[0];
  303. }
  304. });
  305. })
  306. .one('click', '#isCanceled', function(e) {
  307. // Remove event handler to allow to close modal and restart dialog without multiple submits
  308. $('#ConfirmDeleteModal').off();
  309. $('#ConfirmDeleteModal').modal('hide');
  310. });
  311. });
  312. });