MediaWiki:Gadget-HotCat.js : Différence entre versions
De NVCwiki
(version english commons.wikimedia.org) |
(retour à la version française) |
||
Ligne 1 : | Ligne 1 : | ||
− | //< | + | //<nowiki> |
+ | /** | ||
+ | * Ajoute des liens après les catégories pour rapidement | ||
+ | * supprimer / modifier / ajouter une catégorie sur un article. | ||
+ | * Utilise Ajax et l'api MediaWiki pour rechercher une catégorie. | ||
+ | * Version adaptée pour WP:fr de [[:commons:MediaWiki:HotCat.js]] | ||
+ | * | ||
+ | * Auteur original : Magnus Manske | ||
+ | * Adaptation pour WP:fr : Zelda | ||
+ | * [[Catégorie:MediaWiki:Fonction Monobook en JavaScript]] | ||
+ | */ | ||
− | + | // VARIABLES PERSONNALISABLES | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | var hotcat_suggestion_delay = | + | // Suggestion delay in ms |
− | + | var hotcat_suggestion_delay = 200; | |
− | |||
− | var | + | // Taille de la liste déroulante (en items) |
− | + | var hotcat_list_size = 10; | |
− | |||
− | |||
− | |||
− | + | // Nombre de categories suggérées lors de la recherche | |
− | + | var hotcat_list_items = 50; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // Permet d'enregistrer automatiquement la modif sans repasser par la fenêtre d'édition | |
− | + | // var hotcat_autocommit = false; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // Permet d'afficher la liste de suggestion vers le bas | |
− | + | // var hotcat_list_down = false; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // FIN DE LA PERSONNALISATION DES VARIABLES | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | var hotcat_running = 0 ; | |
− | + | var hotcat_last_v = "" ; | |
− | + | var hotcat_exists_yes = "http://upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png" ; | |
− | + | var hotcat_exists_no = "http://upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png" ; | |
− | + | if( wgNamespaceNumber%2 == 0 && document.URL.indexOf("printable=yes")==-1) addOnloadHook ( hotcat ) ; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
function hotcat () { | function hotcat () { | ||
− | + | if ( hotcat_check_action() ) return ; // Edited page, reloading anyway | |
− | |||
− | if ( | ||
− | |||
− | |||
− | |||
− | + | var catlinks = document.getElementById("catlinks"); | |
− | |||
− | |||
− | |||
− | + | if (! catlinks) { // all except discussion | |
− | + | // let's create a fake one | |
− | + | var bodyC = document.getElementById("bodyContent"); | |
− | + | catlinks = document.createElement("div"); | |
− | + | catlinks.id = "catlinks"; | |
− | + | catlinks.className = "catlinks"; | |
− | + | catlinks = bodyC.appendChild(catlinks); | |
− | + | } | |
− | + | ||
− | + | var catline = document.getElementById ('mw-normal-catlinks'); // Since MW 1.13alpha | |
− | + | if( !catline ) { | |
− | + | catline = catlinks.insertBefore(document.createElement("div"), catlinks.firstChild); | |
− | + | catline.id = "mw-normal-catlinks"; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | if ( | + | if ( catline == null || typeof catline == 'undefined' ) return ; |
− | |||
− | |||
− | + | hotcat_modify_existing ( catline ) ; | |
− | + | hotcat_append_add_span ( catline ) ; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | var hotcat_edit_id_map=[]; | |
− | + | ||
− | + | function hotcatGetEditId(ob) | |
− | + | { | |
− | + | var i; | |
− | + | for(i=0;i<hotcat_edit_id_map.length;i++) | |
− | + | if (hotcat_edit_id_map[i]===ob) return i; | |
− | + | i=hotcat_edit_id_map.length; | |
− | + | hotcat_edit_id_map.push(ob); | |
− | + | return i; | |
} | } | ||
function hotcat_append_add_span ( catline ) { | function hotcat_append_add_span ( catline ) { | ||
var span_add = document.createElement ( "span" ) ; | var span_add = document.createElement ( "span" ) ; | ||
− | if ( catline. | + | var span_sep = document.createTextNode ( " | " ) ; |
− | + | if ( catline.firstChild ) catline.appendChild ( span_sep ) ; | |
− | + | else { | |
− | + | var a = document.createElement('a'); | |
− | catline.appendChild ( span_add ); | + | a.href = '/wiki/Catégorie:Accueil'; |
− | hotcat_create_span ( span_add ); | + | a.title = 'Catégorie:Accueil'; |
+ | a.appendChild(document.createTextNode('Catégories')); | ||
+ | catline.appendChild(a); | ||
+ | catline.appendChild(document.createTextNode(' : ')); | ||
+ | } | ||
+ | catline.appendChild ( span_add ) ; | ||
+ | hotcat_create_span ( span_add ) ; | ||
} | } | ||
Ligne 239 : | Ligne 95 : | ||
} | } | ||
− | + | function hotcat_modify_span ( span ) { | |
− | + | var cat_title = span.firstChild.getAttribute ( "title" ) ; | |
− | + | // Removing leading Category: | |
− | + | cat_title = cat_title.substr(cat_title.indexOf(":") + 1); | |
− | + | var sep1 = document.createTextNode ( " " ) ; | |
− | + | var a1 = document.createTextNode ( "(–)" ) ; | |
− | |||
− | |||
− | |||
− | function hotcat_modify_span ( span | ||
− | |||
− | // | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
var remove_link = document.createElement ( "a" ) ; | var remove_link = document.createElement ( "a" ) ; | ||
− | + | remove_link.href = "javascript:hotcat_remove(\"" + cat_title + "\");" ; | |
− | + | remove_link.appendChild ( a1 ) ; | |
− | remove_link.href = " | + | span.appendChild ( sep1 ) ; |
− | |||
− | remove_link.appendChild ( | ||
− | span.appendChild ( | ||
span.appendChild ( remove_link ) ; | span.appendChild ( remove_link ) ; | ||
− | + | var i=hotcatGetEditId(span); | |
var mod_id = "hotcat_modify_" + i ; | var mod_id = "hotcat_modify_" + i ; | ||
+ | var sep2 = document.createTextNode ( " " ) ; | ||
+ | var a2 = document.createTextNode ( "(±)" ) ; | ||
var modify_link = document.createElement ( "a" ) ; | var modify_link = document.createElement ( "a" ) ; | ||
modify_link.id = mod_id ; | modify_link.id = mod_id ; | ||
modify_link.href = "javascript:hotcat_modify(\"" + mod_id + "\");" ; | modify_link.href = "javascript:hotcat_modify(\"" + mod_id + "\");" ; | ||
− | modify_link.appendChild ( | + | modify_link.appendChild ( a2 ) ; |
− | span.appendChild ( | + | span.appendChild ( sep2 ) ; |
span.appendChild ( modify_link ) ; | span.appendChild ( modify_link ) ; | ||
− | |||
} | } | ||
Ligne 301 : | Ligne 122 : | ||
var spans = catline.getElementsByTagName ( "span" ) ; | var spans = catline.getElementsByTagName ( "span" ) ; | ||
for ( var i = 0 ; i < spans.length ; i++ ) { | for ( var i = 0 ; i < spans.length ; i++ ) { | ||
− | hotcat_modify_span ( spans[i] | + | hotcat_modify_span ( spans[i] ) ; |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
} | } | ||
− | function | + | function hotcat_remove ( cat_title ) { |
− | { | + | var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href; |
− | + | if (window.confirm("Voulez-vous vraiment supprimer la catégorie '" + cat_title + "' ?")) { | |
− | + | document.location = editlk + '&hotcat_removecat=' + encodeURIComponent(cat_title) ; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | if ( | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
function hotcatGetParamValue(paramName, h) { | function hotcatGetParamValue(paramName, h) { | ||
− | + | if (typeof h == 'undefined' ) { h = document.location.href; } | |
− | + | var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)'); | |
− | + | var m=cmdRe.exec(h); | |
− | + | if (m) { | |
− | + | try { | |
− | + | return decodeURIComponent(m[1]); | |
− | + | } catch (someError) {} | |
− | + | } | |
− | + | return null; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
function hotcat_check_action () { | function hotcat_check_action () { | ||
− | var ret = 0; | + | var ret = 0 ; |
− | if (wgAction != | + | if ( wgAction != "edit" ) return ret ; // Not an edit page, so no business... |
− | |||
var summary = new Array () ; | var summary = new Array () ; | ||
var t = document.editform.wpTextbox1.value ; | var t = document.editform.wpTextbox1.value ; | ||
− | var prevent_autocommit = | + | var prevent_autocommit = 1 ; |
− | if | + | if ( typeof hotcat_autocommit != 'undefined' && hotcat_autocommit ) prevent_autocommit = 0 ; |
− | |||
− | |||
− | + | // Remove existing category? | |
− | var | + | var hrc = hotcatGetParamValue('hotcat_removecat') ; |
− | + | // Add new category? | |
− | var | + | var hnc = hotcatGetParamValue('hotcat_newcat') ; |
− | if ( | + | if ( typeof hrc != "undefined" && hrc != null && hrc != "" ) { |
− | + | var hcre = new RegExp("(\\s*)\\[\\[ *(?:Catégorie|Category) *: *" + hrc.replace(/([\\\^\$\*\+\?\.\|\{\}\[\]\(\)])/g, "\\$1") + " *(\\|[^\\]]*)?\\]\\]", "gi"); | |
− | var matches = | + | var matches = t.match(hcre); |
− | if (! | + | if (matches != null && matches.length == 1) { // Found one occurrence of the category - good! |
− | + | if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) { | |
− | + | t = t.replace(hcre, "$1[[Catégorie:" + hnc + "$2]]"); | |
− | + | summary.push ( "Modification de la catégorie [[Catégorie:" + hrc + "]] → [[Catégorie:" + hnc + "]]" ) ; | |
− | + | } else { | |
− | + | t = t.replace(hcre, ""); | |
− | + | summary.push ( "Retrait de la catégorie [[Catégorie:" + hrc + "]]" ) ; | |
+ | } | ||
+ | ret = 1 ; | ||
} else { | } else { | ||
− | + | alert ( "Impossible de trouver une occurrence unique de \"" + hrc + "\" - elle est peut-être incluse via un modèle" ) ; | |
− | + | prevent_autocommit = 1 ; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | } | + | } else { |
− | + | // Only adding? | |
− | + | if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) { | |
− | + | // Looking for last cat | |
− | + | var re = /\[\[(?:Catégorie|Category):[^\]]+\]\]/ig | |
− | + | var index = -1; | |
− | + | while (re.exec(t) != null) index = re.lastIndex; | |
− | + | var txt = "[[Catégorie:" + hnc + "]]" ; | |
− | + | if (index < 0) { | |
− | + | t = t + '\n' + txt ; | |
− | + | } else { | |
− | + | t = t.substring(0, index) + '\n' + txt + t.substring(index); | |
− | |||
− | |||
} | } | ||
− | ret = 1; | + | summary.push ( "Ajout rapide de la catégorie [[Catégorie:" + hnc + "]]" ) ; |
+ | ret = 1 ; | ||
} | } | ||
} | } | ||
− | if (ret) { | + | |
+ | if ( ret ) { | ||
document.editform.wpTextbox1.value = t ; | document.editform.wpTextbox1.value = t ; | ||
− | document.editform.wpSummary.value = summary.join( "; " ) | + | document.editform.wpSummary.value = summary.join( " ; " ) + " (avec [[MediaWiki:Gadget-HotCats.js|HotCats]])" ; |
− | |||
document.editform.wpMinoredit.checked = true ; | document.editform.wpMinoredit.checked = true ; | ||
− | if (!prevent_autocommit) { | + | if ( !prevent_autocommit ) { |
− | // | + | document.getElementById("bodyContent").style.display = "none" ; // Hiding the entire edit section so as not to tempt the user into editing... |
− | + | document.editform.wpSave.click(); | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
− | return ret; | + | |
+ | // This is the end, my friend, the end... | ||
+ | return ret ; | ||
} | } | ||
Ligne 509 : | Ligne 223 : | ||
var link = document.getElementById ( link_id ) ; | var link = document.getElementById ( link_id ) ; | ||
var span = link.parentNode ; | var span = link.parentNode ; | ||
− | var catname = span. | + | var catname = span.firstChild.firstChild.data ; |
while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ; | while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ; | ||
Ligne 523 : | Ligne 237 : | ||
hotcat_last_v = "" ; | hotcat_last_v = "" ; | ||
hotcat_create_new_span ( span_add , "" ) ; | hotcat_create_new_span ( span_add , "" ) ; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
Ligne 546 : | Ligne 242 : | ||
var form = document.createElement ( "form" ) ; | var form = document.createElement ( "form" ) ; | ||
form.method = "post" ; | form.method = "post" ; | ||
− | form.onsubmit = function () { hotcat_ok(); return false; } ; | + | form.onsubmit = function () { hotcat_ok(); return false; } ; |
form.id = "hotcat_form" ; | form.id = "hotcat_form" ; | ||
form.style.display = "inline" ; | form.style.display = "inline" ; | ||
− | var | + | var list = document.createElement ( "select" ) ; |
− | + | list.id = "hotcat_list" ; | |
− | + | list.onclick = function () { document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value ; hotcat_text_changed() ; } ; | |
− | + | list.ondblclick = function () { document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value ; hotcat_text_changed() ; hotcat_ok(); } ; | |
− | + | list.style.display = "none" ; | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
var text = document.createElement ( "input" ) ; | var text = document.createElement ( "input" ) ; | ||
− | + | text.size = 40 ; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | text.size = | ||
text.id = "hotcat_text" ; | text.id = "hotcat_text" ; | ||
text.type = "text" ; | text.type = "text" ; | ||
text.value = init_text ; | text.value = init_text ; | ||
− | text.onkeyup = | + | text.onkeyup = function () { window.setTimeout("hotcat_text_changed();", hotcat_suggestion_delay ); } ; |
− | |||
− | |||
− | |||
− | |||
− | var | + | var exists = document.createElement ( "img" ) ; |
− | + | exists.id = "hotcat_exists" ; | |
− | + | exists.src = hotcat_exists_no ; | |
− | |||
− | |||
− | |||
var OK = document.createElement ( "input" ) ; | var OK = document.createElement ( "input" ) ; | ||
OK.type = "button" ; | OK.type = "button" ; | ||
− | OK.value = | + | OK.value = "OK" ; |
− | OK.onclick = | + | OK.onclick = hotcat_ok ; |
var cancel = document.createElement ( "input" ) ; | var cancel = document.createElement ( "input" ) ; | ||
cancel.type = "button" ; | cancel.type = "button" ; | ||
− | cancel.value = | + | cancel.value = "Annuler" ; |
cancel.onclick = hotcat_cancel ; | cancel.onclick = hotcat_cancel ; | ||
− | + | form.appendChild ( list ) ; | |
form.appendChild ( text ) ; | form.appendChild ( text ) ; | ||
− | + | form.appendChild ( exists ) ; | |
form.appendChild ( OK ) ; | form.appendChild ( OK ) ; | ||
form.appendChild ( cancel ) ; | form.appendChild ( cancel ) ; | ||
Ligne 626 : | Ligne 282 : | ||
} | } | ||
− | function hotcat_ok ( | + | function hotcat_ok () { |
var text = document.getElementById ( "hotcat_text" ) ; | var text = document.getElementById ( "hotcat_text" ) ; | ||
− | var v = text.value | + | var v = text.value ; |
− | + | ||
− | |||
// Empty category ? | // Empty category ? | ||
− | if ( | + | if ( v == "" ) { |
hotcat_cancel() ; | hotcat_cancel() ; | ||
return ; | return ; | ||
} | } | ||
− | + | var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href; | |
− | var | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ; | var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ; | ||
// Editing existing? | // Editing existing? | ||
var span = text.parentNode.parentNode ; // span.form.text | var span = text.parentNode.parentNode ; // span.form.text | ||
− | if ( span.id != "hotcat_add" ) { // Not plain "addition" | + | if ( span.id != "hotcat_add" ) { // Not plain "addition" |
− | url += '&hotcat_removecat=' + encodeURIComponent ( | + | var cat_title = span.firstChild.innerHTML ; |
+ | // Removing leading Category: | ||
+ | cat_title = cat_title.substr(cat_title.indexOf(":") + 1); | ||
+ | url += '&hotcat_removecat=' + encodeURIComponent( cat_title ) ; | ||
} | } | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
document.location = url ; | document.location = url ; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
Ligne 784 : | Ligne 314 : | ||
while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ; | while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ; | ||
span.firstChild.style.display = "" ; | span.firstChild.style.display = "" ; | ||
− | + | hotcat_modify_span ( span , i ) ; | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
Ligne 798 : | Ligne 324 : | ||
if ( hotcat_last_v == v ) return ; // Nothing's changed... | if ( hotcat_last_v == v ) return ; // Nothing's changed... | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
hotcat_running = 1 ; | hotcat_running = 1 ; | ||
hotcat_last_v = v ; | hotcat_last_v = v ; | ||
if ( v != "" ) { | if ( v != "" ) { | ||
− | var url = wgServer + wgScriptPath | + | var url = wgServer + "/" + wgScriptPath + "/api.php?format=xml&action=query&list=allpages&apnamespace=14&apfrom=" + encodeURIComponent( v ) + "&aplimit=" + encodeURIComponent( hotcat_list_items ); |
− | + | if ( typeof ( hotcat_xmlhttp ) != "undefined" ) hotcat_xmlhttp.abort() ; // Just to make sure... | |
− | + | hotcat_xmlhttp = new sajax_init_object() ; | |
− | + | hotcat_xmlhttp.open('GET', url, true); | |
− | if ( | + | hotcat_xmlhttp.onreadystatechange = function () { |
− | + | if ( typeof hotcat_xmlhttp == "undefined" ) return ; | |
− | + | if (hotcat_xmlhttp.readyState == 4) { | |
− | + | var xml = hotcat_xmlhttp.responseXML ; | |
− | + | if ( xml == null ) return ; | |
− | + | var pages = xml.getElementsByTagName( "p" ) ; | |
− | + | var titles = new Array () ; | |
− | + | for ( var i = 0 ; i < pages.length ; i++ ) { | |
− | + | var s = pages[i].getAttribute("title"); | |
− | + | // Removing leading "Category:" | |
− | + | s = s.substr(s.indexOf(":") + 1); | |
− | + | if ( s.substr ( 0 , hotcat_last_v.length ) != hotcat_last_v ) break ; | |
− | + | titles.push ( s ) ; | |
− | + | } | |
− | + | hotcat_show_suggestions ( titles ) ; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
}; | }; | ||
− | + | hotcat_xmlhttp.send(null); | |
− | |||
− | |||
} else { | } else { | ||
− | + | var titles = new Array () ; | |
+ | hotcat_show_suggestions ( titles ) ; | ||
} | } | ||
hotcat_running = 0 ; | hotcat_running = 0 ; | ||
Ligne 859 : | Ligne 361 : | ||
var list = document.getElementById ( "hotcat_list" ) ; | var list = document.getElementById ( "hotcat_list" ) ; | ||
var icon = document.getElementById ( "hotcat_exists" ) ; | var icon = document.getElementById ( "hotcat_exists" ) ; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
if ( titles.length == 0 ) { | if ( titles.length == 0 ) { | ||
list.style.display = "none" ; | list.style.display = "none" ; | ||
Ligne 873 : | Ligne 367 : | ||
} | } | ||
− | + | var listh = hotcat_list_size * 20 ; | |
− | + | if (titles.length < hotcat_list_size) { | |
− | + | listh = titles.length * 20 ; | |
− | // | + | } |
− | if ( | + | var nl = parseInt ( text.parentNode.offsetLeft ) - 1 ; |
+ | var nt = parseInt(text.offsetTop) - listh ; | ||
+ | // Parameter to show suggestion list beneath categories instead of above | ||
+ | if (typeof hotcat_list_down != "undefined" && hotcat_list_down) { | ||
+ | nt = text.offsetTop + text.offsetHeight; | ||
+ | } | ||
+ | list.size = 5 ; | ||
list.style.align = "left" ; | list.style.align = "left" ; | ||
list.style.zIndex = 5 ; | list.style.zIndex = 5 ; | ||
list.style.position = "absolute" ; | list.style.position = "absolute" ; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
list.style.top = nt + "px" ; | list.style.top = nt + "px" ; | ||
− | list.style.width = ""; | + | list.style.width = text.offsetWidth + "px" ; |
list.style.height = listh + "px" ; | list.style.height = listh + "px" ; | ||
list.style.left = nl + "px" ; | list.style.left = nl + "px" ; | ||
Ligne 901 : | Ligne 390 : | ||
var ot = document.createTextNode ( titles[i] ) ; | var ot = document.createTextNode ( titles[i] ) ; | ||
opt.appendChild ( ot ) ; | opt.appendChild ( ot ) ; | ||
− | + | opt.setAttribute( "value", titles[i] ); | |
list.appendChild ( opt ) ; | list.appendChild ( opt ) ; | ||
} | } | ||
+ | list.style.display = "block" ; | ||
+ | |||
icon.src = hotcat_exists_yes ; | icon.src = hotcat_exists_yes ; | ||
− | |||
− | |||
var first_title = titles.shift () ; | var first_title = titles.shift () ; | ||
− | + | if ( first_title == hotcat_last_v ) return ; | |
− | |||
− | if ( first_title == | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | var suggestion = first_title; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | text.value = suggestion ; | |
− | + | if (text.createTextRange) { | |
− | + | // IE | |
− | + | var ra = text.createTextRange(); | |
− | + | ra.moveStart("character", hotcat_last_v.length); | |
− | + | ra.moveEnd("character", suggestion.length); | |
− | + | ra.select(); | |
− | + | } else if( is_khtml ) { | |
− | if | + | text.setSelectionRange( hotcat_last_v.length, suggestion.length ); |
− | + | } else { | |
− | + | text.selectionStart = hotcat_last_v.length ; | |
− | + | text.selectionEnd = suggestion.length ; | |
− | |||
− | |||
− | // | ||
− | |||
− | |||
− | var | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
} | } | ||
− | + | //</nowiki> | |
− | |||
− | |||
− | |||
− | //</ |
Version du 2 septembre 2009 à 19:42
//<nowiki> /** * Ajoute des liens après les catégories pour rapidement * supprimer / modifier / ajouter une catégorie sur un article. * Utilise Ajax et l'api MediaWiki pour rechercher une catégorie. * Version adaptée pour WP:fr de [[:commons:MediaWiki:HotCat.js]] * * Auteur original : Magnus Manske * Adaptation pour WP:fr : Zelda * [[Catégorie:MediaWiki:Fonction Monobook en JavaScript]] */ // VARIABLES PERSONNALISABLES // Suggestion delay in ms var hotcat_suggestion_delay = 200; // Taille de la liste déroulante (en items) var hotcat_list_size = 10; // Nombre de categories suggérées lors de la recherche var hotcat_list_items = 50; // Permet d'enregistrer automatiquement la modif sans repasser par la fenêtre d'édition // var hotcat_autocommit = false; // Permet d'afficher la liste de suggestion vers le bas // var hotcat_list_down = false; // FIN DE LA PERSONNALISATION DES VARIABLES var hotcat_running = 0 ; var hotcat_last_v = "" ; var hotcat_exists_yes = "http://upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png" ; var hotcat_exists_no = "http://upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png" ; if( wgNamespaceNumber%2 == 0 && document.URL.indexOf("printable=yes")==-1) addOnloadHook ( hotcat ) ; function hotcat () { if ( hotcat_check_action() ) return ; // Edited page, reloading anyway var catlinks = document.getElementById("catlinks"); if (! catlinks) { // all except discussion // let's create a fake one var bodyC = document.getElementById("bodyContent"); catlinks = document.createElement("div"); catlinks.id = "catlinks"; catlinks.className = "catlinks"; catlinks = bodyC.appendChild(catlinks); } var catline = document.getElementById ('mw-normal-catlinks'); // Since MW 1.13alpha if( !catline ) { catline = catlinks.insertBefore(document.createElement("div"), catlinks.firstChild); catline.id = "mw-normal-catlinks"; } if ( catline == null || typeof catline == 'undefined' ) return ; hotcat_modify_existing ( catline ) ; hotcat_append_add_span ( catline ) ; } var hotcat_edit_id_map=[]; function hotcatGetEditId(ob) { var i; for(i=0;i<hotcat_edit_id_map.length;i++) if (hotcat_edit_id_map[i]===ob) return i; i=hotcat_edit_id_map.length; hotcat_edit_id_map.push(ob); return i; } function hotcat_append_add_span ( catline ) { var span_add = document.createElement ( "span" ) ; var span_sep = document.createTextNode ( " | " ) ; if ( catline.firstChild ) catline.appendChild ( span_sep ) ; else { var a = document.createElement('a'); a.href = '/wiki/Catégorie:Accueil'; a.title = 'Catégorie:Accueil'; a.appendChild(document.createTextNode('Catégories')); catline.appendChild(a); catline.appendChild(document.createTextNode(' : ')); } catline.appendChild ( span_add ) ; hotcat_create_span ( span_add ) ; } String.prototype.ucFirst = function () { return this.substr(0,1).toUpperCase() + this.substr(1,this.length); } function hotcat_modify_span ( span ) { var cat_title = span.firstChild.getAttribute ( "title" ) ; // Removing leading Category: cat_title = cat_title.substr(cat_title.indexOf(":") + 1); var sep1 = document.createTextNode ( " " ) ; var a1 = document.createTextNode ( "(–)" ) ; var remove_link = document.createElement ( "a" ) ; remove_link.href = "javascript:hotcat_remove(\"" + cat_title + "\");" ; remove_link.appendChild ( a1 ) ; span.appendChild ( sep1 ) ; span.appendChild ( remove_link ) ; var i=hotcatGetEditId(span); var mod_id = "hotcat_modify_" + i ; var sep2 = document.createTextNode ( " " ) ; var a2 = document.createTextNode ( "(±)" ) ; var modify_link = document.createElement ( "a" ) ; modify_link.id = mod_id ; modify_link.href = "javascript:hotcat_modify(\"" + mod_id + "\");" ; modify_link.appendChild ( a2 ) ; span.appendChild ( sep2 ) ; span.appendChild ( modify_link ) ; } function hotcat_modify_existing ( catline ) { var spans = catline.getElementsByTagName ( "span" ) ; for ( var i = 0 ; i < spans.length ; i++ ) { hotcat_modify_span ( spans[i] ) ; } } function hotcat_remove ( cat_title ) { var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href; if (window.confirm("Voulez-vous vraiment supprimer la catégorie '" + cat_title + "' ?")) { document.location = editlk + '&hotcat_removecat=' + encodeURIComponent(cat_title) ; } } function hotcatGetParamValue(paramName, h) { if (typeof h == 'undefined' ) { h = document.location.href; } var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)'); var m=cmdRe.exec(h); if (m) { try { return decodeURIComponent(m[1]); } catch (someError) {} } return null; } function hotcat_check_action () { var ret = 0 ; if ( wgAction != "edit" ) return ret ; // Not an edit page, so no business... var summary = new Array () ; var t = document.editform.wpTextbox1.value ; var prevent_autocommit = 1 ; if ( typeof hotcat_autocommit != 'undefined' && hotcat_autocommit ) prevent_autocommit = 0 ; // Remove existing category? var hrc = hotcatGetParamValue('hotcat_removecat') ; // Add new category? var hnc = hotcatGetParamValue('hotcat_newcat') ; if ( typeof hrc != "undefined" && hrc != null && hrc != "" ) { var hcre = new RegExp("(\\s*)\\[\\[ *(?:Catégorie|Category) *: *" + hrc.replace(/([\\\^\$\*\+\?\.\|\{\}\[\]\(\)])/g, "\\$1") + " *(\\|[^\\]]*)?\\]\\]", "gi"); var matches = t.match(hcre); if (matches != null && matches.length == 1) { // Found one occurrence of the category - good! if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) { t = t.replace(hcre, "$1[[Catégorie:" + hnc + "$2]]"); summary.push ( "Modification de la catégorie [[Catégorie:" + hrc + "]] → [[Catégorie:" + hnc + "]]" ) ; } else { t = t.replace(hcre, ""); summary.push ( "Retrait de la catégorie [[Catégorie:" + hrc + "]]" ) ; } ret = 1 ; } else { alert ( "Impossible de trouver une occurrence unique de \"" + hrc + "\" - elle est peut-être incluse via un modèle" ) ; prevent_autocommit = 1 ; } } else { // Only adding? if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) { // Looking for last cat var re = /\[\[(?:Catégorie|Category):[^\]]+\]\]/ig var index = -1; while (re.exec(t) != null) index = re.lastIndex; var txt = "[[Catégorie:" + hnc + "]]" ; if (index < 0) { t = t + '\n' + txt ; } else { t = t.substring(0, index) + '\n' + txt + t.substring(index); } summary.push ( "Ajout rapide de la catégorie [[Catégorie:" + hnc + "]]" ) ; ret = 1 ; } } if ( ret ) { document.editform.wpTextbox1.value = t ; document.editform.wpSummary.value = summary.join( " ; " ) + " (avec [[MediaWiki:Gadget-HotCats.js|HotCats]])" ; document.editform.wpMinoredit.checked = true ; if ( !prevent_autocommit ) { document.getElementById("bodyContent").style.display = "none" ; // Hiding the entire edit section so as not to tempt the user into editing... document.editform.wpSave.click(); } } // This is the end, my friend, the end... return ret ; } function hotcat_clear_span ( span_add ) { while ( span_add.firstChild ) span_add.removeChild ( span_add.firstChild ) ; } function hotcat_create_span ( span_add ) { hotcat_clear_span ( span_add ) ; var a_add = document.createElement ( "a" ) ; var a_text = document.createTextNode ( "(+)" ) ; span_add.id = "hotcat_add" ; a_add.href = "javascript:hotcat_add_new()" ; a_add.appendChild ( a_text ) ; span_add.appendChild ( a_add ) ; } function hotcat_modify ( link_id ) { var link = document.getElementById ( link_id ) ; var span = link.parentNode ; var catname = span.firstChild.firstChild.data ; while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ; span.firstChild.style.display = "none" ; hotcat_create_new_span ( span , catname ) ; hotcat_last_v = "" ; hotcat_text_changed () ; // Update icon } function hotcat_add_new () { var span_add = document.getElementById ( "hotcat_add" ) ; hotcat_clear_span ( span_add ) ; hotcat_last_v = "" ; hotcat_create_new_span ( span_add , "" ) ; } function hotcat_create_new_span ( thespan , init_text ) { var form = document.createElement ( "form" ) ; form.method = "post" ; form.onsubmit = function () { hotcat_ok(); return false; } ; form.id = "hotcat_form" ; form.style.display = "inline" ; var list = document.createElement ( "select" ) ; list.id = "hotcat_list" ; list.onclick = function () { document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value ; hotcat_text_changed() ; } ; list.ondblclick = function () { document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value ; hotcat_text_changed() ; hotcat_ok(); } ; list.style.display = "none" ; var text = document.createElement ( "input" ) ; text.size = 40 ; text.id = "hotcat_text" ; text.type = "text" ; text.value = init_text ; text.onkeyup = function () { window.setTimeout("hotcat_text_changed();", hotcat_suggestion_delay ); } ; var exists = document.createElement ( "img" ) ; exists.id = "hotcat_exists" ; exists.src = hotcat_exists_no ; var OK = document.createElement ( "input" ) ; OK.type = "button" ; OK.value = "OK" ; OK.onclick = hotcat_ok ; var cancel = document.createElement ( "input" ) ; cancel.type = "button" ; cancel.value = "Annuler" ; cancel.onclick = hotcat_cancel ; form.appendChild ( list ) ; form.appendChild ( text ) ; form.appendChild ( exists ) ; form.appendChild ( OK ) ; form.appendChild ( cancel ) ; thespan.appendChild ( form ) ; text.focus () ; } function hotcat_ok () { var text = document.getElementById ( "hotcat_text" ) ; var v = text.value ; // Empty category ? if ( v == "" ) { hotcat_cancel() ; return ; } var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href; var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ; // Editing existing? var span = text.parentNode.parentNode ; // span.form.text if ( span.id != "hotcat_add" ) { // Not plain "addition" var cat_title = span.firstChild.innerHTML ; // Removing leading Category: cat_title = cat_title.substr(cat_title.indexOf(":") + 1); url += '&hotcat_removecat=' + encodeURIComponent( cat_title ) ; } document.location = url ; } function hotcat_cancel () { var span = document.getElementById("hotcat_form").parentNode ; if ( span.id == "hotcat_add" ) { hotcat_create_span ( span ) ; } else { while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ; span.firstChild.style.display = "" ; hotcat_modify_span ( span , i ) ; } } function hotcat_text_changed () { if ( hotcat_running ) return ; var text = document.getElementById ( "hotcat_text" ) ; var v = text.value.ucFirst() ; if ( hotcat_last_v == v ) return ; // Nothing's changed... hotcat_running = 1 ; hotcat_last_v = v ; if ( v != "" ) { var url = wgServer + "/" + wgScriptPath + "/api.php?format=xml&action=query&list=allpages&apnamespace=14&apfrom=" + encodeURIComponent( v ) + "&aplimit=" + encodeURIComponent( hotcat_list_items ); if ( typeof ( hotcat_xmlhttp ) != "undefined" ) hotcat_xmlhttp.abort() ; // Just to make sure... hotcat_xmlhttp = new sajax_init_object() ; hotcat_xmlhttp.open('GET', url, true); hotcat_xmlhttp.onreadystatechange = function () { if ( typeof hotcat_xmlhttp == "undefined" ) return ; if (hotcat_xmlhttp.readyState == 4) { var xml = hotcat_xmlhttp.responseXML ; if ( xml == null ) return ; var pages = xml.getElementsByTagName( "p" ) ; var titles = new Array () ; for ( var i = 0 ; i < pages.length ; i++ ) { var s = pages[i].getAttribute("title"); // Removing leading "Category:" s = s.substr(s.indexOf(":") + 1); if ( s.substr ( 0 , hotcat_last_v.length ) != hotcat_last_v ) break ; titles.push ( s ) ; } hotcat_show_suggestions ( titles ) ; } }; hotcat_xmlhttp.send(null); } else { var titles = new Array () ; hotcat_show_suggestions ( titles ) ; } hotcat_running = 0 ; } function hotcat_show_suggestions ( titles ) { var text = document.getElementById ( "hotcat_text" ) ; var list = document.getElementById ( "hotcat_list" ) ; var icon = document.getElementById ( "hotcat_exists" ) ; if ( titles.length == 0 ) { list.style.display = "none" ; icon.src = hotcat_exists_no ; return ; } var listh = hotcat_list_size * 20 ; if (titles.length < hotcat_list_size) { listh = titles.length * 20 ; } var nl = parseInt ( text.parentNode.offsetLeft ) - 1 ; var nt = parseInt(text.offsetTop) - listh ; // Parameter to show suggestion list beneath categories instead of above if (typeof hotcat_list_down != "undefined" && hotcat_list_down) { nt = text.offsetTop + text.offsetHeight; } list.size = 5 ; list.style.align = "left" ; list.style.zIndex = 5 ; list.style.position = "absolute" ; list.style.top = nt + "px" ; list.style.width = text.offsetWidth + "px" ; list.style.height = listh + "px" ; list.style.left = nl + "px" ; while ( list.firstChild ) list.removeChild ( list.firstChild ) ; for ( var i = 0 ; i < titles.length ; i++ ) { var opt = document.createElement ( "option" ) ; var ot = document.createTextNode ( titles[i] ) ; opt.appendChild ( ot ) ; opt.setAttribute( "value", titles[i] ); list.appendChild ( opt ) ; } list.style.display = "block" ; icon.src = hotcat_exists_yes ; var first_title = titles.shift () ; if ( first_title == hotcat_last_v ) return ; var suggestion = first_title; text.value = suggestion ; if (text.createTextRange) { // IE var ra = text.createTextRange(); ra.moveStart("character", hotcat_last_v.length); ra.moveEnd("character", suggestion.length); ra.select(); } else if( is_khtml ) { text.setSelectionRange( hotcat_last_v.length, suggestion.length ); } else { text.selectionStart = hotcat_last_v.length ; text.selectionEnd = suggestion.length ; } } //</nowiki>