blob: 66748b1666bbdeaac495300466d136685b589e8d [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolo86bc26a2017-01-18 11:06:47 -080019import * as _ from 'lodash';
20import {IXosNavigationService} from '../../core/services/navigation';
Matteo Scandolo47860fe2017-02-02 12:05:55 -080021import {IXosModelStoreService} from '../stores/model.store';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080022import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080023import {IXosState} from '../../core/services/runtime-states';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080024
25export interface IXosSearchResult {
26 label: string;
27 state: string | {name: string, params: any};
28 type?: string;
29}
30
31export interface IXosSearchService {
32 search(query: string): IXosSearchResult[];
33}
34
35export class SearchService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080036 static $inject = ['$rootScope', '$log', 'XosNavigationService', 'XosModelStore', 'ConfigHelpers'];
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080037 private states: IXosState[];
38
39 constructor (
40 private $rootScope: ng.IScope,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080041 private $log: ng.ILogService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080042 private NavigationService: IXosNavigationService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080043 private XosModelStore: IXosModelStoreService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080044 private ConfigHelpers: IXosConfigHelpersService
45 ) {
46 this.$rootScope.$on('xos.core.modelSetup', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080047 this.$log.info(`[XosSearchService] Loading views`);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080048 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 Scandolo9b460042017-04-14 16:24:45 -070062 this.$log.debug(`[XosSearchService] States: `, this.states);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080063 });
64 }
65
66 public search(query: string): IXosSearchResult[] {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080067 this.$log.debug(`[XosSearchService] Searching for: ${query}`);
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080068 const routes: IXosSearchResult[] = _.filter(this.states, s => {
69 return s.label.toLowerCase().indexOf(query) > -1;
70 }).map(r => {
71 r.type = 'View';
72 return r;
73 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080074 // 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 Scandolo86bc26a2017-01-18 11:06:47 -080077 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}