MediaWiki:JSScripts/HotCats

Un article de Wikipèdia, l'enciclopèdia liura.
Salta a la navegació Salta a la cerca

//

Illustracion
  1 /**
  2  * Ajusta de ligams aprèp las categorias per rapidament
  3  * suprimir / modificar / ajustar una categoria sus un article.
  4  * Utiliaz Ajax e l'api MediaWiki per recercar una categoria.
  5  * Version adaptada per WP:oc de [[:commons:MediaWiki:HotCat.js]]
  6  * Auter originala : Magnus Manske
  7  * Adaptacion per WP:oc : Cedric31
  8  * Paramètres personalizables via lo monobook :
  9  * - hotcat_autocommit : permet d'enregistrar automaticament
 10  *   la modif sens tornar passar per la fenèstra d'edicion
 11  * - hotcat_list_down : permet d'afichar la lista de suggestion
 12  *   vèrs lo bas
 13  */
 14 addOnloadHook ( hotcat ) ;
 15 
 16 var hotcat_running = 0 ;
 17 var hotcat_last_v = "" ;
 18 var hotcat_exists_yes = "//upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png" ;
 19 var hotcat_exists_no = "//upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png" ;
 20 
 21 function hotcat () {
 22   if ( hotcat_check_action() ) return ; // Edited page, reloading anyway
 23 
 24   var catlinks = document.getElementById("catlinks");
 25 
 26   if (! catlinks && wgNamespaceNumber %2 == 0) { // all except discussion
 27    // let's create a fake one
 28    var bodyC = document.getElementById("bodyContent");
 29    catlinks = document.createElement("div");
 30    catlinks.id = "catlinks";
 31    catlinks.appendChild(document.createElement("p"));
 32    catlinks.firstChild.className = "catlinks";
 33    bodyC.appendChild(catlinks);
 34   }
 35 
 36   var catline = getElementsByClassName ( document , "p" , "catlinks" ) [0] ;
 37 
 38   if ( catline == null || typeof catline == 'undefined' ) return ;
 39 
 40   hotcat_modify_existing ( catline ) ;
 41   hotcat_append_add_span ( catline ) ;
 42 }
 43 
 44 function hotcat_append_add_span ( catline ) {
 45   var span_add = document.createElement ( "span" ) ;
 46   var span_sep = document.createTextNode ( " | " ) ;
 47   if ( catline.firstChild ) catline.appendChild ( span_sep ) ;
 48   catline.appendChild ( span_add ) ;
 49   hotcat_create_span ( span_add ) ;
 50 }
 51 
 52 String.prototype.ucFirst = function () {
 53    return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
 54 }
 55 
 56 function hotcat_modify_span ( span , i ) {
 57   var cat_title = span.firstChild.getAttribute ( "title" ) ;
 58   // Removing leading Category:
 59   cat_title = cat_title.substr(cat_title.indexOf(":") + 1);
 60   var sep1 = document.createTextNode ( " " ) ;
 61   var a1 = document.createTextNode ( "(-)" ) ;
 62   var remove_link = document.createElement ( "a" ) ;
 63   remove_link.href = "javascript:hotcat_remove(\"" + cat_title + "\");" ;
 64   remove_link.appendChild ( a1 ) ;
 65   span.appendChild ( sep1 ) ;
 66   span.appendChild ( remove_link ) ;
 67 
 68   var mod_id = "hotcat_modify_" + i ;
 69   var sep2 = document.createTextNode ( " " ) ;
 70   var a2 = document.createTextNode ( "(±)" ) ;
 71   var modify_link = document.createElement ( "a" ) ;
 72   modify_link.id = mod_id ;
 73   modify_link.href = "javascript:hotcat_modify(\"" + mod_id + "\");" ;
 74   modify_link.appendChild ( a2 ) ;
 75   span.appendChild ( sep2 ) ;
 76   span.appendChild ( modify_link ) ;
 77 }
 78 
 79 function hotcat_modify_existing ( catline ) {
 80   var spans = catline.getElementsByTagName ( "span" ) ;
 81   for ( var i = 0 ; i < spans.length ; i++ ) {
 82     hotcat_modify_span ( spans[i] , i ) ;
 83   }
 84 }
 85 
 86 function hotcat_remove ( cat_title ) {
 87   var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
 88   if (window.confirm("Volètz vertadièrament suprimir la categoria '" + cat_title + "' ?")) {
 89     document.location = editlk + '&hotcat_removecat=' + encodeURIComponent(cat_title) ;
 90   }
 91 }
 92 
 93 function hotcatGetParamValue(paramName, h) {
 94         if (typeof h == 'undefined' ) { h = document.location.href; }
 95         var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
 96         var m=cmdRe.exec(h);
 97         if (m) {
 98                 try {
 99                         return decodeURIComponent(m[1]);
100                 } catch (someError) {}
101         }
102         return null;
103 }
104 
105 function hotcat_check_action () {
106   var ret = 0 ;
107   if ( wgAction != "edit" ) return ret ; // Not an edit page, so no business...
108   var summary = new Array () ;
109   var t = document.editform.wpTextbox1.value ;
110   var prevent_autocommit = 1 ;
111   if ( typeof hotcat_autocommit != 'undefined' && hotcat_autocommit ) prevent_autocommit = 0 ;
112 
113   // Remove existing category?
114   var hrc = hotcatGetParamValue('hotcat_removecat') ;
115   // Add new category?
116   var hnc = hotcatGetParamValue('hotcat_newcat') ;
117 
118   if ( typeof hrc != "undefined" && hrc != null && hrc != "" ) {
119     var hcre = new RegExp("(\\s*)\\[\\[ *(?:Categoria|Category) *: *" + hrc.replace(/([\\\^\$\*\+\?\.\|\{\}\[\]\(\)])/g, "\\$1") + " *(\\|[^\\]]*)?\\]\\]", "gi");
120     var matches = t.match(hcre);
121     if (matches != null && matches.length == 1) { // Found one occurrence of the category - good!
122       if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) {
123         t = t.replace(hcre, "$1[[Categoria:" + hnc + "$2]]");
124         summary.push ( "Modificacion de la categoria [[Categoria:" + hrc + "]] → [[Categoria:" + hnc + "]]" ) ;
125       } else {
126         t = t.replace(hcre, "");
127         summary.push ( "Retrach de la categoria [[Categoria:" + hrc + "]]" ) ;
128       }
129       ret = 1 ;
130     } else {
131       alert ( "Impossible de trobar una occurréncia unica de \"" + hrc + "\" - benlèu es inclusa via un modèl" ) ;
132       prevent_autocommit = 1 ;
133     }
134   } else {
135     // Only adding?
136     if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) {
137       // Looking for last cat
138       var re = /\[\[(?:Categoria|Category):[^\]]+\]\]/ig
139       var index = -1;
140       while (re.exec(t) != null) index = re.lastIndex;
141       var txt = "[[Categoria:" + hnc + "]]" ;
142       if (index < 0) {
143         t = t + '\n' + txt ;
144       } else {
145         t = t.substring(0, index) + '\n' + txt + t.substring(index);
146       }
147       summary.push ( "Ajust rapid de la categoria [[Categoria:" + hnc + "]]" ) ;
148       ret = 1 ;
149     }
150   }
151 
152   if ( ret ) {
153     document.editform.wpTextbox1.value = t ;
154     document.editform.wpSummary.value = summary.join( " ; " ) + " (amb [[MediaWiki:JSScripts/HotCats|HotCats]])" ;
155     document.editform.wpMinoredit.checked = true ;
156     if ( !prevent_autocommit ) {
157       document.getElementById("bodyContent").style.display = "none" ; // Hiding the entire edit section so as not to tempt the user into editing...
158       document.editform.wpSave.click();
159     }
160   }
161 
162   // This is the end, my friend, the end...
163   return ret ;
164 }
165 
166 function hotcat_clear_span ( span_add ) {
167   while ( span_add.firstChild ) span_add.removeChild ( span_add.firstChild ) ;
168 }
169 
170 function hotcat_create_span ( span_add ) {
171   hotcat_clear_span ( span_add ) ;
172   var a_add = document.createElement ( "a" ) ;
173   var a_text = document.createTextNode ( "(+)" ) ;
174   span_add.id = "hotcat_add" ;
175   a_add.href = "javascript:hotcat_add_new()" ;
176   a_add.appendChild ( a_text ) ;
177   span_add.appendChild ( a_add ) ;
178 }
179 
180 function hotcat_modify ( link_id ) {
181   var link = document.getElementById ( link_id ) ;
182   var span = link.parentNode ;
183   var catname = span.firstChild.firstChild.data ;
184   
185   while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
186   span.firstChild.style.display = "none" ;
187   hotcat_create_new_span ( span , catname ) ;
188   hotcat_last_v = "" ;
189   hotcat_text_changed () ; // Update icon
190 }
191 
192 function hotcat_add_new () {
193   var span_add = document.getElementById ( "hotcat_add" ) ;
194   hotcat_clear_span ( span_add ) ;
195   hotcat_last_v = "" ;
196   hotcat_create_new_span ( span_add , "" ) ;
197 }
198 
199 function hotcat_create_new_span ( thespan , init_text ) {
200   var form = document.createElement ( "form" ) ;
201   form.method = "post" ;
202   form.onsubmit = function () { hotcat_ok(); return false; } ;
203   form.id = "hotcat_form" ;
204   form.style.display = "inline" ;
205 
206   var list = document.createElement ( "select" ) ;
207   list.id = "hotcat_list" ;
208   list.onclick = function () { document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value ; hotcat_text_changed() ; } ;
209   list.ondblclick = function () { document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value ; hotcat_text_changed() ; hotcat_ok(); } ;
210   list.style.display = "none" ;
211 
212   var text = document.createElement ( "input" ) ;
213   text.size = 40 ;
214   text.id = "hotcat_text" ;
215   text.type = "text" ;
216   text.value = init_text ;
217   text.onkeyup = function () { window.setTimeout("hotcat_text_changed();", 100 ); } ;
218 
219   var exists = document.createElement ( "img" ) ;
220   exists.id = "hotcat_exists" ;
221   exists.src = hotcat_exists_no ;
222 
223   var OK = document.createElement ( "input" ) ;
224   OK.type = "button" ;
225   OK.value = "OK" ;
226   OK.onclick = hotcat_ok ;
227 
228   var cancel = document.createElement ( "input" ) ;
229   cancel.type = "button" ;
230   cancel.value = "Anullar" ;
231   cancel.onclick = hotcat_cancel ;
232 
233   form.appendChild ( list ) ;
234   form.appendChild ( text ) ;
235   form.appendChild ( exists ) ;
236   form.appendChild ( OK ) ;
237   form.appendChild ( cancel ) ;
238   thespan.appendChild ( form ) ;
239   text.focus () ;
240 }
241 
242 function hotcat_ok () {
243   var text = document.getElementById ( "hotcat_text" ) ;
244   var v = text.value ;
245 
246   // Empty category ?
247   if ( v == "" ) {
248     hotcat_cancel() ;
249     return ;
250   }
251 
252   var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
253   var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ;
254 
255   // Editing existing?
256   var span = text.parentNode.parentNode ; // span.form.text
257   if ( span.id != "hotcat_add" ) { // Not plain "addition"
258     var cat_title = span.firstChild.getAttribute ( "title" ) ;
259     // Removing leading Category:
260     cat_title = cat_title.substr(cat_title.indexOf(":") + 1);
261     url += '&hotcat_removecat=' + encodeURIComponent( cat_title ) ;
262   }
263 
264   document.location = url ;
265 }
266 
267 function hotcat_cancel () {
268   var span = document.getElementById("hotcat_form").parentNode ;
269   if ( span.id == "hotcat_add" ) {
270     hotcat_create_span ( span ) ;
271   } else {
272     while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
273     span.firstChild.style.display = "" ;
274     for ( var i = 0 ; i < span.parentNode.childNodes.length ; i++ ) {
275       if ( span.parentNode.childNodes[i] != span ) continue ;
276       hotcat_modify_span ( span , i ) ;
277       break ;
278     }
279   }
280 }
281 
282 function hotcat_text_changed () {
283   if ( hotcat_running ) return ;
284   var text = document.getElementById ( "hotcat_text" ) ;
285   var v = text.value.ucFirst() ;
286   if ( hotcat_last_v == v ) return ; // Nothing's changed...
287 
288   hotcat_running = 1 ;
289   hotcat_last_v = v ;
290 
291   if ( v != "" ) {
292     var url = wgServer + "/" + wgScriptPath + "/api.php?format=xml&action=query&list=allpages&apnamespace=14&apfrom=" + encodeURIComponent( v ) ;
293     if ( typeof ( hotcat_xmlhttp ) != "undefined" ) hotcat_xmlhttp.abort() ; // Just to make sure...
294     hotcat_xmlhttp = new sajax_init_object() ;
295     hotcat_xmlhttp.open('GET', url, true);
296     hotcat_xmlhttp.onreadystatechange = function () {
297           if ( typeof hotcat_xmlhttp == "undefined" ) return ;
298           if (hotcat_xmlhttp.readyState == 4) {
299               var xml = hotcat_xmlhttp.responseXML ;
300               if ( xml == null ) return ;
301               var pages = xml.getElementsByTagName( "p" ) ;
302               var titles = new Array () ;
303               for ( var i = 0 ; i < pages.length ; i++ ) {
304                 var s = pages[i].getAttribute("title");
305                 // Removing leading "Category:"
306                 s = s.substr(s.indexOf(":") + 1);
307                 if ( s.substr ( 0 , hotcat_last_v.length ) != hotcat_last_v ) break ;
308                 titles.push ( s ) ;
309               }
310               hotcat_show_suggestions ( titles ) ;
311           }
312       };
313     hotcat_xmlhttp.send(null);
314   } else {
315     var titles = new Array () ;
316     hotcat_show_suggestions ( titles ) ;
317   }
318   hotcat_running = 0 ;
319 }
320 
321 function hotcat_show_suggestions ( titles ) {
322   var text = document.getElementById ( "hotcat_text" ) ;
323   var list = document.getElementById ( "hotcat_list" ) ;
324   var icon = document.getElementById ( "hotcat_exists" ) ;
325   if ( titles.length == 0 ) {
326     list.style.display = "none" ;
327     icon.src = hotcat_exists_no ;
328     return ;
329   }
330   
331   var listh = titles.length * 20 ;
332   var nl = parseInt ( text.parentNode.offsetLeft ) - 1 ;
333   var nt = parseInt(text.offsetTop) - listh ;
334   // Parameter to show suggestion list beneath categories instead of above
335   if (typeof hotcat_list_down != "undefined" && hotcat_list_down) {
336      nt = text.offsetTop + text.offsetHeight;
337   }
338   list.size = 5 ;
339   list.style.align = "left" ;
340   list.style.zIndex = 5 ;
341   list.style.position = "absolute" ;
342   list.style.top = nt + "px" ;
343   list.style.width = text.offsetWidth + "px" ;
344   list.style.height = listh + "px" ;
345   list.style.left = nl + "px" ;
346   while ( list.firstChild ) list.removeChild ( list.firstChild ) ;
347   for ( var i = 0 ; i < titles.length ; i++ ) {
348     var opt = document.createElement ( "option" ) ;
349     var ot = document.createTextNode ( titles[i] ) ;
350     opt.appendChild ( ot ) ;
351     opt.setAttribute( "value", titles[i] );
352     list.appendChild ( opt ) ;
353   }
354   
355   list.style.display = "block" ;
356 
357   icon.src = hotcat_exists_yes ;
358 
359   var first_title = titles.shift () ;
360   if ( first_title == hotcat_last_v ) return ;
361 
362   var nosel = hotcat_last_v.length ;
363   
364   text.value = first_title ;
365   if( is_khtml )
366    text.setSelectionRange( nosel, first_title.length );
367   else
368   {
369    text.selectionStart = nosel ;
370    text.selectionEnd = first_title.length ;
371   }
372 }
373 
374 //