Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 19 | import * as _ from 'lodash'; |
| 20 | import {IXosNavigationService} from '../../core/services/navigation'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 21 | import {IXosModelStoreService} from '../stores/model.store'; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 22 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 23 | import {IXosState} from '../../core/services/runtime-states'; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 24 | |
| 25 | export interface IXosSearchResult { |
| 26 | label: string; |
| 27 | state: string | {name: string, params: any}; |
| 28 | type?: string; |
| 29 | } |
| 30 | |
| 31 | export interface IXosSearchService { |
| 32 | search(query: string): IXosSearchResult[]; |
| 33 | } |
| 34 | |
| 35 | export class SearchService { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 36 | static $inject = ['$rootScope', '$log', 'XosNavigationService', 'XosModelStore', 'ConfigHelpers']; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 37 | private states: IXosState[]; |
| 38 | |
| 39 | constructor ( |
| 40 | private $rootScope: ng.IScope, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 41 | private $log: ng.ILogService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 42 | private NavigationService: IXosNavigationService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 43 | private XosModelStore: IXosModelStoreService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 44 | private ConfigHelpers: IXosConfigHelpersService |
| 45 | ) { |
| 46 | this.$rootScope.$on('xos.core.modelSetup', () => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 47 | this.$log.info(`[XosSearchService] Loading views`); |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 48 | this.states = this.NavigationService.query().reduce((list, state) => { |
| 49 | // if it does not have child (otherwise it is abstract) |
| 50 | if (!state.children || state.children.length === 0) { |
| 51 | list.push(state); |
| 52 | } |
| 53 | // else push child |
| 54 | if (state.children && state.children.length > 0) { |
| 55 | state.children.forEach(c => { |
| 56 | list.push(c); |
| 57 | }); |
| 58 | } |
| 59 | return list; |
| 60 | }, []); |
| 61 | this.states = _.uniqBy(this.states, 'state'); |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 62 | this.$log.debug(`[XosSearchService] States: `, this.states); |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 63 | }); |
| 64 | } |
| 65 | |
| 66 | public search(query: string): IXosSearchResult[] { |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 67 | this.$log.debug(`[XosSearchService] Searching for: ${query}`); |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 68 | const routes: IXosSearchResult[] = _.filter(this.states, s => { |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 69 | return s.label.toLowerCase().indexOf(query.toLowerCase()) > -1; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 70 | }).map(r => { |
| 71 | r.type = 'View'; |
| 72 | return r; |
| 73 | }); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 74 | // TODO XosModelStore.search throws an error, |
| 75 | // probably there is something wrong saved in the cache!! |
| 76 | const models = _.map(this.XosModelStore.search(query), m => { |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 77 | return { |
| 78 | label: m.humanReadableName ? m.humanReadableName : m.name, |
| 79 | state: this.ConfigHelpers.stateWithParamsForJs(m.modelName, m), |
| 80 | type: m.modelName |
| 81 | }; |
| 82 | }); |
| 83 | return routes.concat(models); |
| 84 | } |
| 85 | } |