Skip to content

Commit 3a1f352

Browse files
committed
Merge branch 'Autocomplete'
2 parents 9ddaa0c + 0f8fd4a commit 3a1f352

File tree

7 files changed

+79
-16
lines changed

7 files changed

+79
-16
lines changed

LICENSE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
NOTE: This license document covers all software within this project
2+
except auto-complete.css and auto-complete.min.js which originated
3+
from Pixabay (https://github.com/Pixabay/JavaScript-autoComplete) and
4+
are distributed under the MIT license.
5+
16
This is free and unencumbered software released into the public domain.
27

38
Anyone is free to copy, modify, publish, use, compile, sell, or

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,34 @@ Example HTML, CSS, and JavaScript for searching for items in a PASTA repository
55

66
## Motivation
77

8-
[LTER sites](https://lternet.edu/) which archive data at [EDI](https://environmentaldatainitiative.org/) may wish to use the [PASTA API](http://pastaplus-core.readthedocs.io/en/latest/doc_tree/pasta_api/data_package_manager_api.html) to create a searchable data catalog on their [static websites](https://en.wikipedia.org/wiki/Static_web_page). Data catalogs must support searching by term as well as browsing by LTER core area. This project includes example code supporting such functionality, along with pagination since thousands of results could be returned.
8+
[LTER sites](https://lternet.edu/) which archive data at [EDI](https://environmentaldatainitiative.org/) may wish to use the [PASTA API](https://pastaplus-core.readthedocs.io/en/latest/doc_tree/pasta_api/data_package_manager_api.html#search-data-packages) to create a searchable data catalog on their website. This project includes example code supporting such functionality.
99

10-
## Usage
10+
## Usage for Your Site
1111

12-
Open the HTML file in your browser and enter a search term like water. When you click Search, the application searches for datasets at EDI for the [NWT LTER site](http://niwot.colorado.edu/). Results are summarized on the page with external links to more details at the original data archive.
12+
Start with the demo in the `public` folder and adapt the code for your site. See the `PASTA_CONFIG` variable in `pasta.js` for basic configuration options. For example, you will change `PASTA_CONFIG["filter"]` to filter results for your site. The example assumes the site used the knb-lter-[three letter acronym] pattern in its dataset metadata. If your site follows a different pattern, than change the filter accordingly. See the [PASTA documentation](https://pastaplus-core.readthedocs.io/en/latest/doc_tree/pasta_api/data_package_manager_api.html#search-data-packages) for possible query options.
1313

14-
You can click Search with no terms specified to show the entire catalog, which is the default behavior when you load the page.
14+
You may want to adjust the layout and styles in the demo page to match your website.
1515

16-
Note: We assume the LTER Core Area was included as a **keyword** in the metadata when the data publisher submitted the data to the archive. If your site follows different conventions, you will need to modify `pasta.js` to filter by core area.
16+
### Pagination
1717

18-
## Customization
18+
PASTA allows you to limit the number of results returned per page. If you do not wish to use pagination, set `PASTA_CONFIG["limit"]` to a number higher than the number of datasets available for your site.
1919

20-
To change parameters such as how many search results to show at a time, see the `PASTA_CONFIG` variable in `pasta.js`. There you can also set the group or user whose datasets you want to search.
20+
If you want pagination but do not need page links both above and below search results, simply replace the element ID in `PASTA_CONFIG["pagesBotElementId"]` (or `pagesTopElementId`) with an empty string, `""`.
21+
22+
### Hiding Other Items
23+
24+
Similar to pagination, you can hide the search URL, the text summarizing the result count, and even the sorting options by replacing element IDs with empty strings in `PASTA_CONFIG`.
25+
26+
### Autocomplete
27+
28+
In the demo page, creator and taxonomy input fields support autocomplete. Try typing a couple of characters into the creator box and see what happens.
29+
30+
We use [Pixabay's autocomplete](https://github.com/Pixabay/JavaScript-autoComplete) plugin. Thanks Pixabay!
31+
32+
Autocomplete requires creating a list of possible choices, and activating autocomplete for a given input via code in `pasta.js`. The list is imported as a script containing a `PASTA_LOOKUP` variable with arrays of choices for each desired autocomplete category. Once you create your choice arrays, don't forget to enable autocomplete for the appropriate inputs. See how the `creator` input was handled in `pasta.js` for an example.
33+
34+
One way to generate the choice arrays is to harvest metadata for all datasets for your site from PASTA. The `harvester` folder contains a `pasta_harvester.js` script demonstrating one way of doing that. Note that sometimes metadata is messy, such as taxonomy information which may include freeform text. The harvester requires `Node.js` with the `node-fetch` and `xml2js` packages installed.
35+
36+
### Caveats
37+
38+
The success of search queries depends upon the metadata that the LTER site provided when submitting data to the archive. For example, we assume the LTER Core Area was included as a **keyword** in the metadata. If your site follows different conventions, you will need to modify `pasta.js` to filter accordingly.

public/auto-complete.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.autocomplete-suggestions {
2+
text-align: left; cursor: default; border: 1px solid #ccc; border-top: 0; background: #fff; box-shadow: -1px 1px 3px rgba(0,0,0,.1);
3+
4+
/* core styles should not be changed */
5+
position: absolute; display: none; z-index: 9999; max-height: 254px; overflow: hidden; overflow-y: auto; box-sizing: border-box;
6+
}
7+
.autocomplete-suggestion { position: relative; padding: 0 .6em; line-height: 23px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 1.02em; color: #333; }
8+
.autocomplete-suggestion b { font-weight: normal; color: #1f8dd6; }
9+
.autocomplete-suggestion.selected { background: #f0f0f0; }

public/auto-complete.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/demo.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU"
88
crossorigin="anonymous">
99
<link href="search.css" rel="stylesheet" type="text/css">
10+
<link href="auto-complete.css" rel="stylesheet" type="text/css">
1011
<script src="cors.js"></script>
1112
<script src="pagination.js"></script>
13+
<script src="auto-complete.min.js"></script>
14+
<script src="pasta_lookup.js"></script>
1215
<script src="pasta.js"></script>
1316
</head>
1417

1518
<body>
1619
<p>Enter a search term like "water", or click <strong>Advanced</strong> for more options.
17-
Datasets from the NWT LTER site will be searched.</p>
20+
Datasets from the NWT LTER site will be summarized on the page with external links to more details at the
21+
original data archive. If you do not enter any search terms, all datasets for the given LTER site will be
22+
returned, which is the default behavior when first loading the page.</p>
1823
<form id="dataSearchForm" name="dataSearchForm">
1924
<input type="hidden" id="sort" name="sort" value="score">
2025
<input class="search-input" name="q" placeholder="Enter search term" type="text">

public/pagination.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ function escapeHtml(unsafe) {
8383

8484

8585
function showResultCount(query, total, limitPerPage, currentStartIndex, domElementId) {
86-
if (total == 0) {
86+
var element = document.getElementById(domElementId);
87+
if (total == 0 || !element) {
8788
return;
8889
}
8990

@@ -110,6 +111,5 @@ function showResultCount(query, total, limitPerPage, currentStartIndex, domEleme
110111
}
111112
var showing = (". Showing results " + fromCount + " to " + toCount + ".</p>");
112113
}
113-
var element = document.getElementById(domElementId);
114114
element.innerHTML = found + forQuery + showing;
115115
}

public/pasta.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ function parsePastaResults(xmlDoc) {
3333
var docs = xmlDoc.getElementsByTagName("document");
3434
var html = [];
3535
var sortDiv = document.getElementById(PASTA_CONFIG["sortDiv"]);
36-
if (docs.length)
37-
sortDiv.style.display = "block";
38-
else
39-
sortDiv.style.display = "none";
36+
if (sortDiv) {
37+
if (docs.length)
38+
sortDiv.style.display = "block";
39+
else
40+
sortDiv.style.display = "none";
41+
}
4042
for (var i = 0; i < docs.length; i++) {
4143
var doc = docs[i];
4244
var authors = doc.getElementsByTagName("responsibleParties")[0].childNodes[0].nodeValue.trim();
@@ -128,8 +130,9 @@ function errorCallback() {
128130

129131
// Writes CORS request URL to the page so user can see it
130132
function showUrl(url) {
131-
var txt = '<a href="' + url + '" target="_blank">' + url + '</a>';
132133
var element = document.getElementById(PASTA_CONFIG["urlElementId"]);
134+
if (!element) return;
135+
var txt = '<a href="' + url + '" target="_blank">' + url + '</a>';
133136
element.innerHTML = txt;
134137
}
135138

@@ -176,7 +179,7 @@ function searchPasta(userQuery, coreArea, creator, sYear, eYear, datayear, pubye
176179
var limit = "&rows=" + PASTA_CONFIG["limit"];
177180
var start = "&start=" + pageStart;
178181
var query = "&q=" + userQuery;
179-
if (creator) query += "+AND+author:" + creator;
182+
if (creator) query += '+AND+(author:"' + creator + '"+OR+organization:"' + creator + '")';
180183
if (pkgId) {
181184
pkgId = pkgId.replace(":", "%5C:");
182185
query += "+AND+(doi:" + pkgId + "+packageid:" + pkgId + "+id:" + pkgId + ")";
@@ -292,6 +295,23 @@ function isInteger(x) {
292295
}
293296

294297

298+
function makeAutocomplete(elementId, choices, minChars) {
299+
if (!minChars) minChars = 2;
300+
var autocomplete = new autoComplete({
301+
selector: "#" + elementId,
302+
minChars: minChars,
303+
source: function (term, suggest) {
304+
term = term.toLowerCase();
305+
var suggestions = [];
306+
for (var i = 0; i < choices.length; i++)
307+
if (~choices[i].toLowerCase().indexOf(term)) suggestions.push(choices[i]);
308+
suggest(suggestions);
309+
}
310+
});
311+
return autocomplete;
312+
}
313+
314+
295315
// When the window loads, read query parameters and perform search
296316
window.onload = function () {
297317
var query = getParameterByName("q");
@@ -332,4 +352,7 @@ window.onload = function () {
332352
if (!query) query = "*"; // default for empty query
333353
searchPasta(query, coreArea, creator, sYear, eYear, datayear, pubyear,
334354
pkgId, taxon, geo, sortBy, pageStart);
355+
356+
makeAutocomplete("creator", PASTA_LOOKUP["responsibleParties"]);
357+
makeAutocomplete("taxon", PASTA_LOOKUP["taxonomic"]);
335358
};

0 commit comments

Comments
 (0)