1 // (c) Benoît PIN 2006-2007
11 var isTextMime
= /^text\/.+/i;
13 FragmentImporter = function(url
, onAfterPopulate
, baseElement
) {
14 var thisImporter
= this;
16 this.onAfterPopulate
= (!onAfterPopulate
) ? function(){return;} : onAfterPopulate
;
17 this.baseElement
= baseElement
;
18 if (baseElement
&& window
.console
)
19 console
.warn('Deprecation warning : usage of baseElement will be removed. Use an xml based response.');
22 FragmentImporter
.prototype._load = function(url
) {
23 var req
= new XMLHttpRequest();
24 var thisImporter
= this;
25 req
.onreadystatechange = function() {
26 switch (req
.readyState
) {
32 if (! isTextMime
.exec(req
.getResponseHeader('Content-Type'))) {
33 req
.onreadystatechange
= null;
36 window
.location
.href
= thisImporter
._fallBackUrl
;
43 if (req
.status
== '200')
44 thisImporter
.populateBaseElement(req
);
46 alert('Error: ' + req
.status
);
51 req
.open("GET", url
, true);
55 FragmentImporter
.prototype.load = function(fallBackUrl
) {
57 this._fallBackUrl
= fallBackUrl
;
59 this._fallBackUrl
= this.url
;
63 FragmentImporter
.prototype.useMacro = function(template
, macro
, fragmentId
, queryString
) {
64 var url
= this.url
+ "/use_macro?template=" + template
+ "¯o=" + macro
+ "&fragmentId=" + fragmentId
;
66 url
+= '&' + queryString
;
71 FragmentImporter
.prototype.populateBaseElement = function(req
) {
72 // :( IE : innerHTML is read-only for these tags:
73 // COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR
74 var contentType
= req
.getResponseHeader('Content-Type');
75 if (! isTextMime
.exec(contentType
)) {
76 window
.location
.href
= this._fallBackUrl
;
79 if (contentType
.indexOf('text/xml') != -1) {
80 var fragments
= req
.responseXML
.documentElement
.childNodes
;
81 var fragment
, dest
, scripts
;
82 for (var i
=0 ; i
< fragments
.length
; i
++) {
83 fragment
= fragments
[i
];
84 if (fragment
.nodeName
== 'fragment') {
85 dest
= document
.getElementById(fragment
.getAttribute('id'));
88 dest
.innerHTML
= fragment
.firstChild
.nodeValue
;
90 scripts
= dest
.getElementsByTagName('script');
91 for (var j
=0 ; j
< scripts
.length
; j
++)
92 globalScriptRegistry
.loadScript(scripts
[j
]);
97 this.baseElement
.innerHTML
= req
.responseText
;
99 var scripts
= this.baseElement
.getElementsByTagName('script');
100 for (var i
=0 ; i
< scripts
.length
; i
++)
101 globalScriptRegistry
.loadScript(scripts
[i
]);
104 var onAfterPopulate
= this.onAfterPopulate
;
105 if (typeof(onAfterPopulate
) == "string") {
107 console
.warn('Deprecation warning : onAfterPopulate may not be a string (' + onAfterPopulate
+ ')');
108 onAfterPopulate
= eval(onAfterPopulate
);