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';
10 templateUrl: 'splash.component.html'
13 export class StaffSplashComponent implements OnInit {
15 @ViewChild('noPermissionString', { static: true }) noPermissionString: StringComponent;
16 catSearchQuery: string;
17 portalEntries: any[][] = [];
18 portalHeaders: any[] = [];
21 private renderer: Renderer2,
22 private pcrud: PcrudService,
23 private auth: AuthService,
24 private org: OrgService,
25 private router: Router,
26 private toast: ToastService
30 const tmpPortalEntries: any[][] = [];
31 const wsAncestors = this.org.ancestors(this.auth.user().ws_ou(), true);
32 this.pcrud.search('cusppe', {owner: wsAncestors}).subscribe(
34 const page_col = item.page_col();
35 if (tmpPortalEntries[page_col] === undefined) {
36 tmpPortalEntries[page_col] = [];
38 if (tmpPortalEntries[page_col][item.col_pos()] === undefined) {
39 tmpPortalEntries[page_col][item.col_pos()] = [];
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);
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) {
60 filteredPortalEntries.push(filtered);
67 filteredPortalEntries = [];
71 // munge the results so that we don't need to
72 // care if there are gaps in the page_col or col_pos
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;
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;
95 if (this.router.url === '/staff/no_permission') {
96 this.noPermissionString.current()
98 this.toast.danger(str);
99 this.router.navigate(['/staff']);
104 searchCatalog(): void {
105 if (!this.catSearchQuery) { return; }
107 this.router.navigate(
108 ['/staff/catalog/search'],
109 {queryParams: {query : this.catSearchQuery}}
115 selector: '[egAutofocus]'
117 export class AutofocusDirective implements AfterViewInit {
118 constructor(private host: ElementRef) {}
121 this.host.nativeElement.focus();