/* adapté de ] (version du 12 septembre 2008) par Chphe
* {{Projet:JavaScript/Script|Evaluation}}
* rajouter l'onglet suivi des users
*
*/
//<source lang="javascript">//<pre><nowiki>
/* jshint laxbreak: true */
/* globals mw, $ */
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 && mw.config.get( 'wgAction' ) === 'view' ) {
mw.loader.using( , function () {
$( ev_addEvalTab );
} );
}
function ev_addEvalTab( $ ) {
var portletLink = mw.util.addPortletLink( 'p-cactions', '#', 'Évaluer' );
$( portletLink ).click( function ( e ) {
e.preventDefault();
ev_launchEval();
} );
if ( window.alwaysEval ) {
ev_launchEval();
}
}
//initialise les variables
function ev_initVars() {
if ( typeof window.initEvalProjectNames !== "undefined" ) {
window.initEvalProjectNames();
} else {
window.evalProjectNames = ;
}
window.evaluation_multiprojets = {};
window.evaluation_multiprojets.project = ;
window.evaluation_multiprojets.importance = ;
window.evaluation_multiprojets.state = ;
window.evaluation_multiprojets.avancement_global = "?";
window.evaluation_multiprojets.autres = {}; // pour ramasser le reste (WP1.0, todo, wikiconcours, ...)
}
function ev_launchEval() {
if ( mw.util.getParamValue( 'printable' ) === 'yes' ) {
/* version imprimable */
return;
}
var EvalDiv = document.getElementById( 'EvalDiv' );
if ( !EvalDiv ) {
EvalDiv = document.createElement( 'div' );
EvalDiv.id = "EvalDiv";
EvalDiv.align = "center";
$( '#mw-content-text' ).prepend( EvalDiv );
}
EvalDiv.innerHTML = "Interrogation de la page de discussion...";
ev_initVars();
var params = {
action: 'query',
titles: mw.config.get( 'wgFormattedNamespaces' ) + ':' + mw.config.get( 'wgPageName' ),
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
formatversion: 2,
};
new mw.Api()
.get( params )
.done( function ( results ) {
var page = results.query.pages;
window.evaluation_multiprojets.pdd_existe = !page.missing;
if ( !page.missing ) {
var pageContent = page.revisions.slots.main.content;
ev_ExtractEvaluation( pageContent );
}
ev_AddMyProjects();
ev_drawEvaluation();
} );
}
function ev_addCell( line, html, bgcolor, rowSpan ) {
var cell = line.insertCell();
cell.innerHTML = html;
if ( bgcolor ) {
cell.style.backgroundColor = "#" + bgcolor;
}
if ( rowSpan ) {
cell.rowSpan = rowSpan;
}
return cell;
}
function ev_getRadioboxHtml( id, selected, n, prop, value ) {
if ( prop === "avancement_global" ) {
return "<input onclick='window.evaluation_multiprojets.avancement_global=\""
+ value
+ "\"' name='"
+ id
+ "_mp' type='radio'"
+ ( selected ? " checked='true'/>" : "/>" );
} else {
return "<input onclick='window.evaluation_multiprojets."
+ prop
+ "["
+ n
+ "]=\""
+ value
+ "\"' name='"
+ id
+ "_mp' type='radio'"
+ ( selected ? " checked='true'/>" : "/>" );
}
}
function ev_addEvalLine( tab, project, im, state, n ) {
var line = tab.insertRow();
ev_addCell(
line,
'<a title="Projet:'
+ mw.html.escape( project )
+ '" href="https://wikines.com/fr/Projet:'
+ encodeURIComponent( project )
+ '">'
+ project
+ "</a>"
);
ev_addCell(
line,
"<input onclick='window.evaluation_multiprojets.state["
+ n
+ "]=this.checked' type='checkbox'"
+ ( state ? " checked='true'/>" : "/>" )
);
ev_addCell( line, ev_getRadioboxHtml( n + "i", im === "maximum", n, "importance", "maximum" ) );
ev_addCell( line, ev_getRadioboxHtml( n + "i", im === "élevée", n, "importance", "élevée" ) );
ev_addCell( line, ev_getRadioboxHtml( n + "i", im === "moyenne", n, "importance", "moyenne" ) );
ev_addCell( line, ev_getRadioboxHtml( n + "i", im === "faible", n, "importance", "faible" ) );
ev_addCell( line, ev_getRadioboxHtml( n + "i", im === "?", n, "importance", "?" ) );
}
function ev_drawEvaluation() {
var line;
var tab = document.createElement( "table" );
tab.className = 'wikitable';
tab.style.textAlign = 'center';
tab.style.fontSize = '90%';
line = tab.insertRow();
ev_addCell( line, "<b>Avancement commun</b>", null, 2 );
ev_addCell( line, "<b>AdQ</b>", "6699FF" );
ev_addCell( line, "<b>BA</b>", "66FF66" );
ev_addCell( line, "<b>A</b>", "66FFFF" );
ev_addCell( line, "<b>B</b>", "FFFF66" );
ev_addCell( line, "<b>BD</b>", "FFAA66" );
ev_addCell( line, "<b>Ébauche</b>", "FF6666" );
ev_addCell( line, "<b>?</b>", "F8F8F8" );
line = tab.insertRow();
var av = window.evaluation_multiprojets.avancement_global;
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "AdQ", null, "avancement_global", "AdQ" ) );
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "BA", null, "avancement_global", "BA" ) );
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "A", null, "avancement_global", "A" ) );
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "B", null, "avancement_global", "B" ) );
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "BD", null, "avancement_global", "BD" ) );
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "ébauche", null, "avancement_global", "ébauche" ) );
ev_addCell( line, ev_getRadioboxHtml( "ag", av === "?", null, "avancement_global", "?" ) );
var tab2 = document.createElement( "table" );
tab2.className = 'wikitable';
tab2.style.textAlign = 'center';
tab2.style.fontSize = '90%';
line = tab2.insertRow();
ev_addCell( line, "<b>Projet</b>", "F8F8F8" );
ev_addCell( line, "<b>Bandeau présent</b>", "F8F8F8" );
ev_addCell( line, "<b>Maximum</b>", "FF00FF" );
ev_addCell( line, "<b>Élevée</b>", "FF88FF" );
ev_addCell( line, "<b>Moyenne</b>", "FFCCFF" );
ev_addCell( line, "<b>Faible</b>", "FFEEFF" );
ev_addCell( line, "<b>?</b>", "F8F8F8" );
for ( var i = 0; i < window.evaluation_multiprojets.project.length; i++ ) {
ev_addEvalLine(
tab2,
window.evaluation_multiprojets.project,
window.evaluation_multiprojets.importance,
window.evaluation_multiprojets.state,
i,
true
);
}
var button = document.createElement( "button" );
button.textContent = "Exporter cette évaluation vers la page de discussion";
button.onclick = ev_setEvaluation;
button.id = 'EvalButton';
$( '#EvalDiv' )
.empty()
.append( tab, tab2, button );
}
function ev_AddMyProjects() {
for ( var i = 0; i < window.evalProjectNames.length; i++ ) {
if ( window.evaluation_multiprojets.project.indexOf( window.evalProjectNames ) === -1 ) {
var t = window.evaluation_multiprojets.project.length;
window.evaluation_multiprojets.project = window.evalProjectNames;
window.evaluation_multiprojets.importance = "?";
window.evaluation_multiprojets.state = false;
}
}
}
function ev_ExtractEvaluation( discussion ) {
var reg_modele_multiprojet = /{{ikiprojet\s*\|(*)}}/;
var m = reg_modele_multiprojet.exec( discussion );
if ( m ) {
var tab = m.split( "|" );
var i = 0;
var ind = window.evaluation_multiprojets.project.length;
while ( i < tab.length ) {
var t = tab.split( "=" );
if ( t.length === 2 ) {
if ( t.trim() === "avancement" ) {
window.evaluation_multiprojets.avancement_global = ev_cleanAvancement( t.trim() );
} else {
window.evaluation_multiprojets.autres] = t.trim();
}
} else if ( t.length === 1 ) {
var _proj = tab.trim();
window.evaluation_multiprojets.project = _proj;
if ( i === tab.length - 1 ) alert( "Erreur de lecture du modèle {{Wikiprojet}}" );
window.evaluation_multiprojets.importance = ev_cleanImportance( tab.trim() );
window.evaluation_multiprojets.state = true;
ind++;
i++;
}
i++;
}
}
}
function ev_cleanAvancement( val ) {
val = val.toLowerCase();
if ( val === "adq" ) { return "AdQ"; }
if ( val === "ba" ) { return "BA"; }
if ( val === "a" ) { return "A"; }
if ( val === "b" ) { return "B"; }
if ( val === "bd" ) { return "BD"; }
if ( val === "ébauche" || val === "e" ) { return "ébauche"; }
return "?";
}
function ev_cleanImportance( val ) {
val = val.toLowerCase();
if ( val === "maximum" || val === "maximale" ) { return "maximum"; }
if ( val === "élevée" ) { return "élevée"; }
if ( val === "moyenne" ) { return "moyenne"; }
if ( val === "faible" ) { return "faible"; }
return "?";
}
function ev_setEvaluation() {
document.getElementById( 'EvalButton' ).disabled = true;
var talkpage = mw.config.get( 'wgFormattedNamespaces' ) + ':' + mw.config.get( 'wgPageName' );
var summary = ']: ' + ev_getSummary_multiprojects();
if ( window.evaluation_multiprojets.pdd_existe ) {
new mw.Api()
.edit( talkpage, function ( revision ) {
return {
text: ev_processText( revision.content ),
summary: summary
};
} )
.done( ev_doneCallback );
} else {
new mw.Api()
.create( talkpage, { summary: summary }, ev_processText( '' ) )
.done( ev_doneCallback );
}
}
function ev_processText( text ) {
text = ev_suppr_anc_eval( text );
text = ev_process_multiprojects( text );
text = ev_remise_en_tete( text );
return text;
}
function ev_doneCallback() {
document.getElementById( 'EvalButton' ).disabled = false;
}
function ev_getSummary_multiprojects() {
var summary = " Multiprojet (" + window.evaluation_multiprojets.avancement_global + ")";
for ( var i = 0; i < window.evaluation_multiprojets.project.length; i++ ) {
if ( window.evaluation_multiprojets.state ) {
summary += " " + window.evaluation_multiprojets.project + "|" + window.evaluation_multiprojets.importance;
}
}
return summary + ")";
}
function ev_suppr_anc_eval( text ) {
var regtxt = /\{\{(ikiprojet )*\}\}/g;
return text.replace( regtxt, '' );
}
function ev_process_multiprojects( text ) {
var regtxt = /\{\{ikiprojet\s*\|*\}\}/;
var newtxt = "{{Wikiprojet";
var regtodo = /\{\{(odo\|?\*?| faire\|?\*?)\}\}/;
var ok = false;
var i;
for ( i = 0; i < window.evaluation_multiprojets.project.length; i++ ) {
if ( window.evaluation_multiprojets.state ) {
newtxt += "\n|{{subst:Wikiprojet/alias|projet="
+ window.evaluation_multiprojets.project
+ "}}|"
+ window.evaluation_multiprojets.importance;
ok = true;
}
}
ok = ok || ( window.evaluation_multiprojets.avancement_global !== "?" );
newtxt += "\n|avancement=" + window.evaluation_multiprojets.avancement_global;
for ( i in window.evaluation_multiprojets.autres ) {
newtxt += "\n|" + i + "=" + window.evaluation_multiprojets.autres;
}
newtxt += "\n}}";
if ( ok ) {
if ( regtxt.test( text ) ) {
text = text.replace( regtxt, newtxt );
} else {
text = newtxt + "\n" + text;
}
text = text.replace( regtodo, "" );
}
return text;
}
function ev_remise_en_tete( text ) {
var reg = /(\{\{raduit de\s*\|*\}\})/;
var m = reg.exec( text );
if ( m ) {
var left = RegExp.leftContext;
var right = RegExp.rightContext;
text = m
+ "\n"
+ left.replace( /\s+$/, '' )
+ "\n"
+ right.replace( /^\s+/, '' );
}
return text;
}
//</nowiki></pre></source>