Mercurial > defr > DualBlog
comparison admin.js @ 75:49b0a2f3a118
Fix IE: Rend l'administration utilisable avec Internet Explorer 6
En réalité, seulement trois réels changements sont introduits ici:
- il existait quelques problèmes dans le code supposé être executés lorsque
le navigateur ne connait pas les treewalkers, le code fait désormais ce
qu'il faut (la mauvaise fonction était associé à l'édition des brouillons,
la fonction de test ne renvoyait pas true / false)
- Internet Explorer 6 ne connait pas textContent. Une fonction essayant
diverses possibilités de remplacement, et faisant un parcours récursif de
tous les noeuds fils dans le pire des cas a été mise en place
- le code cherchant un remplacement à DOMParser supposait que l'objet ActiveX
XMLDOM fournissait exactement la même API, ce qui aurait été trop beau. Un
wrapper a été développé autour de XMLDOM qui fournit la seule fonction de
DOMParser utilisé dans le script. Evidemment ce n'est pas une solution
totalement générique...
Avec ces modifications, il est possible de poster depuis Internet Explorer
(testé sur la version 6), Safari (testé avec la version 3.1) et Firefox (testé
avec les versions 2 et 3)
author | Franck Deroche <webmaster@defr.org> |
---|---|
date | Mon, 31 Mar 2008 15:07:21 +0200 |
parents | 0aa843c6571b |
children |
comparison
equal
deleted
inserted
replaced
74:d9490757a111 | 75:49b0a2f3a118 |
---|---|
35 evtListener:function(elt, evt, func, useCpt) { | 35 evtListener:function(elt, evt, func, useCpt) { |
36 if(elt.addEventListener) | 36 if(elt.addEventListener) |
37 elt.addEventListener(evt, func, useCpt); | 37 elt.addEventListener(evt, func, useCpt); |
38 else if(elt.attachEvent) | 38 else if(elt.attachEvent) |
39 elt.attachEvent("on" + evt, func); | 39 elt.attachEvent("on" + evt, func); |
40 else if(elt["on" + evt]) | |
41 elt["on" + evt] = function() {elt["on" + evt](); func();} | |
40 else | 42 else |
41 elt["on" + evt] = func; | 43 elt["on" + evt] = func; |
44 }, | |
45 getTextContent: function(node) { | |
46 if(node.textContent) | |
47 return node.textContent; | |
48 if(node.innerText) | |
49 return node.innerText; | |
50 // We tried hard to use something fast, it didn't work, so we'll recurse | |
51 if(node.nodeType == 3 || node.nodeType == 4) | |
52 return node.data; | |
53 var textContent = '', i; | |
54 for(i = 0; i < node.childNodes.length; i++) | |
55 textContent += t.getTextContent(node.childNodes[i]); | |
56 return textContent; | |
57 }, | |
58 empty: function(node) { | |
59 while(node.firstChild) | |
60 node.removeChild(node.firstChild); | |
42 } | 61 } |
43 } | 62 } |
44 | 63 |
45 t.evtListener(window, "load", xmlRequest.init, false); | 64 t.evtListener(window, "load", xmlRequest.init, false); |
46 t.evtListener(window, "load", hideForms, false); | 65 t.evtListener(window, "load", hideForms, false); |
66 t.evtListener(window, "load", init, false); | |
47 | 67 |
48 var editFilter = { | 68 var editFilter = { |
69 accept: function(node) { | |
70 if(node.nodeName.toLowerCase() == 'a' && | |
71 node.firstChild && | |
72 node.firstChild.nodeValue == 'Editer') | |
73 return true; | |
74 return false; | |
75 }, | |
76 | |
49 acceptNode: function(node) { | 77 acceptNode: function(node) { |
50 if(node.nodeName == 'a' && node.firstChild && node.firstChild.nodeValue == 'Editer') | 78 return this.accept(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; |
51 return NodeFilter.FILTER_ACCEPT; | |
52 return NodeFilter.FILTER_SKIP; | |
53 } | 79 } |
54 } | 80 } |
55 | 81 |
56 var brouillonsFilter = { | 82 var brouillonsFilter = { |
57 acceptNode:function(node) { | 83 accept: function(node) { |
58 if(node.nodeName == 'a' && node.firstChild && ( | 84 if(node.nodeName.toLowerCase() == 'a' && node.firstChild && ( |
59 node.firstChild.nodeValue == 'Editer' || | 85 node.firstChild.nodeValue == 'Editer' || |
60 node.firstChild.nodeValue == 'Publier' || | 86 node.firstChild.nodeValue == 'Publier' || |
61 node.firstChild.nodeValue == 'Supprimer')) | 87 node.firstChild.nodeValue == 'Supprimer')) |
62 return NodeFilter.FILTER_ACCEPT; | 88 return true; |
63 return NodeFilter.FILTER_SKIP; | 89 return false; |
64 } | 90 }, |
65 } | 91 |
66 | 92 acceptNode: function(node) { |
67 | 93 return this.accept(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; |
68 | 94 } |
69 window.onload=function() { | 95 } |
96 | |
97 | |
98 | |
99 function init() { | |
70 var ar=document.getElementsByTagName('li'); | 100 var ar=document.getElementsByTagName('li'); |
71 for(var i=0; i<ar.length; i++) { | 101 for(var i=0; i<ar.length; i++) { |
72 if(ar[i].parentNode.id=='Menu') { | 102 if(ar[i].parentNode.id=='Menu') { |
73 arLIMenu[arLIMenu.length]=ar[i]; | 103 arLIMenu[arLIMenu.length]=ar[i]; |
74 if(ar[i].firstChild.id=='Brouillons') { | 104 if(ar[i].firstChild.id=='Brouillons') { |
80 if(ar[i].className=='Add') | 110 if(ar[i].className=='Add') |
81 t.evtListener(ar[i].firstChild, 'click', showAddForm, true); | 111 t.evtListener(ar[i].firstChild, 'click', showAddForm, true); |
82 } | 112 } |
83 | 113 |
84 try { dp = new DOMParser();} | 114 try { dp = new DOMParser();} |
85 catch(e) {dp = new ActiveXObject("Microsoft.XMLDOM");} | 115 catch(e) { |
116 dp = { | |
117 obj: new ActiveXObject("Microsoft.XMLDOM"), | |
118 parseFromString: function(text, type) | |
119 { | |
120 if(!this.obj.loadXML(text)) | |
121 { | |
122 var error = this.obj.parseError.reason, errorNode; | |
123 this.obj.loadXML('<error />'); | |
124 errorNode = this.obj.createElement('parsererror'); | |
125 errorNode.appendChild(this.obj.createTextNode(error)); | |
126 this.obj.documentElement.appendChild(errorNode); | |
127 } | |
128 return this.obj; | |
129 } | |
130 } | |
131 } | |
86 t.evtListener(document.getElementById("editPost").Save, "click", saveFormContent, false); | 132 t.evtListener(document.getElementById("editPost").Save, "click", saveFormContent, false); |
87 // TreeWalkers in all their glory | 133 // TreeWalkers in all their glory |
88 try { | 134 try { |
89 wEdit = document.createTreeWalker(document.getElementById("GestPosts"), NodeFilter.SHOW_ALL, editFilter, true); | 135 wEdit = document.createTreeWalker(document.getElementById("GestPosts"), NodeFilter.SHOW_ELEMENT, editFilter, true); |
90 while(wEdit.nextNode()) { | 136 while(wEdit.nextNode()) { |
91 t.evtListener(wEdit.currentNode, "click", editPubl, false); | 137 t.evtListener(wEdit.currentNode, "click", editPubl, false); |
92 } | 138 } |
93 | 139 |
94 wBr = document.createTreeWalker(document.getElementById("GestBrouillons"), NodeFilter.SHOW_ALL, brouillonsFilter, true); | 140 wBr = document.createTreeWalker(document.getElementById("GestBrouillons"), NodeFilter.SHOW_ELEMENT, brouillonsFilter, true); |
95 while(wBr.nextNode()) { | 141 while(wBr.nextNode()) { |
96 if(wBr.currentNode.firstChild.nodeValue == 'Editer') t.evtListener(wBr.currentNode,"click", editBrouillon, false); | 142 if(wBr.currentNode.firstChild.nodeValue == 'Editer') t.evtListener(wBr.currentNode,"click", editBrouillon, false); |
97 else if(wBr.currentNode.firstChild.nodeValue == 'Publier') t.evtListener(wBr.currentNode, "click", pubBrouillon, false); | 143 else if(wBr.currentNode.firstChild.nodeValue == 'Publier') t.evtListener(wBr.currentNode, "click", pubBrouillon, false); |
98 } | 144 } |
99 } catch(e) { | 145 } catch(e) { |
100 var tmpLinks = document.getElementById("GestPosts") | 146 var tmpLinks = document.getElementById("GestPosts") |
101 .getElementsByTagName("a"); | 147 .getElementsByTagName("a"); |
102 for(var i = 0; i < tmpLinks.length; i++) { | 148 for(var i = 0; i < tmpLinks.length; i++) { |
103 if(editFilter.acceptNode(tmpLinks[i])) { | 149 if(editFilter.accept(tmpLinks[i])) { |
104 t.evtListener(tmpLinks[i], "click", editPubl, false) | 150 t.evtListener(tmpLinks[i], "click", editPubl, false) |
105 } | 151 } |
106 } | 152 } |
107 | 153 |
108 tmpLinks = document.getElementById("GestBrouillons") | 154 tmpLinks = document.getElementById("GestBrouillons") |
109 .getElementsByTagName("a"); | 155 .getElementsByTagName("a"); |
110 for(var i = 0; i < tmpLinks.length; i++) { | 156 for(var i = 0; i < tmpLinks.length; i++) { |
111 if(brouillonsFilter.acceptNode(tmpLinks[i])) { | 157 if(brouillonsFilter.accept(tmpLinks[i])) { |
112 t.evtListener(tmpLinks[i], "click", editPubl, false) | 158 if(tmpLinks[i].firstChild.nodeValue == 'Editer') |
159 t.evtListener(tmpLinks[i], "click", editBrouillon, false); | |
160 else if(tmpLinks[i].firstChild.nodeValue == 'Publier') | |
161 t.evtListener(tmpLinks[i], "click", pubBrouillon, false); | |
113 } | 162 } |
114 } | 163 } |
115 } | 164 } |
116 } | 165 } |
117 | 166 |
144 whichVersion.id.value = el.href.substring(pos + 4, el.href.length); | 193 whichVersion.id.value = el.href.substring(pos + 4, el.href.length); |
145 el.parentNode.appendChild(whichVersion); | 194 el.parentNode.appendChild(whichVersion); |
146 whichVersion.style.display='block'; | 195 whichVersion.style.display='block'; |
147 el.pubIsShown = true; | 196 el.pubIsShown = true; |
148 var data = xmlRequest.get("admin_xml.php?type=brouillon&id=" + whichVersion.id.value); | 197 var data = xmlRequest.get("admin_xml.php?type=brouillon&id=" + whichVersion.id.value); |
149 var doc = dp.parseFromString("<post>" + data.getElementsByTagName("contenu")[0].firstChild.data + "</post>", "text/xml"); | 198 var doc = dp.parseFromString("<post>" + t.getTextContent(data.getElementsByTagName("contenu")[0]) + "</post>", "text/xml"); |
150 var parserErrors = doc.getElementsByTagName('parsererror'); | 199 t.empty(pubComment); |
151 if(parserErrors.length > 0) { | 200 if(doc.getElementsByTagName('parsererror').length > 0) { |
152 pubComment.textContent = ""; | 201 var textNode = document.createTextNode(t.getTextContent(doc.documentElement) + "\n"); |
153 pubComment.appendChild(doc.firstChild); | 202 pubComment.appendChild(textNode); |
154 } | 203 } |
155 else { | 204 else { |
156 pubComment.textContent = "Le post est fait de XML valide, bon pour être poster"; | 205 var textNode = document.createTextNode("Le post est fait de XML valide, bon pour être poster"); |
206 pubComment.appendChild(textNode); | |
157 whichVersion.pubButton.disabled = false; | 207 whichVersion.pubButton.disabled = false; |
158 } | 208 } |
159 } else { | 209 } else { |
160 curBPShown = 0; | 210 curBPShown = 0; |
161 el.parentNode.parentNode.className = ''; | 211 el.parentNode.parentNode.className = ''; |
182 var data = xmlRequest.get("admin_xml.php?type=" + type + "&id=" + id); | 232 var data = xmlRequest.get("admin_xml.php?type=" + type + "&id=" + id); |
183 el.parentNode.appendChild(editPost); | 233 el.parentNode.appendChild(editPost); |
184 el.editPostIsShown = 1; | 234 el.editPostIsShown = 1; |
185 el.parentNode.parentNode.className = 'Pinned'; | 235 el.parentNode.parentNode.className = 'Pinned'; |
186 editPost.style.display = 'block'; | 236 editPost.style.display = 'block'; |
187 editPost.Titre.value = data.getElementsByTagName('titre')[0].textContent; | 237 editPost.Titre.value = t.getTextContent(data.getElementsByTagName('titre')[0]); |
188 editPost.Contenu.value = data.getElementsByTagName('contenu')[0].firstChild.data; | 238 editPost.Contenu.value = t.getTextContent(data.getElementsByTagName('contenu')[0]); |
189 if(type == 'post') { | 239 if(type == 'post') { |
190 editPost.mood.style.display = ''; | 240 editPost.mood.style.display = ''; |
191 editPost.mood.value = data.getElementsByTagName('mood')[0].textContent; | 241 editPost.mood.value = t.getTextContent(data.getElementsByTagName('mood')[0]); |
192 editPost.Tags.style.display = ''; | 242 editPost.Tags.style.display = ''; |
193 editPost.Tags.value = ''; | 243 editPost.Tags.value = ''; |
194 var tags = data.getElementsByTagName('tag'); | 244 var tags = data.getElementsByTagName('tag'), tag; |
195 for(var i = 0; i < tags.length; i++) | 245 for(var i = 0; i < tags.length; i++) |
196 { | 246 { |
197 if(tags[i].textContent.indexOf(' ') > -1) | 247 tag = t.getTextContent(tags[i]); |
248 if(tag.indexOf(' ') > -1) | |
198 { | 249 { |
199 editPost.Tags.value += '"' + tags[i].textContent + '"'; | 250 editPost.Tags.value += '"' + tag + '"'; |
200 } else { | 251 } else { |
201 editPost.Tags.value += tags[i].textContent; | 252 editPost.Tags.value += tag; |
202 } | 253 } |
203 editPost.Tags.value += ' '; | 254 editPost.Tags.value += ' '; |
204 } | 255 } |
205 } else { | 256 } else { |
206 editPost.mood.style.display = 'none'; | 257 editPost.mood.style.display = 'none'; |
219 while(targetAc.nodeName.toLowerCase()!='form') targetAc=targetAc.nextSibling; // Permet d'avoir eventuellement des espaces | 270 while(targetAc.nodeName.toLowerCase()!='form') targetAc=targetAc.nextSibling; // Permet d'avoir eventuellement des espaces |
220 targetAc.style.display = (targetAc.style.display=='block')?'none':'block'; | 271 targetAc.style.display = (targetAc.style.display=='block')?'none':'block'; |
221 } | 272 } |
222 | 273 |
223 function saveFormContent(e) { | 274 function saveFormContent(e) { |
224 var el = e.target.form; // Should be the form... | 275 var el = bpEvt(e).rTarget.form; // Should be the form... |
225 var toggleLink = el.parentNode.firstChild; | 276 var toggleLink = el.parentNode.firstChild; |
226 var id=toggleLink.href.substring(toggleLink.href.indexOf("#") + 4, toggleLink.href.length); | 277 var id=toggleLink.href.substring(toggleLink.href.indexOf("#") + 4, toggleLink.href.length); |
227 if(xmlRequest.send('admin_xml.php?id=' + id + '&mode=edit&type=' + el.type, | 278 if(xmlRequest.send('admin_xml.php?id=' + id + '&mode=edit&type=' + el.type, |
228 'title=' + postValue(el.Titre.value) + | 279 'title=' + postValue(el.Titre.value) + |
229 '&content=' + postValue(el.Contenu.value) + | 280 '&content=' + postValue(el.Contenu.value) + |