Sebuahhobi98

loader slims

Dec 30th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. /**
  2. * Arie Nugraha 2009
  3. * this file need jQuery library
  4. * library to works
  5. *
  6. * AJAX related functions
  7. */
  8.  
  9. jQuery.extend({
  10. ajaxHistory: new Array(),
  11. addAjaxHistory: function(strURL, strElement) {
  12. jQuery.ajaxHistory.unshift({url: strURL, elmt: strElement});
  13. // delete the last element
  14. if (jQuery.ajaxHistory.length > 5) {
  15. jQuery.ajaxHistory.pop();
  16. }
  17. },
  18. ajaxPrevious: function() {
  19. if (jQuery.ajaxHistory.length < 1) {
  20. return;
  21. }
  22. var moveBack = 1;
  23. if (arguments[0] != undefined) {
  24. moveBack = arguments[0];
  25. }
  26. if (moveBack >= jQuery.ajaxHistory.length) {
  27. moveBack -= 1;
  28. }
  29. if (jQuery.ajaxHistory.length <= 1) {
  30. top.location.href = location.pathname + location.search;
  31. return;
  32. }
  33. $(jQuery.ajaxHistory[moveBack].elmt).simbioAJAX(jQuery.ajaxHistory[moveBack].url, {method: 'get'});
  34. }
  35. });
  36.  
  37. /**
  38. * Function to Set AJAX content
  39. *
  40. * @param string strSelector : string of CSS and XPATH selector
  41. * @param string strURL : URL of AJAX request
  42. * @return void
  43. */
  44. jQuery.fn.simbioAJAX = async function(strURL, params)
  45. {
  46. var options = {
  47. method: 'get',
  48. insertMode: 'replace',
  49. addData: '',
  50. beforeSend: function (){
  51. $('#loader-wrapper').show();
  52. },
  53. returnType: 'html',
  54. loadingMessage: 'LOADING CONTENT... PLEASE WAIT' };
  55. jQuery.extend(options, params);
  56.  
  57. var ajaxContainer = $(this);
  58. var loader = $(".loader");
  59. var currLoaderMessage = loader.html();
  60. var doc = $(document);
  61. loader.html(options.loadingMessage);
  62. doc.ajaxStart(function(){
  63. $(this).addClass('loadingImage');
  64. });
  65. doc.ajaxSuccess(function(){
  66. loader.html(currLoaderMessage);
  67. // no history on post AJAX request
  68. if (options.method != 'post') {
  69. var historyURL = strURL;
  70. if (options.addData.length > 0) {
  71. var addParam = options.addData;
  72. if (Array.prototype.isPrototypeOf(options.addData)) {
  73. addParam = jQuery.param(options.addData);
  74. }
  75. if (historyURL.indexOf('?', 0) > -1) {
  76. historyURL += '&' + addParam;
  77. } else {
  78. historyURL += '?' + addParam;
  79. }
  80. }
  81. jQuery.addAjaxHistory(historyURL, ajaxContainer[0]);
  82. }
  83. });
  84. doc.ajaxStop(function(){ loader.removeClass('loadingImage'); });
  85. doc.ajaxError(function(event, request, settings) {
  86. // Fixing error message position
  87. /* Modified by Drajat Hasan */
  88. loader.attr('style', 'background: #D9534F;color: white;position: relative !important;font-weight: bold;'),
  89. loader.html("<div class=\"error\">Error requesting page : <strong>" + settings.url + "</strong>" + request.responseText + " <br> Press F5 to hide this error message.</div>");
  90. /* End */
  91. })
  92.  
  93. var ajaxResponse;
  94. try {
  95. ajaxResponse = await $.ajax({
  96. type : options.method, url : strURL,
  97. data : options.addData, async: false })
  98.  
  99. // clear error message
  100. if (loader.html().indexOf('Error') > -1) {
  101. loader.html('&nbsp;')
  102. }
  103. loader.removeAttr('style')
  104. } catch (err) {
  105. console.error(err)
  106. }
  107.  
  108. // add to elements
  109. if (options.insertMode == 'before') {
  110. ajaxContainer.prepend(ajaxResponse);
  111. } else if (options.insertMode == 'after') {
  112. ajaxContainer.append(ajaxResponse);
  113. } else { ajaxContainer.html(ajaxResponse).hide().fadeIn('fast'); }
  114.  
  115. ajaxContainer.trigger('simbioAJAXloaded');
  116.  
  117. return ajaxContainer;
  118. }
  119.  
  120. /* invoke UCS upload catalog */
  121. var ucsUpload = function(strUploadHandler, strData) {
  122. var confUpload = false;
  123. strData = jQuery.trim(strData);
  124. if (strData) {
  125. confUpload = confirm('Are you sure to upload selected data to Union Catalog Server?');
  126. } else {
  127. alert('Please select bibliographic data to upload!');
  128. return;
  129. }
  130. if (!confUpload) {
  131. return;
  132. }
  133. jQuery.ajax({
  134. url: strUploadHandler,
  135. type: 'POST',
  136. data: strData,
  137. dataType: 'json',
  138. success: function(ajaxRespond) {
  139. var jsonObj = ajaxRespond;
  140. // alert(jsonObj.status + ': ' + jsonObj.message);
  141. alert(jsonObj.message);
  142. },
  143. error: function(ajaxRespond) {
  144. alert('UCS Upload error with message: ' + ajaxRespond.responseText);
  145. }
  146. });
  147. }
  148.  
  149. /* invoke UCS record update */
  150. var ucsUpdate = function(strURLHandler, strData) {
  151. strData = jQuery.trim(strData);
  152. jQuery.ajax({
  153. url: strURLHandler,
  154. type: 'POST',
  155. data: strData,
  156. dataType: 'json',
  157. error: function(jqXHR, textStatus, errorThrown) { alert('Error updating UCS : ' + textStatus + ' (' + errorThrown + ')'); },
  158. success: function(data, textStatus, jqXHR) { alert('UCS record(s) updated'); }
  159. });
  160. }
Add Comment
Please, Sign In to add comment