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