Menü aufrufen
Toggle preferences menu
Persönliches Menü aufrufen
Nicht angemeldet
Ihre IP-Adresse wird öffentlich sichtbar sein, wenn Sie Änderungen vornehmen.

Benutzer:Col. o'neill/CatTools.js

aus Stargate Wiki, dem deutschsprachigen Stargate-Lexikon

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
/**
 * JS-Skript für CatTools auf [[Benutzer:Col. o'neill/Tools]]
 * Für Import des Skripts einfach folgende Zeile (ohne den Stern am Anfang natürlich) in der eigenen Common.js einfügen:
 * if(mw.config.get('wgPageName')==="Benutzer:Col._o'neill/Tools") importScript("Benutzer:Col. o'neill/CatTools.js");
**/

var CatTools = {
	activeInputs: [0],
	modes: [],
	cats: [],
	catsExist: [false],
	catResults: [],
	results: [],
	init: function(){
		$('#CatToolsInputWrapper').html(
			"<div id='CatToolsInputLine0'>"+
				"<select style='width: 6em; margin: 4px 0; visibility: hidden;'></select>"+
				"<input id='CatToolsInputCat0' type='text' size='50' placeholder='Kategorie eingeben...' list='CatToolsSugg' style='margin: 4px;'>"+
				"<datalist id='CatToolsSugg'></datalist>"+
			"</div>"+
			"<input id='CatToolsAddLine' type='button' value='Zeile hinzufügen' style='margin: 8px 5px 0 0;'>"+
			"<input id='CatToolsSubmit' type='button' value='Seiten anzeigen' style='margin: 8px 5px 0 0;' disabled>"
			);
		CatTools.loadSuggestions('');
		CatTools.addInputLine();
		$('#CatToolsInputCat0').on('input', function(){
			CatTools.existsFeedback(0);
		});
		$('#CatToolsAddLine').on('click', function(){
			CatTools.addInputLine();
		});
		$('#CatToolsSubmit').on('click', function(){
			CatTools.reset();
			CatTools.activeInputs.forEach(function(index){
				CatTools.cats[index]=$('#CatToolsInputCat'+index).val();
				CatTools.modes[index]=$('#CatToolsInputMode'+index).val();
			});
			CatTools.exec();
		});
	},
	addInputLine: function(){
		var newIndex = CatTools.activeInputs[CatTools.activeInputs.length-1]+1;
		$('#CatToolsAddLine').before(
			"<div id='CatToolsInputLine"+newIndex+"'>"+
				"<select id='CatToolsInputMode"+newIndex+"' style='width: 6em; margin: 4px 0;'>"+
					"<option value='AND' selected>und</option>"+
					"<option value='OR'>oder</option>"+
					"<option value='WITHOUT'>ohne</option>"+
				"</select>"+
				"<input id='CatToolsInputCat"+newIndex+"' type='text' size='50' placeholder='Kategorie eingeben...' list='CatToolsSugg' style='margin: 4px;'>"+
				"<input id='CatToolsDeleteRow"+newIndex+"' class='textbutton' type='button' value='&#10006;' title='Zeile löschen' style='margin: 4px;'>"+
			"</div>"
		);
		CatTools.activeInputs.push(newIndex);
		CatTools.catsExist[newIndex] = false;
		
		$('#CatToolsInputCat'+newIndex).on('input', {curIndex: newIndex}, function(ev){
			var curIndex = ev.data.curIndex;
			CatTools.existsFeedback(curIndex);
		});
		$('#CatToolsDeleteRow'+newIndex).on('click', {curIndex: newIndex}, function(ev){
			var curIndex = ev.data.curIndex;
			$('#CatToolsInputLine'+curIndex).remove();
			CatTools.activeInputs.splice(CatTools.activeInputs.indexOf(curIndex), 1);
			CatTools.updateSubmitAllowed();
		});
		document.getElementById('CatToolsSubmit').disabled = 'true';
	},
	loadSuggestions: function(cont){
		$.get(wgServer+wgScriptPath+'/api.php?action=query&format=json&list=allcategories&aclimit=max&acmin=1'+(cont.length ? '&accontinue='+cont : ''), function(d){
			if('string'==typeof d) d=JSON.parse(d);
			
			d.query.allcategories.forEach(function(v){
				$('#CatToolsSugg').append($('<option>').attr('value',v['*']));
			});
			
			if(d['query-continue']){
				CatTools.loadSuggestions(d['query-continue'].allcategories.accontinue);
			}
		}, 'json');
	},
	existsFeedback: function(catIndex){
		var input = $('#CatToolsInputCat'+catIndex);
		var exists = $('#CatToolsSugg option[value="'+input.val()+'"]').length > 0;
		input.css('background-color', (exists ? '#a7f4a7' : '#f4a7a7'));
		CatTools.catsExist[catIndex] = exists;
		CatTools.updateSubmitAllowed();
	},
	updateSubmitAllowed: function(){
		document.getElementById('CatToolsSubmit').disabled = CatTools.catsExist.some(function(e, i){return (!e && CatTools.activeInputs.includes(i));});
	},
	exec: function(){
		$('#CatToolsOutputWrapper').show();
		document.getElementById('CatToolsSubmit').disabled = 'true';
		var requests = [];
		CatTools.activeInputs.forEach(function(index){
			requests.push(CatTools.getCatMembers(index, ''));
		});
		$.when.apply( $, requests ).done(function(){
			var currentResults = CatTools.catResults[0];
			for(var i=1; i<CatTools.activeInputs.length; i++){
				var index = CatTools.activeInputs[i];
				var nextResults = [];
				switch(CatTools.modes[index]){
					default:
					case 'AND':
						currentResults.forEach(function(v){
							if(CatTools.catResults[index].includes(v)) nextResults.push(v);
						});
						break;
					case 'OR':
						nextResults = currentResults.slice();
						CatTools.catResults[index].forEach(function(v){
							if(!nextResults.includes(v)) nextResults.push(v);
						});
						break;
					case 'WITHOUT':
						currentResults.forEach(function(v){
							if(!CatTools.catResults[index].includes(v)) nextResults.push(v);
						});
				}
				currentResults = nextResults;
			}
			CatTools.results = currentResults;
			
			CatTools.render();
			
			CatTools.updateSubmitAllowed();
		});
		
	},
	getCatMembers: function(catIndex, cont){
		var catName = CatTools.cats[catIndex];
		var storage = CatTools.catResults[catIndex];
		return $.get(wgServer+wgScriptPath+'/api.php?action=query&format=json&list=categorymembers&cmtitle=Kategorie:'+encodeURI(catName)+'&cmlimit=max'+(cont.length ? '&cmcontinue='+cont : '')).then(function(d){
			if('string'==typeof d) d=JSON.parse(d);
			
			d.query.categorymembers.forEach(function(v){
				storage.push(v.title);
			});
			
			if(d['query-continue']){
				return CatTools.getCatMembers(catIndex, d['query-continue'].categorymembers.cmcontinue);
			}
		});
	},
	render: function(){
		$('#CatToolsLoading').hide();
		$('#CatToolsResults').show().append($('<p>')).text('Die Anfrage lieferte insgesamt '+CatTools.results.length+' Ergebnisse'+(CatTools.results.length ? ':' : '.'));
		$('#CatToolsResults').append($('<ul>'));
		CatTools.results.forEach(function(v){
			$('#CatToolsResults ul').append($('<li>').append($('<a>').attr('href',wgServer+wgArticlePath.replace('$1','')+encodeURI(v)).attr('title',v).text(v)));
		});
	},
	reset: function(){
		$('#CatToolsOutputWrapper').hide();
		$('#CatToolsLoading').show();
		$('#CatToolsResults').hide().empty();
		CatTools.activeInputs.forEach(function(index){
			CatTools.catResults[index] = [];
		});
		CatTools.results = [];
	}
};
$(CatTools.init);