1 import {Component, OnInit, AfterViewInit, Renderer2} from '@angular/core';
2 import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {CatalogService} from '@eg/share/catalog/catalog.service';
6 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
7 import {StaffCatalogService} from './catalog.service';
8 import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
10 // Maps opac-style default tab names to local tab names.
11 const LEGACY_TAB_NAME_MAP = {
18 selector: 'eg-catalog-search-form',
19 styleUrls: ['search-form.component.css'],
20 templateUrl: 'search-form.component.html'
22 export class SearchFormComponent implements OnInit, AfterViewInit {
24 context: CatalogSearchContext;
25 ccvmMap: {[ccvm: string]: IdlObject[]} = {};
26 cmfMap: {[cmf: string]: IdlObject} = {};
27 showSearchFilters = false;
28 copyLocations: IdlObject[];
31 // Display the full form if true, otherwise display the expandy.
35 private renderer: Renderer2,
36 private router: Router,
37 private route: ActivatedRoute,
38 private org: OrgService,
39 private cat: CatalogService,
40 private staffCat: StaffCatalogService
42 this.copyLocations = [];
44 // Some search scenarios, like rendering a search template,
45 // will not be searchable and thus not resovle to a specific
46 // search tab. Check to see if a specific tab is requested
48 this.route.queryParams.subscribe(params => {
49 if (params.searchTab) {
50 this.searchTab = params.searchTab;
54 this.router.events.subscribe(routeEvent => {
55 if (routeEvent instanceof NavigationEnd) {
56 if (routeEvent.url.match(/catalog\/record/)) {
57 this.showThyself = false;
59 this.showThyself = true;
66 this.ccvmMap = this.cat.ccvmMap;
67 this.cmfMap = this.cat.cmfMap;
68 this.context = this.staffCat.searchContext;
70 // Start with advanced search options open
71 // if any filters are active.
72 this.showSearchFilters = this.filtersActive();
76 // Query inputs are generated from search context data,
77 // so they are not available until after the first render.
78 // Search context data is extracted synchronously from the URL.
80 // Avoid changing the tab in the lifecycle hook thread.
83 if (this.context.identSearch.queryType === '') {
84 this.context.identSearch.queryType = 'identifier|isbn';
87 // Apply a tab if none was already specified
88 if (!this.searchTab) {
89 // Assumes that only one type of search will be searchable
91 if (this.context.marcSearch.isSearchable()) {
92 this.searchTab = 'marc';
93 } else if (this.context.identSearch.isSearchable()) {
94 this.searchTab = 'ident';
95 } else if (this.context.browseSearch.isSearchable()) {
96 this.searchTab = 'browse';
97 } else if (this.context.termSearch.isSearchable()) {
98 this.searchTab = 'term';
103 LEGACY_TAB_NAME_MAP[this.staffCat.defaultTab]
104 || this.staffCat.defaultTab || 'term';
108 if (this.searchTab === 'term') {
109 this.refreshCopyLocations();
113 this.focusTabInput();
117 onTabChange(evt: NgbTabChangeEvent) {
118 this.searchTab = evt.nextId;
120 // Focus after tab-change event has a chance to complete
121 // or the tab body and its input won't exist yet and no
122 // elements will be focus-able.
123 setTimeout(() => this.focusTabInput());
127 // Select a DOM node to focus when the tab changes.
128 let selector: string;
129 switch (this.searchTab) {
131 selector = '#ident-query-input';
134 selector = '#first-marc-tag';
137 selector = '#browse-term-input';
140 selector = '#cnbrowse-term-input';
143 this.refreshCopyLocations();
144 selector = '#first-query-input';
148 // TODO: sometime the selector is not available in the DOM
149 // until even later (even with setTimeouts). Need to fix this.
150 // Note the error is thrown from selectRootElement(), not the
151 // call to .focus() on a null reference.
152 this.renderer.selectRootElement(selector).focus();
157 * Display the advanced/extended search options when asked to
158 * or if any advanced options are selected.
160 showFilters(): boolean {
161 // Note that filters may become active due to external
162 // actions on the search context. Always show the filters
163 // if filter values are applied.
164 return this.showSearchFilters || this.filtersActive();
168 this.showSearchFilters = !this.showSearchFilters;
169 this.refreshCopyLocations();
172 filtersActive(): boolean {
174 if (this.context.termSearch.copyLocations[0] !== '') { return true; }
176 // ccvm filters may be present without any filters applied.
177 // e.g. if filters were applied then removed.
179 Object.keys(this.context.termSearch.ccvmFilters).forEach(ccvm => {
180 if (this.context.termSearch.ccvmFilters[ccvm][0] !== '') {
188 orgOnChange = (org: IdlObject): void => {
189 this.context.searchOrg = org;
190 this.refreshCopyLocations();
193 refreshCopyLocations() {
194 if (!this.showFilters()) { return; }
196 // TODO: is this how we avoid displaying too many locations?
197 const org = this.context.searchOrg;
198 if (org.id() === this.org.root().id()) {
199 this.copyLocations = [];
203 this.cat.fetchCopyLocations(org).then(() =>
204 this.copyLocations = this.cat.copyLocations
208 orgName(orgId: number): string {
209 return this.org.get(orgId).shortname();
212 addSearchRow(index: number): void {
213 this.context.termSearch.query.splice(index, 0, '');
214 this.context.termSearch.fieldClass.splice(index, 0, 'keyword');
215 this.context.termSearch.joinOp.splice(index, 0, '&&');
216 this.context.termSearch.matchOp.splice(index, 0, 'contains');
219 delSearchRow(index: number): void {
220 this.context.termSearch.query.splice(index, 1);
221 this.context.termSearch.fieldClass.splice(index, 1);
222 this.context.termSearch.joinOp.splice(index, 1);
223 this.context.termSearch.matchOp.splice(index, 1);
226 addMarcSearchRow(index: number): void {
227 this.context.marcSearch.tags.splice(index, 0, '');
228 this.context.marcSearch.subfields.splice(index, 0, '');
229 this.context.marcSearch.values.splice(index, 0, '');
232 delMarcSearchRow(index: number): void {
233 this.context.marcSearch.tags.splice(index, 1);
234 this.context.marcSearch.subfields.splice(index, 1);
235 this.context.marcSearch.values.splice(index, 1);
238 searchByForm(): void {
239 this.context.pager.offset = 0; // New search
241 // Form search overrides basket display
242 this.context.showBasket = false;
244 this.context.scrub(this.searchTab);
246 switch (this.searchTab) {
251 this.staffCat.search();
255 this.staffCat.browse();
259 this.staffCat.cnBrowse();
264 // https://stackoverflow.com/questions/42322968/angular2-dynamic-input-field-lose-focus-when-input-changes
265 trackByIdx(index: any, item: any) {
269 searchIsActive(): boolean {
270 return this.context.searchState === CatalogSearchState.SEARCHING;
273 // It's possible to chose invalid combos depending on the order of selection
274 preventBogusCombos(idx: number) {
275 if (this.context.termSearch.fieldClass[idx] === 'keyword') {
276 const op = this.context.termSearch.matchOp[idx];
277 if (op === 'exact' || op === 'starts') {
278 this.context.termSearch.matchOp[idx] = 'contains';