@abi-software/map-side-bar 1.3.32 → 1.3.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,182 +1,182 @@
1
- /* eslint-disable no-alert, no-console */
2
- import algoliasearch from 'algoliasearch'
3
-
4
- // export `createAlgoliaClient` to use it in page components
5
- export class AlgoliaClient {
6
- constructor(algoliaId, algoliaKey, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
7
- this.client = algoliasearch(
8
- algoliaId,
9
- algoliaKey
10
- )
11
- this.PENNSIEVE_API_LOCATION = PENNSIEVE_API_LOCATION
12
- }
13
- initIndex(ALGOLIA_INDEX) {
14
- this.index = this.client.initIndex(ALGOLIA_INDEX);
15
- }
16
-
17
- getAlgoliaFacets(propPathMapping) {
18
- const map = new Map(Object.entries(propPathMapping));
19
- const facetPropPaths = Array.from(map.keys());
20
- let facetData = []
21
- let facetId = 0
22
- return this.index
23
- .search('', {
24
- sortFacetValuesBy: 'alpha',
25
- facets: facetPropPaths
26
- })
27
- .then(response => {
28
- facetPropPaths.map((facetPropPath) => {
29
- var children = []
30
- const responseFacets = response.facets
31
- if (responseFacets === undefined) { return }
32
- const responseFacetChildren =
33
- responseFacets[facetPropPath] == undefined
34
- ? {}
35
- : responseFacets[facetPropPath]
36
- Object.keys(responseFacetChildren).map(facet => {
37
- children.push({
38
- label: facet,
39
- id: facetId++,
40
- facetPropPath: facetPropPath
41
- })
42
- })
43
- if (children.length > 0) {
44
- facetData.push({
45
- label: map.get(facetPropPath),
46
- id: facetId++,
47
- children: children,
48
- key: facetPropPath
49
- })
50
- }
51
- })
52
- return facetData
53
- })
54
- }
55
-
56
- // Returns all DOIs of all versions for a given discover dataset
57
- _discoverAllDois(discoverId, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
58
- return new Promise(resolve => {
59
- fetch(`${PENNSIEVE_API_LOCATION}/discover/datasets/${discoverId}/versions`).then(r => r.json()).then(dataset => {
60
- resolve(dataset.map(version => version.doi))
61
- })
62
- })
63
- }
64
-
65
- // Get all dois given a list of discoverIds
66
- _expandDois(discoverIds, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
67
- return new Promise(resolve => {
68
- let promiseList = discoverIds.map(discoverId => this._discoverAllDois(discoverId, PENNSIEVE_API_LOCATION))
69
- Promise.all(promiseList).then((values) => {
70
- resolve(values.flat())
71
- });
72
- })
73
- }
74
-
75
- _processResultsForCards(results) {
76
- let newResults = []
77
- let newResult = {}
78
- for (let res of results) {
79
- newResult = { ...res }
80
- newResult = {
81
- doi: res.item.curie.split(':')[1],
82
- name: res.item.name,
83
- description: res.item.description,
84
- updated: res.pennsieve.updatedAt,
85
- publishDate: res.pennsieve.publishDate,
86
- datasetId: res.objectID,
87
- detailsReady: false
88
- }
89
- newResults.push(newResult)
90
- }
91
- return newResults
92
- }
93
-
94
- _processAnatomy(hits) {
95
- let foundKeyWords = []
96
- let uniqueKeywords = []
97
- hits.forEach(hit => {
98
- if (hit.item && hit.item.keywords) {
99
- hit.item.keywords.forEach(keywordObj => {
100
- let keyword = keywordObj.keyword.toUpperCase()
101
- if (keyword.includes('UBERON') || keyword.includes('ILX')) {
102
- foundKeyWords.push(this._processUberonURL(keyword))
103
- }
104
- })
105
- }
106
- if (hit.anatomy && hit.anatomy.organ ) {
107
- hit.anatomy.organ.forEach(anatomy => {
108
- if (anatomy.curie) {
109
- foundKeyWords.push(anatomy.curie)
110
- }
111
- })
112
- }
113
- })
114
- uniqueKeywords = [...new Set(foundKeyWords)]
115
- return uniqueKeywords
116
- }
117
-
118
- _processUberonURL(url) {
119
- let ub = url.split('/').pop()
120
- return ub.replace('_', ':')
121
- }
122
-
123
- /**
124
- * Get Search results
125
- * This is using fetch from the Algolia API
126
- */
127
- search(filter, query = '', hitsperPage = 10, page = 1) {
128
- return new Promise(resolve => {
129
- this.index
130
- .search(query, {
131
- facets: ['*'],
132
- hitsPerPage: hitsperPage,
133
- page: page - 1,
134
- filters: filter,
135
- attributesToHighlight: [],
136
- attributesToRetrieve: [
137
- 'pennsieve.publishDate',
138
- 'pennsieve.updatedAt',
139
- 'item.curie',
140
- 'item.name',
141
- 'item.description',
142
- 'objectID',
143
- ],
144
- })
145
- .then(response => {
146
- let searchData = {
147
- items: this._processResultsForCards(response.hits),
148
- total: response.nbHits,
149
- discoverIds: response.hits.map(r => r.pennsieve.identifier),
150
- dois: response.hits.map(r => r.item.curie.split(':')[1])
151
- }
152
- resolve(searchData)
153
- })
154
- })
155
- }
156
-
157
- /**
158
- * Get key words
159
- * This is used to return all keywords for a given search. Note that you often want the hits per page to be maxed out
160
- */
161
- anatomyInSearch(filter, query = '', hitsperPage = 999999, page = 1) {
162
- return new Promise(resolve => {
163
- this.index
164
- .search(query, {
165
- facets: ['*'],
166
- hitsPerPage: hitsperPage,
167
- page: page - 1,
168
- filters: filter,
169
- attributesToHighlight: [],
170
- attributesToRetrieve: [
171
- 'item.keywords.keyword',
172
- 'anatomy.organ.name',
173
- 'anatomy.organ.curie'
174
- ],
175
- })
176
- .then(response => {
177
- let anatomyAsUberons = this._processAnatomy(response.hits)
178
- resolve(anatomyAsUberons)
179
- })
180
- })
181
- }
182
- }
1
+ /* eslint-disable no-alert, no-console */
2
+ import algoliasearch from 'algoliasearch'
3
+
4
+ // export `createAlgoliaClient` to use it in page components
5
+ export class AlgoliaClient {
6
+ constructor(algoliaId, algoliaKey, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
7
+ this.client = algoliasearch(
8
+ algoliaId,
9
+ algoliaKey
10
+ )
11
+ this.PENNSIEVE_API_LOCATION = PENNSIEVE_API_LOCATION
12
+ }
13
+ initIndex(ALGOLIA_INDEX) {
14
+ this.index = this.client.initIndex(ALGOLIA_INDEX);
15
+ }
16
+
17
+ getAlgoliaFacets(propPathMapping) {
18
+ const map = new Map(Object.entries(propPathMapping));
19
+ const facetPropPaths = Array.from(map.keys());
20
+ let facetData = []
21
+ let facetId = 0
22
+ return this.index
23
+ .search('', {
24
+ sortFacetValuesBy: 'alpha',
25
+ facets: facetPropPaths
26
+ })
27
+ .then(response => {
28
+ facetPropPaths.map((facetPropPath) => {
29
+ var children = []
30
+ const responseFacets = response.facets
31
+ if (responseFacets === undefined) { return }
32
+ const responseFacetChildren =
33
+ responseFacets[facetPropPath] == undefined
34
+ ? {}
35
+ : responseFacets[facetPropPath]
36
+ Object.keys(responseFacetChildren).map(facet => {
37
+ children.push({
38
+ label: facet,
39
+ id: facetId++,
40
+ facetPropPath: facetPropPath
41
+ })
42
+ })
43
+ if (children.length > 0) {
44
+ facetData.push({
45
+ label: map.get(facetPropPath),
46
+ id: facetId++,
47
+ children: children,
48
+ key: facetPropPath
49
+ })
50
+ }
51
+ })
52
+ return facetData
53
+ })
54
+ }
55
+
56
+ // Returns all DOIs of all versions for a given discover dataset
57
+ _discoverAllDois(discoverId, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
58
+ return new Promise(resolve => {
59
+ fetch(`${PENNSIEVE_API_LOCATION}/discover/datasets/${discoverId}/versions`).then(r => r.json()).then(dataset => {
60
+ resolve(dataset.map(version => version.doi))
61
+ })
62
+ })
63
+ }
64
+
65
+ // Get all dois given a list of discoverIds
66
+ _expandDois(discoverIds, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
67
+ return new Promise(resolve => {
68
+ let promiseList = discoverIds.map(discoverId => this._discoverAllDois(discoverId, PENNSIEVE_API_LOCATION))
69
+ Promise.all(promiseList).then((values) => {
70
+ resolve(values.flat())
71
+ });
72
+ })
73
+ }
74
+
75
+ _processResultsForCards(results) {
76
+ let newResults = []
77
+ let newResult = {}
78
+ for (let res of results) {
79
+ newResult = { ...res }
80
+ newResult = {
81
+ doi: res.item.curie.split(':')[1],
82
+ name: res.item.name,
83
+ description: res.item.description,
84
+ updated: res.pennsieve ? res.pennsieve.updatedAt : undefined,
85
+ publishDate: res.pennsieve ? res.pennsieve.publishDate : undefined,
86
+ datasetId: res.objectID,
87
+ detailsReady: false
88
+ }
89
+ newResults.push(newResult)
90
+ }
91
+ return newResults
92
+ }
93
+
94
+ _processAnatomy(hits) {
95
+ let foundKeyWords = []
96
+ let uniqueKeywords = []
97
+ hits.forEach(hit => {
98
+ if (hit.item && hit.item.keywords) {
99
+ hit.item.keywords.forEach(keywordObj => {
100
+ let keyword = keywordObj.keyword.toUpperCase()
101
+ if (keyword.includes('UBERON') || keyword.includes('ILX')) {
102
+ foundKeyWords.push(this._processUberonURL(keyword))
103
+ }
104
+ })
105
+ }
106
+ if (hit.anatomy && hit.anatomy.organ ) {
107
+ hit.anatomy.organ.forEach(anatomy => {
108
+ if (anatomy.curie) {
109
+ foundKeyWords.push(anatomy.curie)
110
+ }
111
+ })
112
+ }
113
+ })
114
+ uniqueKeywords = [...new Set(foundKeyWords)]
115
+ return uniqueKeywords
116
+ }
117
+
118
+ _processUberonURL(url) {
119
+ let ub = url.split('/').pop()
120
+ return ub.replace('_', ':')
121
+ }
122
+
123
+ /**
124
+ * Get Search results
125
+ * This is using fetch from the Algolia API
126
+ */
127
+ search(filter, query = '', hitsperPage = 10, page = 1) {
128
+ return new Promise(resolve => {
129
+ this.index
130
+ .search(query, {
131
+ facets: ['*'],
132
+ hitsPerPage: hitsperPage,
133
+ page: page - 1,
134
+ filters: filter,
135
+ attributesToHighlight: [],
136
+ attributesToRetrieve: [
137
+ 'pennsieve.publishDate',
138
+ 'pennsieve.updatedAt',
139
+ 'item.curie',
140
+ 'item.name',
141
+ 'item.description',
142
+ 'objectID',
143
+ ],
144
+ })
145
+ .then(response => {
146
+ let searchData = {
147
+ items: this._processResultsForCards(response.hits),
148
+ total: response.nbHits,
149
+ discoverIds: response.hits.map(r => r.pennsieve ? r.pennsieve.identifier : r.objectID),
150
+ dois: response.hits.map(r => r.item.curie.split(':')[1])
151
+ }
152
+ resolve(searchData)
153
+ })
154
+ })
155
+ }
156
+
157
+ /**
158
+ * Get key words
159
+ * This is used to return all keywords for a given search. Note that you often want the hits per page to be maxed out
160
+ */
161
+ anatomyInSearch(filter, query = '', hitsperPage = 999999, page = 1) {
162
+ return new Promise(resolve => {
163
+ this.index
164
+ .search(query, {
165
+ facets: ['*'],
166
+ hitsPerPage: hitsperPage,
167
+ page: page - 1,
168
+ filters: filter,
169
+ attributesToHighlight: [],
170
+ attributesToRetrieve: [
171
+ 'item.keywords.keyword',
172
+ 'anatomy.organ.name',
173
+ 'anatomy.organ.curie'
174
+ ],
175
+ })
176
+ .then(response => {
177
+ let anatomyAsUberons = this._processAnatomy(response.hits)
178
+ resolve(anatomyAsUberons)
179
+ })
180
+ })
181
+ }
182
+ }
@@ -1,70 +1,70 @@
1
- /* eslint-disable no-alert, no-console */
2
-
3
- // Mapping between display categories and their Algolia index property path
4
- // Used for populating the Dataset Search Results facet menu dynamically
5
- export const facetPropPathMapping = {
6
- 'anatomy.organ.name' : 'Anatomical Structure',
7
- 'organisms.primary.species.name' : 'Species',
8
- 'item.modalities.keyword' : 'Experimental Approach',
9
- 'attributes.subject.sex.value' : 'Sex',
10
- 'attributes.subject.ageCategory.value' : 'Age Categories',
11
- 'item.keywords.keyword' : 'Keywords'
12
- }
13
-
14
- // Same as above, but these show on the sidebar filters
15
- export const shownFilters = {
16
- 'anatomy.organ.name' : 'Anatomical Structure',
17
- 'organisms.primary.species.name' : 'Species',
18
- 'item.modalities.keyword' : 'Experimental Approach',
19
- 'attributes.subject.sex.value' : 'Sex',
20
- 'attributes.subject.ageCategory.value' : 'Age Categories',
21
- }
22
-
23
- /* Returns filter for searching algolia. All facets of the same category are joined with OR,
24
- * and each of those results is then joined with an AND.
25
- * i.e. (color:blue OR color:red) AND (shape:circle OR shape:red) */
26
- export function getFilters(selectedFacetArray=undefined) {
27
- // return all datasets if no filter
28
- if (selectedFacetArray === undefined) {
29
- return 'NOT item.published.status:embargo'
30
- }
31
-
32
- // Switch the 'term' attribute to 'label' if 'label' does not exist
33
- selectedFacetArray.forEach(f=>f.label=f.facet)
34
-
35
-
36
- let facets = removeShowAllFacets(selectedFacetArray)
37
-
38
- let filters = "NOT item.published.status:embargo";
39
- filters = `(${filters}) AND `;
40
-
41
- const facetPropPaths = Object.keys(facetPropPathMapping);
42
- facetPropPaths.map((facetPropPath) => {
43
- const facetsToBool = facets.filter(
44
- (facet) => facet.facetPropPath == facetPropPath
45
- );
46
- let orFilters = "";
47
- let andFilters = "";
48
- facetsToBool.map((facet) => {
49
- if (facet.AND){
50
- andFilters += `AND "${facetPropPath}":"${facet.label}"`;
51
- } else {
52
- orFilters += `"${facetPropPath}":"${facet.label}" OR `;
53
- }
54
- });
55
- if (orFilters == "" && andFilters =="") {
56
- return;
57
- }
58
- orFilters = `(${orFilters.substring(0, orFilters.lastIndexOf(" OR "))})` // remove last OR
59
-
60
- filters += `${orFilters + andFilters} AND `; // Put them together
61
- // (Note that we add an extra AND in case there are facets at a higher level)
62
-
63
- filters = filters.split('()AND ').join(''); // Handle case where there where no OR facets
64
- });
65
- return filters.substring(0, filters.lastIndexOf(" AND "));
66
- }
67
-
68
- function removeShowAllFacets(facetArray){
69
- return facetArray.filter( f => f.label !== 'Show all')
1
+ /* eslint-disable no-alert, no-console */
2
+
3
+ // Mapping between display categories and their Algolia index property path
4
+ // Used for populating the Dataset Search Results facet menu dynamically
5
+ export const facetPropPathMapping = {
6
+ 'anatomy.organ.name' : 'Anatomical Structure',
7
+ 'organisms.primary.species.name' : 'Species',
8
+ 'item.modalities.keyword' : 'Experimental Approach',
9
+ 'attributes.subject.sex.value' : 'Sex',
10
+ 'attributes.subject.ageCategory.value' : 'Age Categories',
11
+ 'item.keywords.keyword' : 'Keywords'
12
+ }
13
+
14
+ // Same as above, but these show on the sidebar filters
15
+ export const shownFilters = {
16
+ 'anatomy.organ.name' : 'Anatomical Structure',
17
+ 'organisms.primary.species.name' : 'Species',
18
+ 'item.modalities.keyword' : 'Experimental Approach',
19
+ 'attributes.subject.sex.value' : 'Sex',
20
+ 'attributes.subject.ageCategory.value' : 'Age Categories',
21
+ }
22
+
23
+ /* Returns filter for searching algolia. All facets of the same category are joined with OR,
24
+ * and each of those results is then joined with an AND.
25
+ * i.e. (color:blue OR color:red) AND (shape:circle OR shape:red) */
26
+ export function getFilters(selectedFacetArray=undefined) {
27
+ // return all datasets if no filter
28
+ if (selectedFacetArray === undefined) {
29
+ return 'NOT item.published.status:embargo'
30
+ }
31
+
32
+ // Switch the 'term' attribute to 'label' if 'label' does not exist
33
+ selectedFacetArray.forEach(f=>f.label=f.facet)
34
+
35
+
36
+ let facets = removeShowAllFacets(selectedFacetArray)
37
+
38
+ let filters = "NOT item.published.status:embargo";
39
+ filters = `(${filters}) AND `;
40
+
41
+ const facetPropPaths = Object.keys(facetPropPathMapping);
42
+ facetPropPaths.map((facetPropPath) => {
43
+ const facetsToBool = facets.filter(
44
+ (facet) => facet.facetPropPath == facetPropPath
45
+ );
46
+ let orFilters = "";
47
+ let andFilters = "";
48
+ facetsToBool.map((facet) => {
49
+ if (facet.AND){
50
+ andFilters += `AND "${facetPropPath}":"${facet.label}"`;
51
+ } else {
52
+ orFilters += `"${facetPropPath}":"${facet.label}" OR `;
53
+ }
54
+ });
55
+ if (orFilters == "" && andFilters =="") {
56
+ return;
57
+ }
58
+ orFilters = `(${orFilters.substring(0, orFilters.lastIndexOf(" OR "))})` // remove last OR
59
+
60
+ filters += `${orFilters + andFilters} AND `; // Put them together
61
+ // (Note that we add an extra AND in case there are facets at a higher level)
62
+
63
+ filters = filters.split('()AND ').join(''); // Handle case where there where no OR facets
64
+ });
65
+ return filters.substring(0, filters.lastIndexOf(" AND "));
66
+ }
67
+
68
+ function removeShowAllFacets(facetArray){
69
+ return facetArray.filter( f => f.label !== 'Show all')
70
70
  }