libs.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * ============================== - Async Help
  3. * _ _ _
  4. * /\ | | | | | |
  5. * / \ ___ _ _ _ __ ___ | |__| | ___| |_ __
  6. * / /\ \ / __| | | | '_ \ / __| | __ |/ _ \ | '_ \
  7. * / ____ \\__ \ |_| | | | | (__ | | | | __/ | |_) |
  8. * /_/ \_\___/\__, |_| |_|\___| |_| |_|\___|_| .__/
  9. * __/ | | |
  10. * |___/ |_|
  11. *
  12. */
  13. function asyncRedirect(target, method) {
  14. $('#progress').removeClass('hidden');
  15. let request = fetch(target, {
  16. method: method,
  17. headers: {
  18. 'Content-Type': 'application/x-www-form-urlencoded',
  19. 'X-Requested-With': 'fetch'
  20. }
  21. }).then(function (response) {
  22. return response.text();
  23. }).then(function (data) {
  24. $('body #content').html(data).trigger('reloaded');
  25. history.replaceState({}, '', target);
  26. }).catch(function (error) {
  27. }).finally(function () {
  28. $('#progress').addClass('hidden');
  29. });
  30. }
  31. /*
  32. * Fill a given element with content on a
  33. * remote location
  34. *
  35. */
  36. function asyncFill(target, method, location, callback = function () { }, params = {}, payload = {}) {
  37. fetch(target, {
  38. method: method,
  39. headers: {
  40. 'Content-Type': 'application/x-www-form-urlencoded',
  41. 'X-Requested-With': 'fetch'
  42. }
  43. }).then(function (response) {
  44. return response.text();
  45. }).then(function (data) {
  46. $(location).html(data).trigger('filled');
  47. }).catch(function (error) {
  48. }).finally(function () {
  49. callback(params);
  50. });
  51. }
  52. /**
  53. * Search any type of content on a remote location
  54. *
  55. **/
  56. async function asyncContent(target, method, type, callback = function () { }) {
  57. return fetch(target, {
  58. method: method,
  59. headers: {
  60. 'Content-Type': 'application/x-www-form-urlencoded',
  61. 'X-Requested-With': 'fetch'
  62. }
  63. }).then(function (response) {
  64. var content = "";
  65. if (type == 'text') {
  66. content = response.text();
  67. } else {
  68. content = response.json();
  69. }
  70. return {
  71. "status": response.ok,
  72. "content": content
  73. };
  74. }).then(function (data) {
  75. callback(data.content);
  76. return data;
  77. }).finally(function (content) {
  78. });
  79. }
  80. /*
  81. * ============================== Modal
  82. * _ __ _ _
  83. * | \/ | | | | |
  84. * | \ / | ___ __| | __ _| |
  85. * | |\/| |/ _ \ / _` |/ _` | |
  86. * | | | | (_) | (_| | (_| | |
  87. * |_| |_|\___/ \__,_|\__,_|_|
  88. *
  89. */
  90. var scan;
  91. var modalTarget = {}; // Store field where the modal will place the selected stuff
  92. let modal = new bootstrap.Modal($('#modal'), { keyboard: true });
  93. var modalClose;
  94. $('#modal').on('hidden.bs.modal', function (evt) {
  95. $(this).find('.modal-body').html('');
  96. $(this).find('.modal-title').html('');
  97. if (typeof modalClose === "function") {
  98. modalClose();
  99. }
  100. });
  101. async function openModal(params) {
  102. modalTarget = params.target;
  103. modalClose = params.close;
  104. modal.toggle();
  105. }