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: Unterschied zwischen den Versionen

aus Stargate Wiki, dem deutschsprachigen Stargate-Lexikon
Die Seite wurde neu angelegt: „var CatTools = { modes: { AND: '', //TODO OR: '', //TODO WITHOUT: '', //TODO }, mode: CatTools.modes.AND, cat1: '', cat2: '', suggestions: [], con…“
 
KKeine Bearbeitungszusammenfassung
 
(23 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
/**
* 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 = {
var CatTools = {
modes: {
activeInputs: [0],
AND: '', //TODO
modes: [],
OR: '', //TODO
cats: [],
WITHOUT: '', //TODO
catsExist: [false],
},
catResults: [],
mode: CatTools.modes.AND,
cat1: '',
cat2: '',
suggestions: [],
continu: '',
cat1Results: [],
cat2Results: [],
results: [],
results: [],
init: function(){
init: function(){
//TODO add EventListeners / MutationObservers to fill cat1, cat2, mode and to call suggest, existsFeedback, exec
$('#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';
},
},
suggest: function(phrase){
loadSuggestions: function(cont){
$.get(wgServer+wgScriptPath+'/api.php?action=opensearch&format=json&namespace=14&limit=10&search='+encodeURI(phrase), function(d){
$.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);
if('string'==typeof d) d=JSON.parse(d);
d[1].forEach(function(v){
//TODO: display v.substring(10) as suggestion
d.query.allcategories.forEach(function(v){
$('#CatToolsSugg').append($('<option>').attr('value',v['*']));
});
});
if(d['query-continue']){
CatTools.loadSuggestions(d['query-continue'].allcategories.accontinue);
}
}, 'json');
}, 'json');
},
},
existsFeedback: function(inputID){
existsFeedback: function(catIndex){
var exists;
var input = $('#CatToolsInputCat'+catIndex);
$.get(wgServer+wgScriptPath+'/api.php?action=query&format=json&titles='+encodeURI($(inputID).val()), function(d){
var exists = $('#CatToolsSugg option[value="'+input.val()+'"]').length > 0;
if('string'==typeof d) d=JSON.parse(d);
input.css('background-color', (exists ? '#a7f4a7' : '#f4a7a7'));
if(d.query.pages.hasOwnProperty('-1')) exists = false;
CatTools.catsExist[catIndex] = exists;
else exists = true;
CatTools.updateSubmitAllowed();
}, 'json');
},
updateSubmitAllowed: function(){
$(inputID).css('background-color', (exists ? '#a7f4a7' : '#f4a7a7'));
document.getElementById('CatToolsSubmit').disabled = CatTools.catsExist.some(function(e, i){return (!e && CatTools.activeInputs.includes(i));});
},
},
exec: function(){
exec: function(){
CatTools.getCatMembers(CatTools.cat1, 'cat1Results', '');
$('#CatToolsOutputWrapper').show();
while(CatTools.continu.length){
document.getElementById('CatToolsSubmit').disabled = 'true';
CatTools.getCatMembers(CatTools.cat1, 'cat1Results', CatTools.continu);
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();
});
CatTools.getCatMembers(CatTools.cat2, 'cat2Results', '');
while(CatTools.continu.length){
CatTools.getCatMembers(CatTools.cat2, 'cat2Results', CatTools.continu);
}
switch(CatTools.mode){
default:
case CatTools.modes.AND:
CatTools.cat1Results.forEach(function(v){
if(CatTools.cat2Results.includes(v)) results.push(v);
});
break;
case CatTools.modes.OR:
CatTools.cat1Results.forEach(function(v){
if(!CatTools.results.includes(v)) results.push(v);
});
CatTools.cat2Results.forEach(function(v){
if(!CatTools.results.includes(v)) results.push(v);
});
break;
case CatTools.modes.WITHOUT:
CatTools.cat1Results.forEach(function(v){
if(!CatTools.cat2Results.includes(v)) results.push(v);
});
}
CatTools.render();
},
},
getCatMembers: function(catName, storage, cont){
getCatMembers: function(catIndex, cont){
$.get(wgServer+wgScriptPath+'/api.php?action=query&list=categorymembers&cmtitle=Kategorie:'+encodeURI(catName)+'&cmlimit=max'+(cont.length ? '&cmcontinue='+cont : ''), function(d){
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);
if('string'==typeof d) d=JSON.parse(d);
if(d['query-continue']) CatTools.continu = d['query-continue'].categorymembers.cmcontinue;
else CatTools.continu = '';
d.query.categorymembers.forEach(function(v){
d.query.categorymembers.forEach(function(v){
CatTools[storage].push(v.title);
storage.push(v.title);
});
});
}, 'json');
if(d['query-continue']){
return CatTools.getCatMembers(catIndex, d['query-continue'].categorymembers.cmcontinue);
}
});
},
},
render: function(){
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){
CatTools.results.forEach(function(v){
$('#CatToolsResultWrapper').append($('<a>').attr('href','http://stargate-wiki.de/wiki/'+encodeURI(v)).attr('title',v).text(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);
$(CatTools.init);