6caaaa23c78386b8db78376b2a2736f5e060e1b5
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 element
, dest
, scripts
, i
, j
;
79 for (i
=0 ; i
< fragments
.length
; i
++) {
80 element
= fragments
[i
];
81 switch (element
.nodeName
) {
83 dest
= document
.getElementById(element
.getAttribute('id'));
85 dest
.innerHTML
= element
.firstChild
.nodeValue
;
86 scripts
= dest
.getElementsByTagName('script');
87 for (j
=0 ; j
< scripts
.length
; j
++) {
88 globalScriptRegistry
.loadScript(scripts
[j
]); }
92 var headBase
= document
.getElementsByTagName('base');
93 if (headBase
.length
) {
94 headBase
[0].setAttribute('href', element
.getAttribute('href'));
95 console
.info('set base href:', headBase
[0].href
);
101 this.onAfterPopulate();