Skip to content

Commit

Permalink
Added initial instrument filter to browse page.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigsapp committed Feb 11, 2022
1 parent e9a1e17 commit 73fada1
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 10 deletions.
3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ composer_index_popc1: "https://data.nifc.humdrum.org/popc1-composer-index.json"
# siglum_index: JSON list of RISM sigla for browse and work pages.
siglum_index: "https://data.nifc.humdrum.org/siglum-index.json"

# instrument_index: JSON list of Humdrum instrument codes for browse page.
instrument_index: "https://data.nifc.humdrum.org/instrument-index.json"

# pitch_index: TEXT list of melodic pitches for browse page searching.
pitch_index_popc2: "https://data.nifc.humdrum.org/popc2-pitch-index.txt"
pitch_index_popc1: "https://data.nifc.humdrum.org/popc1-pitch-index.txt"
Expand Down
1 change: 1 addition & 0 deletions _includes/browse/buildBrowseFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ POPC2.prototype.buildBrowseFilters = function (index) {
this.buildCenturyFilter(index);
this.buildSiglumFilter(index);
this.buildGenreFilter(index);
this.buildInstrumentFilter(index);
this.buildNationalityFilter(index);
this.buildTitleFilter();
this.buildLyricsFilter();
Expand Down
115 changes: 115 additions & 0 deletions _includes/browse/buildInstrumentFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{% comment %}
//
// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Thu Feb 10 21:48:42 PST 2022
// Last Modified: Thu Feb 10 21:48:45 PST 2022
// Filename: _includes/browse/buildInstrumentFilter.js
// Used by: _incuode/browse/doBrowseSearch();
// Included in: _includes/browse/main.html
// Syntax: ECMAScript 6
// vim: ts=3:nowrap
//
// Description: Create the search entry for instruments on the browse page.
// This is done by searching through the browse index (or any
// provided index) and collating the instrument fields
// for each entry in the index. The instrument list also
// gives the number of entries in the full index in
// the instrument search list in parentheses after instrument
// abbreviation.
//
{% endcomment %}

POPC2.prototype.buildInstrumentFilter = function (index, target) {
this.DebugMessageFunctionVerbose();
if (!index) {
index = this.VARS.SEARCH_INDEX;
}
if (!target) {
target = "#filter-instrument";
}
if (!index) {
console.error("ERROR: Cannot find browse index for creating instrument filter.");
return;
}
let element = document.querySelector(target);
if (!element) {
console.error("ERROR: Cannot find target", target);
return;
}

let instruments = {};
if (index.length === this.VARS.SEARCH_INDEX.length) {
// Use cached instruments counts
instruments = this.VARS.BROWSE_MENU_OPTIONS.instrument;
} else {
for (let i=0; i<index.length; i++) {
let ain = index[i].AIN;
if (!ain) {
continue;
}
ain = ain.trim();
let ains = ain.split(/\s+/);
for (let j=0; j<ains.length; j++) {
if (ains[j].match(/^[a-z]/)) {
let instrument = ains[j];
if (instrument === "empty") {
continue;
}
if (!instruments[instrument]) {
instruments[instrument] = 1;
} else {
instruments[instrument]++;
}
}
}
}
}

let limitedKeys = Object.getOwnPropertyNames(instruments);
let fullKeys = Object.getOwnPropertyNames(this.VARS.BROWSE_MENU_OPTIONS.instrument);

fullKeys.sort(function(a, b) {
return a.localeCompare(b);
});

// In the future allow multiple instruments to be selected.
let selectedInstrument = "";
if (this.VARS.SEARCH && this.VARS.SEARCH.instrument) {
selectedInstrument = this.VARS.SEARCH.instrument;
}

let output = "<select class='filter instrument'>\n";

output += "<option value=''>";
output += this.getTranslation("instrument");
output += ` [${limitedKeys.length}]`;
output += "</option>\n";

for (let i=0; i<fullKeys.length; i++) {
output += '<option value="';
let instrument = fullKeys[i];
let displayInstrument = this.getTranslation(instrument);
output += instrument.replace(/"/g, '\\"');
output += '"'
if (selectedInstrument === instrument) {
output += " selected";
}
output += '>';
output += displayInstrument;
if (instruments[instrument]) {
output += ` (${instruments[instrument]})`;
}
output += "</option>\n";
}

output += "</select>\n";

element.innerHTML = output;
let that = this;
element.onchange = function() { that.doBrowseSearch(); };
};

Object.defineProperty(POPC2.prototype.buildInstrumentFilter, "name", { value: "buildInstrumentFilter" });



3 changes: 3 additions & 0 deletions _includes/browse/doBrowseSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// Used by: _includes/browse/buildPitchFilter.js
// Used by: _includes/browse/buildNationalityFilter.js
// Used by: _includes/browse/buildSiglumFilter.js
// Used by: _includes/browse/buildInstrumentFilter.js
// Used by: _includes/browse/buildTitleFilter.js
// Used by: _includes/browse/displayBrowsePage.js
// Included in: _includes/browse/main.html
Expand Down Expand Up @@ -44,6 +45,7 @@ POPC2.prototype.doBrowseSearch = function (index) {
results = this.filterByGenre(results);
results = this.filterByNationality(results);
results = this.filterByTitle(results);
results = this.filterByInstrument(results);
results = this.filterByLyrics(results);
results = this.filterByPitch(results);
results = this.filterByTonic(results);
Expand All @@ -54,6 +56,7 @@ POPC2.prototype.doBrowseSearch = function (index) {
this.buildComposerFilter(results);
this.buildCenturyFilter(results);
this.buildSiglumFilter(results);
this.buildInstrumentFilter(results);
this.buildGenreFilter(results);
this.buildNationalityFilter(results);
this.buildTonicFilter(results);
Expand Down
49 changes: 49 additions & 0 deletions _includes/browse/filterByInstrument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% comment %}
//
// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Oct 6 12:27:04 PDT 2021
// Last Modified: Wed Oct 6 12:27:07 PDT 2021
// Filename: _includes/browse/filterByInstrument.js
// Used by: _includes/browse/doBrowseSearch.js
// Included in: _includes/browse/main.html
// Syntax: ECMAScript 6
// vim: ts=3:nowrap
//
// Description:
//
{% endcomment %}

POPC2.prototype.filterByInstrument = function (input) {
this.DebugMessageFunctionVerbose();
let type = "instrument";
let field = "AIN";
if (!input) {
return [];
}
if (input.length == 0) {
return input;
}
let element = document.querySelector(`select.filter.${type}`);
let target = "";
if (element) {
target = element.value;
}
if (target) {
this.VARS.SEARCH[type] = target;
let output = [];
let re = new RegExp("\\b" + target + "\\b");
for (let i=0; i<input.length; i++) {
if (re.exec(input[i][field])) {
output.push(input[i]);
}
}
return output;
} else {
return input;
}
};

Object.defineProperty(POPC2.prototype.filterByInstrument, "name", { value: "filterByInstrument" });



2 changes: 2 additions & 0 deletions _includes/browse/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
{% include browse/buildCenturyFilter.js %}
{% include browse/buildComposerFilter.js %}
{% include browse/buildGenreFilter.js %}
{% include browse/buildInstrumentFilter.js %}
{% include browse/buildLyricsFilter.js %}
{% include browse/buildModeFilter.js %}
{% include browse/buildNationalityFilter.js %}
Expand All @@ -37,6 +38,7 @@
{% include browse/filterByCentury.js %}
{% include browse/filterByComposer.js %}
{% include browse/filterByGenre.js %}
{% include browse/filterByInstrument.js %}
{% include browse/filterByLyrics.js %}
{% include browse/filterByMode.js %}
{% include browse/filterByNationality.js %}
Expand Down
3 changes: 2 additions & 1 deletion _includes/handlebars/template-browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h1 class="browse-page">{{showTitle}}</h1>
<div id="filters">
<div class="filter-div" id="filter-composer"></div>
<div class="filter-div hidden" id="filter-nationality"></div>
<div class="filter-div" id="filter-century"></div>

<div id="clear-button" class="trans fas fa-backspace"
data-transatt="title:clear_help" onclick="popc2.resetBrowseSearchFields();">
Expand All @@ -37,7 +38,7 @@ <h1 class="browse-page">{{showTitle}}</h1>

<div class="filter-div" id="filter-siglum"></div>
<div class="filter-div" id="filter-genre"></div>
<div class="filter-div" id="filter-century"></div>
<div class="filter-div" id="filter-instrument"></div>
<br id="more-break"/>

<div class="filter-div" id="filter-title"></div>
Expand Down
6 changes: 6 additions & 0 deletions _includes/initialize/POPC2.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ function POPC2() {
// downloaded from {{ site.siglum_index}}. Indexed by siglum (country-library).
this.VARS.SIGLUM_INDEX = {};

// INSTRUMENT_INDEX -- A list of Humdrum instrument codes and English/Polish names.
// downloaded from {{ site.instrument_index}}. Indexed by instrument code.
this.VARS.INSTRUMENT_INDEX = {};


//////////////////////////////
//
Expand Down Expand Up @@ -265,6 +269,8 @@ function POPC2() {

siglum_index: "{{ site.siglum_index }}",

instrument_index: "{{ site.instrument_index }}",

pitch_index: "{{ site.pitch_index_popc2 }}", // active pitch index
pitch_index_popc1: "{{ site.pitch_index_popc1 }}",
pitch_index_popc2: "{{ site.pitch_index_popc2 }}",
Expand Down
47 changes: 47 additions & 0 deletions _includes/listeners/downloadInstrumentIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% comment %}
//
// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Thu Feb 10 21:00:28 PST 2022
// Last Modified: Thu Feb 10 21:00:30 PST 2022
// Filename: _includes/listeners/downloadInstrumentIndex.js
// Used by:
// Included in:
// Syntax: ECMAScript 6
// vim: ts=3:nowrap
//
// Description: Download the instrument index and store its contents
// into VARS.INSTRUMENT_INDEX global variable.
//
{% endcomment %}

POPC2.prototype.downloadInstrumentIndex = function () {
this.DebugMessageFunction();
let url = this.SETTINGS.instrument_index;
this.DebugMessage("DOWNLOADING INSTRUMENT INDEX FROM " + url, "lightblue");
let that = this;
fetch(url)
.then(res => res.json())
.then(data => {
that.VARS.INSTRUMENT_INDEX = data;
if (!this.VARS.TRANSLATIONS) {
console.error("NO TRANSLATIONS DATABASE FOR INSTRUMENT CODES");
} else {
// Store instrument code translations.
for (property in data) {
let entry = {};
entry.TAG = property;
entry.EN = data[property]["EN"];
entry.PL = data[property]["PL"];
this.VARS.TRANSLATIONS[entry.TAG] = entry;
}
}

that.DebugMessage("DOWNLOADED INSTRUMENT INDEX FROM " + url, "lightblue");
})
.catch(err => { console.error("downloadInstrumentIndex:", err); });
};

Object.defineProperty(POPC2.prototype.downloadInstrumentIndex, "name", { value: "downloadInstrumentIndex" });



1 change: 1 addition & 0 deletions _includes/listeners/downloadScoreIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ document.addEventListener("DOMContentLoaded", function() {
popc2.prepareBrowseSelectOptions();
popc2.loadBookmarksFromLocalStorage();
popc2.downloadSiglumIndex();
popc2.downloadInstrumentIndex();

if (popc2.VARS.WORK_ID) {
// Display of work page handled by downloadComposerIndex().
Expand Down
4 changes: 2 additions & 2 deletions _includes/listeners/downloadSiglumIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// vim: ts=3:nowrap
//
// Description: Download the siglum index and store its contents
// into VARS.SIGLUM global variable.
// into VARS.SIGLUM_INDEX global variable.
//
{% endcomment %}

Expand Down Expand Up @@ -113,7 +113,7 @@ POPC2.prototype.downloadSiglumIndex = function () {
}
}

that.DebugMessage("DOWNLOADED SIGLUM INDEX FROM " + url, "purple");
that.DebugMessage("DOWNLOADED SIGLUM INDEX FROM " + url, "plum");
})
.catch(err => { console.error("downloadSiglumIndex:", err); });
};
Expand Down
1 change: 1 addition & 0 deletions _includes/listeners/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{% include listeners/downloadLyricsIndex.js %}
{% include listeners/downloadPitchIndex.js %}
{% include listeners/downloadSiglumIndex.js %}
{% include listeners/downloadInstrumentIndex.js %}
{% include listeners/getBrowseWorkId.js %}
{% include listeners/handleFreeFilterInput.js %}
{% include listeners/isInBrowseTable.js %}
Expand Down
29 changes: 23 additions & 6 deletions _includes/listeners/prepareBrowseSelectOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ POPC2.prototype.prepareBrowseSelectOptions = function () {
data.composer = {};
data.century = {};
data.siglum = {};
data.instrument = {};
data.genre = {};
data.tonic = {};
data.mode = {};
Expand All @@ -31,12 +32,13 @@ POPC2.prototype.prepareBrowseSelectOptions = function () {
let index = this.VARS.SEARCH_INDEX;
for (let i=0; i<index.length; i++) {
let entry = index[i];
let composer = entry.COM || "";
let century = entry.cenid || "";
let siglum = entry.siglum || "";
let genre = entry.AGN || "";
let key = entry.key || "";
let nationality = entry.CNT || "";
let composer = entry.COM || "";
let century = entry.cenid || "";
let siglum = entry.siglum || "";
let ain = entry.AIN || "";
let genre = entry.AGN || "";
let key = entry.key || "";
let nationality = entry.CNT || "";
if (century) {
century = century.replace(/:.*/, "");
if (!century.match(/^1[5678]xx$/)) {
Expand Down Expand Up @@ -65,6 +67,21 @@ POPC2.prototype.prepareBrowseSelectOptions = function () {
data.siglum[siglum] = 1;
}
}
if (ain) {
let ains = ain.split(/\s+/);
for (let j=0; j<ains.length; j++) {
if (ains[j].match(/^[a-z]/)) {
if (ains[j] === "empty") {
continue;
}
if (data.instrument[ains[j]]) {
data.instrument[ains[j]]++;
} else {
data.instrument[ains[j]] = 1;
}
}
}
}
if (genre) {
genre = genre.trim();
pieces = genre.split(/\s*;\s*/);
Expand Down
Loading

0 comments on commit 73fada1

Please sign in to comment.