User:Jts1882/taxonomybrowser.js: Difference between revisions

Content deleted Content added
update from my commons.js
update (fixes error for high level taxa)
 
Line 287:
// we don't ahve the rank, so first get the rank (needed to get speciesboxes for genus)
var search = 'hastemplateintitle:Taxonomy\\/' + escapeSeq(taxon) + ' insource:/\| *rank *= *[a-zA-Z]+/';
console.log(search);
 
jQuery.getJSON(
mw.util.wikiScript( 'api' ),
{ 'format': 'json', 'action': 'query', 'list': 'search', 'srnamespace' :10, 'srlimit' : 5005000, 'srsearch' : search },
function( data ) {
if (data.query == undefined) return; // intermittant and seemingly random - not clear why
Line 299 ⟶ 301:
if (nResults === 0) {
console.log ('no results for rank of taxon ' + taxon );
addChildrenInteractive(taxon, level, mode , depth, taxonId); // pass without a rank
}
else { // so we have some results
$.each ( data.query.search , function( index , sr ) {
if (sr.title == "Template:Taxonomy/" + taxon ) {
//console.log(sr.snippet);
var match = sr.snippet.match(/rank[\s]*=[\s]*([a-zA-Z ]*)/);
if (match && match[1] ) {
Line 328 ⟶ 331:
taxon = taxon.replace(")","\\)");
taxon = taxon.replace("?","\\?");
//taxon = taxon.replace("/","\\/");
taxon = taxon.replace(/\//g,"\\/");
taxon = taxon.replace(".","\\.");
Line 691 ⟶ 695:
// if (debug) alert ('no results for taxon (rank) = ' + taxon + ' (' + rank + ')');
// speciesboxes search now launched after the end of the if else results loop
 
if ( 2==1 && showSpeciesboxes && InfraGenericRank(rank) ) {
if (debug) alert ('No child templates for taxon (rank) = ' + taxon + '. We need to look for speciesboxes associated with this taxon');
addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parent);
}
output = ' [no child templates]'; // "zero hits: " + taxon;
if (hideChildless) {
Line 702:
else {
if (showParameters) $('#'+taxonId+' span.tree-info').append(output);
//if (rank != "genus") $('#'+taxonId+'-collapse').hide(); // hide collapse button
if (rank != "genus" && rank != "subgenus" && rank != "sectio" && rank != "series") {
$('#'+taxonId+'-collapse').html('<span style="color:grey;"> &#8865; </span>').off("click"); // change symbols and disable
//$('#'+taxonId+'-expand').off("click");
}
}
Line 713 ⟶ 711:
$('#'+taxonId).append('<ul class="' + bullets + '"></ul>'); // add ul element to contain children
//output = '<ul>';
var listCount = 0;
Line 758 ⟶ 755:
variant = ' [parent='+parent+']';
}
 
//if (!parent && sameAs) { // accept if no parent but sameas (any or =taxon)
if ( sameAs && $("#search-sameas").prop("checked")) { // accept if sameas (any or =taxon)
accept = true;
Line 861 ⟶ 858:
}); // end @each loop
if (listCount == 0 ) { // we are here when there are no strict results found
// if there are no results, addSpeciesboxes is launched above when zero results returned
// however when also loose/all results, it will iterate through the results
// we are here when there are no strict results
// e.g. with Panthera, which has results with "parent=Pantherapsida"
// rank ="genus";
console.log("no results with strict matching of " + taxon + ' (' + rank + ')');
}
// speciesbox now handled below
if (1==2) {
// if ( showSpeciesboxes && InfraGenericRank(rank) ) {
if (showSpeciesboxes) {
if (rank == 'genus' ) {
if (debug) alert ('No results: will look for speciesboxes associated with this genus');
addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parent); // look for species boxes
} else if ( rank == 'subgenus' || rank == 'sectio' || rank == 'series' ) {
if (debug) alert ('No results: will look for speciesboxes associated with this infrageneric (is this used?)');
addSpeciesboxes(parent, level, mode, depth, taxonId, rank, parent); // TODO needs parent taxon???
}
}
}
if (2==1 && showSpeciesboxes) { // MOVED BELOW check speciesboxes even when there are child templates
if (rank == 'genus' || rank == 'subgenus' || rank == 'sectio' || rank == 'series' ) {
alert ('rank = ' + rank + '; taxon = ' + taxon + '; parent = ' + parent );
addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parent);
}
}
 
if (showSpeciesboxesAll ) { // NEEDS MOVING? check speciesboxes at genus level
if (rank == 'genus' || rank == 'subgenus' ) { //alway check speciesboxes at genus level
//if (debug) alert ('Checking for all speciesboxes associated with this genus');
if ($('#'+taxonId + ' ul.speciesboxes').length ) {
//if (debug) alert ('Speciesbox block already exists');
} else {
//if (debug) alert ("Speciesbox block doesn't exist; will add one");
addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parent); // look for species boxes
}
}
}
if (showParameters) {
$('#'+taxonId+' > span.tree-info').append(" [" + count + " child templates]"); // number of results; excludes variants
Line 905 ⟶ 868:
} // end if results
if (showSpeciesboxesshowSpeciesboxesAll ) { // check speciesboxes evenat whengenus therelevel are(including childinfrageneric templatesranks)
if (rank == 'genus' || rank == 'subgenus' || rank) =={ 'sectio'//alway ||check rankspeciesboxes ==at 'series'genus ) {level
//if (debug) alert ('rankChecking =for 'all +speciesboxes rankassociated +with '; taxon =this genus' + taxon + '; parent = ' + parent );
addSpeciesboxesif (taxon,$('#'+taxonId level,+ mode,' depth,ul.speciesboxes').length taxonId, rank, parent); {
//if (debug) alert ('Speciesbox block already }exists');
} else {
//if (debug) alert ("Speciesbox block doesn't exist; will add one");
addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parent); // look for species boxes
}
}
}
else if (showSpeciesboxes) { // check speciesboxes even when there are child templates
if (rank == 'genus' || rank == 'subgenus' || rank == 'sectio' || rank == 'series' ) {
//if (debug) alert ('rank = ' + rank + '; taxon = ' + taxon + '; parent = ' + parent );
addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parent);
}
}
 
} // end processing function (data)
);
Line 917 ⟶ 893:
//------------------------------------function to check species boxes under generic taxa---------------------------------------------
function addSpeciesboxes(taxon, level, mode, depth, taxonId, rank, parentparentTaxon) {
 
//if (debug) alert ('Call to addSpeciesboxs for taxon (rank) = ' + taxon + ' (' + rank + '); parent = ' + parent );
Line 1,011 ⟶ 987:
$.each ( data.query.search , function( index , sr ) {
//ifvar rank = getRank(hideExtinctindex &&, extinctsr) return true; // don't show extinct taxa
//var extinct = getExtinct(index , sr);
//if (hideExtinct && extinct) return true; // don't show extinct taxa (info not available for speciesboxes)
// display the selected child taxa
count += 1;
 
//
var child = sr.title.replace(" ", "_");
//var rankgenus = getRank(index""; , sr);
//var extinctspecies = getExtinct(index""; , sr);
var parent = "";
//console.log("sr.snippet="var +binomial sr.snippet)= "";
var snippetTaxoncomment = "";
if ( sr.snippet.search(/taxon *=/) > 0 ) {
var snippet = sr.snippet.replace ('<span class="searchmatch">', ''); // remove highlighting from search snippet
snippet = snippet.replace ('</span>', '');
//var snippetTaxon = snippet.replace(/[\s\S]+taxon *= *(\w+) (\w+)[\s\S]+/, "$1 $2");
// var snippetTaxon = snippet.replace(/[\s\S]+taxon *= *(\w+) ([ ×\w]+)[\s\S]+/, "$1 $2");
// consolesnippetTaxon = snippet.logreplace("/[\s\S]+taxon *=" *(\w+ snippetTaxon\w+)[\s\S]+/, "$1");
if (snippetTaxon == snippet) {
if (debug) comment = " [taxon not available in search snippet]"; //
} else {
binomial = snippetTaxon; // should snippetTaxon be checke?
}
console.log("taxon=" + snippetTaxon + comment);
}
else if (sr.snippet.search(/genus *=/) > 0 && sr.snippet.search(/species *=/) > 0 ) { // if speciesbox with genus= and species=
else {
var genus = sr.snippet.replace(/[\s\S]+genus *= *(\w+)[\s\S]+/, "$1");
var species = sr.snippet.replace(/[\s\S]+species *= *([\w ×]+)[\s\S]+*/, "$1");
var parentbinomial = genus sr.snippet.replace(/[\s\S]+parent *=" " *(\w+)[\s\S]+/, "$1")species;
var comment = "";
if (species == sr.snippet) { // if species epithet not available in search snippet
species = "</i>sp.";
comment = " [species epithet not available in search snippet]"; // when species not available in snippet
}
if (parent == sr.parent) { // if parent not captured
comment += " parent not captured."
} else {
comment += " parent = " + parent;
}
snippetTaxon = genus + " " + species;
// snippetTaxon += " " + comment;
console.log("taxon(genus+species)=" + genus + " " + species + " " + comment);
} else {
}
comment = " [snippet doesn't contain taxon or genus and species]";
binomial = "not available";
}
parent = sr.snippet.replace(/[\s\S]+parent *= *(\w+[\w \.]*\w+)[\s\S]+/, "$1");
//parent = sr.snippet.replace(/[\s\S]+parent *= *(\w+)[\s\S]+/, "$1");
if (parent == sr.snippet) { // if parent not captured
if (debug) comment += " [no parent or not in snippet]";
} else {
if (debug) comment += " [parent = " + parent + "]";
}
 
var name = sr.title;
if ( binomial == sr.title ) {
binomial = ""; // don't show if binomial if same as page title
name = "<i>" + name + "</i>" ; // italicise page title if binomial
} else {
binomial = " (<i>" + binomial + "</i>)" ; // display binomial after page title if different
if ( genus == sr.title ) name = "<i>" + name + "</i>" ; // italicise page title is monotypic genus
}
 
var titleString = ' title=""';
if (showParameters) {
Line 1,054 ⟶ 1,048:
}
 
var name = sr.title
var childId = sr.title.replace(" ", "_") + "_SPECIES"; // + '-li';
output = '<li id="' + childId + '" class="taxon_identifier" >';
//if (extinct) output += '†'; // cannot know extinct status for speciesbox
output += '<a href="/wiki/' + sr.title + '" ' + titleString + ' target="_blank" ><i>' + name + '</i></a>';
 
output += '<span class="tree-info" style="color:grey;" >'; // span for add information
output += binomial;
if (debug) {
//output += extraText(index , sr);
output += comment;
}
output += '</span>'; // close info span