012-api.js 12 KB

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