PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/js/mage/adminhtml/loader.js

https://gitlab.com/LisovyiEvhenii/ismextensions
JavaScript | 285 lines | 196 code | 29 blank | 60 comment | 52 complexity | 36e96b5613937bf18b8c02abc11d41e7 MD5 | raw file
  1. /**
  2. * Magento
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Academic Free License (AFL 3.0)
  7. * that is bundled with this package in the file LICENSE_AFL.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@magento.com so we can send you a copy immediately.
  13. *
  14. * DISCLAIMER
  15. *
  16. * Do not edit or add to this file if you wish to upgrade Magento to newer
  17. * versions in the future. If you wish to customize Magento for your
  18. * needs please refer to http://www.magento.com for more information.
  19. *
  20. * @category Mage
  21. * @package Mage_Adminhtml
  22. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. */
  25. var SessionError = Class.create();
  26. SessionError.prototype = {
  27. initialize: function(errorText) {
  28. this.errorText = errorText;
  29. },
  30. toString: function()
  31. {
  32. return 'Session Error:' + this.errorText;
  33. }
  34. };
  35. Ajax.Request.addMethods({
  36. initialize: function($super, url, options){
  37. $super(options);
  38. this.transport = Ajax.getTransport();
  39. if (!url.match(new RegExp('[?&]isAjax=true',''))) {
  40. url = url.match(new RegExp('\\?',"g")) ? url + '&isAjax=true' : url + '?isAjax=true';
  41. }
  42. if (Object.isString(this.options.parameters)
  43. && this.options.parameters.indexOf('form_key=') == -1
  44. ) {
  45. this.options.parameters += '&' + Object.toQueryString({
  46. form_key: FORM_KEY
  47. });
  48. } else {
  49. if (!this.options.parameters) {
  50. this.options.parameters = {
  51. form_key: FORM_KEY
  52. };
  53. }
  54. if (!this.options.parameters.form_key) {
  55. this.options.parameters.form_key = FORM_KEY;
  56. }
  57. }
  58. this.request(url);
  59. },
  60. respondToReadyState: function(readyState) {
  61. var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
  62. if (state == 'Complete') {
  63. try {
  64. this._complete = true;
  65. if (response.responseText.isJSON()) {
  66. var jsonObject = response.responseText.evalJSON();
  67. if (jsonObject.ajaxExpired && jsonObject.ajaxRedirect) {
  68. window.location.replace(jsonObject.ajaxRedirect);
  69. throw new SessionError('session expired');
  70. }
  71. }
  72. (this.options['on' + response.status]
  73. || this.options['on' + (this.success() ? 'Success' : 'Failure')]
  74. || Prototype.emptyFunction)(response, response.headerJSON);
  75. } catch (e) {
  76. this.dispatchException(e);
  77. if (e instanceof SessionError) {
  78. return;
  79. }
  80. }
  81. var contentType = response.getHeader('Content-type');
  82. if (this.options.evalJS == 'force'
  83. || (this.options.evalJS && this.isSameOrigin() && contentType
  84. && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) {
  85. this.evalResponse();
  86. }
  87. }
  88. try {
  89. (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
  90. Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
  91. } catch (e) {
  92. this.dispatchException(e);
  93. }
  94. if (state == 'Complete') {
  95. // avoid memory leak in MSIE: clean up
  96. this.transport.onreadystatechange = Prototype.emptyFunction;
  97. }
  98. }
  99. });
  100. Ajax.Updater.respondToReadyState = Ajax.Request.respondToReadyState;
  101. //Ajax.Updater = Object.extend(Ajax.Updater, {
  102. // initialize: function($super, container, url, options) {
  103. // this.container = {
  104. // success: (container.success || container),
  105. // failure: (container.failure || (container.success ? null : container))
  106. // };
  107. //
  108. // options = Object.clone(options);
  109. // var onComplete = options.onComplete;
  110. // options.onComplete = (function(response, json) {
  111. // this.updateContent(response.responseText);
  112. // if (Object.isFunction(onComplete)) onComplete(response, json);
  113. // }).bind(this);
  114. //
  115. // $super((url.match(new RegExp('\\?',"g")) ? url + '&isAjax=1' : url + '?isAjax=1'), options);
  116. // }
  117. //});
  118. var varienLoader = new Class.create();
  119. varienLoader.prototype = {
  120. initialize : function(caching){
  121. this.callback= false;
  122. this.cache = $H();
  123. this.caching = caching || false;
  124. this.url = false;
  125. },
  126. getCache : function(url){
  127. if(this.cache.get(url)){
  128. return this.cache.get(url)
  129. }
  130. return false;
  131. },
  132. load : function(url, params, callback){
  133. this.url = url;
  134. this.callback = callback;
  135. if(this.caching){
  136. var transport = this.getCache(url);
  137. if(transport){
  138. this.processResult(transport);
  139. return;
  140. }
  141. }
  142. if (typeof(params.updaterId) != 'undefined') {
  143. new varienUpdater(params.updaterId, url, {
  144. evalScripts : true,
  145. onComplete: this.processResult.bind(this),
  146. onFailure: this._processFailure.bind(this)
  147. });
  148. }
  149. else {
  150. new Ajax.Request(url,{
  151. method: 'post',
  152. parameters: params || {},
  153. onComplete: this.processResult.bind(this),
  154. onFailure: this._processFailure.bind(this)
  155. });
  156. }
  157. },
  158. _processFailure : function(transport){
  159. location.href = BASE_URL;
  160. },
  161. processResult : function(transport){
  162. if(this.caching){
  163. this.cache.set(this.url, transport);
  164. }
  165. if(this.callback){
  166. this.callback(transport.responseText);
  167. }
  168. }
  169. }
  170. if (!window.varienLoaderHandler)
  171. var varienLoaderHandler = new Object();
  172. varienLoaderHandler.handler = {
  173. onCreate: function(request) {
  174. if(request.options.loaderArea===false){
  175. return;
  176. }
  177. request.options.loaderArea = $$('#html-body .wrapper')[0]; // Blocks all page
  178. if(request && request.options.loaderArea){
  179. Element.clonePosition($('loading-mask'), $(request.options.loaderArea), {offsetLeft:-2})
  180. toggleSelectsUnderBlock($('loading-mask'), false);
  181. Element.show('loading-mask');
  182. setLoaderPosition();
  183. if(request.options.loaderArea=='html-body'){
  184. //Element.show('loading-process');
  185. }
  186. }
  187. else{
  188. //Element.show('loading-process');
  189. }
  190. },
  191. onComplete: function(transport) {
  192. if(Ajax.activeRequestCount == 0) {
  193. //Element.hide('loading-process');
  194. toggleSelectsUnderBlock($('loading-mask'), true);
  195. Element.hide('loading-mask');
  196. }
  197. }
  198. };
  199. /**
  200. * @todo need calculate middle of visible area and scroll bind
  201. */
  202. function setLoaderPosition(){
  203. var elem = $('loading_mask_loader');
  204. if (elem && Prototype.Browser.IE) {
  205. var elementDims = elem.getDimensions();
  206. var viewPort = document.viewport.getDimensions();
  207. var offsets = document.viewport.getScrollOffsets();
  208. elem.style.left = Math.floor(viewPort.width / 2 + offsets.left - elementDims.width / 2) + 'px';
  209. elem.style.top = Math.floor(viewPort.height / 2 + offsets.top - elementDims.height / 2) + 'px';
  210. elem.style.position = 'absolute';
  211. }
  212. }
  213. /*function getRealHeight() {
  214. var body = document.body;
  215. if (window.innerHeight && window.scrollMaxY) {
  216. return window.innerHeight + window.scrollMaxY;
  217. }
  218. return Math.max(body.scrollHeight, body.offsetHeight);
  219. }*/
  220. function toggleSelectsUnderBlock(block, flag){
  221. if(Prototype.Browser.IE){
  222. var selects = document.getElementsByTagName("select");
  223. for(var i=0; i<selects.length; i++){
  224. /**
  225. * @todo: need check intersection
  226. */
  227. if(flag){
  228. if(selects[i].needShowOnSuccess){
  229. selects[i].needShowOnSuccess = false;
  230. // Element.show(selects[i])
  231. selects[i].style.visibility = '';
  232. }
  233. }
  234. else{
  235. if(Element.visible(selects[i])){
  236. // Element.hide(selects[i]);
  237. selects[i].style.visibility = 'hidden';
  238. selects[i].needShowOnSuccess = true;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. Ajax.Responders.register(varienLoaderHandler.handler);
  245. var varienUpdater = Class.create(Ajax.Updater, {
  246. updateContent: function($super, responseText) {
  247. if (responseText.isJSON()) {
  248. var responseJSON = responseText.evalJSON();
  249. if (responseJSON.ajaxExpired && responseJSON.ajaxRedirect) {
  250. window.location.replace(responseJSON.ajaxRedirect);
  251. }
  252. } else {
  253. $super(responseText);
  254. }
  255. }
  256. });