]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/search-form.component.ts
LP#1775466 Angular(6) base application
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / search-form.component.ts
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';
7
8 @Component({
9   selector: 'eg-catalog-search-form',
10   styleUrls: ['search-form.component.css'],
11   templateUrl: 'search-form.component.html'
12 })
13 export class SearchFormComponent implements OnInit, AfterViewInit {
14
15     searchContext: CatalogSearchContext;
16     ccvmMap: {[ccvm: string]: IdlObject[]} = {};
17     cmfMap: {[cmf: string]: IdlObject} = {};
18     showAdvancedSearch = false;
19
20     constructor(
21         private renderer: Renderer2,
22         private org: OrgService,
23         private cat: CatalogService,
24         private staffCat: StaffCatalogService
25     ) {}
26
27     ngOnInit() {
28         this.ccvmMap = this.cat.ccvmMap;
29         this.cmfMap = this.cat.cmfMap;
30         this.searchContext = this.staffCat.searchContext;
31
32         // Start with advanced search options open
33         // if any filters are active.
34         this.showAdvancedSearch = this.hasAdvancedOptions();
35
36     }
37
38     ngAfterViewInit() {
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.
42
43         if (this.searchContext.identQuery) {
44             // Focus identifier query input if identQuery is in progress
45             this.renderer.selectRootElement('#ident-query-input').focus();
46         } else {
47             // Otherwise focus the main query input
48             this.renderer.selectRootElement('#first-query-input').focus();
49         }
50     }
51
52     /**
53      * Display the advanced/extended search options when asked to
54      * or if any advanced options are selected.
55      */
56     showAdvanced(): boolean {
57         return this.showAdvancedSearch;
58     }
59
60     hasAdvancedOptions(): boolean {
61         // ccvm filters may be present without any filters applied.
62         // e.g. if filters were applied then removed.
63         let show = false;
64         Object.keys(this.searchContext.ccvmFilters).forEach(ccvm => {
65             if (this.searchContext.ccvmFilters[ccvm][0] !== '') {
66                 show = true;
67             }
68         });
69
70         if (this.searchContext.identQuery) {
71             show = true;
72         }
73
74         return show;
75     }
76
77     orgOnChange = (org: IdlObject): void => {
78         this.searchContext.searchOrg = org;
79     }
80
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');
86     }
87
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);
93     }
94
95     formEnter(source) {
96         this.searchContext.pager.offset = 0;
97
98         switch (source) {
99
100             case 'query': // main search form query input
101
102                 // Be sure a previous ident search does not take precedence
103                 // over the newly entered/submitted search query
104                 this.searchContext.identQuery = null;
105                 break;
106
107             case 'ident': // identifier query input
108                 const iq = this.searchContext.identQuery;
109                 const qt = this.searchContext.identQueryType;
110                 if (iq) {
111                     // Ident queries ignore search-specific filters.
112                     this.searchContext.reset();
113                     this.searchContext.identQuery = iq;
114                     this.searchContext.identQueryType = qt;
115                 }
116                 break;
117         }
118
119         this.searchByForm();
120     }
121
122     // https://stackoverflow.com/questions/42322968/angular2-dynamic-input-field-lose-focus-when-input-changes
123     trackByIdx(index: any, item: any) {
124        return index;
125     }
126
127     searchByForm(): void {
128         this.staffCat.search();
129     }
130
131     searchIsActive(): boolean {
132         return this.searchContext.searchState === CatalogSearchState.SEARCHING;
133     }
134
135 }
136
137