dabory.data-linker.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (function($) {
  2. $.fn.dataLinker = function(options) {
  3. }
  4. $.fn.dataLinker.appBase64 = null;
  5. $.fn.dataLinker.daboryApp = function (url, request, callback, async = true) {
  6. getParameter()
  7. $.ajax({
  8. url: `${$.fn.dataLinker.serverUrl}/dabory-app/${url}`,
  9. data: JSON.stringify(request),
  10. beforeSend: function (xhr) {
  11. xhr.setRequestHeader('Api23Key', $.fn.dataLinker.api23Key);
  12. },
  13. method: 'POST',
  14. dataType: 'json',
  15. async: async,
  16. })
  17. .done(function (json) {
  18. callback(json)
  19. })
  20. .fail(function(json, textStatus, errorThrown) {
  21. callback(json);
  22. iziToast.error({ title: 'Error', message: 'Dabory App API Error' });
  23. });
  24. }
  25. $.fn.dataLinker.api23Js = function (url, request, callback, async = true) {
  26. getParameter()
  27. $.ajax({
  28. url: `${$.fn.dataLinker.serverUrl}/dabory-app/api23-js`,
  29. data: JSON.stringify(request),
  30. beforeSend: function (xhr) {
  31. xhr.setRequestHeader('Url', url);
  32. xhr.setRequestHeader('Api23Key', $.fn.dataLinker.api23Key);
  33. },
  34. method: 'POST',
  35. dataType: 'json',
  36. async: async,
  37. })
  38. .done(function (json) {
  39. callback(json)
  40. })
  41. .fail(function(json, textStatus, errorThrown) {
  42. callback(json);
  43. iziToast.error({ title: 'Error', message: 'Dabory App api23-js API Error' });
  44. });
  45. }
  46. $.fn.dataLinker.localApi = function (url, request, callback, async = true) {
  47. getParameter()
  48. $.ajaxSetup({
  49. headers: {
  50. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  51. }
  52. });
  53. $.ajax({
  54. url: '/ajax/get-data',
  55. data: {
  56. url: url,
  57. data: JSON.stringify(request),
  58. encode_status: true,
  59. },
  60. method: 'POST',
  61. dataType: 'json',
  62. async: async,
  63. })
  64. .done(function (json) {
  65. callback(json)
  66. })
  67. .fail(function(json, textStatus, errorThrown) {
  68. callback(json);
  69. iziToast.error({ title: 'Error', message: 'Dabory App api23-js API Error' });
  70. });
  71. }
  72. function getParameter() {
  73. if ($.fn.dataLinker.api23Key && $.fn.dataLinker.serverUrl) {
  74. return
  75. }
  76. let libFileName = '/dabory/js/widget.js',
  77. scripts = document.getElementsByTagName('script'),
  78. i, j, src, parts, basePath, options = {};
  79. for (i = 0; i < scripts.length; i++) {
  80. src = scripts[i].src;
  81. if (src.indexOf(libFileName) !== -1) {
  82. parts = src.split('?');
  83. basePath = parts[0].replace(libFileName, '');
  84. if (parts[1]) {
  85. const opt = parts[1].split('&');
  86. for (j = opt.length-1; j >= 0; --j) {
  87. const pair = opt[j].split(/=(.*)/s);
  88. options[pair[0]] = pair[1];
  89. }
  90. }
  91. break;
  92. }
  93. }
  94. $.fn.dataLinker.api23Key = options['api23Key']
  95. $.fn.dataLinker.serverUrl = options['serverUrl']
  96. }
  97. }(jQuery));