blob: 2a77ab6f0ebd7be0ca84aeb2f68d4de5e3a4abab [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');
Matteo Scandolo9b460042017-04-14 16:24:45 -070044 this.$log.debug(`[XosSearchService] States: `, this.states);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080045 });
46 }
47
48 public search(query: string): IXosSearchResult[] {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080049 this.$log.debug(`[XosSearchService] Searching for: ${query}`);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080050 const routes: IXosSearchResult[] = _.filter(this.states, s => {
51 return s.label.toLowerCase().indexOf(query) > -1;
52 }).map(r => {
53 r.type = 'View';
54 return r;
55 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080056 // TODO XosModelStore.search throws an error,
57 // probably there is something wrong saved in the cache!!
58 const models = _.map(this.XosModelStore.search(query), m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080059 return {
60 label: m.humanReadableName ? m.humanReadableName : m.name,
61 state: this.ConfigHelpers.stateWithParamsForJs(m.modelName, m),
62 type: m.modelName
63 };
64 });
65 return routes.concat(models);
66 }
67}