view admin.js @ 83:d49afe579884

DataAccess: Passe debugQuery en mysqli aussi
author Franck Deroche <franck@defr.org>
date Thu, 22 Nov 2018 23:13:52 +0100
parents 49b0a2f3a118
children
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 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) {
	  return this.accept(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
	}
}

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



function init() {
 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 = {
     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_ELEMENT, editFilter, true);
	 while(wEdit.nextNode()) {
		t.evtListener(wEdit.currentNode, "click", editPubl, false);
	 }
	 
	 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);
	 }
 } catch(e) {
	 var tmpLinks = document.getElementById("GestPosts")
                            .getElementsByTagName("a");
     for(var i = 0; i < tmpLinks.length; i++) {
        if(editFilter.accept(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.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);
        }
     }
 }
}

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>" + 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 {
	    var textNode = document.createTextNode("Le post est fait de XML valide, bon pour être poster");
		pubComment.appendChild(textNode);
		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 = 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 = t.getTextContent(data.getElementsByTagName('mood')[0]);
	    editPost.Tags.style.display = '';
	    editPost.Tags.value = '';
	    var tags = data.getElementsByTagName('tag'), tag;
	    for(var i = 0; i < tags.length; i++)
	    {
		tag = t.getTextContent(tags[i]);
		if(tag.indexOf(' ') > -1)
		{
		    editPost.Tags.value += '"' + tag + '"';
		} else {
		    editPost.Tags.value +=  tag;
		}
		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 = 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,
					 '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;
}