012-api.js 12 KB

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