Jump to content

User:Fuddle/Cold Default Sort.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fuddle (talk | contribs) at 14:26, 14 April 2020 (Created page with '** Cold Default Sort **: // Modified from Hot Default Sort // Adds an default sort key editor at the bottom of the page near the categories // Documentation...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*** 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}}}}