Utilisateur:Zebulon84/Gadget-BandeauxEbauches.js
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.//<noinclude>{{Catégorisation JS|BandeauxEbauches}}</noinclude>
//
// Documentation : [[Projet:JavaScript/Notices/BandeauxEbauches]]
//<source lang=javascript>
//<pre><nowiki>
/***
* VARIABLES PERSONNALISABLES
*/
// Nombre maximum de paramètres pour le modèle {{Ébauche}}
window.BandeauxEbauches_MaxParams = 6;
// Publication automatique
if ( window.BandeauxEbauches_AutoEdit === undefined ) {
window.BandeauxEbauches_AutoEdit = true;
}
// Modification mineure
if ( window.BandeauxEbauches_MinorEdit === undefined ) {
window.BandeauxEbauches_MinorEdit = 1;
}
// Suivre la page
if ( window.BandeauxEbauches_Watchthis === undefined ) {
window.BandeauxEbauches_Watchthis = -1;
}
// Nombre de suggestions
if ( window.BandeauxEbauches_TailleListe === undefined ) {
window.BandeauxEbauches_TailleListe = 5;
}
// Délai (en secondes ) lors de la sélection d'une suggestion au clavier
if ( window.BandeauxEbauches_KeyboardDelay === undefined ) {
window.BandeauxEbauches_KeyboardDelay = 0.2;
}
// Groupe nécessaire pour appliquer une balise
if ( window.BandeauxEbauches_TagGroup === undefined ) {
window.BandeauxEbauches_TagGroup = "user";
}
// Balise à appliquer aux modifs
if ( window.BandeauxEbauches_Tag === undefined ) {
window.BandeauxEbauches_Tag = "";
if ( mw.config.get( 'wgServer' ) === '//fr.wikipedia.org' ) {
window.BandeauxEbauches_Tag = "BandeauxEbauches";
}
}
/***
* EDITION
*/
window.BandeauxEbauches_DoEdit = function ( data, OldModele, NewModele ) {
while ( document.body.firstChild ) {
document.body.removeChild( document.body.firstChild );
}
document.body.innerHTML = data;
if ( window.BandeauxEbauches_AutoEdit ) {
document.editform.style.display = 'none';
}
var OldText = document.getElementById( 'wpTextbox1' ).value;
OldText = OldText.replace( /\{\{ébauche/g, "{{Ébauche" );
OldText = OldText.replace( /\{\{Ébauche \|/g, "{{Ébauche|" );
var BeforeModele = OldText.substring( 0, OldText.indexOf( '{{Ébauche|' ) );
var AfterModele = OldText.split( BeforeModele ).join( '' ).split( OldModele + '\n' ).join( '' );
var TexteFinal = BeforeModele + NewModele + AfterModele;
document.getElementById( 'wpMinoredit' ).checked = ( window.BandeauxEbauches_MinorEdit ? "checked" : false );
document.getElementById( 'wpTextbox1' ).value = TexteFinal;
document.getElementById( 'wpSummary' ).value = 'Changement [[Modèle:Ébauche|{{Ébauche}}]] ; avec [[MediaWiki:Gadget-BandeauxEbauches.js|BandeauxEbauches]]';
if ( window.BandeauxEbauches_AutoEdit ) {
var watchthisparam = {};
watchthisparam[-1] = "nochange";
watchthisparam[ 0] = "unwatch";
watchthisparam[ 1] = "watch";
var datas = {
action : 'edit',
title : mw.config.get( 'wgPageName' ),
section : 0,
text : TexteFinal,
summary : document.editform.wpSummary.value,
minor : ( document.editform.wpMinoredit.checked ? 1 : 0 ),
watchlist : watchthisparam[ window.BandeauxEbauches_Watchthis ],
token : document.editform.wpEditToken.value
};
var ApplyTag = window.BandeauxEbauches_gettag();
if ( ApplyTag ) {
datas.summary = 'Changement [[Modèle:Ébauche|{{Ébauche}}]]';
datas.tags = ApplyTag;
}
var api = new mw.Api();
api.post(
datas,
{ 'contentType':'application/x-www-form-urlencoded' }
).then( function () {
window.location.reload();
});
}
};
window.BandeauxEbauches_Edit = function ( id, OldListe, Supprimer ) {
var a;
OldListe = decodeURIComponent( OldListe ).replace( /ZAPOSTROPHE/g, "'" ).replace( /SPLIT/g, "|" );
var OldModele = '{{Ébauche' + OldListe + '}}';
var NewModele = '';
if ( !Supprimer ) {
NewModele += '{{Ébauche';
var BandeauForm = document.getElementById( 'FormBandeau_'+id );
var Inputs = BandeauForm.getElementsByTagName( 'input' );
var InputValue;
for ( a = 0; a < Inputs.length; a++ ) {
if ( Inputs[a].type === "text" ) {
InputValue = Inputs[a].value;
if ( InputValue ) {
NewModele += '|' + InputValue;
}
}
}
NewModele += '}}\n';
if ( NewModele.indexOf( '|' ) === -1 ) {
NewModele = '';
}
}
var SurEtCertain = confirm( OldModele + ' -> ' + NewModele + '\n\nOK ?' );
if ( !SurEtCertain ) {
window.BandeauxEbauches_Cancel( id, OldListe );
return;
}
window.BandeauxEbauches_Edit_running = 1;
$.get(
mw.config.get( 'wgScript' ),
{
title: mw.config.get( 'wgPageName' ),
action: 'edit',
section: 0
}
).done( function ( data ) {
window.BandeauxEbauches_DoEdit( data, OldModele, NewModele );
});
};
/***
* SUGGESTIONS
*/
window.BandeauxEbauches_TextChanged_running = [];
window.BandeauxEbauches_Edit_running = false;
window.BandeauxEbauches_GetListeEbauche = function () {
var api = new mw.Api();
api.get({
format: 'json',
action: 'expandtemplates',
text: '{{#invoke:Bandeau|listeEbauches}}',
prop: 'wikitext',
utf8: true
}).done( function ( data ) {
var i, dataList,
rawList = data.expandtemplates.wikitext,
docfrag = document.createDocumentFragment();
if ( rawList ) {
window.BandeauxEbauches_Data.listEbauches = rawList.split( '\n' );
dataList = document.getElementById( 'OptionsBandeauEbauche' );
for ( i = 0; i < window.BandeauxEbauches_Data.listEbauches.length ; i++ ) {
option = document.createElement( 'option' );
option.value = window.BandeauxEbauches_Data.listEbauches[ i ];
docfrag.appendChild( option );
}
dataList.appendChild( docfrag );
}
});
};
window.BandeauEbauches_GetBandeauHtml = function ( Modele, fnct ) {
var api = new mw.Api();
api.get({
format: 'json',
action: 'parse',
title: mw.config.get( 'wgPageName' ),
text: Modele,
prop: 'text',
disablelimitreport: true
}).done( function ( data ) {
var NewWrap,
PreviewHtml = document.createElement( 'div' );
PreviewHtml.innerHTML = data.parse.text['*'];
NewWrap = $( PreviewHtml ).find( '.bandeau-niveau-ebauche' )[0].innerHTML;
fnct( NewWrap );
});
};
window.BandeauEbauches_Previsulise = function ( BandeauCount ) {
var a, Input,
Liste = '',
Modele = '{{Ébauche',
WrapBandeau = document.getElementById( 'WrapBandeau_' + BandeauCount );
for ( a = 0; a < window.BandeauxEbauches_MaxParams; a++ ) {
Input = document.getElementById( 'InputBandeau_' + BandeauCount + '_' + a );
if ( !Input
|| Input.value
&& window.BandeauxEbauches_Data.listEbauches.indexOf( Input.value ) === -1
) {
return;
}
if ( Input.value ) {
Liste += 'SPLIT' + Input.value;
Modele += '|' + Input.value;
}
}
Liste = encodeURIComponent( Liste.replace( /'/g, "ZAPOSTROPHE" ) );
if ( window.BandeauxEbauches_Data[ Liste ] ) {
WrapBandeau.innerHTML = window.BandeauxEbauches_Data[ Liste ];
} else {
Modele += '}}';
window.BandeauEbauches_GetBandeauHtml(
Modele,
function ( NewWrap ) {
window.BandeauxEbauches_Data[ Liste ] = NewWrap;
WrapBandeau.innerHTML = NewWrap;
}
);
}
};
window.BandeauxEbauches_TextChanged = function ( id1, id2 ) {
clearTimeout( window.BandeauxEbauches_TextChanged_running[id2] );
window.BandeauxEbauches_TextChanged_running[id2] = setTimeout(
function () {
var idInput, Input, Value;
if ( window.BandeauxEbauches_Edit_running ) {
return;
}
idInput = 'InputBandeau_' + id1 + '_' + id2;
Input = document.getElementById( idInput );
if ( !Input ) {
return;
}
Value = Input.value;
if ( window.BandeauxEbauches_Data.listEbauches.indexOf( Value ) > -1 ) {
Input.style.color = "";
window.BandeauEbauches_Previsulise( id1 );
} else {
Input.style.color = "red";
}
},
window.BandeauxEbauches_KeyboardDelay * 1000
);
};
/***
* FORMULAIRE
*/
window.BandeauxEbauches_BouttonModifier = function ( BandeauCount ) {
var modifier = document.createElement( 'button' );
modifier.id = 'ModifierBandeau_' + BandeauCount;
modifier.title = "Modifier le bandeau d\'ébauche";
modifier.onclick = function() {
window.BandeauxEbauches_GetBandeaux( BandeauCount );
};
modifier.textContent = 'modifier';
modifier.style.float = 'right';
return modifier;
};
window.BandeauxEbauches_Cancel = function ( BandeauCount, Liste ) {
var Bandeau = document.getElementById( 'DivBandeau_' + BandeauCount );
if ( Liste ) {
Bandeau.innerHTML = window.BandeauxEbauches_Data.initial;
Bandeau.insertBefore(
window.BandeauxEbauches_BouttonModifier( BandeauCount ),
Bandeau.firstChild
);
} else {
Bandeau.parentNode.removeChild( Bandeau );
}
};
window.BandeauxEbauches_ModifyBandeau = function ( BandeauCount, ListeBandeaux ) {
var a, b, Input,
Bandeau = document.getElementById( 'DivBandeau_' + BandeauCount ),
Modifier = document.getElementById( 'ModifierBandeau_' + BandeauCount ),
WrapBandeau = document.createElement( 'div' ),
FormBandeau = document.createElement( 'form' ),
Valider = document.createElement( 'input' ),
Annuler = document.createElement( 'input' ),
FormLabel = document.createElement( 'label' ),
LabelLine = document.createElement( 'p' ),
InputLine = document.createElement( 'p' ),
Datalist = document.createElement( 'datalist' );
Liste = '',
AllBandeaux = [],
TextChanged = function (i) {
return function () {
window.BandeauxEbauches_TextChanged( BandeauCount, i );
};
};
if ( !Bandeau || Bandeau.firstElementChild.id === 'WrapBandeau_' + BandeauCount ) {
return;
}
if ( Modifier ) {
Modifier.parentNode.removeChild( Modifier );
}
window.BandeauxEbauches_Data = {
initial: Bandeau.innerHTML
};
WrapBandeau.id = 'WrapBandeau_' + BandeauCount;
Bandeau.insertBefore( WrapBandeau, Bandeau.firstChild );
while ( Bandeau.childNodes[1] ) {
WrapBandeau.appendChild( Bandeau.childNodes[1] );
}
for ( a = 0; a < window.BandeauxEbauches_MaxParams; a++ ) {
if ( ListeBandeaux[a] ) {
AllBandeaux[a] = ListeBandeaux[a];
Liste += 'SPLIT' + ListeBandeaux[a];
} else {
AllBandeaux[a] = '';
}
}
Liste = encodeURIComponent( Liste.replace( /'/g, "ZAPOSTROPHE" ) );
window.BandeauxEbauches_Data[ Liste ] = WrapBandeau.innerHTML;
FormLabel.htmlFor = 'FormBandeau_' + BandeauCount;
FormLabel.textContent = 'Sélectionner les bandeaux d\'ébauche à apposer.';
FormLabel.style.fontWeight = 'bold';
FormLabel.style.paddingRight = '1em';
Valider.type = 'button';
Valider.value = 'Valider';
Valider.onclick = function () {
window.BandeauxEbauches_Edit( BandeauCount, Liste );
};
Annuler.type = 'button';
Annuler.value = 'Annuler';
Annuler.onclick = function () {
window.BandeauxEbauches_Cancel( BandeauCount, Liste );
};
LabelLine.appendChild( FormLabel );
LabelLine.appendChild( Valider );
LabelLine.appendChild( Annuler );
Datalist.id = 'OptionsBandeauEbauche';
InputLine.appendChild( Datalist );
if ( !window.BandeauxEbauches_Data.listEbauches ) {
window.BandeauxEbauches_GetListeEbauche();
}
for ( a = 0; a < window.BandeauxEbauches_MaxParams; a++ ) {
Input = document.createElement( 'input' );
Input.id = 'InputBandeau_' + BandeauCount + '_' + a;
Input.type = 'text';
Input.value = AllBandeaux[a];
Input.setAttribute( 'list', 'OptionsBandeauEbauche' );
Input.addEventListener( 'input', TextChanged( a ) );
InputLine.appendChild( Input );
}
FormBandeau.id = 'FormBandeau_' + BandeauCount;
FormBandeau.style.marginTop = '1em';
FormBandeau.style.borderTop = '1px solid #aaa';
FormBandeau.appendChild( LabelLine );
FormBandeau.appendChild( InputLine );
Bandeau.appendChild( FormBandeau );
document.getElementById( 'InputBandeau_' + BandeauCount + '_0' ).focus();
};
window.BandeauxEbauches_CreateBandeau = function () {
var Content = document.getElementById( 'bodyContent' );
if ( !Content ) {
Content = document.getElementById( 'mw_contentholder' );
}
if ( !Content ) {
Content = document.getElementById( 'article' );
}
if ( !Content ) {
return;
}
var ListeBandeaux = [];
var NewBandeau = document.createElement( 'div' );
NewBandeau.className = "plainlinks bandeau-niveau-ebauche bandeau-article";
NewBandeau.id = 'DivBandeau_0';
window.BandeauEbauches_GetBandeauHtml(
'{{Ébauche}}',
function ( NewWrap ) {
NewBandeau.innerHTML = NewWrap;
window.BandeauxEbauches_ModifyBandeau( 0, ListeBandeaux );
}
);
var ContentSub;
if ( ["monobook","chick","myskin","simple","vector"].indexOf( mw.config.get( 'skin' ) ) !== -1 ) {
ContentSub = document.getElementById( 'contentSub' );
Content.insertBefore( NewBandeau, ContentSub.nextSibling );
} else if ( mw.config.get( 'skin' ) === "modern" ) {
ContentSub = Content.firstChild;
Content.insertBefore( NewBandeau, ContentSub.nextSibling );
} else if ( mw.config.get( 'skin' ) === "cologneblue" ) {
ContentSub = Content.getElementsByTagName( 'p' )[0];
Content.insertBefore( NewBandeau, ContentSub.nextSibling );
} else if ( mw.config.get( 'skin' ) === "standard" ) {
ContentSub = document.getElementById( 'catlinks' );
Content.insertBefore( NewBandeau, ContentSub.nextSibling );
} else if ( mw.config.get( 'skin' ) === "nostalgia" ) {
Content.insertBefore( NewBandeau, Content.firstChild );
}
};
/***
* RECHERCHE DES PARAMÈTRES DU BANDEAU ACTUEL
*/
window.BandeauxEbauches_UpdateDone = function ( data, BandeauCount ) {
var Temp = document.createElement( 'div' );
Temp.innerHTML = data;
var OldText = Temp.getElementsByTagName( 'textarea' )[0].value;
OldText = OldText.replace( /\{\{ébauche/g, "{{Ébauche" );
OldText = OldText.replace( /\{\{Ébauche \|/g, "{{Ébauche|" );
var BeforeModele = OldText.substring( 0, OldText.indexOf( '{{Ébauche' ) );
var Modele = OldText.split( BeforeModele + '{{Ébauche' ).join( '' );
while ( Modele.indexOf( '}}' ) !== -1 ) {
Modele = Modele.substring( 0, Modele.indexOf( '}}' ) );
if ( Modele.indexOf( '}}' ) !== -1 ) {
break;
}
}
if ( Modele!=='' ) {
Modele = Modele + '|';
}
var ListeBandeaux = [];
var ListeBandeauxCount = -1;
while ( Modele.indexOf( '|' ) !== -1 ) {
var Param = Modele.split( '|' )[1].split( '|' )[0];
ListeBandeauxCount++;
ListeBandeaux[ListeBandeauxCount] = Param;
Modele = Modele.split( '|'+Param ).join( '' );
}
window.BandeauxEbauches_ModifyBandeau( BandeauCount, ListeBandeaux );
};
window.BandeauxEbauches_GetBandeaux = function ( BandeauCount ) {
$.get(
mw.config.get( 'wgScript' ),
{
title: mw.config.get( 'wgPageName' ),
action: 'edit',
section: 0
}
).done( function ( data ) {
window.BandeauxEbauches_UpdateDone( data, BandeauCount );
});
};
/***
* LANCEMENT
*/
window.BandeauxEbauches_gettag = function () {
if ( !window.BandeauxEbauches_Tag || !window.BandeauxEbauches_TagGroup ) {
return false;
}
var UserGroups = mw.config.get( 'wgUserGroups' );
if ( UserGroups.indexOf( window.BandeauxEbauches_TagGroup ) !== -1 ) {
return window.BandeauxEbauches_Tag;
}
return false;
};
window.BandeauxEbauches = function () {
var Cactions, ul, li, a,
Ebauche = false;
$( 'div.bandeau-niveau-ebauche.bandeau-article' ).each( function ( index ) {
var modifier = window.BandeauxEbauches_BouttonModifier( index );
this.insertBefore( modifier, this.firstChild );
this.id = 'DivBandeau_' + index;
Ebauche = true;
} );
if ( !Ebauche ) {
Cactions = document.getElementById( 'p-cactions' );
if ( Cactions ) {
ul = Cactions.getElementsByTagName( 'ul' )[0];
if ( ul ) {
li = document.createElement( 'li' );
a = document.createElement( 'a' );
a.title = "Ajouter un bandeau d\'ébauche";
a.onclick = window.BandeauxEbauches_CreateBandeau;
a.textContent = 'Ébauche';
li.appendChild( a );
ul.appendChild( li );
$( Cactions ).removeClass( 'emptyPortlet' );
}
}
}
};
if ( ( mw.config.get( 'wgAction' ) === 'view' ||
mw.config.get( 'wgAction' ) === 'purge' ) &&
mw.config.get( 'wgNamespaceNumber' ) > -1 ) {
$( window.BandeauxEbauches );
}
//</nowiki></pre></source>