1 // (c) Benoît PIN 2006-2007
11 var isTextMime
= /^text\/.+/i;
13 FragmentImporter = function(url
, onAfterPopulate
) {
14 var thisImporter
= this;
16 this.onAfterPopulate
= (!onAfterPopulate
) ? function(){return;} : onAfterPopulate
;
19 FragmentImporter
.prototype._load = function(url
) {
20 var req
= new XMLHttpRequest();
21 var thisImporter
= this;
22 req
.onreadystatechange = function() {
23 switch (req
.readyState
) {
29 if (! isTextMime
.exec(req
.getResponseHeader('Content-Type'))) {
30 req
.onreadystatechange
= null;
33 window
.location
.href
= thisImporter
._fallBackUrl
;
40 if (req
.status
=== 200) {
41 thisImporter
.populateBaseElement(req
); }
43 alert('Error: ' + req
.status
); }
48 req
.open("GET", url
, true);
52 FragmentImporter
.prototype.load = function(fallBackUrl
) {
54 this._fallBackUrl
= fallBackUrl
; }
56 this._fallBackUrl
= this.url
; }
60 FragmentImporter
.prototype.useMacro = function(template
, macro
, fragmentId
, queryString
) {
61 var url
= this.url
+ "/use_macro?template=" + template
+ "¯o=" + macro
+ "&fragmentId=" + fragmentId
;
63 url
+= '&' + queryString
; }
68 FragmentImporter
.prototype.populateBaseElement = function(req
) {
69 // :( IE : innerHTML is read-only for these tags:
70 // COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR
71 var contentType
= req
.getResponseHeader('Content-Type');
72 if (! isTextMime
.exec(contentType
)) {
73 window
.location
.href
= this._fallBackUrl
;
76 if (contentType
.indexOf('text/xml') !== -1) {
77 var fragments
= req
.responseXML
.documentElement
.childNodes
;
78 var fragment
, dest
, scripts
, i
, j
;
79 for (i
=0 ; i
< fragments
.length
; i
++) {
80 fragment
= fragments
[i
];
81 if (fragment
.nodeName
=== 'fragment') {
82 dest
= document
.getElementById(fragment
.getAttribute('id'));
84 dest
.innerHTML
= fragment
.firstChild
.nodeValue
;
85 scripts
= dest
.getElementsByTagName('script');
86 for (j
=0 ; j
< scripts
.length
; j
++) {
87 globalScriptRegistry
.loadScript(scripts
[j
]); }
92 this.onAfterPopulate();