1 import {Component, OnInit, AfterViewInit, Renderer2} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {OrgService} from '@eg/core/org.service';
4 import {CatalogService} from '@eg/share/catalog/catalog.service';
5 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
6 import {StaffCatalogService} from './catalog.service';
9 selector: 'eg-catalog-search-form',
10 styleUrls: ['search-form.component.css'],
11 templateUrl: 'search-form.component.html'
13 export class SearchFormComponent implements OnInit, AfterViewInit {
15 searchContext: CatalogSearchContext;
16 ccvmMap: {[ccvm: string]: IdlObject[]} = {};
17 cmfMap: {[cmf: string]: IdlObject} = {};
18 showAdvancedSearch = false;
21 private renderer: Renderer2,
22 private org: OrgService,
23 private cat: CatalogService,
24 private staffCat: StaffCatalogService
28 this.ccvmMap = this.cat.ccvmMap;
29 this.cmfMap = this.cat.cmfMap;
30 this.searchContext = this.staffCat.searchContext;
32 // Start with advanced search options open
33 // if any filters are active.
34 this.showAdvancedSearch = this.hasAdvancedOptions();
39 // Query inputs are generated from search context data,
40 // so they are not available until after the first render.
41 // Search context data is extracted synchronously from the URL.
43 if (this.searchContext.identQuery) {
44 // Focus identifier query input if identQuery is in progress
45 this.renderer.selectRootElement('#ident-query-input').focus();
47 // Otherwise focus the main query input
48 this.renderer.selectRootElement('#first-query-input').focus();
53 * Display the advanced/extended search options when asked to
54 * or if any advanced options are selected.
56 showAdvanced(): boolean {
57 return this.showAdvancedSearch;
60 hasAdvancedOptions(): boolean {
61 // ccvm filters may be present without any filters applied.
62 // e.g. if filters were applied then removed.
64 Object.keys(this.searchContext.ccvmFilters).forEach(ccvm => {
65 if (this.searchContext.ccvmFilters[ccvm][0] !== '') {
70 if (this.searchContext.identQuery) {
77 orgOnChange = (org: IdlObject): void => {
78 this.searchContext.searchOrg = org;
81 addSearchRow(index: number): void {
82 this.searchContext.query.splice(index, 0, '');
83 this.searchContext.fieldClass.splice(index, 0, 'keyword');
84 this.searchContext.joinOp.splice(index, 0, '&&');
85 this.searchContext.matchOp.splice(index, 0, 'contains');
88 delSearchRow(index: number): void {
89 this.searchContext.query.splice(index, 1);
90 this.searchContext.fieldClass.splice(index, 1);
91 this.searchContext.joinOp.splice(index, 1);
92 this.searchContext.matchOp.splice(index, 1);
96 this.searchContext.pager.offset = 0;
100 case 'query': // main search form query input
102 // Be sure a previous ident search does not take precedence
103 // over the newly entered/submitted search query
104 this.searchContext.identQuery = null;
107 case 'ident': // identifier query input
108 const iq = this.searchContext.identQuery;
109 const qt = this.searchContext.identQueryType;
111 // Ident queries ignore search-specific filters.
112 this.searchContext.reset();
113 this.searchContext.identQuery = iq;
114 this.searchContext.identQueryType = qt;
122 // https://stackoverflow.com/questions/42322968/angular2-dynamic-input-field-lose-focus-when-input-changes
123 trackByIdx(index: any, item: any) {
127 searchByForm(): void {
128 this.staffCat.search();
131 searchIsActive(): boolean {
132 return this.searchContext.searchState === CatalogSearchState.SEARCHING;