blob: fd038a25bbcd0d60186784d3734e80fead85e111 [file] [log] [blame]
Matteo Scandolo86bc26a2017-01-18 11:06:47 -08001import * as _ from 'lodash';
2import {IXosNavigationService} from '../../core/services/navigation';
Matteo Scandolo47860fe2017-02-02 12:05:55 -08003import {IXosModelStoreService} from '../stores/model.store';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -08004import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08005import {IXosState} from '../../core/services/runtime-states';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -08006
7export interface IXosSearchResult {
8 label: string;
9 state: string | {name: string, params: any};
10 type?: string;
11}
12
13export interface IXosSearchService {
14 search(query: string): IXosSearchResult[];
15}
16
17export class SearchService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080018 static $inject = ['$rootScope', '$log', 'XosNavigationService', 'XosModelStore', 'ConfigHelpers'];
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080019 private states: IXosState[];
20
21 constructor (
22 private $rootScope: ng.IScope,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080023 private $log: ng.ILogService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080024 private NavigationService: IXosNavigationService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080025 private XosModelStore: IXosModelStoreService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080026 private ConfigHelpers: IXosConfigHelpersService
27 ) {
28 this.$rootScope.$on('xos.core.modelSetup', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080029 this.$log.info(`[XosSearchService] Loading views`);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080030 this.states = this.NavigationService.query().reduce((list, state) => {
31 // if it does not have child (otherwise it is abstract)
32 if (!state.children || state.children.length === 0) {
33 list.push(state);
34 }
35 // else push child
36 if (state.children && state.children.length > 0) {
37 state.children.forEach(c => {
38 list.push(c);
39 });
40 }
41 return list;
42 }, []);
43 this.states = _.uniqBy(this.states, 'state');
44 });
45 }
46
47 public search(query: string): IXosSearchResult[] {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080048 this.$log.debug(`[XosSearchService] Searching for: ${query}`);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080049 const routes: IXosSearchResult[] = _.filter(this.states, s => {
50 return s.label.toLowerCase().indexOf(query) > -1;
51 }).map(r => {
52 r.type = 'View';
53 return r;
54 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080055 // TODO XosModelStore.search throws an error,
56 // probably there is something wrong saved in the cache!!
57 const models = _.map(this.XosModelStore.search(query), m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080058 return {
59 label: m.humanReadableName ? m.humanReadableName : m.name,
60 state: this.ConfigHelpers.stateWithParamsForJs(m.modelName, m),
61 type: m.modelName
62 };
63 });
64 return routes.concat(models);
65 }
66}