comparison admin.js @ 0:629389204276

Import initial
author Franck Deroche <webmaster@defr.org>
date Sat, 20 Oct 2007 14:15:10 +0200
parents
children 750613da68ec
comparison
equal deleted inserted replaced
-1:000000000000 0:629389204276
1 var arLIMenu = new Array();
2 var curBPShown = 0;
3 var wEdit, wBr, dp;
4
5 var xmlRequest = {
6 init: function() {
7 try {
8 xmlRequest.obj = new XMLHttpRequest();
9 } catch(e) {
10 xmlRequest.obj = new ActiveXObject("Msxml2.XMLHTTP");
11 }
12 },
13
14 get: function(url) {
15 xmlRequest.obj.open("GET", url, false);
16 xmlRequest.obj.send(null);
17
18 return xmlRequest.obj.responseXML;
19 },
20
21 send: function(url, data) {
22 try {
23 xmlRequest.obj.open("POST", url, false);
24 xmlRequest.obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
25 xmlRequest.obj.send(data);
26 return true;
27 } catch(e) {
28 return false;
29 }
30 }
31
32 };
33
34 var t = {
35 evtListener:function(elt, evt, func, useCpt) {
36 if(elt.addEventListener)
37 elt.addEventListener(evt, func, useCpt);
38 else if(elt.attachEvent)
39 elt.attachEvent("on" + evt, func);
40 else
41 elt["on" + evt] = func;
42 }
43 }
44
45 t.evtListener(window, "load", xmlRequest.init, false);
46 t.evtListener(window, "load", hideForms, false);
47
48 var editFilter = {
49 acceptNode: function(node) {
50 if(node.nodeName == 'a' && node.firstChild && node.firstChild.nodeValue == 'Editer') return NodeFilter.FILTER_ACCEPT;
51 return NodeFilter.FILTER_SKIP;
52 }
53 }
54
55 var brouillonsFilter = {
56 acceptNode:function(node) {
57 if(node.nodeName == 'a' && node.firstChild && (
58 node.firstChild.nodeValue == 'Editer' ||
59 node.firstChild.nodeValue == 'Publier' ||
60 node.firstChild.nodeValue == 'Supprimer'))
61 return NodeFilter.FILTER_ACCEPT;
62 return NodeFilter.FILTER_SKIP;
63 }
64 }
65
66
67
68 window.onload=function() {
69 var ar=document.getElementsByTagName('li');
70 for(var i=0; i<ar.length; i++) {
71 if(ar[i].parentNode.id=='Menu') {
72 arLIMenu[arLIMenu.length]=ar[i];
73 if(ar[i].firstChild.id=='Brouillons') {
74 ar[i].className="Actif";
75 document.getElementById('Gest' + ar[i].firstChild.id).style.display='block';
76 }
77 t.evtListener(ar[i], 'click', switchSection, true);
78 }
79 if(ar[i].className=='Add')
80 t.evtListener(ar[i].firstChild, 'click', showAddForm, true);
81 }
82
83 try { dp = new DOMParser();}
84 catch(e) {dp = new ActiveXObject("Microsoft.XMLDOM");}
85 t.evtListener(document.getElementById("editPost").Save, "click", saveFormContent, false);
86 // TreeWalkers in all their glory
87 try {
88 wEdit = document.createTreeWalker(document.getElementById("GestPosts"), NodeFilter.SHOW_ALL, editFilter, true);
89 while(wEdit.nextNode()) {
90 t.evtListener(wEdit.currentNode, "click", editPubl, false);
91 }
92
93 wBr = document.createTreeWalker(document.getElementById("GestBrouillons"), NodeFilter.SHOW_ALL, brouillonsFilter, true);
94 while(wBr.nextNode()) {
95 if(wBr.currentNode.firstChild.nodeValue == 'Editer') t.evtListener(wBr.currentNode,"click", editBrouillon, false);
96 else if(wBr.currentNode.firstChild.nodeValue == 'Publier') t.evtListener(wBr.currentNode, "click", pubBrouillon, false);
97 }
98 } catch(e) {
99 //Well, right now, we're sort of screwed...
100 }
101 }
102
103 function switchSection(e) {
104 var targetAc = bpEvt(e, 'li').rTarget;
105 for(i=0; i<arLIMenu.length; i++) {
106 tmpObj=document.getElementById('Gest' + arLIMenu[i].firstChild.id);
107 if(arLIMenu[i]==targetAc) {
108 arLIMenu[i].className='Actif';
109 tmpObj.style.display='block';
110 } else {
111 arLIMenu[i].className='';
112 tmpObj.style.display='none';
113 }
114 }
115 }
116
117
118 function pubBrouillon(e) {
119 var whichVersion = document.getElementById("whichVersion");
120 var pubComment = document.getElementById("pubComment");
121 var el = bpEvt(e).rTarget;
122 if(el != curBPShown) {
123 if(curBPShown != 0) curBPShown.parentNode.parentNode.className = '';
124 curBPShown = el;
125 el.parentNode.parentNode.className = 'Pinned';
126 var pos = el.href.indexOf("#");
127 whichVersion.pubButton.disabled = true;
128 pubComment.textContent = "Vérification en cours de la validité XML du post";
129 whichVersion.id.value = el.href.substring(pos + 4, el.href.length);
130 el.parentNode.appendChild(whichVersion);
131 whichVersion.style.display='block';
132 el.pubIsShown = true;
133 var data = xmlRequest.get("admin_xml.php?type=brouillon&id=" + whichVersion.id.value);
134 var doc = dp.parseFromString("<post>" + data.getElementsByTagName("contenu")[0].firstChild.data + "</post>", "text/xml");
135 var parserErrors = doc.getElementsByTagName('parsererror');
136 if(parserErrors.length > 0) {
137 pubComment.textContent = "";
138 pubComment.appendChild(doc.firstChild);
139 }
140 else {
141 pubComment.textContent = "Le post est fait de XML valide, bon pour être poster";
142 whichVersion.pubButton.disabled = false;
143 }
144 } else {
145 curBPShown = 0;
146 el.parentNode.parentNode.className = '';
147 hideForms();
148 el.pubIsShown = false;
149 }
150 return false;
151 }
152
153 function editBrouillon(e) {
154 edit(e, "brouillon");
155 }
156
157 function editPubl(e) {
158 edit(e, "post");
159 }
160
161 function edit(e, type) {
162 var editPost = document.getElementById("editPost");
163 editPost.type = type;
164 var el = bpEvt(e).rTarget;
165 if(el.editPostIsShown !=1) {
166 var id = el.href.substring(el.href.indexOf("#") + 4, el.href.length);
167 var data = xmlRequest.get("admin_xml.php?type=" + type + "&id=" + id);
168 el.parentNode.appendChild(editPost);
169 el.editPostIsShown = 1;
170 editPost.style.display = 'block';
171 editPost.Titre.value = data.getElementsByTagName('titre')[0].textContent;
172 editPost.Contenu.value = data.getElementsByTagName('contenu')[0].firstChild.data;
173 if(type == 'post') {
174 editPost.mood.style.display = '';
175 editPost.mood.value = data.getElementsByTagName('mood')[0].textContent;
176 editPost.Tags.style.display = '';
177 editPost.Tags.value = '';
178 var tags = data.getElementsByTagName('tag');
179 for(var i = 0; i < tags.length; i++)
180 {
181 if(tags[i].textContent.indexOf(' ') > -1)
182 {
183 editPost.Tags.value += '"' + tags[i].textContent + '"';
184 } else {
185 editPost.Tags.value += tags[i].textContent;
186 }
187 editPost.Tags.value += ' ';
188 }
189 } else {
190 editPost.mood.style.display = 'none';
191 editPost.Tags.style.display = 'none';
192 }
193
194 } else {
195 hideForms();
196 el.editPostIsShown = 0;
197 }
198 }
199
200 function showAddForm(e) {
201 var targetAc = bpEvt(e, "a").rTarget;
202 while(targetAc.nodeName.toLowerCase()!='form') targetAc=targetAc.nextSibling; // Permet d'avoir eventuellement des espaces
203 targetAc.style.display = (targetAc.style.display=='block')?'none':'block';
204 }
205
206 function saveFormContent(e) {
207 var el = e.target.form; // Should be the form...
208 var toggleLink = el.parentNode.firstChild;
209 var id=toggleLink.href.substring(toggleLink.href.indexOf("#") + 4, toggleLink.href.length);
210 if(xmlRequest.send('admin_xml.php?id=' + id + '&mode=edit&type=' + el.type,
211 'title=' + postValue(el.Titre.value) +
212 '&content=' + postValue(el.Contenu.value) +
213 '&mood=' + postValue(el.mood.value) +
214 '&tags=' + postValue(el.Tags.value)))
215 {
216 toggleLink.editPostIsShown = 0;
217 hideForms();
218 } else {
219 el.submit();
220 }
221 return true;
222 }
223
224 function hideForms() {
225 document.getElementById('whichVersion').style.display='none';
226 document.getElementById('editPost').style.display='none';
227 }
228
229 /* Bullet-Proofing of the event variable... Needed because of the huge mess out there */
230 function bpEvt(eIni, tag) {
231 var e = (eIni)?eIni:window.event;
232 e.rTarget = (e.target)?e.target:e.srcElement;
233 if(tag != undefined)
234 while(e.rTarget.parentNode && e.rTarget.nodeName.toLowerCase() != tag) e.rTarget = e.rTarget.parentNode;
235 return e;
236 }
237
238 function postValue(str)
239 {
240 str = str.replace(/&/g, "%26");
241 str = str.replace(/\+/g, "%2B");
242 return str;
243 }