Mercurial > defr > DualBlog
changeset 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 | d9490757a111 |
children | 55582b82c43d |
files | admin.js |
diffstat | 1 files changed, 79 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/admin.js Mon Mar 31 14:53:49 2008 +0200 +++ b/admin.js Mon Mar 31 15:07:21 2008 +0200 @@ -37,36 +37,66 @@ elt.addEventListener(evt, func, useCpt); else if(elt.attachEvent) elt.attachEvent("on" + evt, func); + else if(elt["on" + evt]) + elt["on" + evt] = function() {elt["on" + evt](); func();} else elt["on" + evt] = func; + }, + getTextContent: function(node) { + if(node.textContent) + return node.textContent; + if(node.innerText) + return node.innerText; + // We tried hard to use something fast, it didn't work, so we'll recurse + if(node.nodeType == 3 || node.nodeType == 4) + return node.data; + var textContent = '', i; + for(i = 0; i < node.childNodes.length; i++) + textContent += t.getTextContent(node.childNodes[i]); + return textContent; + }, + empty: function(node) { + while(node.firstChild) + node.removeChild(node.firstChild); } } t.evtListener(window, "load", xmlRequest.init, false); t.evtListener(window, "load", hideForms, false); +t.evtListener(window, "load", init, false); var editFilter = { + accept: function(node) { + if(node.nodeName.toLowerCase() == 'a' && + node.firstChild && + node.firstChild.nodeValue == 'Editer') + return true; + return false; + }, + acceptNode: function(node) { - if(node.nodeName == 'a' && node.firstChild && node.firstChild.nodeValue == 'Editer') - return NodeFilter.FILTER_ACCEPT; - return NodeFilter.FILTER_SKIP; + return this.accept(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; } } var brouillonsFilter = { - acceptNode:function(node) { - if(node.nodeName == 'a' && node.firstChild && ( + accept: function(node) { + if(node.nodeName.toLowerCase() == 'a' && node.firstChild && ( node.firstChild.nodeValue == 'Editer' || node.firstChild.nodeValue == 'Publier' || node.firstChild.nodeValue == 'Supprimer')) - return NodeFilter.FILTER_ACCEPT; - return NodeFilter.FILTER_SKIP; + return true; + return false; + }, + + acceptNode: function(node) { + return this.accept(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; } } -window.onload=function() { +function init() { var ar=document.getElementsByTagName('li'); for(var i=0; i<ar.length; i++) { if(ar[i].parentNode.id=='Menu') { @@ -82,16 +112,32 @@ } try { dp = new DOMParser();} - catch(e) {dp = new ActiveXObject("Microsoft.XMLDOM");} + catch(e) { + dp = { + obj: new ActiveXObject("Microsoft.XMLDOM"), + parseFromString: function(text, type) + { + if(!this.obj.loadXML(text)) + { + var error = this.obj.parseError.reason, errorNode; + this.obj.loadXML('<error />'); + errorNode = this.obj.createElement('parsererror'); + errorNode.appendChild(this.obj.createTextNode(error)); + this.obj.documentElement.appendChild(errorNode); + } + return this.obj; + } + } + } t.evtListener(document.getElementById("editPost").Save, "click", saveFormContent, false); // TreeWalkers in all their glory try { - wEdit = document.createTreeWalker(document.getElementById("GestPosts"), NodeFilter.SHOW_ALL, editFilter, true); + wEdit = document.createTreeWalker(document.getElementById("GestPosts"), NodeFilter.SHOW_ELEMENT, editFilter, true); while(wEdit.nextNode()) { t.evtListener(wEdit.currentNode, "click", editPubl, false); } - wBr = document.createTreeWalker(document.getElementById("GestBrouillons"), NodeFilter.SHOW_ALL, brouillonsFilter, true); + wBr = document.createTreeWalker(document.getElementById("GestBrouillons"), NodeFilter.SHOW_ELEMENT, brouillonsFilter, true); while(wBr.nextNode()) { if(wBr.currentNode.firstChild.nodeValue == 'Editer') t.evtListener(wBr.currentNode,"click", editBrouillon, false); else if(wBr.currentNode.firstChild.nodeValue == 'Publier') t.evtListener(wBr.currentNode, "click", pubBrouillon, false); @@ -100,7 +146,7 @@ var tmpLinks = document.getElementById("GestPosts") .getElementsByTagName("a"); for(var i = 0; i < tmpLinks.length; i++) { - if(editFilter.acceptNode(tmpLinks[i])) { + if(editFilter.accept(tmpLinks[i])) { t.evtListener(tmpLinks[i], "click", editPubl, false) } } @@ -108,8 +154,11 @@ tmpLinks = document.getElementById("GestBrouillons") .getElementsByTagName("a"); for(var i = 0; i < tmpLinks.length; i++) { - if(brouillonsFilter.acceptNode(tmpLinks[i])) { - t.evtListener(tmpLinks[i], "click", editPubl, false) + if(brouillonsFilter.accept(tmpLinks[i])) { + if(tmpLinks[i].firstChild.nodeValue == 'Editer') + t.evtListener(tmpLinks[i], "click", editBrouillon, false); + else if(tmpLinks[i].firstChild.nodeValue == 'Publier') + t.evtListener(tmpLinks[i], "click", pubBrouillon, false); } } } @@ -146,14 +195,15 @@ whichVersion.style.display='block'; el.pubIsShown = true; var data = xmlRequest.get("admin_xml.php?type=brouillon&id=" + whichVersion.id.value); - var doc = dp.parseFromString("<post>" + data.getElementsByTagName("contenu")[0].firstChild.data + "</post>", "text/xml"); - var parserErrors = doc.getElementsByTagName('parsererror'); - if(parserErrors.length > 0) { - pubComment.textContent = ""; - pubComment.appendChild(doc.firstChild); + var doc = dp.parseFromString("<post>" + t.getTextContent(data.getElementsByTagName("contenu")[0]) + "</post>", "text/xml"); + t.empty(pubComment); + if(doc.getElementsByTagName('parsererror').length > 0) { + var textNode = document.createTextNode(t.getTextContent(doc.documentElement) + "\n"); + pubComment.appendChild(textNode); } else { - pubComment.textContent = "Le post est fait de XML valide, bon pour être poster"; + var textNode = document.createTextNode("Le post est fait de XML valide, bon pour être poster"); + pubComment.appendChild(textNode); whichVersion.pubButton.disabled = false; } } else { @@ -184,21 +234,22 @@ el.editPostIsShown = 1; el.parentNode.parentNode.className = 'Pinned'; editPost.style.display = 'block'; - editPost.Titre.value = data.getElementsByTagName('titre')[0].textContent; - editPost.Contenu.value = data.getElementsByTagName('contenu')[0].firstChild.data; + editPost.Titre.value = t.getTextContent(data.getElementsByTagName('titre')[0]); + editPost.Contenu.value = t.getTextContent(data.getElementsByTagName('contenu')[0]); if(type == 'post') { editPost.mood.style.display = ''; - editPost.mood.value = data.getElementsByTagName('mood')[0].textContent; + editPost.mood.value = t.getTextContent(data.getElementsByTagName('mood')[0]); editPost.Tags.style.display = ''; editPost.Tags.value = ''; - var tags = data.getElementsByTagName('tag'); + var tags = data.getElementsByTagName('tag'), tag; for(var i = 0; i < tags.length; i++) { - if(tags[i].textContent.indexOf(' ') > -1) + tag = t.getTextContent(tags[i]); + if(tag.indexOf(' ') > -1) { - editPost.Tags.value += '"' + tags[i].textContent + '"'; + editPost.Tags.value += '"' + tag + '"'; } else { - editPost.Tags.value += tags[i].textContent; + editPost.Tags.value += tag; } editPost.Tags.value += ' '; } @@ -221,7 +272,7 @@ } function saveFormContent(e) { - var el = e.target.form; // Should be the form... + var el = bpEvt(e).rTarget.form; // Should be the form... var toggleLink = el.parentNode.firstChild; var id=toggleLink.href.substring(toggleLink.href.indexOf("#") + 4, toggleLink.href.length); if(xmlRequest.send('admin_xml.php?id=' + id + '&mode=edit&type=' + el.type,