-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v2.0-sandbox' into sp-collection-card
- Loading branch information
Showing
12 changed files
with
477 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
services/ucd-lib-client/client/public/elements/utils/app-browse-by.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { LitElement } from 'lit-element'; | ||
import render from "./app-browse-by.tpl.js"; | ||
|
||
import "@ucd-lib/cork-pagination"; | ||
|
||
/** | ||
* @class AppBrowseBy | ||
* @description base class for the browse by [facet] page elements | ||
* | ||
* Bound to app-state-update, rendering when the url matches /browse/[id] where | ||
* element id is. You must provide facet-query-name and label as well. | ||
* | ||
* Three slots are available for images; 'header-icon', 'left-image' and 'right-image' | ||
* | ||
* @property {String} id required so page is rendered on correct app-state-update event | ||
* @property {String} facet-query-name the record property to be queried on | ||
* @property {String} label nice label text for query facet | ||
* @property {Array} sortByOptions override this property to change the default sorts | ||
*/ | ||
export default class AppBrowseBy extends Mixin(LitElement) | ||
.with(LitCorkUtils) { | ||
|
||
static get properties() { | ||
return { | ||
facetQueryName : {type: String, attribute: 'facet-query-name'}, | ||
label : {type: String}, | ||
sortByOptions : {type: Array}, | ||
results : {type: Array}, | ||
totalResults : {type: Number}, | ||
resultsPerPage : {type: Number}, | ||
currentIndex : {type: Number} | ||
}; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
this.render = render.bind(this); | ||
|
||
this.sortByOptions = [ | ||
{label : 'A-Z', type: 'key', dir : 'asc', selected: true}, | ||
{label : 'Item Quantity', dir : 'dsc', type: 'count'} | ||
]; | ||
|
||
this.reset(); | ||
|
||
this._injectModel('BrowseByModel', 'AppStateModel', 'RecordModel'); | ||
} | ||
|
||
/** | ||
* @method reset | ||
* @description reset search properties | ||
*/ | ||
reset() { | ||
this.results = []; | ||
this.totalResults = 0; | ||
this.resultsPerPage = 20; | ||
this.currentIndex = 0; | ||
this.label = ''; | ||
} | ||
|
||
/** | ||
* @method _onAppStateUpdate | ||
* @description bound to AppStateModel app-state-update event | ||
* | ||
* @param {Object} e | ||
* @returns {Promise} | ||
*/ | ||
async _onAppStateUpdate(e) { | ||
if( e.location.page !== 'browse' ) return; | ||
if( e.location.path.length < 2 ) return; | ||
if( e.location.path[1] !== this.id ) return; // the page | ||
|
||
if( this.totalResults === 0 ) { | ||
this.loading = true; | ||
this.allResults = await this.BrowseByModel.getFacets(this.facetQueryName); | ||
this.totalResults = this.allResults.payload.length; | ||
this.loading = false; | ||
} | ||
|
||
if( e.location.path.length > 2 ) { | ||
this.currentIndex = parseInt(e.location.path[2]); | ||
} else { | ||
this.currentIndex = 0; | ||
} | ||
|
||
let sort = 0; | ||
if( e.location.path.length > 2 ) { | ||
sort = parseInt(e.location.path[2]); | ||
} | ||
this.sortByOptions.forEach((item, index) => item.selected = (sort === index)); | ||
|
||
this._renderResults(); | ||
} | ||
|
||
/** | ||
* @method _renderResults | ||
* @description render the results array based on currentPage and sort | ||
* params | ||
*/ | ||
_renderResults() { | ||
let sort = this.sortByOptions.find(item => item.selected); | ||
|
||
if( this.sortedAs !== sort.type ) { | ||
this.allResults.payload.sort((a, b) => { | ||
if( a[sort.type] > b[sort.type] ) return (sort.dir === 'asc') ? 1 : -1; | ||
if( a[sort.type] < b[sort.type] ) return (sort.dir === 'asc') ? -1 : 1; | ||
return 0; | ||
}); | ||
this.sortedAs = sort.type; | ||
} | ||
|
||
this.results = this.allResults.payload.slice( | ||
this.currentIndex, | ||
this.currentIndex + this.resultsPerPage | ||
); | ||
|
||
this.requestUpdate(); | ||
} | ||
|
||
/** | ||
* @method _onPaginationNav | ||
* @description bound to cork-pagination nav event | ||
* | ||
* @param {Object} e | ||
*/ | ||
_onPaginationNav(e) { | ||
this.currentIndex = (e.detail.page-1) * this.resultsPerPage; | ||
this._renderResults(); | ||
} | ||
|
||
/** | ||
* @method _onSortChange | ||
* @description bound to sort radio button change events | ||
* | ||
* @param {Object} e | ||
*/ | ||
_onSortChange(e) { | ||
let sortIndex = parseInt(e.currentTarget.getAttribute('index')); | ||
this.sortByOptions.forEach((item, index) => item.selected = (index === sortIndex)); | ||
this.currentIndex = 0; | ||
this._renderResults(); | ||
} | ||
|
||
/** | ||
* @method getFilterUrl | ||
* @description used by UI to create anchor tag urls for search queries | ||
* based on given facet | ||
* | ||
* @param {Object} item facet result item | ||
* @returns {String} | ||
*/ | ||
getFilterUrl(item) { | ||
let searchDocument = this.RecordModel.emptySearchDocument(); | ||
this.RecordModel.appendKeywordFilter(searchDocument, this.facetQueryName, item.key); | ||
return '/search/'+this.RecordModel.searchDocumentToUrl(searchDocument); | ||
} | ||
|
||
} | ||
|
||
customElements.define('app-browse-by', AppBrowseBy); |
95 changes: 95 additions & 0 deletions
95
services/ucd-lib-client/client/public/elements/utils/app-browse-by.tpl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { html } from 'lit-element'; | ||
|
||
export default function render() { | ||
return html` | ||
<style> | ||
:host { | ||
display: block; | ||
} | ||
.header { | ||
display: flex; | ||
justify-content : center; | ||
} | ||
.sort { | ||
display: flex; | ||
align-items: center; | ||
} | ||
.body { | ||
display: flex; | ||
} | ||
.results { | ||
width: 100%; | ||
box-sizing: border-box; | ||
max-width: 400px; | ||
} | ||
.results > * { | ||
display: flex; | ||
box-sizing: border-box; | ||
width: 100%; | ||
} | ||
.results > * > *:first-child { | ||
flex: 1; | ||
} | ||
</style> | ||
<div class="header"> | ||
<div> | ||
<div><slot name="header-icon"></slot></div> | ||
<div> | ||
<h1>Browse <span>${this.label}s</span></h1> | ||
</div> | ||
<div class="sort"> | ||
<div>Sort By:</div> | ||
${this.sortByOptions.map((item, index) => html` | ||
<div> | ||
<input type="radio" | ||
id="browse-by-${this.facetQueryName}-${item.label}" | ||
name="browse-by-${this.facetQueryName}" | ||
index="${index}" | ||
.checked="${item.selected}" | ||
@change="${this._onSortChange}" /> | ||
<label for="browse-by-${this.facetQueryName}-${item.label}">${item.label}</label> | ||
</div> | ||
`)} | ||
</div> | ||
</div> | ||
</div> | ||
<div class="body"> | ||
<div style="flex: 1"> | ||
<slot name="left-image"></slot> | ||
</div> | ||
<div class="results"> | ||
<h5> | ||
<div>${this.label}</div> | ||
<div>Items</div> | ||
</h5> | ||
${this.results.map(item => html` | ||
<div> | ||
<div><a href="${this.getFilterUrl(item)}">${item.key}</a></div> | ||
<div>${item.count}</div> | ||
</div> | ||
`)} | ||
</div> | ||
<div style="flex: 1"> | ||
<slot name="right-image"></slot> | ||
</div> | ||
</div> | ||
<cork-pagination | ||
total-results="${this.totalResults}" | ||
items-per-page="${this.resultsPerPage}" | ||
current-index="${this.currentIndex}" | ||
@nav="${this._onPaginationNav}"> | ||
</cork-pagination> | ||
`;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.