165 lines
3.9 KiB
TypeScript
Raw Normal View History

import Controller from '@ember/controller';
import { computed } from '@ember-decorators/object';
import { debounce } from '@ember-decorators/runloop';
import { facetToParamUrl, facetFromParamUrl, facetToDynamicCounts } from 'wherehows-web/utils/api/search/search';
2018-09-17 23:03:32 -07:00
import { IFacetsSelectionsMap, IFacetsCounts } from 'wherehows-web/typings/app/search/facets';
import { set, setProperties, get } from '@ember/object';
// gradual refactor into es class, hence extends EmberObject instance
2018-09-11 11:11:36 -07:00
export default class SearchController extends Controller {
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
*/
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
*/
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
*/
onFacetsChange(selections: IFacetsSelectionsMap) {
2018-09-17 23:03:32 -07:00
set(this, 'searchLoading', true);
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
*/
@debounce(1000)
onFacetsChangeDebounced(selections: IFacetsSelectionsMap) {
2018-09-17 23:03:32 -07:00
setProperties(this, {
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
*/
@computed('facets')
2018-09-14 16:40:45 -07:00
get facetsSelections(): IFacetsSelectionsMap {
return facetFromParamUrl(this.facets || '');
}
2018-09-14 16:40:45 -07:00
/**
* Will return false if there is data to display
*/
@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-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;
} 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;
} 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;
} 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;
} else {
return false;
2017-02-13 14:17:49 -08:00
}
} else {
return false;
}
}
}