Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

236 lignes
6.9 KiB

  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. var varienLoader = new Class.create();
  82. varienLoader.prototype = {
  83. initialize: function (caching) {
  84. this.callback = false;
  85. this.cache = $H();
  86. this.caching = caching || false;
  87. this.url = false;
  88. },
  89. getCache: function (url) {
  90. if (this.cache.get(url)) {
  91. return this.cache.get(url);
  92. }
  93. return false;
  94. },
  95. load: function (url, params, callback) {
  96. this.url = url;
  97. this.callback = callback;
  98. if (this.caching) {
  99. var transport = this.getCache(url);
  100. if (transport) {
  101. this.processResult(transport);
  102. return;
  103. }
  104. }
  105. if (typeof params.updaterId != 'undefined') {
  106. new varienUpdater(params.updaterId, url, {
  107. evalScripts: true,
  108. onComplete: this.processResult.bind(this),
  109. onFailure: this._processFailure.bind(this)
  110. });
  111. } else {
  112. new Ajax.Request(url,{
  113. method: 'post',
  114. parameters: params || {},
  115. onComplete: this.processResult.bind(this),
  116. onFailure: this._processFailure.bind(this)
  117. });
  118. }
  119. },
  120. _processFailure: function (transport) {
  121. location.href = BASE_URL;
  122. },
  123. processResult: function (transport) {
  124. if (this.caching) {
  125. this.cache.set(this.url, transport);
  126. }
  127. if (this.callback) {
  128. this.callback(transport.responseText);
  129. }
  130. }
  131. };
  132. if (!window.varienLoaderHandler)
  133. var varienLoaderHandler = new Object();
  134. varienLoaderHandler.handler = {
  135. onCreate: function (request) {
  136. if (request.options.loaderArea === false) {
  137. return;
  138. }
  139. jQuery('body').trigger('processStart');
  140. },
  141. onException: function (transport) {
  142. jQuery('body').trigger('processStop');
  143. },
  144. onComplete: function (transport) {
  145. jQuery('body').trigger('processStop');
  146. }
  147. };
  148. /**
  149. * @todo need calculate middle of visible area and scroll bind
  150. */
  151. function setLoaderPosition() {
  152. var elem = $('loading_mask_loader');
  153. if (elem && Prototype.Browser.IE) {
  154. var elementDims = elem.getDimensions();
  155. var viewPort = document.viewport.getDimensions();
  156. var offsets = document.viewport.getScrollOffsets();
  157. elem.style.left = Math.floor(viewPort.width / 2 + offsets.left - elementDims.width / 2) + 'px';
  158. elem.style.top = Math.floor(viewPort.height / 2 + offsets.top - elementDims.height / 2) + 'px';
  159. elem.style.position = 'absolute';
  160. }
  161. }
  162. function toggleSelectsUnderBlock(block, flag) {
  163. if (Prototype.Browser.IE) {
  164. var selects = document.getElementsByTagName('select');
  165. for (var i = 0; i < selects.length; i++) {
  166. /**
  167. * @todo: need check intersection
  168. */
  169. if (flag) {
  170. if (selects[i].needShowOnSuccess) {
  171. selects[i].needShowOnSuccess = false;
  172. // Element.show(selects[i])
  173. selects[i].style.visibility = '';
  174. }
  175. } else if (Element.visible(selects[i])) {
  176. // Element.hide(selects[i]);
  177. selects[i].style.visibility = 'hidden';
  178. selects[i].needShowOnSuccess = true;
  179. }
  180. }
  181. }
  182. }
  183. Ajax.Responders.register(varienLoaderHandler.handler);
  184. var varienUpdater = Class.create(Ajax.Updater, {
  185. updateContent: function ($super, responseText) {
  186. if (responseText.isJSON()) {
  187. var responseJSON = responseText.evalJSON();
  188. if (responseJSON.ajaxExpired && responseJSON.ajaxRedirect) {
  189. window.location.replace(responseJSON.ajaxRedirect);
  190. }
  191. } else {
  192. $super(responseText);
  193. }
  194. }
  195. });