view admin.js @ 59:caef2b6e5690

Necessaire pour IE ! Le tag est sinon vide et se retrouve compacté J'avais modifie ca pour rendre le validateur HTML5 content, mais, ce n'est pas vraiment une bonne idee, car sinon le parser XML voit un tag vide, et par consequent lorsqu'il prepare la sortie, il produit un <script /> qu'IE est incapable de gerer correctement.
author Franck Deroche <webmaster@defr.org>
date Tue, 19 Feb 2008 02:54:58 -0800
parents 0aa843c6571b
children 49b0a2f3a118
line wrap: on
line source
var arLIMenu = new Array();
var curBPShown = 0;
var wEdit, wBr, dp;

var xmlRequest = {
	init: function() {
		try {
			xmlRequest.obj = new XMLHttpRequest();
		} catch(e) {
			xmlRequest.obj = new ActiveXObject("Msxml2.XMLHTTP");
		}
	},
	
	get: function(url) {
		xmlRequest.obj.open("GET", url, false);
		xmlRequest.obj.send(null);
		
		return xmlRequest.obj.responseXML;
	},
	
	send: function(url, data) {
		try {
			xmlRequest.obj.open("POST", url, false);
			xmlRequest.obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
			xmlRequest.obj.send(data);
			return true;
		} catch(e) {
			return false;
		}
	}
	
};

var t = {
	evtListener:function(elt, evt, func, useCpt) {
		if(elt.addEventListener)
			elt.addEventListener(evt, func, useCpt);
		else if(elt.attachEvent)
			elt.attachEvent("on" + evt, func);
		else
			elt["on" + evt] = func;
	}
}

t.evtListener(window, "load", xmlRequest.init, false);
t.evtListener(window, "load", hideForms, false);

var editFilter = {
	acceptNode: function(node) {
		if(node.nodeName == 'a' && node.firstChild && node.firstChild.nodeValue == 'Editer') 
			return NodeFilter.FILTER_ACCEPT;
		return NodeFilter.FILTER_SKIP;
	}
}

var brouillonsFilter = {
	acceptNode:function(node) {
		if(node.nodeName == 'a' && node.firstChild && (
			node.firstChild.nodeValue == 'Editer' ||
			node.firstChild.nodeValue == 'Publier' ||
			node.firstChild.nodeValue == 'Supprimer'))
				return NodeFilter.FILTER_ACCEPT;
		return NodeFilter.FILTER_SKIP;
	}
}



window.onload=function() {
 var ar=document.getElementsByTagName('li');
 for(var i=0; i<ar.length; i++) {
  if(ar[i].parentNode.id=='Menu') {
  	arLIMenu[arLIMenu.length]=ar[i];
	if(ar[i].firstChild.id=='Brouillons') {
		ar[i].className="Actif";
		document.getElementById('Gest' + ar[i].firstChild.id).style.display='block';
	}
	t.evtListener(ar[i], 'click', switchSection, true);
   }
   if(ar[i].className=='Add')
     t.evtListener(ar[i].firstChild, 'click', showAddForm, true);
 }

 try { dp = new DOMParser();}
 catch(e) {dp = new ActiveXObject("Microsoft.XMLDOM");}
 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);
	 while(wEdit.nextNode()) {
		t.evtListener(wEdit.currentNode, "click", editPubl, false);
	 }
	 
	 wBr = document.createTreeWalker(document.getElementById("GestBrouillons"), NodeFilter.SHOW_ALL, 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);
	 }
 } catch(e) {
	 var tmpLinks = document.getElementById("GestPosts")
                            .getElementsByTagName("a");
     for(var i = 0; i < tmpLinks.length; i++) {
        if(editFilter.acceptNode(tmpLinks[i])) {
            t.evtListener(tmpLinks[i], "click", editPubl, false)
        }
     }

     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)
        }
     }
 }
}

function switchSection(e) {
 var targetAc = bpEvt(e, 'li').rTarget;
 for(i=0; i<arLIMenu.length; i++) {
  tmpObj=document.getElementById('Gest' + arLIMenu[i].firstChild.id);
   if(arLIMenu[i]==targetAc) {
    arLIMenu[i].className='Actif';
    tmpObj.style.display='block';
   } else {
    arLIMenu[i].className='';
    tmpObj.style.display='none';
   }
 }
}


