Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 1 | import * as _ from 'lodash'; |
| 2 | import {IXosNavigationService} from '../../core/services/navigation'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 3 | import {IXosModelStoreService} from '../stores/model.store'; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 4 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 5 | import {IXosState} from '../../core/services/runtime-states'; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 6 | |
| 7 | export interface IXosSearchResult { |
| 8 | label: string; |
| 9 | state: string | {name: string, params: any}; |
| 10 | type?: string; |
| 11 | } |
| 12 | |
| 13 | export interface IXosSearchService { |
| 14 | search(query: string): IXosSearchResult[]; |
| 15 | } |
| 16 | |
| 17 | export class SearchService { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 18 | static $inject = ['$rootScope', '$log', 'XosNavigationService', 'XosModelStore', 'ConfigHelpers']; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 19 | private states: IXosState[]; |
| 20 | |
| 21 | constructor ( |
| 22 | private $rootScope: ng.IScope, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 23 | private $log: ng.ILogService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 24 | private NavigationService: IXosNavigationService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 25 | private XosModelStore: IXosModelStoreService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 26 | private ConfigHelpers: IXosConfigHelpersService |
| 27 | ) { |
| 28 | this.$rootScope.$on('xos.core.modelSetup', () => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 29 | this.$log.info(`[XosSearchService] Loading views`); |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 30 | 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 Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 44 | this.$log.debug(`[XosSearchService] Views Loaded: `, this.states); |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 45 | }); |
| 46 | } |
| 47 | |
| 48 | public search(query: string): IXosSearchResult[] { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 49 | this.$log.info(`[XosSearchService] Searching for: ${query}`); |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 50 | 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 Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 56 | // 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 Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 59 | 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 | } |