Preview dans ckeditor : y'a une couille avec la locasization. On met ça en stand-by…
[Plinn.git] / skins / ajax_scripts / fragment_importer.js
1 // (c) Benoît PIN 2006-2007
2 // http://plinn.org
3 // Licence GPL
4 //
5 //
6
7 var FragmentImporter;
8
9 (function(){
10
11 var isTextMime = /^text\/.+/i;
12
13 FragmentImporter = function(url, onAfterPopulate, baseElement) {
14 var thisImporter = this;
15 this.url = url;
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.');
20 };
21
22 FragmentImporter.prototype._load = function(url) {
23 var req = new XMLHttpRequest();
24 var thisImporter = this;
25 req.onreadystatechange = function() {
26 switch (req.readyState) {
27 case 1 :
28 showProgressImage();
29 break;
30 case 2 :
31 try {
32 if (! isTextMime.exec(req.getResponseHeader('Content-Type'))) {
33 req.onreadystatechange = null;
34 req.abort();
35 hideProgressImage();
36 window.location.href = thisImporter._fallBackUrl;
37 }
38 }
39 catch(e){}
40 break;
41 case 4 :
42 hideProgressImage();
43 if (req.status == '200')
44 thisImporter.populateBaseElement(req);
45 else
46 alert('Error: ' + req.status);
47
48 };
49 };
50
51 req.open("GET", url, true);
52 req.send(null);
53 };
54
55 FragmentImporter.prototype.load = function(fallBackUrl) {
56 if (fallBackUrl)
57 this._fallBackUrl = fallBackUrl;
58 else
59 this._fallBackUrl = this.url;
60 this._load(this.url);
61 };
62
63 FragmentImporter.prototype.useMacro = function(template, macro, fragmentId, queryString) {
64 var url = this.url + "/use_macro?template=" + template + "&macro=" + macro + "&fragmentId=" + fragmentId;
65 if (queryString)
66 url += '&' + queryString;
67 this._load(url);
68 };
69
70
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;
77 return;
78 }
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'));
86 if(!dest)
87 continue;
88 dest.innerHTML = fragment.firstChild.nodeValue;
89
90 scripts = dest.getElementsByTagName('script');
91 for (var j=0 ; j < scripts.length ; j++)
92 globalScriptRegistry.loadScript(scripts[j]);
93 }
94 }
95 }
96 else {
97 this.baseElement.innerHTML = req.responseText;
98
99 var scripts = this.baseElement.getElementsByTagName('script');
100 for (var i=0 ; i < scripts.length ; i++)
101 globalScriptRegistry.loadScript(scripts[i]);
102 }
103
104 var onAfterPopulate = this.onAfterPopulate;
105 if (typeof(onAfterPopulate) == "string") {
106 if (console)
107 console.warn('Deprecation warning : onAfterPopulate may not be a string (' + onAfterPopulate + ')');
108 onAfterPopulate = eval(onAfterPopulate);
109 }
110 onAfterPopulate();
111 };
112
113 })();