]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/splash.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / splash.component.ts
1 import {Component, OnInit, AfterViewInit, Directive, ElementRef, Renderer2, ViewChild} from '@angular/core';
2 import {OrgService} from '@eg/core/org.service';
3 import {AuthService} from '@eg/core/auth.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {StringComponent} from '@eg/share/string/string.component';
7 import {Router} from '@angular/router';
8
9 @Component({
10     templateUrl: 'splash.component.html'
11 })
12
13 export class StaffSplashComponent implements OnInit {
14
15     @ViewChild('noPermissionString', { static: true }) noPermissionString: StringComponent;
16     catSearchQuery: string;
17     portalEntries: any[][] = [];
18     portalHeaders: any[] = [];
19
20     constructor(
21         private renderer: Renderer2,
22         private pcrud: PcrudService,
23         private auth: AuthService,
24         private org: OrgService,
25         private router: Router,
26         private toast: ToastService
27     ) {}
28
29     ngOnInit() {
30         const tmpPortalEntries: any[][] = [];
31         const wsAncestors = this.org.ancestors(this.auth.user().ws_ou(), true);
32         this.pcrud.search('cusppe', {owner: wsAncestors}).subscribe(
33             item => {
34                 const page_col = item.page_col();
35                 if (tmpPortalEntries[page_col] === undefined) {
36                     tmpPortalEntries[page_col] = [];
37                 }
38                 if (tmpPortalEntries[page_col][item.col_pos()] === undefined) {
39                     tmpPortalEntries[page_col][item.col_pos()] = [];
40                 }
41                 // we push here, then flatten the results when we filter
42                 // by owner later because (page_col, col_pos) is not
43                 // guaranteed to be unique
44                 tmpPortalEntries[page_col][item.col_pos()].push(item);
45             },
46             err => {},
47             () => {
48                 // find the first set of entries belonging to the
49                 // workstation OU or one of its ancestors
50                 let filteredPortalEntries: any[][] = [];
51                 let foundMatch = false;
52                 for (const ou of wsAncestors) {
53                     tmpPortalEntries.forEach((col) => {
54                         if (col !== undefined) {
55                             const filtered = col.reduce((prev, curr) => prev.concat(curr), [])
56                                                 .filter(x => x !== undefined)
57                                                 .filter(x => ou === x.owner());
58                             if (filtered.length) {
59                                 foundMatch = true;
60                                 filteredPortalEntries.push(filtered);
61                             }
62                         }
63                     });
64                     if (foundMatch) {
65                         break;
66                     } else {
67                         filteredPortalEntries = [];
68                     }
69                 }
70
71                 // munge the results so that we don't need to
72                 // care if there are gaps in the page_col or col_pos
73                 // sequences
74                 filteredPortalEntries.forEach((col) => {
75                     if (col !== undefined) {
76                         const filtered = col.filter(x => x !== undefined);
77                         this.portalEntries.push(filtered);
78                         filtered.forEach((entry) => {
79                             if (entry.entry_type() === 'header') {
80                                 this.portalHeaders[this.portalEntries.length - 1] = entry;
81                             }
82                         });
83                     }
84                 });
85                 // supply an empty header entry in case a column was
86                 // defined without a header
87                 this.portalEntries.forEach((col, i) => {
88                     if (this.portalHeaders.length <= i) {
89                         this.portalHeaders[i] = undefined;
90                     }
91                 });
92             }
93         );
94
95         if (this.router.url === '/staff/no_permission') {
96             this.noPermissionString.current()
97                 .then(str => {
98                     this.toast.danger(str);
99                     this.router.navigate(['/staff']);
100                 });
101         }
102     }
103
104     searchCatalog(): void {
105         if (!this.catSearchQuery) { return; }
106
107         this.router.navigate(
108             ['/staff/catalog/search'],
109             {queryParams: {query : this.catSearchQuery}}
110         );
111     }
112 }
113
114 @Directive({
115     selector: '[egAutofocus]'
116 })
117 export class AutofocusDirective implements AfterViewInit {
118     constructor(private host: ElementRef) {}
119
120     ngAfterViewInit() {
121         this.host.nativeElement.focus();
122     }
123 }