User:DStoykov/defaultsort.js: Difference between revisions

Content deleted Content added
No edit summary
m Remove legacy globals per phab:T72470 (via WP:JWB)
 
(8 intermediate revisions by 2 users not shown)
Line 1:
/* <pre> [[User:DStoykov/defaultsort.js]] <nowiki> */
 
// [[User:DStoykov/defaultsort.js]]
 
function defaultsort() {
//var apiurl = mw.config.get('wgServer')+mw.config.get('wgScriptPath')+'/api.php?action=query&meta=siteinfo&siprop=namespaces&format=xml';
var txt = document.editform.wpTextbox1;
//var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;
//xmlhttp.open('GET',apiurl, false);
//xmlhttp.send(null);
//alert(xmlhttp.responseXML.getElementById('14').childNodes[0].nodeValue);
 
var txt = document.editform.wpTextbox1.value;
var sortkey = '';
// createSave anthe arrayposition of allthe categoryeditbox linksscrollbar
var scrollPosition = document.editform.wpTextbox1.scrollTop;
var catlinks = txt.value.match(/\[\[\s*((C|K)at(e|é)gor(y|i|ie|ia|ía|ija|io)|Кат(е|э)гор(ия|ија|ія|ыя)|Luokka|Flokkur|Κατηγορία)\s*:.*\]\]/ig);
// Create an array of all category links
if (txt.value.match(/\{\{\s*defaultsort/i)) {
alert('There\'s already a defaultsort statement!');
return;
}
var catlinks = txt.value.match(/\[\[\s*((C|K)at(e|ée)gor(y|i|ie|ia|íaia|ija|io)|Кат(е|э)гор(ия|ија|ія|ыя)|Luokka|Flokkur|Κατηγορία)\s*:.*\]\]/ig);
if (!catlinks) {
alert('There are no category links!');
Line 17 ⟶ 23:
}
for (i=0; i< catlinks.length; i++) {
// extractExtract the name of the category
var catname = catlinks[i].replace(/.+\s*:\s*([^|]*[^|\s])\s*(\|.*)?\]\]/,"$1");
// extractExtract the sort key
var match = /\|[^\]]+/.exec(catlinks[i]);
if (match == null) {
alert('Category '+catname+' doesn\'t include a sort key!');
return;
}
// stripStrip the '|' and any trailing spaces
var sk = match[0].replace(/\|(.+)/, "$1").replace(/(\S) +$/,"$1");
if (i == 0) {
sortkey = sk;
Line 37 ⟶ 43:
}
// Now that we know that everything is OK, we can proceed with modifying the content of the editbox
txt.value = txt.value.replace(catlinks[0], "{{DEFAULTSORT:"+sortkey+"}}\n"+catlinks[0]);
for (i=0; i< catlinks.length; i++) {
subst = catlinks[i].replace(/\|[^\]]+/,"");
txt.value = txt.value.replace(catlinks[i], subst);
}
document.editform.wpTextbox1.value=txt;
// Restore scroll position
document.editform.wpTextbox1.scrollTop = scrollPosition;
}
 
addOnloadHook$(function () {
if(document.forms.editform) {
mw.util.addPortletLink('p-cactions', 'javascript:defaultsort()', 'defaultsort', 'ca-defaultsort', '', '', document.getElementById('ca-purge'));
}
});