Utilisateur:Dr Brains/CategoryDeluxe.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./*
{{Sommaire à droite}}
= Informations =
* Author : Dr Brains ( https://fr.wikipedia.org/wiki/Utilisateur:Dr_Brains )
* License : CC0
* Documentation : https://fr.wikipedia.org/wiki/Projet:JavaScript/Notices/CategoryDeluxe
{{Catégorisation JS|CategoryDeluxe}}
= Code source =
<syntaxhighlight lang=javascript> */
if(typeof(CategoryDeluxeLoaded)==="undefined") window.CategoryDeluxeLoaded = (function($, mw){
"use strict";
/* </syntaxhighlight>
== Textes ==
<syntaxhighlight lang=javascript> */
window.CategoryDeluxe_Texts = {
"SeeMoreText_subcat" : "$1 {{PLURAL:$1|catégorie supplémentaire|catégories supplémentaires}}",
"SeeMoreText_page" : "$1 {{PLURAL:$1|page supplémentaire|pages supplémentaires}}",
"SeeMoreText_file" : "$1 {{PLURAL:$1|fichier supplémentaire|fichiers supplémentaires}}",
"SeeMoreTitle_subcat" : "Afficher $1 {{PLURAL:$1|catégorie supplémentaire|catégories supplémentaires}}",
"SeeMoreTitle_page" : "Afficher $1 {{PLURAL:$1|page supplémentaire|pages supplémentaires}}",
"SeeMoreTitle_file" : "Afficher $1 {{PLURAL:$1|fichier supplémentaire|fichiers supplémentaires}}",
"FileSize_TextTemplate" : "$1 $2",
"Categorytree_InfosTextP" : "$1 P",
"Categorytree_InfosTextC" : " • $1 C",
"Categorytree_InfosTextF" : " • $1 F",
"Categorytree_InfosTextE" : "vide",
"Categorytree_InfosTitle" : "Contient $1 {{PLURAL:$1|sous-catégorie|sous-catégories}}, $2 {{PLURAL:$2|page|pages}} et $3 {{PLURAL:$3|fichier|fichiers}}",
"Categorytree_Loading" : "chargement...",
"Categorytree_Collapse" : "Replier",
"Categorytree_Expand" : "Développer"
};
/* </syntaxhighlight>
== Options ==
<syntaxhighlight lang=javascript> */
window.CategoryDeluxe_RequestLimits = [1, 10, 25, 50, 100, 200];
window.CategoryDeluxe_PagesOnCategoryPage = 200;
/* </syntaxhighlight>
== Styles CSS ==
<syntaxhighlight lang=javascript> */
mw.loader.addStyleTag("" +
".CD_NewLI_subcat { border-bottom:1px dashed red; } " +
".CD_NewLI_file { border-bottom:1px dashed red; } " +
".CD_NewLI_page { border-bottom:1px dashed red; } " +
".CD_file { margin-right: 0.345em; } " +
".ltr #CD_morelinks_subcat { float:right; } " +
".ltr #CD_morelinks_file { float:right; } " +
".ltr #CD_morelinks_page { float:right; } " +
".rtl #CD_morelinks_subcat { float:left; } " +
".rtl #CD_morelinks_file { float:left; } " +
".rtl #CD_morelinks_page { float:left; } " +
"#CD_morelinks_subcat a { margin-left:0.25em;margin-right:0.25em; } " +
"#CD_morelinks_file a { margin-left:0.25em;margin-right:0.25em; } " +
"#CD_morelinks_page a { margin-left:0.25em;margin-right:0.25em; } " +
".CategoryTreeChildren_NotDone { display:none; } "
);
/* </syntaxhighlight>
== Fonctions utilitaires ==
=== Création d'un lien ===
<syntaxhighlight lang=javascript> */
var CreateLink = function(Args){
var Path = mw.config.get('wgServer') + ( Args.more ? mw.config.get('wgScript') + "?title=$1" : mw.config.get('wgArticlePath'));
var Link = '<a '
+ (typeof(Args.classes)=="object" && Args.classes.length > 0 ? 'class="'+Args.classes.join(" ")+'" ' : '')
+ (typeof(Args.target) != "undefined" ? 'target="'+Args.target+'" ' : '')
+ 'href="'+Path.split('$1').join(encodeURIComponent(Args.page)) + (Args.more ? Args.more : '') + '" '
+ 'title="'+(Args.title ? Args.title : Args.page )+'"'
+ '>'+(Args.text ? Args.text : Args.page )+'</a>';
return Link;
};
/* </syntaxhighlight>
=== Formatage des nombres ===
<syntaxhighlight lang=javascript> */
var FormatNum = function(num){
num = parseInt(num);
var numStr = num.toString();
if(num < 1000 && num > -1000) return numStr;
/* TODO : formatage des nombres ( -1000 < X < 1000 ) (selon la langue de l'utilisateur ?) */
return numStr;
}
/* </syntaxhighlight>
=== Gestion du pluriel ===
<syntaxhighlight lang=javascript> */
var Plural = function(Text){
var PluralRegExp = new RegExp("\\{\\{PLURAL[^\\}]+\\}\\}", "ig")
var Matches = Text.match(PluralRegExp);
if(Matches!=null){
for(var b=0,m=Matches.length;b<m;b++){
var Match = Matches[b];
var Params = Match.split('}}').join('').split('|');
var thisnumber = parseInt(Params[0].replace(/[^0-9]/g, ""));
var Result = "";
if(thisnumber > 1){
Result = Params[2];
}else{
Result = Params[1];
}
if(!Result) Result = "";
Text = Text.replace(Match, Result);
}
}
return Text;
}
/* </syntaxhighlight>
== Fonction principale ==
<syntaxhighlight lang=javascript> */
var NoGalleryTag = true;
var CategorytreeEnabled = false;
var CategoryPageCount = {
subcat : { total : null, shown : null, left:0 },
page : { total : null, shown : null, left:0 },
file : { total : null, shown : null, left:0 }
};
var CategoryPageIDs = { page : "mw-pages", file : "mw-category-media", subcat : "mw-subcategories" };
var CategoryDeluxe_APILimit = 500;
var UserGroups = mw.config.get('wgUserGroups');
if( UserGroups ){
if((UserGroups.indexOf("sysop")!=-1)||(UserGroups.indexOf("bot")!=-1)) CategoryDeluxe_APILimit = 5000;
}
var OnCategoryPage = function(){
var paramFrom = mw.util.getParamValue('from');
if(paramFrom) return;
var Limits = [];
for(var a=0,l=CategoryDeluxe_RequestLimits.length;a<l;a++){
if( CategoryDeluxe_RequestLimits[a] > CategoryDeluxe_APILimit ) continue;
Limits.push(CategoryDeluxe_RequestLimits[a]);
}
CategoryDeluxe_RequestLimits = Limits;
for(var type in CategoryPageIDs){
var container = document.getElementById(CategoryPageIDs[type]);
if(!container) continue;
var P = false;
var Child = container.firstChild;
while(Child){
var Tag = Child.tagName;
if(!Tag || Tag.toLowerCase() === 'a'){
var NodeToRemove = Child;
Child = Child.nextSibling;
NodeToRemove.parentNode.removeChild(NodeToRemove);
}else{
if(Tag && Tag.toLowerCase() === 'p' && !P){
P = Child;
}
Child = Child.nextSibling;
}
}
if(!P) continue;
var NextItems = GetNextItemsNumber(type, P);
if(!NextItems) continue;
GetTotalItemsNumber(type, P);
CategoryPageCount[type].left = parseInt( CategoryPageCount[type].total - CategoryPageCount[type].shown );
var NextItems = document.getElementById("remainingitems");
if(NextItems) NextItems.innerHTML = FormatNum(CategoryPageCount[type].shown);
if(CategoryPageCount[type].left > 0){
if(type === "file"){
var GalleryLis = $.makeArray( $(container).find("li.gallerybox"));
if(GalleryLis.length > 0) NoGalleryTag = false;
}else if(type=== "subcat"){
var CategoryTreeDivs = $.makeArray( $(container).find("div.CategoryTreeSection"));
if(CategoryTreeDivs.length > 0) CategorytreeEnabled = true;
}
GetCategoryMembers({
type:type,
limit:CategoryDeluxe_PagesOnCategoryPage,
initialrequest:true
});
}
}
};
var GetNextItemsNumber = function(type, P){
var Pvalue = P.innerHTML;
var CountValue = CategoryDeluxe_PagesOnCategoryPage.toString();
var Matches = Pvalue.match(/[\d\s]+/g);
if(Matches === null) return false;
for(var a=0,l=Matches.length;a<l;a++){
if(/\d/.exec(Matches[a]) === false) continue;
var realnumber = Matches[a].replace(/\s/g, "");
if(realnumber == CountValue){
CategoryPageCount[type].shown = parseInt(realnumber);
Pvalue = Pvalue.replace(Matches[a], ' <span id="remainingitems" class="realnumber">(R)</span> ');
}
}
P.innerHTML = Pvalue;
if(CategoryPageCount[type].shown === null) return false;
return true;
}
var GetTotalItemsNumber = function(type, P){
var Pvalue = P.innerHTML;
var total = parseInt(Pvalue.replace(/\D/g, ""));
CategoryPageCount[type].total = total;
};
/* </syntaxhighlight>
== Fonction de mise à jour ==
=== Requête des pages de la catégorie ===
<syntaxhighlight lang=javascript> */
var CategoryPageList = { subcat : [], page : [], file : [] };
var CategoryAllPages = [];
var CategoryPageNextSortKey = false;
var GetCategoryMembers = function(Args){
var Category = (Args.category || mw.config.get('wgPageName'));
var type = Args.type;
var Members = (Args.members || [] );
var UpdatedLis = $.makeArray( $(document).find(".CD_NewLI_"+type));
for(var a=0,l=UpdatedLis.length;a<l;a++){
$(UpdatedLis[a]).removeClass("CD_NewLI_"+type);
}
var queryopt = {
action: 'query',
list: 'categorymembers',
cmlimit: ((Args.limit + 1) || CategoryDeluxe_RequestLimit),
cmtitle: Category,
cmtype:type,
cmprop: ["title","sortkey","sortkeyprefix"],
};
if(Args.cmcontinue) queryopt.cmcontinue = Args.cmcontinue;
queryopt.continue = "";
if(CategoryPageNextSortKey && !Args.nosortkey) queryopt.cmstarthexsortkey = CategoryPageNextSortKey;
var api = new mw.Api();
api.get( queryopt ).then( function ( data ) {
var categorymembers = data.query.categorymembers;
if ( categorymembers ) {
for(var a=0,l=categorymembers.length;a<l;a++){
var thispagetitle = categorymembers[a].title;
var thispagesortkey = categorymembers[a].sortkey;
var thispagesortkeyprefix = categorymembers[a].sortkeyprefix;
if( thispagesortkeyprefix === "" ){
var NS = mw.config.get('wgFormattedNamespaces');
thispagesortkeyprefix = thispagetitle.split(NS[14]+":").join("").split(NS[6]+":").join("");
}
thispagesortkeyprefix = thispagesortkeyprefix.substr(0,1);
var NewCat = {title:thispagetitle,sortkey:thispagesortkey,sortkeyprefix:thispagesortkeyprefix,shown:false};
Members.push(NewCat);
}
}
if(Args.allitems && data.continue && data.continue.cmcontinue){
Args.cmcontinue = data.continue.cmcontinue;
GetCategoryMembers(Args);
}else{
if(typeof(Args.callback) === "function"){
Args.members = Members;
Args.callback(Args);
}else{
for(var a=0,l=Members.length;a<l;a++){
var member = Members[a];
var title = member.title;
if(CategoryAllPages.indexOf(title)===-1){
CategoryPageList[type].push(member);
CategoryAllPages.push(title);
CategoryPageNextSortKey = member.sortkey;
}
}
if(!data.continue || !data.continue.cmcontinue) CategoryPageNextSortKey = false;
UpdatePage(Args);
}
}
} );
};
/* </syntaxhighlight>
=== Récupération du résultat de la requête ===
<syntaxhighlight lang=javascript> */
var UpdatePage = function(Args){
var type = Args.type;
var Limit = Args.limit;
var Itemsadded = 0;
for(var a=0,l=CategoryPageList[type].length;a<l;a++){
var thiscat = CategoryPageList[type][a];
if(thiscat.shown) continue;
if(Itemsadded === Limit) continue;
if(!Args.initialrequest){
AddCatToPage(thiscat, type);
CategoryPageCount[type].left--;
CategoryPageCount[type].shown++;
}
CategoryPageList[type][a].shown = true;
Itemsadded++;
}
var NextItems = document.getElementById("remainingitems");
if(NextItems) NextItems.innerHTML = FormatNum(CategoryPageCount[type].shown);
CreateMoreLinks(type);
}
/* </syntaxhighlight>
=== Ajout d'un élément dans la page ===
<syntaxhighlight lang=javascript> */
var AddCatToPage = function(catObject, type){
var title = catObject.title;
var sortkeyprefix = catObject.sortkeyprefix;
var container = document.getElementById(CategoryPageIDs[type]);
if(!container) return;
if(type === "file" && !NoGalleryTag){ // ------------------------ Gallery
var NewLi = document.createElement('li');
var AllLis = $.makeArray( $(container).find("li.gallerybox") );
var LastLi = AllLis[(AllLis.length-1)];
NewLi.className = (LastLi.className ? LastLi.className : "" ) + ( $(LastLi).hasClass("CD_NewLI_"+type) ? "" : " CD_file CD_NewLI_"+type);
var Width = parseInt(LastLi.style.width.split("px").join(""));
NewLi.setAttribute("style", LastLi.getAttribute("style"));
LastLi.parentNode.appendChild(NewLi);
GetFileImage({li:NewLi, title:title,dimension:Width});
}else{ // ------------------------------------------------------------- Other cases
var Hs = $.makeArray( $(container).find("h3") );
if(Hs.length === 0) return;
var NewLi = document.createElement('li');
NewLi.innerHTML = CreateLink({ page:title, text:(type==="subcat" ? title.split(mw.config.get('wgFormattedNamespaces')[14]+":").join("") : title) });
NewLi.className = "CD_NewLI_"+type;
var Target = false;
for(var a=0,l=Hs.length;a<l;a++){
var H = Hs[a];
var ThisSortKey = H.innerHTML;
if(ThisSortKey != sortkeyprefix) continue;
Target = H.parentNode;
}
if(!Target){
var LastH = Hs[(Hs.length-1)];
var Target = LastH.parentNode;
if($(Target).hasClass("mw-category-group")){
var NewDiv = document.createElement('div');
NewDiv.className = "mw-category-group";
Target.parentNode.appendChild(NewDiv);
Target = NewDiv;
}
var NewH = document.createElement('h3');
NewH.innerHTML = sortkeyprefix;
var NewUL = document.createElement('ul');
NewUL.appendChild(NewLi);
Target.appendChild(NewH);
Target.appendChild(NewUL);
}
var ULs = Target.getElementsByTagName('ul');
var UL = ULs[(ULs.length-1)];
UL.appendChild(NewLi);
if(type === "subcat" && CategorytreeEnabled){ // ------- Categorytree
GetCategorytreeInfos({link:NewLi.firstChild, title:title});
}
}
};
/* </syntaxhighlight>
=== Gestion categorytree ===
<syntaxhighlight lang=javascript> */
var GetCategorytreeInfos = function(Args){
var queryopt = {
action: 'query',
prop: 'categoryinfo',
titles : Args.title
};
var api = new mw.Api();
api.get( queryopt ).then( function ( data ) {
var pages = data.query.pages;
if(!pages) return;
for(var id in pages){
var ThisItem = pages[id];
if(ThisItem.title !== Args.title) continue;
Args.infos = ThisItem.categoryinfo;
CreateCategorytreeLink(Args);
}
} );
}
var CreateCategorytreeLink = function(Args){
var title = Args.title;
var infos = Args.infos;
var Link = Args.link;
if(!Link) return;
if($(Link).hasClass("CategoryTreeLabel")) return;
Link.className = "CategoryTreeLabel CategoryTreeLabelNs14 CategoryTreeLabelCategory";
var CategoryTreeSection = document.createElement('div');
CategoryTreeSection.className = "CategoryTreeSection";
Link.parentNode.appendChild(CategoryTreeSection);
var CategoryTreeItem = document.createElement('div');
CategoryTreeItem.className = 'CategoryTreeItem';
CategoryTreeSection.appendChild(CategoryTreeItem);
var CategoryTreeChildren = document.createElement('div');
CategoryTreeChildren.className = 'CategoryTreeChildren';
CategoryTreeChildren.style.display = "none";
CategoryTreeSection.appendChild(CategoryTreeChildren);
Link.parentNode.removeChild(Link);
CategoryTreeItem.appendChild(Link);
var TextInfo = {
"P" : ( infos.pages ? CategoryDeluxe_Texts["Categorytree_InfosTextP"].split("$1").join(infos.pages) : ""),
"C" : ( infos.subcats ? CategoryDeluxe_Texts["Categorytree_InfosTextC"].split("$1").join(infos.subcats) : ""),
"F" : ( infos.files ? CategoryDeluxe_Texts["Categorytree_InfosTextF"].split("$1").join(infos.files) : "")
};
var LTR = $(document.body).hasClass("ltr");
var RecapSpan = document.createElement('span');
RecapSpan.innerHTML = CategoryDeluxe_Texts["Categorytree_InfosTextE"];
var BulletSpan = document.createElement('span');
if(LTR){
Link.parentNode.insertBefore(BulletSpan, Link);
Link.parentNode.insertBefore(document.createTextNode(" "), Link);
Link.parentNode.insertBefore(RecapSpan, Link.nextSibling);
Link.parentNode.insertBefore(document.createTextNode(" "), Link.nextSibling);
}else{
Link.parentNode.insertBefore(BulletSpan, Link.nextSibling);
Link.parentNode.insertBefore(document.createTextNode(" "), Link.nextSibling);
Link.parentNode.insertBefore(RecapSpan, Link);
Link.parentNode.insertBefore(document.createTextNode(" "), Link);
}
var RecapTitle = CategoryDeluxe_Texts["Categorytree_InfosTitle"];
RecapTitle = RecapTitle.split("$1").join(infos.subcats);
RecapTitle = RecapTitle.split("$2").join(infos.pages);
RecapTitle = RecapTitle.split("$3").join(infos.files);
RecapSpan.title = Plural(RecapTitle);
var TotalTextInfos = TextInfo["P"] + TextInfo["C"] + TextInfo["F"];
if(TotalTextInfos !== ""){
var FinaleText = "";
var Infos = [" – "]
for(var info in TotalTextInfos){
var thisInfo = TotalTextInfos[info];
if( typeof(thisInfo)==="string" && thisInfo !== "") Infos.push(thisInfo);
}
if(!LTR){
Infos = Infos.reverse();
}
for(var a=0,l=Infos.length;a<l;a++){
FinaleText += Infos[a];
}
RecapSpan.innerHTML = FinaleText;
}
if(TextInfo["C"] === ""){
$(BulletSpan).addClass("CategoryTreeEmptyBullet");
BulletSpan.innerHTML = (LTR ? "►" : "◄");
}else{
$(BulletSpan).addClass("CategoryTreeBullet");
var BulletLink = document.createElement('span');
$(BulletLink).addClass("CategoryTreeToggle");
BulletLink.innerHTML = (LTR ? "►" : "◄");
BulletLink.title = CategoryDeluxe_Texts["Categorytree_Expand"];
BulletLink.onclick = function(){
CT_ShowHideNode(this, CategoryTreeChildren, title);
return false;
}
BulletSpan.appendChild(BulletLink);
}
if(typeof(Args.callback)==="function") Args.callback(Args);
}
var CT_ShowHideNode = function(Link, CategoryTreeChildren, title){
var LTR = $(document.body).hasClass("ltr");
if(Link.title == CategoryDeluxe_Texts["Categorytree_Expand"]){
Link.innerHTML = "▼";
Link.title = CategoryDeluxe_Texts["Categorytree_Collapse"];
CategoryTreeChildren.style.display = "";
if(!CategoryTreeChildren.firstChild){
CategoryTreeChildren.innerHTML = '<div class="LoadingSpan">' + CategoryDeluxe_Texts["Categorytree_Loading"] + '</div>';
GetCategoryMembers({
category : title,
type : "subcat",
limit : (CategoryDeluxe_APILimit-1),
container : CategoryTreeChildren,
nosortkey : true,
callback : function(Args){
var Members = Args.members;
var CategoryCount = 0;
for(var item in Members){
CategoryCount++;
}
for(var item in Members){
var member = Members[item];
var title = member.title;
var NewLink = document.createElement('a');
NewLink.title = title;
NewLink.className = "CategoryTreeChildren_NotDone";
NewLink.innerHTML = title.split(mw.config.get('wgFormattedNamespaces')[14]+":").join("");
NewLink.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(encodeURIComponent(title));
CategoryTreeChildren.appendChild(NewLink);
GetCategorytreeInfos({
title : title,
link : NewLink,
mainnode : CategoryTreeChildren,
catcount : CategoryCount,
callback : DeleteLoadingSpan
});
}
}
});
}
}else{
Link.innerHTML = (LTR ? "►" : "◄");
Link.title = CategoryDeluxe_Texts["Categorytree_Expand"];
CategoryTreeChildren.style.display = "none";
}
}
var DeleteLoadingSpan = function(Args){
var allchilds = [];
var First = Args.mainnode.firstChild;
while(First){
if(First.tagName && First.tagName.toLowerCase() === "div" && $(First).hasClass("CategoryTreeSection")) allchilds.push(First);
First = First.nextSibling;
if(!First) break;
}
var ChildDivsLength = allchilds.length;
if(ChildDivsLength < Args.catcount) return;
var LoadingSpan = $.makeArray( $(Args.mainnode).find(".LoadingSpan"))[0];
if(LoadingSpan) LoadingSpan.parentNode.removeChild(LoadingSpan);
}
/* </syntaxhighlight>
=== Gestion galerie de fichiers ===
<syntaxhighlight lang=javascript> */
var GetFileImage = function(Args){
var Li = Args.li;
var title = Args.title;
var dimension = Args.dimension;
var queryopt = {
action : 'query',
prop : 'imageinfo',
titles : title,
iiprop : "size",
continue : ""
};
var apiSize = new mw.Api();
apiSize.get( queryopt ).then( function ( data ) {
var pages = data.query.pages;
if(!pages) return;
for(var itemindex in pages){
var item = pages[itemindex];
if(item.title !== title) continue;
var width = parseInt(item.imageinfo[0].width);
var height = parseInt(item.imageinfo[0].height);
var max = (dimension - 35);
var ratio = Math.max((width / max),(height / max));
var targetwidth = parseInt(width/ratio);
var targetheight = parseInt(height/ratio);
queryopt = {
action : 'query',
prop : 'imageinfo',
titles : title,
iiprop : ["url","size"],
iiurlwidth : targetwidth,
continue : ""
};
var apiURL = new mw.Api();
apiURL.get( queryopt ).then( function ( data ) {
pages = data.query.pages;
if(!pages) return;
for(var pageindex in pages){
var item = pages[pageindex];
if(item.title !== title) continue;
Args.url = item.imageinfo[0].thumburl;
Args.size = parseInt(item.imageinfo[0].size);
Args.width = ( ratio > 1 ? parseInt(item.imageinfo[0].thumbwidth ) : targetwidth);
Args.height = ( ratio > 1 ? parseInt(item.imageinfo[0].thumbheight) : targetheight);
ShowFileImage(Args);
}
} );
}
} );
};
var ShowFileImage = function(Args){
var Li = Args.li;
var title = Args.title;
var dimension = Args.dimension;
var imagesrc = Args.url;
var imagewidth = Args.width;
var imageheight = Args.height;
var imagesize = Args.size;
var MainDiv = document.createElement('div');
MainDiv.style.width = dimension + "px";
Li.appendChild(MainDiv);
var ThumbDiv = document.createElement('div');
ThumbDiv.className = "thumb";
ThumbDiv.style.width = (dimension-5) + "px";
MainDiv.appendChild(ThumbDiv);
var MarginDiv = document.createElement('div')
var remainingspace = ((dimension-5) - imageheight);
if( remainingspace > 0) MarginDiv.style.margin = parseInt((remainingspace*5)/10) + "px auto";
ThumbDiv.appendChild(MarginDiv);
var Link = document.createElement('a');
Link.className = "image";
Link.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(encodeURIComponent(title));
MarginDiv.appendChild(Link);
var Image = document.createElement('img');
Image.width = imagewidth;
Image.height = imageheight;
Image.src = imagesrc;
Link.appendChild(Image);
var CaptionDiv = document.createElement('div');
CaptionDiv.className = "gallerytext";
MainDiv.appendChild(CaptionDiv);
var SizeText;
var Units = { "o" : 0, "Kio" : 1000, "Mio":1000000};
for(var unit in Units){
var max = Units[unit];
if(imagesize < max) continue;
var Size = ( max !== 0 ? parseInt( (imagesize / max) ) : imagesize);
SizeText = CategoryDeluxe_Texts["FileSize_TextTemplate"].split("$1").join(Size).split("$2").join(unit);
}
CaptionDiv.innerHTML = CreateLink({page:title,text:(title.split(mw.config.get('wgFormattedNamespaces')[6]+":").join(""))}) + "<br />" + SizeText;
};
/* </syntaxhighlight>
=== Création / mise à jour du lien de relance ===
<syntaxhighlight lang=javascript> */
var CreateMoreLinks = function(type){
var P = document.getElementById('CD_morelinks_'+type);
if(!P){
var container = document.getElementById(CategoryPageIDs[type]);
if(!container) return;
var MoreLinkP = document.createElement('p');
MoreLinkP.id = 'CD_morelinks_'+type;
container.appendChild(MoreLinkP);
}else if(CategoryPageCount[type].left === 0){
P.parentNode.removeChild(P);
return;
}
var AllreadyDoneLinks = [];
for(var a=0,l=CategoryDeluxe_RequestLimits.length;a<l;a++){
var NewLink = CreateMoreLink(type, a, AllreadyDoneLinks);
if(NewLink) AllreadyDoneLinks.push(NewLink);
}
}
var CreateMoreLink = function(type, limitposition, AllreadyDoneLinks){
var RequestLimit = CategoryDeluxe_RequestLimits[limitposition];
var LeftCount = ( CategoryPageCount[type].left > RequestLimit ? RequestLimit : CategoryPageCount[type].left );
var LinkText = Plural(CategoryDeluxe_Texts[("SeeMoreText_"+type)].split("$1").join(LeftCount));
var LinkTitle = Plural(CategoryDeluxe_Texts[("SeeMoreTitle_"+type)].split("$1").join(LeftCount));
var MoreLink = document.getElementById("CD_morelink_"+type+"_"+limitposition);
if(MoreLink){
if(CategoryPageCount[type].left < 1 || AllreadyDoneLinks.indexOf(LeftCount) !== -1 ){
MoreLink.parentNode.removeChild(MoreLink);
return LeftCount;
}else{
MoreLink.title = LinkTitle;
MoreLink.innerHTML = "("+LinkText+")";
MoreLink.onclick = function(){
GetCategoryMembers({
type:type,
limit:LeftCount,
initialrequest:false
});
return false;
};
}
}else if(AllreadyDoneLinks.indexOf(LeftCount) === -1){
var MoreLinkP = document.getElementById('CD_morelinks_'+type);
MoreLink = document.createElement('a');
MoreLinkP.appendChild(MoreLink);
MoreLink.id = "CD_morelink_"+type+"_"+limitposition;
MoreLink.title = LinkTitle;
MoreLink.innerHTML = "("+LinkText+")";
MoreLink.href = "javascript:;";
MoreLink.onclick = function(){
GetCategoryMembers({
type:type,
limit:LeftCount,
initialrequest:false
});
return false;
};
}
return LeftCount;
};
/* </syntaxhighlight>
== Lancement ==
<syntaxhighlight lang=javascript> */
var LangModuleImplemented = false;
var CustomDone = false;
var Loading = function(func){
if(typeof(func)!=="function") return;
var Modules = ['mediawiki.api', 'mediawiki.util'];
var Lang = mw.config.get('wgUserLanguage');
if(Lang !== "fr"){
if(!LangModuleImplemented){
LangModuleImplemented = true;
var URL = '//fr.wikipedia.org/w/index.php?title=MediaWiki:Gadget-CategoryDeluxe.js/i18n/'+Lang+'.js&action=raw&ctype=text/javascript';
mw.loader.implement( "categorydeluxelang", [ URL ], {}, {}, {} );
}
Modules.push("categorydeluxelang");
}
mw.loader.using( Modules, function(){
$( document ).ready(function(){
if(!CustomDone){
CustomDone = true;
if( typeof(CategoryDeluxe_getLangCustom) === "function" ) try{ CategoryDeluxe_getLangCustom(); }catch(e){ }
if( typeof(CategoryDeluxe_getSiteCustom) === "function" ) try{ CategoryDeluxe_getSiteCustom(); }catch(e){ }
if( typeof(CategoryDeluxe_getUserCustom) === "function" ) try{ CategoryDeluxe_getUserCustom(); }catch(e){ }
}
func();
});
});
};
if(( mw.config.get('wgNamespaceNumber') == 14 && ["delete","history"].indexOf( mw.config.get('wgAction') ) === -1 )){
Loading(OnCategoryPage);
}
return true; })(jQuery, mediaWiki);
//</syntaxhighlight>