2018-01-20 00:46:47 -08:00
|
|
|
import Controller from '@ember/controller';
|
2018-09-13 18:24:05 -07:00
|
|
|
import { computed } from '@ember-decorators/object';
|
|
|
|
import { debounce } from '@ember-decorators/runloop';
|
2018-09-17 23:03:32 -07:00
|
|
|
import { facetToParamUrl, facetFromParamUrl, facetToDynamicCounts } from 'wherehows-web/utils/api/search';
|
|
|
|
import { IFacetsSelectionsMap, IFacetsCounts } from 'wherehows-web/typings/app/search/facets';
|
|
|
|
import { set, setProperties, get } from '@ember/object';
|
2017-03-24 20:59:43 -07:00
|
|
|
|
2018-03-18 21:43:31 -07:00
|
|
|
// gradual refactor into es class, hence extends EmberObject instance
|
2018-09-11 11:11:36 -07:00
|
|
|
export default class SearchController extends Controller {
|
2018-09-13 18:24:05 -07:00
|
|
|
queryParams = ['category', 'page', 'facets', 'keyword'];
|
2018-09-11 11:11:36 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The category to narrow/ filter search results
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
category = 'datasets';
|
|
|
|
|
|
|
|
/**
|
2018-09-14 16:58:45 -07:00
|
|
|
* Encoded facets state in a restli fashion
|
2018-09-11 11:11:36 -07:00
|
|
|
*/
|
2018-09-13 18:24:05 -07:00
|
|
|
facets: string;
|
2018-09-11 11:11:36 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current search page
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
page = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Header text for search sidebar
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
header = 'Refine By';
|
|
|
|
|
2018-09-14 16:40:45 -07:00
|
|
|
/**
|
|
|
|
* Since the loading of search comes from two parts:
|
|
|
|
* 1. Search Call
|
|
|
|
* 2. During debouncing
|
|
|
|
*
|
|
|
|
* We put it as a flag to control it better
|
|
|
|
*/
|
2018-09-13 18:24:05 -07:00
|
|
|
searchLoading: boolean = false;
|
|
|
|
|
2018-09-14 16:40:45 -07:00
|
|
|
/**
|
|
|
|
* When facets change we set the flag loading and call the debounced fn
|
|
|
|
* @param selections facet selections
|
|
|
|
*/
|
2018-09-13 18:24:05 -07:00
|
|
|
onFacetsChange(selections: IFacetsSelectionsMap) {
|
2018-09-17 23:03:32 -07:00
|
|
|
set(this, 'searchLoading', true);
|
2018-09-13 18:24:05 -07:00
|
|
|
this.onFacetsChangeDebounced(selections);
|
|
|
|
}
|
|
|
|
|
2018-09-14 16:40:45 -07:00
|
|
|
/**
|
|
|
|
* Will set the facets in the URL to start a model refresh (see route)
|
|
|
|
* @param selections Facet selections
|
|
|
|
*/
|
2018-09-13 18:24:05 -07:00
|
|
|
@debounce(1000)
|
|
|
|
onFacetsChangeDebounced(selections: IFacetsSelectionsMap) {
|
2018-09-17 23:03:32 -07:00
|
|
|
setProperties(this, {
|
2018-09-13 18:24:05 -07:00
|
|
|
facets: facetToParamUrl(selections),
|
|
|
|
page: 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-14 16:40:45 -07:00
|
|
|
/**
|
|
|
|
* Will translate backend fields into a dynamic facet
|
|
|
|
* count structure.
|
|
|
|
*/
|
|
|
|
@computed('model')
|
|
|
|
get facetCounts(): IFacetsCounts {
|
|
|
|
return facetToDynamicCounts(this.model);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will read selections from URL and translate it into
|
|
|
|
* our selections object
|
|
|
|
*/
|
2018-09-13 18:24:05 -07:00
|
|
|
@computed('facets')
|
2018-09-14 16:40:45 -07:00
|
|
|
get facetsSelections(): IFacetsSelectionsMap {
|
2018-09-13 18:24:05 -07:00
|
|
|
return facetFromParamUrl(this.facets || '');
|
|
|
|
}
|
2018-09-14 16:40:45 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will return false if there is data to display
|
|
|
|
*/
|
2018-09-13 18:24:05 -07:00
|
|
|
@computed('model.data.length')
|
2018-09-17 23:03:32 -07:00
|
|
|
get showNoResult(): boolean {
|
|
|
|
// @ts-ignore looks like TS is not working for this type of getters
|
|
|
|
return get(this, 'model.data.length') === 0;
|
2018-09-11 11:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('model.category')
|
|
|
|
get isMetrics(): boolean {
|
|
|
|
const model = this.get('model');
|
2017-02-13 14:17:49 -08:00
|
|
|
if (model && model.category) {
|
|
|
|
if (model.category.toLocaleLowerCase() === 'metrics') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-09-11 11:11:36 -07:00
|
|
|
}
|
2018-03-18 21:43:31 -07:00
|
|
|
|
2018-09-11 11:11:36 -07:00
|
|
|
@computed('model.page')
|
|
|
|
get previousPage(): number {
|
|
|
|
const model = this.get('model');
|
2017-02-13 14:17:49 -08:00
|
|
|
if (model && model.page) {
|
|
|
|
var currentPage = model.page;
|
|
|
|
if (currentPage <= 1) {
|
|
|
|
return currentPage;
|
2017-09-18 16:31:47 -07:00
|
|
|
} else {
|
2017-02-13 14:17:49 -08:00
|
|
|
return currentPage - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-11 11:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('model.page')
|
|
|
|
get nextPage(): number {
|
|
|
|
const model = this.get('model');
|
2017-02-13 14:17:49 -08:00
|
|
|
if (model && model.page) {
|
2018-09-11 11:11:36 -07:00
|
|
|
const currentPage = model.page;
|
|
|
|
const totalPages = model.totalPages;
|
2017-02-13 14:17:49 -08:00
|
|
|
if (currentPage >= totalPages) {
|
|
|
|
return totalPages;
|
2017-09-18 16:31:47 -07:00
|
|
|
} else {
|
2017-02-13 14:17:49 -08:00
|
|
|
return currentPage + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-11 11:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('model.page')
|
|
|
|
get first(): boolean {
|
|
|
|
const model = this.get('model');
|
2017-02-13 14:17:49 -08:00
|
|
|
if (model && model.page) {
|
2018-09-11 11:11:36 -07:00
|
|
|
const currentPage = model.page;
|
2017-02-13 14:17:49 -08:00
|
|
|
if (currentPage <= 1) {
|
|
|
|
return true;
|
2017-09-18 16:31:47 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
2017-02-13 14:17:49 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-11 11:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('model.page')
|
|
|
|
get last(): boolean {
|
|
|
|
const model = this.get('model');
|
2017-02-13 14:17:49 -08:00
|
|
|
if (model && model.page) {
|
2018-09-11 11:11:36 -07:00
|
|
|
const currentPage = model.page;
|
|
|
|
const totalPages = model.totalPages;
|
2017-02-13 14:17:49 -08:00
|
|
|
if (currentPage >= totalPages) {
|
|
|
|
return true;
|
2017-09-18 16:31:47 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
2017-02-13 14:17:49 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 21:43:31 -07:00
|
|
|
}
|