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