varienLoader.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. var SessionError = Class.create();
  6. SessionError.prototype = {
  7. initialize: function (errorText) {
  8. this.errorText = errorText;
  9. },
  10. toString: function () {
  11. return 'Session Error:' + this.errorText;
  12. }
  13. };
  14. Ajax.Request.addMethods({
  15. initialize: function ($super, url, options) {
  16. $super(options);
  17. this.transport = Ajax.getTransport();
  18. if (!url.match(new RegExp('[?&]isAjax=true',''))) {
  19. url = url.match(new RegExp('\\?','g')) ? url + '&isAjax=true' : url + '?isAjax=true';
  20. }
  21. if (Object.isString(this.options.parameters) &&
  22. this.options.parameters.indexOf('form_key=') == -1
  23. ) {
  24. this.options.parameters += '&' + Object.toQueryString({
  25. form_key: FORM_KEY
  26. });
  27. } else {
  28. if (!this.options.parameters) {
  29. this.options.parameters = {
  30. form_key: FORM_KEY
  31. };
  32. }
  33. if (!this.options.parameters.form_key) {
  34. this.options.parameters.form_key = FORM_KEY;
  35. }
  36. }
  37. this.request(url);
  38. },
  39. respondToReadyState: function (readyState) {
  40. var state = Ajax.Request.Events[readyState],
  41. response = new Ajax.Response(this);
  42. if (state == 'Complete') {
  43. try {
  44. this._complete = true;
  45. if (response.responseText.isJSON()) {
  46. var jsonObject = response.responseText.evalJSON();
  47. if (jsonObject.ajaxExpired && jsonObject.ajaxRedirect) {
  48. window.location.replace(jsonObject.ajaxRedirect);
  49. throw new SessionError('session expired');
  50. }
  51. }
  52. (this.options['on' + response.status] ||
  53. this.options['on' + (this.success() ? 'Success' : 'Failure')] ||
  54. Prototype.emptyFunction)(response, response.headerJSON);
  55. } catch (e) {
  56. this.dispatchException(e);
  57. if (e instanceof SessionError) {
  58. return;
  59. }
  60. }
  61. var contentType = response.getHeader('Content-type');
  62. if (this.options.evalJS == 'force' ||
  63. this.options.evalJS && this.isSameOrigin() && contentType &&
  64. contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)) {
  65. this.evalResponse();
  66. }
  67. }
  68. try {
  69. (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
  70. Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
  71. } catch (e) {
  72. this.dispatchException(e);
  73. }
  74. if (state == 'Complete') {
  75. // avoid memory leak in MSIE: clean up
  76. this.transport.onreadystatechange = Prototype.emptyFunction;
  77. }
  78. }
  79. });
  80. Ajax.Updater.respondToReadyState = Ajax.Request.respondToReadyState;
  81. //Ajax.Updater = Object.extend(Ajax.Updater, {
  82. // initialize: function($super, container, url, options) {
  83. // this.container = {
  84. // success: (container.success || container),
  85. // failure: (container.failure || (container.success ? null : container))
  86. // };
  87. //
  88. // options = Object.clone(options);
  89. // var onComplete = options.onComplete;
  90. // options.onComplete = (function(response, json) {
  91. // this.updateContent(response.responseText);
  92. // if (Object.isFunction(onComplete)) onComplete(response, json);
  93. // }).bind(this);
  94. //
  95. // $super((url.match(new RegExp('\\?',"g")) ? url + '&isAjax=1' : url + '?isAjax=1'), options);
  96. // }
  97. //});
  98. var varienLoader = new Class.create();
  99. varienLoader.prototype = {
  100. initialize: function (caching) {
  101. this.callback = false;
  102. this.cache = $H();
  103. this.caching = caching || false;
  104. this.url = false;
  105. },
  106. getCache: function (url) {
  107. if (this.cache.get(url)) {
  108. return this.cache.get(url);
  109. }
  110. return false;
  111. },
  112. load: function (url, params, callback) {
  113. this.url = url;
  114. this.callback = callback;
  115. if (this.caching) {
  116. var transport = this.getCache(url);
  117. if (transport) {
  118. this.processResult(transport);
  119. return;
  120. }
  121. }
  122. if (typeof params.updaterId != 'undefined') {
  123. new varienUpdater(params.updaterId, url, {
  124. evalScripts: true,
  125. onComplete: this.processResult.bind(this),
  126. onFailure: this._processFailure.bind(this)
  127. });
  128. } else {
  129. new Ajax.Request(url,{
  130. method: 'post',
  131. parameters: params || {},
  132. onComplete: this.processResult.bind(this),
  133. onFailure: this._processFailure.bind(this)
  134. });
  135. }
  136. },
  137. _processFailure: function (transport) {
  138. location.href = BASE_URL;
  139. },
  140. processResult: function (transport) {
  141. if (this.caching) {
  142. this.cache.set(this.url, transport);
  143. }
  144. if (this.callback) {
  145. this.callback(transport.responseText);
  146. }
  147. }
  148. };
  149. if (!window.varienLoaderHandler)
  150. var varienLoaderHandler = new Object();
  151. varienLoaderHandler.handler = {
  152. onCreate: function (request) {
  153. if (request.options.loaderArea === false) {
  154. return;
  155. }
  156. jQuery('body').trigger('processStart');
  157. },
  158. onException: function (transport) {
  159. jQuery('body').trigger('processStop');
  160. },
  161. onComplete: function (transport) {
  162. jQuery('body').trigger('processStop');
  163. }
  164. };
  165. /**
  166. * @todo need calculate middle of visible area and scroll bind
  167. */
  168. function setLoaderPosition() {
  169. var elem = $('loading_mask_loader');
  170. if (elem && Prototype.Browser.IE) {
  171. var elementDims = elem.getDimensions();
  172. var viewPort = document.viewport.getDimensions();
  173. var offsets = document.viewport.getScrollOffsets();
  174. elem.style.left = Math.floor(viewPort.width / 2 + offsets.left - elementDims.width / 2) + 'px';
  175. elem.style.top = Math.floor(viewPort.height / 2 + offsets.top - elementDims.height / 2) + 'px';
  176. elem.style.position = 'absolute';
  177. }
  178. }
  179. /*function getRealHeight() {
  180. var body = document.body;
  181. if (window.innerHeight && window.scrollMaxY) {
  182. return window.innerHeight + window.scrollMaxY;
  183. }
  184. return Math.max(body.scrollHeight, body.offsetHeight);
  185. }*/
  186. function toggleSelectsUnderBlock(block, flag) {
  187. if (Prototype.Browser.IE) {
  188. var selects = document.getElementsByTagName('select');
  189. for (var i = 0; i < selects.length; i++) {
  190. /**
  191. * @todo: need check intersection
  192. */
  193. if (flag) {
  194. if (selects[i].needShowOnSuccess) {
  195. selects[i].needShowOnSuccess = false;
  196. // Element.show(selects[i])
  197. selects[i].style.visibility = '';
  198. }
  199. } else if (Element.visible(selects[i])) {
  200. // Element.hide(selects[i]);
  201. selects[i].style.visibility = 'hidden';
  202. selects[i].needShowOnSuccess = true;
  203. }
  204. }
  205. }
  206. }
  207. Ajax.Responders.register(varienLoaderHandler.handler);
  208. var varienUpdater = Class.create(Ajax.Updater, {
  209. updateContent: function ($super, responseText) {
  210. if (responseText.isJSON()) {
  211. var responseJSON = responseText.evalJSON();
  212. if (responseJSON.ajaxExpired && responseJSON.ajaxRedirect) {
  213. window.location.replace(responseJSON.ajaxRedirect);
  214. }
  215. } else {
  216. $super(responseText);
  217. }
  218. }
  219. });