]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/prefs.component.ts
LP1859706 Map Angular cat "Patron View" to AngJS "OPAC View"
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / prefs.component.ts
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';
8
9 /* Component for managing catalog preferences */
10
11 const CATALOG_PREFS = [
12     'eg.search.search_lib',
13     'eg.search.pref_lib',
14     'eg.search.adv_pane',
15     'eg.catalog.results.count'
16 ];
17
18 @Component({
19   templateUrl: 'prefs.component.html'
20 })
21 export class PreferencesComponent implements OnInit {
22
23     settings: Object = {};
24
25     @ViewChild('successMsg', {static: false}) successMsg: StringComponent;
26     @ViewChild('failMsg', {static: false}) failMsg: StringComponent;
27
28     constructor(
29         private store: ServerStoreService,
30         private toast: ToastService,
31         private staffCat: StaffCatalogService,
32     ) {}
33
34     ngOnInit() {
35         this.staffCat.createContext();
36
37         // Pre-fetched by the resolver.
38         this.store.getItemBatch(CATALOG_PREFS)
39         .then(settings => this.settings = settings);
40     }
41
42     orgChanged(org: IdlObject, setting: string) {
43         const localVar = setting === 'eg.search.search_lib' ?
44             'defaultSearchOrg' : 'prefOrg';
45
46         this.updateValue(setting, org ? org.id() : null)
47         .then(val => this.staffCat[localVar] = val);
48     }
49
50     paneChanged(entry: ComboboxEntry) {
51         this.updateValue('eg.search.adv_pane', entry ? entry.id : null)
52         .then(value => this.staffCat.defaultTab = value);
53     }
54
55     countChanged() {
56         this.updateValue('eg.catalog.results.count',
57             this.settings['eg.catalog.results.count'] || null)
58         .then(value => {
59             this.staffCat.searchContext.pager.limit = value || 20;
60         });
61     }
62
63     updateValue(setting: string, value: any): Promise<any> {
64         const promise = (value === null) ?
65             this.store.removeItem(setting) :
66             this.store.setItem(setting, value);
67
68         return promise
69             .then(_ => this.toast.success(this.successMsg.text))
70             .then(_ => value);
71     }
72 }
73