1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {StaffCatalogService} from './catalog.service';
4 import {ServerStoreService} from '@eg/core/server-store.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {StringComponent} from '@eg/share/string/string.component';
7 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
9 /* Component for managing catalog preferences */
11 const CATALOG_PREFS = [
12 'eg.search.search_lib',
15 'eg.catalog.results.count',
16 'eg.staffcat.exclude_electronic'
20 templateUrl: 'prefs.component.html'
22 export class PreferencesComponent implements OnInit {
24 settings: Object = {};
26 @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
27 @ViewChild('failMsg', {static: false}) failMsg: StringComponent;
30 private store: ServerStoreService,
31 private toast: ToastService,
32 private staffCat: StaffCatalogService,
36 this.staffCat.createContext();
38 // Pre-fetched by the resolver.
39 this.store.getItemBatch(CATALOG_PREFS)
40 .then(settings => this.settings = settings);
43 orgChanged(org: IdlObject, setting: string) {
44 const localVar = setting === 'eg.search.search_lib' ?
45 'defaultSearchOrg' : 'prefOrg';
47 this.updateValue(setting, org ? org.id() : null)
48 .then(val => this.staffCat[localVar] = val);
51 paneChanged(entry: ComboboxEntry) {
52 this.updateValue('eg.search.adv_pane', entry ? entry.id : null)
53 .then(value => this.staffCat.defaultTab = value);
57 this.updateValue('eg.catalog.results.count',
58 this.settings['eg.catalog.results.count'] || null)
60 this.staffCat.searchContext.pager.limit = value || 20;
64 checkboxChanged(setting: string) {
65 const value = this.settings[setting];
66 this.updateValue(setting, value || null);
68 if (setting === 'eg.staffcat.exclude_electronic') {
69 this.staffCat.showExcludeElectronic = value;
73 updateValue(setting: string, value: any): Promise<any> {
74 const promise = (value === null) ?
75 this.store.removeItem(setting) :
76 this.store.setItem(setting, value);
79 .then(_ => this.toast.success(this.successMsg.text))