function pubBrouillon(e) {
 var whichVersion = document.getElementById("whichVersion");
 var pubComment = document.getElementById("pubComment");
 var el = bpEvt(e).rTarget;
 if(el != curBPShown) {
	if(curBPShown != 0) curBPShown.parentNode.parentNode.className = '';
	curBPShown = el;
	el.parentNode.parentNode.className = 'Pinned';
	var pos = el.href.indexOf("#");
	whichVersion.pubButton.disabled = true;
	pubComment.textContent = "Vérification en cours de la validité XML du post";
	whichVersion.id.value = el.href.substring(pos + 4, el.href.length);
	el.parentNode.appendChild(whichVersion);
	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);
	}
	else {
		pubComment.textContent = "Le post est fait de XML valide, bon pour être poster";
		whichVersion.pubButton.disabled = false;
	}
 } else {
	curBPShown = 0;
	el.parentNode.parentNode.className = '';
	hideForms();
	el.pubIsShown = false;
 }
 return false;
}

function editBrouillon(e) {
 edit(e, "brouillon");
}

function editPubl(e) {
 edit(e, "post");
}

function edit(e, type) {
 var editPost = document.getElementById("editPost");
 editPost.type = type;
 var el = bpEvt(e).rTarget;
 if(el.editPostIsShown !=1) {
	var id = el.href.substring(el.href.indexOf("#") + 4, el.href.length);
	var data = xmlRequest.get("admin_xml.php?type=" + type + "&id=" + id);
	el.parentNode.appendChild(editPost);
	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;
	if(type == 'post') {
	    editPost.mood.style.display = '';
	    editPost.mood.value = data.getElementsByTagName('mood')[0].textContent;
	    editPost.Tags.style.display = '';
	    editPost.Tags.value = '';
	    var tags = data.getElementsByTagName('tag');
	    for(var i = 0; i < tags.length; i++)
	    {
		if(tags[i].textContent.indexOf(' ') > -1)
		{
		    editPost.Tags.value += '"' + tags[i].textContent + '"';
		} else {
		    editPost.Tags.value +=  tags[i].textContent;
		}
		editPost.Tags.value += ' ';
	    }
	} else {
	    editPost.mood.style.display = 'none';
	    editPost.Tags.style.display = 'none';
	}
	
 } else {
	hideForms();
	el.parentNode.parentNode.className = '';
	el.editPostIsShown = 0;
 }
}

function showAddForm(e) {
 var targetAc = bpEvt(e, "a").rTarget;
 while(targetAc.nodeName.toLowerCase()!='form') targetAc=targetAc.nextSibling; // Permet d'avoir eventuellement des espaces
 targetAc.style.display = (targetAc.style.display=='block')?'none':'block';
}

function saveFormContent(e) {
    var el = e.target.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,
					 'title=' + postValue(el.Titre.value) + 
					 '&content=' + postValue(el.Contenu.value) +
					 '&mood=' + postValue(el.mood.value) +
					 '&tags=' + postValue(el.Tags.value)))
    {
	  toggleLink.editPostIsShown = 0;
	  toggleLink.parentNode.parentNode.className = '';
	  hideForms();
    } else {
	  el.submit();
    }
    return true;
}

function hideForms() {
  document.getElementById('whichVersion').style.display='none';
  document.getElementById('editPost').style.display='none';
}

/* Bullet-Proofing of the event variable... Needed because of the huge mess out there */
function bpEvt(eIni, tag) {
	var e = (eIni)?eIni:window.event;
	e.rTarget = (e.target)?e.target:e.srcElement;
	if(tag != undefined)
		while(e.rTarget.parentNode && e.rTarget.nodeName.toLowerCase() != tag) e.rTarget = e.rTarget.parentNode;
	return e;
}

function postValue(str)
{
    str = str.replace(/&/g, "%26");
    str = str.replace(/\+/g, "%2B");
    return str;
}