012-api.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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("type") == 'email') {
  100. var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  101. if (!emailReg.test($(this).val())) {
  102. invalid = true;
  103. $(this).addClass('inputMissingAttr');
  104. } else {
  105. $(this).removeClass('inputMissingAttr');
  106. }
  107. }
  108. if ($(this).attr("max")) {
  109. if (Number($(this).val()) > Number($(this).attr("max"))) {
  110. invalid = true;
  111. $(this).addClass('inputMissingAttr');
  112. } else {
  113. if ($(this).attr("min")) {
  114. if (Number($(this).val()) < Number($(this).attr("min"))) {
  115. invalid = true;
  116. $(this).addClass('inputMissingAttr');
  117. } else {
  118. $(this).removeClass('inputMissingAttr');
  119. }
  120. }
  121. }
  122. }
  123. });
  124. if (!invalid) {
  125. var attr_to_merge = $(this).closest("form").serializeObject();
  126. var api_attr = $.extend(api_attr, attr_to_merge)
  127. } else {
  128. return false;
  129. }
  130. }
  131. // alert(JSON.stringify(api_attr));
  132. // If clicked element #edit_selected has data-item attribute, it is added to "items"
  133. if (typeof $(this).data('item') !== 'undefined') {
  134. var id = $(this).data('id');
  135. if (typeof multi_data[id] == "undefined") {
  136. multi_data[id] = [];
  137. }
  138. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  139. multi_data[id].push($(this).data('item'));
  140. }
  141. if (typeof multi_data[id] == "undefined") return;
  142. api_items = multi_data[id];
  143. for (var i in api_items) {
  144. api_items[i] = decodeURIComponent(api_items[i]);
  145. }
  146. // alert(JSON.stringify(api_attr));
  147. if (Object.keys(api_items).length !== 0) {
  148. if (is_active($(this))) { return false; }
  149. $.ajax({
  150. type: "POST",
  151. dataType: "json",
  152. data: {
  153. "items": JSON.stringify(api_items),
  154. "attr": JSON.stringify(api_attr),
  155. "csrf_token": csrf_token
  156. },
  157. url: '/api/v1/' + api_url,
  158. jsonp: false,
  159. complete: function(data) {
  160. var response = (data.responseText);
  161. if (typeof response !== 'undefined' && response.length !== 0) {
  162. response_obj = JSON.parse(response);
  163. }
  164. if (api_reload_window === true) {
  165. if (api_reload_location != '#') {
  166. window.location.replace(api_reload_location)
  167. } else {
  168. window.location = window.location.href.split("#")[0];
  169. }
  170. }
  171. }
  172. });
  173. }
  174. });
  175. // General API add actions
  176. $(document).on('click', "[data-action='add_item']", function(e) {
  177. e.preventDefault();
  178. var id = $(this).data('id');
  179. var api_url = $(this).data('api-url');
  180. var api_attr = $(this).data('api-attr');
  181. if (typeof $(this).data('api-reload-window') !== 'undefined') {
  182. api_reload_window = $(this).data('api-reload-window');
  183. } else {
  184. api_reload_window = true;
  185. }
  186. // If clicked button is in a form with the same data-id as the button,
  187. // we merge all input fields by {"name":"value"} into api-attr
  188. if ($(this).closest("form").data('id') == id) {
  189. var invalid = false;
  190. $(this).closest("form").find('select, textarea, input').each(function() {
  191. if ($(this).prop('required')) {
  192. if (!$(this).val() && $(this).prop('disabled') === false) {
  193. invalid = true;
  194. $(this).addClass('inputMissingAttr');
  195. } else {
  196. $(this).removeClass('inputMissingAttr');
  197. }
  198. }
  199. if ($(this).attr("type") == 'email') {
  200. var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  201. if (!emailReg.test($(this).val())) {
  202. invalid = true;
  203. $(this).addClass('inputMissingAttr');
  204. } else {
  205. $(this).removeClass('inputMissingAttr');
  206. }
  207. }
  208. if ($(this).attr("max")) {
  209. if (Number($(this).val()) > Number($(this).attr("max"))) {
  210. invalid = true;
  211. $(this).addClass('inputMissingAttr');
  212. } else {
  213. if ($(this).attr("min")) {
  214. if (Number($(this).val()) < Number($(this).attr("min"))) {
  215. invalid = true;
  216. $(this).addClass('inputMissingAttr');
  217. } else {
  218. $(this).removeClass('inputMissingAttr');
  219. }
  220. }
  221. }
  222. }
  223. });
  224. if (!invalid) {
  225. var attr_to_merge = $(this).closest("form").serializeObject();
  226. var api_attr = $.extend(api_attr, attr_to_merge)
  227. } else {
  228. return false;
  229. }
  230. }
  231. if (is_active($(this))) { return false; }
  232. // alert(JSON.stringify(api_attr));
  233. $.ajax({
  234. type: "POST",
  235. dataType: "json",
  236. data: {
  237. "attr": JSON.stringify(api_attr),
  238. "csrf_token": csrf_token
  239. },
  240. url: '/api/v1/' + api_url,
  241. jsonp: false,
  242. complete: function(data) {
  243. var response = (data.responseText);
  244. if (typeof response !== 'undefined' && response.length !== 0) {
  245. response_obj = JSON.parse(response);
  246. unset = true;
  247. $.each(response_obj, function(i, v) {
  248. if (v.type == "danger") {
  249. unset = false;
  250. }
  251. });
  252. if (unset === true) {
  253. unset = null;
  254. // Keep form data for sync jobs
  255. if (id != "add_syncjob") {
  256. $('form').formcache('clear');
  257. $('form').formcache('destroy');
  258. var i = localStorage.length;
  259. while(i--) {
  260. var key = localStorage.key(i);
  261. if(/formcache/.test(key)) {
  262. localStorage.removeItem(key);
  263. }
  264. }
  265. }
  266. }
  267. else {
  268. var add_modal = $('.modal.in').attr('id');
  269. localStorage.setItem("add_modal", add_modal);
  270. }
  271. }
  272. if (api_reload_window === true) {
  273. window.location = window.location.href.split("#")[0];
  274. }
  275. }
  276. });
  277. });
  278. // General API delete actions
  279. $(document).on('click', "[data-action='delete_selected']", function(e) {
  280. e.preventDefault();
  281. var id = $(this).data('id');
  282. // If clicked element #delete_selected has data-item attribute, it is added to "items"
  283. if (typeof $(this).data('item') !== 'undefined') {
  284. var id = $(this).data('id');
  285. if (typeof multi_data[id] == "undefined") {
  286. multi_data[id] = [];
  287. }
  288. multi_data[id].splice($.inArray($(this).data('item'), multi_data[id]), 1);
  289. multi_data[id].push($(this).data('item'));
  290. }
  291. if (typeof $(this).data('text') !== 'undefined') {
  292. $("#DeleteText").empty();
  293. $("#DeleteText").text($(this).data('text'));
  294. }
  295. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  296. data_array = multi_data[id];
  297. api_url = $(this).data('api-url');
  298. $(document).on('show.bs.modal', '#ConfirmDeleteModal', function() {
  299. $("#ItemsToDelete").empty();
  300. for (var i in data_array) {
  301. data_array[i] = decodeURIComponent(data_array[i]);
  302. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  303. }
  304. })
  305. $('#ConfirmDeleteModal').modal({
  306. backdrop: 'static',
  307. keyboard: false
  308. })
  309. .one('click', '#IsConfirmed', function(e) {
  310. if (is_active($('#IsConfirmed'))) { return false; }
  311. $.ajax({
  312. type: "POST",
  313. dataType: "json",
  314. cache: false,
  315. data: {
  316. "items": JSON.stringify(data_array),
  317. "csrf_token": csrf_token
  318. },
  319. url: '/api/v1/' + api_url,
  320. jsonp: false,
  321. complete: function(data) {
  322. window.location = window.location.href.split("#")[0];
  323. }
  324. });
  325. })
  326. .one('click', '#isCanceled', function(e) {
  327. // Remove event handler to allow to close modal and restart dialog without multiple submits
  328. $('#ConfirmDeleteModal').off();
  329. $('#ConfirmDeleteModal').modal('hide');
  330. });
  331. });
  332. });