Jump to content

User:Danski454/stubsearch.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Danski454 (talk | contribs) at 20:49, 24 October 2018 (dev). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
//from a request by Insertcleverphrasehere
//based on [[User:Ais523/stubtagtab2.js]], which is in turn based on [[User:MC10/stubtagtab.js]]

//variables
var unfilteredArray = ["stub"];//does not contain parent
var stubTags = {"stub":{display:"Stub", parent:"stubs", children:null, descendants:null, template:"Stub"},"stubs":{display:"Stubs", parent:null, children:[], descendants:[], template:null}};//generic stub is not loaded by the normal method
var filteredArray = ["stub"];
var stublists = ['Architecture', 'Commerce', 'Culture', 'Education', 'Geography', 'Government, law, and politics',
			'History', 'Leisure', 'Military and weaponry', 'Organizations', 'People',
			'Religion, mythology, faiths, and beliefs', 'Science', 'Sports', 'Technology', 'Transport',
			'Miscellaneous'];

var current = stubTags["stubs"];
var pagesLoaded = 0;

//functions
function stubFilter(){
	filterString = document.getElementById("stub-search-search").value;
	//Special case: no filterString then only show direct children
	console.log(filterString.length);
	if (filterString.length === 0){
		filteredArray = current.children;
		rerenderList();
		return;
	}
	
	var filteringArray = unfilteredArray; //copy unfiltered array
	console.log(typeof filterString);
	filterString = filterString.toLowerCase(); //force lowercase
	var filterArray = filterString.split(" "); //split string
	
	for (var filterIndex = 0; filterIndex < filterArray.length; filterIndex++) {
		filteredArray = [];//clear the filtered output
		filterString = filterArray[filterIndex];
		for (var i = 0; i < filteringArray.length; i++) {
			var testString = filteringArray[i];
			if (testString.indexOf(filterString) > -1){
				filteredArray.push(testString);
			}
		}
		filteringArray = filteredArray;
	}
	rerenderList();
}

function loadStubs(){
	for (var i = 0; i < stublists.length; i++) {
		stubType = stublists[i];
		$.ajax({
			url: mw.config.get("wgServer") + mw.config.get("wgScriptPath") + '/api.php?action=parse&prop=text&text=' + encodeURIComponent('__NOTOC____NOEDITSECTION__\{\{Wikipedia:WikiProject Stub sorting/Stub types/' + stubType + '}}') + '&format=json',
			dataType: "json",
			success: stubSearchLoadSome
		});
	}
}

function stubSearchLoadSome(data) {
	var parseData = $(data.parse.text["*"]);
	parseData.children("ul").children('li').each( function () {
		var val = $(this);//'this' must be converted to jQuery
		while (val.find("small").length !== 0) {//remove small tags (copied from toggleSmall.js)
			var tag = val.find("small").first();
			tag.before(tag.html());
			tag.remove();
		}
		generateStubData(val, "stubs", ["stubs"]);//children of the generic stub
	});
	pagesLoaded++;
	if (pagesLoaded == stublists.length){
		stubFilter();
	}
}

function addStubType(obj, type, parent, ancestors){
	var name = obj.toLowerCase();
	var display = obj;
	var template = null;
	var children = null;
	if (type === "template"){
		template = obj;
		display = "{{" + obj + "}}";
	} else {
		children = [];
	}
	stubTags[name] = {display:display, template:template, children:children, parent:parent, descendants:children};
	stubTags[parent].children.push(name);
	ancestors.forEach( function (value, index, array) {
		stubTags[value].descendants.push(name);
	});
	unfilteredArray.push(name);
	filteredArray.push(name);
}

function generateStubData(data, parent, ancestors){
	var cat;
	cat = data.children("b").first().text();//categories are in bold
	if (cat !== ""){
		addStubType(cat, "cat", parent, ancestors);
		ancestors.push(cat.toLowerCase());
		data.children("ul").children('li').each( function () {
			generateStubData($(this), cat.toLowerCase(), ancestors.slice());
		});
	}
	data.children("a").each( function () {//template links are not wrapped
		var tag = $(this);
		if (tag.attr("href").search(/(wiki\/|=)Template:/) === -1){//must link to a template
			return true;
		}
		if (cat !== ""){
			addStubType(tag.text(), "template", cat.toLowerCase(), ancestors.slice());
		} else {
			addStubType(tag.text(), "template", parent, ancestors.slice());
		}
	});
}

function rerenderList(){
	$('#stub-search-scroll').empty();
	filteredArray.forEach(function (value, index, array) {
		var tag = stubTags[value];
		if (index > 0){
			$('#stub-search-scroll').append("•");
		}
		$('#stub-search-scroll').append('<input type="button" value="'+ tag.display +'" onclick="selectStub(\''+ value +'\');" style="background:none!important;color:inherit;border:none;cursor:pointer;" />');
	});
}

function renderTemplate(){
	$('#stub-search-scroll').empty();
	$('#stub-search-scroll').append('<input type="button" value="Add {{'+ current.template +'}} to this article?" onclick="addStub();" /><br /><span id="stub-preview">Preview loading</span>');
}

function stubSearchUp(){
	selectStub(current.parent);
}

function loadStubSearch(){
	$('#bodyContent').before('<div id="stub-search-box" style="background-color:LightBlue;height:200px;border:1px solid black;padding:15px;">Search: <input type="text" id="stub-search-search"><input id="stub-search-button" type="button" value="Search" onclick="stubFilter();" /> Current selection: <span id="stub-search-cat">Stubs</span> <input id="stub-search-up" type="button" value="Go up to ..." onclick="stubSearchUp();" /><br /><div style="height:150px;overflow:auto;" id="stub-search-scroll">Loading stub templates: Please wait</div></div>');
	$('#stub-search-up').hide();//up button defaults to hidden
	loadStubs();
}

function selectStub(name){
	current = stubTags[name];
	$('#stub-search-cat').text(current.display);
	document.getElementById("stub-search-search").value = "";//clear search
	if (current.parent !== null){
		$('#stub-search-up').val("Go up to " + stubTags[current.parent].display).show();
	} else {
		$('#stub-search-up').hide();
	}
	if (current.template === null){
		unfilteredArray = current.descendants;
		stubFilter();
	} else {
		renderTemplate();
	}
}

$(document).ready(function(){
	if ( mw.config.get( 'wgNamespaceNumber' ) === 0 || mw.config.get( 'wgNamespaceNumber' ) === 2 ) {//usable on articles and user pages (for testing and practice)
		var portletLink = mw.util.addPortletLink('p-cactions', '#', 'Stub search', 'ca-stub-search', 'Tag this article as a stub', '');
		$(portletLink).click(loadStubSearch);
	}
});