User:Fuddle/Cold Default Sort.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Fuddle/Cold Default Sort. |
/*** Cold Default Sort ***/
// Modified from Hot Default Sort
// Adds an default sort key editor at the bottom of the page near the categories
// Documentation at [[User:BrandonXLF/HotDefaultSort]] By [[User:BrandonXLF]]
$(function(){
function resize () {
this.style.width = '0px';
this.style.width = this.scrollWidth + 2 + 'px';
}
$.get( mw.config.get('wgScriptPath') + '/api.php', {
action: 'query',
pageids: mw.config.get('wgArticleId'),
prop: 'pageprops',
format: 'json'
}).then(function(res){
var sortkey = res.query.pages[mw.config.get('wgArticleId')].pageprops ? res.query.pages[mw.config.get('wgArticleId')].pageprops.defaultsort : undefined;
var title = mw.config.get('wgTitle');
var cats = document.getElementById('catlinks');
var sort = document.createElement('div');
sort.innerHTML = 'DEFAULTSORT: ';
cats.appendChild(sort);
var dsort = document.createElement('span');
dsort.innerHTML = sortkey || title;
sort.appendChild(dsort);
var edit = document.createElement('input');
edit.style.minWidth = '100px';
edit.onpaste = resize;
edit.onkeydown = resize;
edit.onkeyup = resize;
edit.onkeypress = resize;
edit.oninput = resize;
edit.onchange = resize;
edit.value = dsort.innerHTML;
var dflt = document.createElement('span');
dflt.innerHTML = '(default)';
dflt.style.marginLeft = '0.25em';
dflt.style.display = sortkey ? 'none' : 'inline';
dflt.style.fontStyle = 'italic';
sort.appendChild(dflt);
var status = document.createElement('span');
status.style.marginLeft = '0.25em';
status.style.display = 'none';
status.innerHTML = 'Saving...';
sort.appendChild(status);
var actions = document.createElement('span');
sort.appendChild(actions);
var save = document.createElement('a');
save.innerHTML = '(✓)';
save.style.marginLeft = '0.25em';
save.title = 'Save changes';
save.onclick = function () {
status.style.display = 'inline';
if (!edit.value) {
cancel.onclick();
remove.onclick();
return;
}
$.get( mw.config.get('wgScriptPath') + '/index.php', {
action: 'raw',
title: mw.config.get('wgPageName')
}).then(function(text){
$.post( mw.config.get('wgScriptPath') + '/api.php', {
action: 'edit',
title: mw.config.get('wgPageName'),
text: text.replace(/\n?\n?{{DEFAULTSORT:.*?}}/g,'') + '\n\n{{DEFAULTSORT:' + edit.value + '}}',
summary: 'Setting default sort key to ' + edit.value + ' using [[User:BrandonXLF/HotDefaultSort|Hot Default Sort]]',
token: mw.user.tokens.get('csrfToken'),
format: 'json'
}).then(function(r){
status.style.display = 'none';
if (r.error) {
mw.notify(r.error.info.substring(0,150) + (r.error.info[151] ? '...' : ''), {type: 'error', title: 'Cannot modify sort key.', tag: 'sortkeymodifyerror'});
edit.value = dsort.innerHTML;
} else {
dsort.innerHTML = edit.value;
modify.innerHTML = '(±)';
modify.title = 'Modify';
remove.style.display = 'inline';
dflt.style.display = 'none';
}
sort.replaceChild(dsort,edit);
actions.replaceChild(remove,save);
actions.replaceChild(modify,cancel);
});
});
};
var cancel = document.createElement('a');
cancel.innerHTML = '(x)';
cancel.style.marginLeft = '0.25em';
cancel.title = 'Cancel';
cancel.onclick = function () {
edit.value = dsort.innerHTML;
sort.replaceChild(dsort,edit);
actions.replaceChild(remove,save);
actions.replaceChild(modify,cancel);
};
var remove = document.createElement('a');
remove.innerHTML = '(−)';
remove.title = 'Remove (replace with default)';
remove.style.marginLeft = '0.25em';
remove.style.display = sortkey ? 'inline' : 'none';
remove.onclick = function () {
status.style.display = 'inline';
$.get( mw.config.get('wgScriptPath') + '/index.php', {
action: 'raw',
title: mw.config.get('wgPageName')
}).then(function(text){
$.post( mw.config.get('wgScriptPath') + '/api.php', {
action: 'edit',
title: mw.config.get('wgPageName'),
text: text.replace(/\n?{{DEFAULTSORT:.*?}}/g,''),
summary: 'Removing default sort key using [[User:BrandonXLF/HotDefaultSort|Hot Default Sort]]',
token: mw.user.tokens.get('csrfToken'),
format: 'json'
}).then(function(r){
status.style.display = 'none';
if (r.error) {
mw.notify(r.error.info.substring(0,150) + (r.error.info[151] ? '...' : ''), {type: 'error', title: 'Cannot remove sort key.', tag: 'sortkeymodifyerror'});
} else {
dsort.innerHTML = title;
edit.value = title;
modify.innerHTML = '(+)';
modify.title = 'Add';
remove.style.display = 'none';
dflt.style.display = 'inline';
}
});
});
};
actions.appendChild(remove);
var modify = document.createElement('a');
modify.innerHTML = sortkey ? '(±)' : '(+)';
modify.title = sortkey ? 'Modify' : 'Add';
modify.style.marginLeft = '0.25em';
modify.onclick = function () {
sort.replaceChild(edit,dsort);
resize.apply(edit);
actions.replaceChild(save,remove);
actions.replaceChild(cancel,modify);
};
actions.appendChild(modify);
});
});
// {{DEFAULTSORT:{{FULLPAGENAME}}}}