-
Notifications
You must be signed in to change notification settings - Fork 0
/
hux.js.bak
816 lines (765 loc) · 24.9 KB
/
hux.js.bak
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/**
HTTP by Using XML (HUX) : Core
Copyright (C) 2011 Florent FAYOLLE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
// hux.core.js
var HUXcore = {
init: function(){
this.Selector.init();
},
namespace: "http://www.example.org/HUX",
/**
* HUX specific event Manager
* Events :
* - beforeInject : triggered before injecting content to the target node (1 argument : the nodes to be added)
* - beforeEmpty : triggered before emptying target node (1 argument : the node to be emptied)
* - requestError : triggered if an XMLHttpRequest failed (1 argument : the XHR object)
*/
HUXevents:{
// array of listener for each event
__arrEv:{
"beforeInject":[],
"beforeEmpty":[],
"requestError":[]
},
// sort of addEventListener
add:function(evName, fn){
var arrEv = this.__arrEv;
if(arrEv[evName])
this.__arrEv[evName].push(fn);
else
throw "the event "+evName+" does not exist for HUX";
},
// sort of removeEventListener
remove: function(evName, fn){
HUXcore.removeElement(this.__arrEv[evName], fn);
},
/**
* trigger all event Listener for a specific event
* trigger(evName, args)
* @evName : the name of the Event (String)
* @args : the arguments to give to each listener
*/
trigger: function(evName, args){
HUXcore.Compat.forEach(HUXcore.HUXevents.__arrEv[evName], function(fn){
var ret = fn.apply(window, args);
if(ret === false)
throw "trigger stoped";
});
}
},
// to log error properly
logError: function(ex){
if(typeof console !== "undefined"){
console.error(ex);
}
},
/**
* DOM Injection manager
*/
Inject:{
/**
* init(target, method, DOMContent)
* @target : the element which will receive @DOMContent
* @method : the string tallying to hux:filling
* @DOMContent : Array of elements to be added to @target
*/
init: function(target, method, DOMContent){
var sMethods = this.sMethods, firstChild;
switch(method){
case sMethods.PREPEND:
if(target.childNodes.length > 0){ // we use InsertBefore
HUXcore.HUXevents.trigger("beforeInject", [DOMContent]);
firstChild = target.firstChild;
HUXcore.Compat.forEach(DOMContent, function(el){
target.insertBefore(el, firstChild);
});
}
else{ // if target has no children, we append
return this.init(target, sMethods.APPEND, DOMContent);
}
break;
case sMethods.APPEND:
HUXcore.HUXevents.trigger("beforeInject", [DOMContent]);
while(DOMContent.length > 0)
target.appendChild(DOMContent[0]);
break;
case sMethods.REPLACE:
// In order to replace the content of the target, we empty it, and we append DOMContent in it
this.empty(target);
return this.init(target, sMethods.APPEND, DOMContent);
}
},
// constants
sMethods:{
PREPEND:"prepend",
APPEND:"append",
REPLACE:"replace"
},
// remove each child of parent
empty: function(parent){
var child;
HUXcore.HUXevents.trigger("beforeEmpty", [parent]);
while( (child=parent.firstChild) !== null ){
parent.removeChild(child);
}
},
/**
* get all the children from a parent Node
*/
getChildren: function(parent){
return parent.childNodes;
},
/**
* convert HTML String to DOM
* @sHtml : String containing HTML
* @context : the node designed to receive the content (optionnal)
*/
htmltodom: function(sHtml, context){
var parent = context ? context.cloneNode(false) : document.createElement('div');
try{
parent.innerHTML = sHtml;
}
catch(e){
// IE doesn't allow using innerHTML with table,
// but allows create a div element in which we inject the HTML String of a TABLE
if(parent.tagName === "TABLE")
parent = this.htmltodom("<TABLE>"+sHtml+"</TABLE>", null)[0];
}
return this.getChildren(parent);
}
},
/**
* inject(target, method, content)
* @target : the element which will receive @DOMContent
* @method : the string tallying to hux:filling (default : "replace")
* @content : HTML String or Array of elements to be added to @target
*/
inject:function(target, method, content){
var DOMContent;
if(typeof content === "string")
DOMContent = HUXcore.Inject.htmltodom(content, target);
else if(content instanceof Array)
DOMContent = content;
else
throw new TypeError("content : invalid argument");
if(! method) // if method === null, default is REPLACEMENT
method = HUXcore.Inject.sMethods.REPLACE;
HUXcore.Inject.init.call(HUXcore.Inject, target, method, DOMContent);
},
/**
* DOM Selector Tool
*/
Selector: {
__prefixTN: "",
init: function(){
// check whether we are using html or xhtml ...
if(document.evaluate !== undefined){
if( this.evaluate("/html").length > 0 )
this.__prefixTN = "";
else if(this.evaluate("/xhtml:html").length > 0)
this.__prefixTN = "xhtml:";
else
throw new TypeError("Document non supported by HUX");
}
},
prefixTagName: function(tagName){
return this.__prefixTN+tagName;
},
/**
* Select nodes by their attributes
* byAttribute(attr, context, fnEach)
* @attr : String, attribute to look for
* @context : the node in which one will search (optionnal)
* @fnEach : function executed for each result (optionnal)
*/
byAttribute: function(tagName, attr, context, fnEach){
var xpath, prefixedTN = this.prefixTagName(tagName);
if(typeof document.evaluate !== "undefined"){
xpath = "//"+prefixedTN+"[@"+attr+"]";
return this.evaluate(xpath, context, fnEach);
}
else
return this.__byAttributeIE(tagName, attr, context, fnEach);
},
/**
* similar to byAttribute()
* but search for HUX attributes whatever the prefix is
*/
byAttributeHUX: function(tagName, attr, context, fnEach){
var xpath, prefixedTN, attrs = HUXcore.HUXattr.getAttrAllPrefixes(attr), sAttrXP, ieRet = [];
prefixedTN = this.prefixTagName(tagName);
if(typeof document.evaluate !== "undefined"){
sAttrXP = attrs.join(" or @"); // sAttrXP = "data-attr OR @data-hux-attr OR @hux:attr"
xpath = "//"+prefixedTN+"[@"+sAttrXP+"]";
return this.evaluate(xpath, context, fnEach);
}
else{
HUXcore.Compat.forEach(attrs, function(attr){
ieRet = ieRet.concat( this.__byAttributeIE(tagName, attr, context, fnEach) );
});
return ieRet;
}
},
/**
* function used by document.evaluate for Namespaces
*/
__nsResolver:function(prefix){
var ns = {
"hux":HUXcore.namespace,
"xhtml":"http://www.w3.org/1999/xhtml"
};
if(!prefix)
return ns.xhtml;
return ns[prefix];
},
/**
* we use document.evaluate instead of document.querySelectorAll because of
* the non-implementation of namespace gestion in CSS3 selectors
*
* evaluate(sXpath, context, fnEach)
* sXpath : xpath String
* context : the node where we will search for results (default : document)
* fnEach : the function executed for each results
*
* See Also prefixTagName for convenience with elements tagnames
*/
evaluate:function(sXpath, context, fnEach){
context = context || document.documentElement;
fnEach = fnEach || function(){};
var results = document.evaluate(sXpath, context, this.__nsResolver, XPathResult.ANY_TYPE, null);
var thisResult;
var ret = [];
while ( (thisResult = results.iterateNext()) !== null) {
ret.push(thisResult);
fnEach(thisResult);
}
return ret;
},
/**
* IE does not implement document.evaluate
* This function is a fallback for Selector.byAttribute
*/
__byAttributeIE: function(tagName, attr, context, fnEach){
var fnFilter = function(){ return this.getAttribute(attr) !== null; };
return this.filterIE(tagName, fnFilter, context, fnEach);
},
/**
* IE does not implement document.evaluate
* This function is a generic fallback
*/
filterIE: function(tagName, fnFilter, context, fnEach){
var ret = [], elts;
context = context || document;
fnEach = fnEach || function(){};
elts = context.getElementsByTagName(tagName);
// for each element found above
HUXcore.Compat.forEach(elts, function(el){
// we test the Filter function given
if( fnFilter.call(el) ){
ret.push(el); // if the filter accepted the condition, we add the current element in the results
fnEach(el); // we call the for-each function given by the user
}
});
return ret;
}
},
/**
* XMLHttpRequest Class
*/
XHR: {
// see HUXcore.xhr(opt)
init: function(opt){
if(!opt.target || opt.url.length === 0)
throw "invalid arguments";
try{
var data = null, xhr;
if(opt.async === undefined)
opt.async = true;
xhr = this.getXhrObject();
// we add GET parameters to the URL. If there are some already, we add a "&"+opt.data to the string. Otherwise, we add "?"+opt.data
if(opt.method.toLowerCase() === "get" && typeof opt.data !== "undefined" && opt.data)
opt.url += (opt.url.indexOf("?") >= 0 ? "&" : "?") +opt.data;
else if(opt.method.toLowerCase() === "post")
data = opt.data;
if( opt.username )
xhr.open(opt.method, opt.url, opt.async, opt.username, opt.password);
else
xhr.open(opt.method, opt.url, opt.async);
this.setReadystatechange(xhr, opt.filling, opt.target);
xhr.setRequestHeader("Content-Type", opt.contentType || "application/x-www-form-urlencoded");
xhr.send(data);
}
catch(ex){
HUXcore.logError(ex); //
}
},
// taken from jQuery, returns an XMLHttpRequest object
getXhrObject: function(){
return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
},
onSuccess: function(rspText, xhr, filling, target){
HUXcore.inject(target, filling, rspText);
},
onError: function(xhr){
HUXcore.HUXevents.trigger("requestError", arguments);
},
setReadystatechange: function(xhr, filling, target){
try{
var self = this;
xhr.onreadystatechange = function(){
try{
if(xhr.readyState === 4){
if(xhr.status === 200)
self.onSuccess(xhr.responseText, xhr, filling, target);
else
self.onError(xhr);
}
}
catch(ex){
HUXcore.logError(ex);
}
};
}
catch(ex){
HUXcore.logError(ex);
}
}
},
/* does an XMLHttpRequest
* argument : opt
* opt.url
* opt.method = POST or GET
* opt.data : data to send
* opt.onSuccess: function executed once xhr request is complete
* opt.onError: function executed if xhr request has failed
* opt.async : request is asynchronous (opt.async = true) or synchronous (opt.async = false) ?
* opt.username, opt.password : login and password
* opt.contentType: Content-Type Request Header
* opt.srcNode: Node that have targetNodes or appendNodes attribute
*/
xhr:function(opt){
return HUXcore.XHR.init.apply(HUXcore.XHR, arguments);
},
/**
* Attribute Manager for HUX
*/
HUXattr: {
__getAttributeNS: function(srcNode, name){
return srcNode.getAttributeNS ? srcNode.getAttributeNS(HUXcore.namespace, name) : srcNode.getAttribute("hux:"+name);
},
getAttributeHUX: function(srcNode, name){
var ret = null, attrs = this.getAttrAllPrefixes(name), i;
for(i = 0; i < attrs.length && ret === null; i++){
ret = srcNode.getAttribute( attrs[i] );
}
if(ret === null) // this might be because of non-support of Opera for getAttribute("hux:...")
ret = HUXcore.HUXattr.__getAttributeNS(srcNode, name);
if(ret === "") // correct odd behaviour of getAttributeNS, which returns "" if the attribute was not found
ret = null;
return ret;
},
getFillingMethod: function(srcNode){
return this.getAttributeHUX(srcNode, "filling");
},
getTargetNode: function(srcNode){
var idTn = this.getAttributeHUX(srcNode, "targetnode");
return document.getElementById(idTn);
},
getAttrAllPrefixes: function(attr){
return [
"data-hux-"+attr,
"data-"+attr,
"hux:"+attr,
attr
];
}
},
// do we use addEventListener or attachEvent ?
__fn_addEventListener : (window.addEventListener? 'addEventListener':'attachEvent'),
__fn_removeEventListener : (window.removeEventListener ? 'removeEventListener':'detachEvent'),
// does the event name have to be prefixed with 'on' ? (yes with attachEvent, no with addEventListener)
__prefix_eventListener: (window.addEventListener? '':'on'),
/**
* HUXcore.addEventListener(target, evName, fn);
* target : the event target
* evName : the name of the event
* fn : the function to call when the event is triggered
*/
addEventListener: function(target, evName, fn){
evName = this.__prefix_eventListener+evName;
return target[this.__fn_addEventListener](evName, fn, false);
},
removeEventListener: function(target, evName, fn){
evName = this.__prefix_eventListener+evName;
return target[this.__fn_removeEventListener](evName, fn, false);
},
getEventTarget: function(ev){
return window.event === undefined ? ev.target : event.srcElement;
},
preventDefault: function(ev){
if(window.event === undefined) // not IE
ev.preventDefault();
else // IE
event.cancelBubble = event.returnValue = false;
},
foreach: function(array, fn){
var i;
for(i = 0; i < array.length; i++)
fn(array[i]);
},
// Inspired From : Array Remove - By John Resig (MIT Licensed)
removeElement: function(array, el){
var index = array.indexOf(el);
if(index < 0)
throw "the element is not present in the array";
var rest = array.slice(index + 1);
// recursion. We remove all "el"
if(rest.indexOf(el) >=0)
rest.removeElement(el);
array.length = index;
return array.push.apply(array, rest);
},
// ensures that any node added by HUX would be listened
recursiveListen: function(jsNamespace){
jsNamespace.listen(document.documentElement);
HUXcore.HUXevents.add("beforeInject", function(children){
HUXcore.Compat.forEach(children, function(child){
jsNamespace.listen(child);
});
});
},
// just call init when the page is loaded...
addModule: function(mod){
HUXcore.addEventListener(window, "load", function(){
mod.init();
});
}
};
HUXcore.addModule(HUXcore);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//hux.simpleListener.js
/**
HTTP by Using XML (HUX) : Simple Watch
Copyright (C) 2011 Florent FAYOLLE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
var HUX_SimpleListener = {
/**
* handler for Click Event
*/
__onclick: function(ev){
var srcNode = HUXcore.getEventTarget(ev) ;
var opt = {
data:null,
url:srcNode.href,
method:'get',
async:true,
filling:HUXcore.HUXattr.getFillingMethod(srcNode),
target:HUXcore.HUXattr.getTargetNode(srcNode),
srcNode:srcNode
};
HUXcore.xhr(opt);
HUXcore.preventDefault(ev);
},
__fnEach: function(el){
HUXcore.addEventListener(el, "click", HUX_SimpleListener.__onclick );
},
listen:function(context){
HUXcore.Selector.byAttributeHUX("a", "targetnode", context, this.__fnEach);
},
init: function(){
HUXcore.recursiveListen(this);
}
};
HUXcore.addModule(HUX_SimpleListener);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var HUX_UrlWatcher = {
asyncReq: false,
hashchangeEnabled: null,
inTreatment: false, // sort of mutex
init:function(){
this.hashchangeEnabled = ( "onhashchange" in window) && (! document.documentMode || document.documentMode >= 8);
if( this.hashchangeEnabled )
this.mgrListener('add');
else
this.__watch();
if(this.IFrameHack.enabled)
this.IFrameHack.createIFrame();
// we listen to any anchor beginning with "#!" (corresponding to CCS3 Selector : a[href^="#!"])
HUXcore.recursiveListen(HUX_UrlWatcher);
// we treat location.hash
HUX_UrlWatcher.handler(null, true);
},
listen: function(context){
var fnFilter, fnEach = HUX_UrlWatcher.__callback_anchor, prefixedTN;
if(document.evaluate !== undefined){
prefixedTN = HUXcore.Selector.prefixTagName("a");
HUXcore.Selector.evaluate("//"+prefixedTN+"[starts-with(@href, '#!')]", context, fnEach);
}
else{
fnFilter = function(){ return this.getAttribute("href").indexOf("#!") === 0; };
HUXcore.Selector.filterIE("a", fnFilter, context, fnEach);
}
},
mgrListener: function(sAction){
switch(sAction){
case 'add':
case 'remove':
if(HUX_UrlWatcher.hashchangeEnabled){
var sFn = sAction+'EventListener'; // sFn = addEventListener or removeEventListener
HUXcore[sFn](window, "hashchange", HUX_UrlWatcher.handleIfChangement);
}
break;
default:
throw new TypeError("mgrListener : no action available for : "+sAction);
}
},
__prev_hash:location.hash,
__watcher_cpt_ex:0,
watcher_cpt_limit:10,
timer:100,
handleIfChangement: function(ev){
var hash = location.hash;
if(hash !== HUX_UrlWatcher.__prev_hash && !HUX_UrlWatcher.inTreatment){
try{
HUX_UrlWatcher.inTreatment = true;
HUX_UrlWatcher.handler(ev);
}
finally{
HUX_UrlWatcher.__prev_hash = hash;
HUX_UrlWatcher.inTreatment = false;
}
}
},
__watch: function(){
try{
HUX_UrlWatcher.handleIfChangement();
}
catch(ex){
HUXcore.logError(ex);
HUX_UrlWatcher.__watcher_cpt_ex++;
throw ex;
}
finally{
if(HUX_UrlWatcher.__watcher_cpt_ex < HUX_UrlWatcher.watcher_cpt_limit)
window.setTimeout(HUX_UrlWatcher.__watch, HUX_UrlWatcher.timer);
}
},
__hashObj:{},
// store the content by default (before having injected content with HUX)
__default_content:{},
__load: function(target, url){
var opt = {
data:null,
url:url,
method:'get',
async:this.asyncReq,
filling:null, // use default option (replace)
target:target
};
HUXcore.xhr(opt);
},
__last_timeStamp:0,
__callback_anchor: function(el){
HUXcore.addEventListener(el, "click", HUX_UrlWatcher.__handle_click);
},
__handle_click:function(ev){
var srcNode = HUXcore.getEventTarget(ev);
location.hash += srcNode.getAttribute("href").replace(/^#/,",");
HUXcore.preventDefault(ev);
},
updateHashSilently: function(hash, keepPrevHash){
if( hash.replace(/^#/, "") !== location.hash.replace(/^#/, "") ){
// temporary, we disable the event Listener
/*HUX_UrlWatcher.mgrListener('remove');*/
// we "cancel" the previous location.hash which may be incorrect
if(!keepPrevHash) // keepPrevHash === true when called by init()
history.back();
location.hash = hash;
/*setTimeout(function(){
HUX_UrlWatcher.mgrListener('add');
}, 0);*/
}
},
// creates the Object for hash : {"[sTarget]":"[url]", ...}
__hashStringToObject: function(hash){
var new_hashObj = {};
var pattern = new RegExp("[#,]!([^=,]+)=([^=,]+)", "g");
while( ( resExec = (pattern.exec(hash)) ) !== null){
sTarget = resExec[1];
url = resExec[2];
new_hashObj[sTarget] = url;
}
return new_hashObj;
},
handler: function(ev, keepPrevHash){
var hash = location.hash.toString();
var resExec, sTarget, target, url, hash_found, sHash = "#";
var new_hashObj = this.__hashStringToObject(hash);
// we do all XHR asked through location.hash
for(sTarget in new_hashObj){
target = document.getElementById(sTarget);
url = new_hashObj[sTarget];
hash_found = this.__hashObj[sTarget];
if(target!== null && url !== "__default"){ // if the URL given is __default, we load the default content in the target node
// we fill a string which will be the new location.hash
sHash += '!'+sTarget+'='+url+',';
if(!hash_found || hash_found !== url){
if(!hash_found){
this.__default_content[sTarget] = target.cloneNode(true);
}
this.__load(target, url);
}
delete this.__hashObj[sTarget]; // later, we will inject the default content in the remaining nodes
}
else if(url === "__default")
delete new_hashObj[sTarget];
}
// for each element remaining in __hashObj, we inject the default content in it
for(sTarget in this.__hashObj){
var replacement;
replacement = this.__default_content[sTarget];
if(replacement !== undefined){
target = document.getElementById(sTarget);
target.parentNode.insertBefore(replacement, target);
target.parentNode.removeChild(target);
}
}
// we update location.hash
this.updateHashSilently( sHash.replace(/,$/, ""), keepPrevHash);
HUX_UrlWatcher.IFrameHack.updateIFrame(); // only if IFrameHack enabled
this.__hashObj = new_hashObj;
},
/*handler:function(){
return HUX_UrlWatcher.__handler.apply(HUX_UrlWatcher, arguments);
},*/
/*
* hack for browsers from which we cannot use previous or next button (for history) to go to the previous hash
*/
IFrameHack: {
/* this hack is only enabled for MSIE 1-7 */
enabled: navigator.userAgent.search(/MSIE [1-7]\./) > 0 || ( "documentMode" in document && document.documentMode < 8 ),
iframe:null,
id:"HUXIFrameHack",
tmpDisableUpd: false, // we need to disable temporary the IFrame update
// creates an iframe if the lasts does not exists, and if the Iframe Hack is enabled
__createIFrame: function(){
this.iframe = document.createElement("iframe");
this.iframe.id=this.id;
this.iframe.src="about:blank";
this.iframe.style.display = "none";
document.body.appendChild(this.iframe);
},
createIFrame: function(){
return HUX_UrlWatcher.IFrameHack.__createIFrame.apply(HUX_UrlWatcher.IFrameHack, arguments);
},
updateIFrame: function(){
if(this.enabled){
if(!this.tmpDisableUpd){
var doc = this.iframe.contentWindow.document;
doc.open("javascript:'<html></html>'");
doc.write("<html><head><scri" + "pt type=\"text/javascript\">top.HUX_UrlWatcher.IFrameHack.setHash('"+location.hash+"');</scri" + "pt></head><body></body></html>");
doc.close();
}
else
this.tmpDisableUpd = false;
}
},
setHash: function(hash){
if(hash !== location.hash){
this.tmpDisableUpd = true;
location.hash = hash;
}
}
}
};
HUXcore.addModule(HUX_UrlWatcher);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var HUX_Form = {
defaultFilling: HUXcore.Inject.sMethods.APPEND,
clearAfterSubmit: true,
init: function(){
HUXcore.recursiveListen(HUX_Form);
},
listen: function(context){
HUXcore.Selector.byAttributeHUX("form", "targetnode", context, this.__fnEach);
},
__fnEach: function(el){
HUXcore.addEventListener(el, "submit", HUX_Form.onSubmit );
},
onSubmit: function(ev){
var arrData = [], form = HUXcore.getEventTarget(ev);
var opt = {
data:null, // set below
url:form.action,
method:form.getAttribute("method"),
async:true,
filling:HUXcore.HUXattr.getFillingMethod(form) || HUX_Form.defaultFilling,
target:HUXcore.HUXattr.getTargetNode(form),
srcNode:form
};
HUXcore.Selector.byAttribute("*", "name", form, function(el){
if( !el.disabled &&
( el.getAttribute("type") === undefined || ! /radio|checkbox/.test(el.type) || el.checked ) ){
arrData.push( el.name+"="+el.value );
if(HUX_Form.clearAfterSubmit && (el.type === "text" || el.tagName === "TEXTAREA") )
el.value = ""
}
});
opt.data = arrData.join("&");
HUXcore.xhr(opt);
HUXcore.preventDefault(ev);
}
};
HUXcore.addModule(HUX_Form);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HUX_checker = {
init: function(){
this.check_targets();
},
logError: function(strErr){
// we call HUXcore.logError with a timeout in order to avoid to stop the rest of the execution
setTimeout(HUXcore.logError, 0, strErr);
},
__get_targets: function(){
var targets = [];
HUXcore.Selector.byAttribute("a", "href", null, function(el){
var re = /!([^=]+)/g, res;
while( (res = re.exec(el.href)) !== null )
targets.push( res[1] );
});
HUXcore.Selector.byAttributeHUX("*", "targetnode", null, function(el){
targets.push( HUXcore.HUXattr.getAttributeHUX(el, "targetnode") );
});
return targets;
},
check_targets: function(){
var targets = this.__get_targets();
HUXcore.Compat.forEach(targets, function(targ){
if( document.getElementById(targ) === null){
this.logError("No Element of id=\""+targ+"\" found");
}
});
}
};
HUXcore.addModule( HUX_checker );