This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
/
ui.atk4_loader.js
334 lines (282 loc) · 9.56 KB
/
ui.atk4_loader.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Welcome to Agile Toolkit JS framework. This is a main file which provides extensions on top of jQuery-UI */
/*
@VERSION
ATK4 Loader introduces a widget, which is used instead of default $('..').load() method.
The new widget adds a number of useful features including:
* verifying loaded content against session timeouts or errors
* provides better ability to refresh content
* handler support. What if loaded content contains unsaved data?
*/
/*
In a complex use case you would be doing the following:
1. For the element which you wish to be a selector, initialise this widget
2. Set a current URL for that element during initialisation (base_url);
3. Define what should happen if user clicks "cancel"
4. Define what should happen if user finishes action successfuly inside
4a. succesful action may pass arguments.
$('#MyDiv')
.atk4_loader({url: '/page.html'})
.atk4_loader({})
THE STRUCTURE OF THIS WIDGET MIGHT BE REWRITTEN
*/
$.widget('ui.atk4_loader', {
options: {
/*
base_url will contain URL which will be used to refresh contents.
*/
base_url: undefined,
/*
loading will be set to true, when contents of this widgets are being
loaded.
*/
loading: false,
cogs: '<div id="banner-loader" class="atk-banner atk-cells atk-visible"><div class="atk-cell atk-align-center atk-valign-middle"><div class="atk-box atk-inline atk-size-zetta atk-banner-cogs"></div></div></div>',
/*
when we are loading URLs, we will automaticaly pass arguments to cut stuff out
*/
cut_mode: 'page',
cut: '1',
history: false
},
/*
Helper contains some extra thingies
*/
helper: undefined,
loader: undefined,
showLoader: function(){
if(!!this.loader) this.loader.show();
},
hideLoader: function(){
if(!!this.loader) this.loader.hide();
},
_create: function(){
var self=this;
/*
this.options.debug=true;
this.options.anchoring=true;
*/
this.element.addClass('atk4_loader');
if(this.options.url){
this.base_url=this.options.url;
}
if(this.options.cut_object){
this.cut_mode='object';
this.cut=this.options.cut_object;
}
if(this.options.cogs){
var l=$(this.options.cogs);
l.prependTo(self.element);
self.loader=l;
self.hideLoader();
}
if(this.options.history){
$(window).bind('popstate', function(event){
var state = event.originalEvent.state;
if (location.href != self.base_url && self.base_url) {
self.options.history=false;
self.loadURL(location.href,function(){
self.options.history=true;
});
}
});
}
if(this.options.debug){
var d=$('<div style="z-index: 2000"/>');
d.css({background:'#fe8',border: '1px solid black',position:'absolute',width:'100px',height:'50px'});
$('<div/>').text('History: '+(this.options.history?'yes':'no')).appendTo(d);
$('<a/>').attr('title','Canceled close').attr('href','javascript: void(0)').text('X').css({float:'right'})
.click(function(){ $(this).closest('div').next().css({border:'0px'});$(this).closest('div').remove(); }).appendTo(d);
d.append(' ');
$('<a/>').attr('title','Reload this region').attr('href','javascript: void(0)').text('Reload')
.click(function(){ self.reload()}).appendTo(d);
d.append(' ');
$('<a/>').attr('title','Show URL').attr('href','javascript: void(0)').text('URL')
.click(function(){ alert(self.base_url)}).appendTo(d);
d.append(' ');
$('<a/>').attr('title','Attempt to remove').attr('href','javascript: void(0)').text('Remove')
.click(function(){ self.remove()}).appendTo(d);
d.append(' ');
d.insertBefore(self.element);
d.draggable();
self.helper=d;
self.element.css({border:'1px dashed green'});
}
},
destroy: function(){
var self=this;
this.element.removeClass('atk4_loader');
if(this.helper){
this.helper.remove();
this.helper=undefined;
}
if(this.loader){
this.loader.remove();
this.loader=undefined;
}
},
/*
This function fetches block of HTML from the server and puts it
inside specified element. This function is very similar to
$('..').load('http://'), however it improves evaluation
of scripts supplied inside loaded chunk.
*/
_loadHTML: function(el, url, callback, reload){
var self=this;
// We preserve support for selectors in URL,
// as compatibility with jQuery, however avoid
// using this. ATK4 will gladly render part of
// the page for you. (cut_object, cut_region, cut_page)
var selector, off = url.indexOf(" ");
if ( off >= 0 ) {
selector = url.slice(off, url.length);
url = url.slice(0, off);
}
// Before actual loading start, we call a method, which might want
// to display loading indicator somewhere on the page.
if(self.loading){
$.univ().loadingInProgress();
return false;
}
var m;
self.loading=true;
self.showLoader();
$.atk4.get(url,null,function(res){
/*
if(res.substr(0,13)=='SESSION OVER:'){
$.univ.dialogOK('Session over','Your session have been timed out',function(){ document.location='/'});
return;
}
*/
if(self.options.history)window.history.pushState({path: self.base_url}, 'foobar', self.base_url);
var scripts=[], source=res;
while((s=source.indexOf("<script"))>=0){
s2=source.indexOf(">",s);
e=source.indexOf("</script",s2);
e2=source.indexOf(">",e);
scripts.push(source.substring(s2+1,e));
source=source.substring(0,s)+source.substring(e2+1);
}
m=el;
//if(!(jQuery.browser.msie))m.hide();
// Parse into Document
var source=$('<div/>').append(source);
var n=source.children();
var oldid=el.attr('id');
if(n.length==1 && (reload || (n.attr('id') && n.attr('id')==oldid))){
el.removeAttr('id');
// Only one child have been returned to us. We also checked ID's and they match
// with existing element. In this case we will be copying contents of
// provided element
//n=n.contents();
el.triggerHandler('remove');
n.insertAfter(el);
el.remove();
// http://forum.jquery.com/topic/jquery-empty-does-not-destroy-ui-widgets-whereas-jquery-remove-does-using-ui-1-8-4
}else{
// otherwise we will be copying all the elements (including text)
if(reload){
console.error('Cannot reload content: ',reload,n[0],n[1],n[2]);
}
el.empty();
n=source.contents();
n.each(function(){
$(this).remove().appendTo(el);
});
}
el.atk4_loader({'base_url':url});
/*
*/
for(var i in scripts){
try{
window.region=el;
if(eval.call)eval.call(window,scripts[i]);else
// IE-pain
with(window)eval(scripts[i]);
}catch(e){
console.error("JS:",e,scripts[i]);
}
};
if(callback)$.atk4(callback,true);
$.atk4(function(){
m.show();
var f=m.find('form:first').find('input:visible,select:visible,textarea:visible').eq(0);
if(!f.hasClass('nofocus'))f.focus();
});
},function(){ // second callback, which is always called, when loading is completed
self.hideLoader();
self.loading=false;
el.trigger('after_html_loaded');
});
},
/*
This function is called before HTML loading is started. Redifine it
or bind to enhance it's functionality
*/
_loadingStart: function(){
var self=this;
if(false === self._trigger('loadingStart')){
return false;
}
},
reload: function(args){
var url=this.base_url;
if(args)this.base_url=$.atk4.addArgument(this.base_url,args);
this.loadURL(this.base_url);
this.base_url=url;
},
remove: function(){
var self=this;
self.helper && self.helper.css({background:'red'});
//if(false === self._trigger('beforeclose')){
if(self.element.find('.form_changed').length){
if(!confirm('Changes on the form will be lost. Continue?'))return false;
}
self.element.find('.form_changed').removeClass('form_changed');
return true;
},
setURL: function(url){
var self=this;
self.base_url=url;
},
loadURL: function(url,fn,strip_layer){
/*
Function provided mainly for compatibility. It will load URL in the selector
and will set "fn" to fire off when loading is complete
Sometimes you would want to reload (atk4_reload) an element. This means,
you will receive the same element from AJAX. New element comes with the
same ID and sub-elements. What we have to do is copy children from the
received data into existing element.
*/
var self = this;
if(self.loading){
$.univ().loadingInProgress();
return false;
}
//if(false === self._trigger('beforeclose')){
if(self.element.find('.form_changed').length){
if(!confirm('Changes on the form will be lost. Continue?'))return false;
}
// remove error messages
$('#tiptip_holder').remove();
self.base_url=url;
url=$.atk4.addArgument(url,"cut_"+self.options.cut_mode+'='+self.options.cut);
this._loadHTML(self.element,url,fn,strip_layer);
}
});
$.extend($.ui.atk4_loader, {
getter: 'remove'
});
$.fn.extend({
atk4_load: function(url,fn){
this.atk4_loader().atk4_loader('loadURL',url,fn);
},
atk4_reload: function(url,arg,fn){
if(arg){
$.each(arg,function(key,value){
url=$.atk4.addArgument(url,key+'='+encodeURIComponent(value));
});
}
this.atk4_loader()
.atk4_loader('loadURL',url,fn,true);
}
});