/
header.component.ts
161 lines (125 loc) · 4.62 KB
/
header.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Component, OnDestroy, OnInit } from '@angular/core';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { MatIconRegistry } from '@angular/material/icon';
import { DomSanitizer } from '@angular/platform-browser';
import { NavigationStart, Router } from '@angular/router';
import { DspMessageData, SearchParams, SessionService } from '@dasch-swiss/dsp-ui';
import { Subscription } from 'rxjs';
import { DialogComponent } from 'src/app/main/dialog/dialog.component';
import { ComponentCommunicationEventService, Events } from 'src/app/main/services/component-communication-event.service';
const { version: appVersion } = require('../../../../package.json');
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit, OnDestroy {
session = false;
show = false;
searchParams: SearchParams;
appVersion: string = 'v' + appVersion;
successMessage: DspMessageData = {
status: 200,
statusText: 'Login successful'
};
showMessage = false;
componentCommsSubscription: Subscription;
constructor(
private _session: SessionService,
private _domSanitizer: DomSanitizer,
private _matIconRegistry: MatIconRegistry,
private _dialog: MatDialog,
private _router: Router,
private _componentCommsService: ComponentCommunicationEventService) {
// create tool icons to use them in mat-icons
this._matIconRegistry.addSvgIcon(
'dasch_icon_black',
this._domSanitizer.bypassSecurityTrustResourceUrl('/assets/images/dasch-icon-black.svg')
);
// logged-in user? show user menu or login button
this._router.events.forEach((event) => {
// console.log('EVENT', event);
if (event instanceof NavigationStart) {
this._session.isSessionValid().subscribe((response) => {
this.session = response;
});
}
});
}
ngOnInit() {
this.componentCommsSubscription = this._componentCommsService.on(
Events.loginSuccess, () => this.showMessage = true);
}
ngOnDestroy() {
// unsubscribe from the ValueOperationEventService when component is destroyed
if (this.componentCommsSubscription !== undefined) {
this.componentCommsSubscription.unsubscribe();
}
}
/**
* navigate to the login page
*/
goToLogin() {
// console.log(decodeURI(this._router.url));
this._router.navigate(['login'], {
queryParams: {
returnUrl: decodeURI(this._router.url)
}
});
}
/**
* show or hide search bar in phone version
*/
showSearchBar() {
this.show = !this.show;
}
doSearch(search: SearchParams) {
// reset search params
this.searchParams = undefined;
// we can do the routing here or send the search param
// to (resource) list view directly
this.searchParams = search;
if (this.searchParams.mode && this.searchParams.query) {
let doSearchRoute = '/search/' + this.searchParams.mode + '/' + encodeURIComponent(this.searchParams.query);
if (this.searchParams.filter && this.searchParams.filter.limitToProject) {
doSearchRoute += '/' + encodeURIComponent(this.searchParams.filter.limitToProject);
}
this._router.navigate([doSearchRoute]);
}
}
openDialog(mode: string, name?: string, iri?: string): void {
const dialogConfig: MatDialogConfig = {
width: '840px',
maxHeight: '80vh',
position: {
top: '112px'
},
data: { mode: mode, title: name, id: iri }
};
const dialogRef = this._dialog.open(
DialogComponent,
dialogConfig
);
dialogRef.afterClosed().subscribe(() => {
// do something
});
}
openNewResourceForm(mode: string, name?: string): void {
const dialogConfig: MatDialogConfig = {
width: '840px',
maxHeight: '80vh',
position: {
top: '112px'
},
data: { mode: mode, title: 'New resource', subtitle: 'Create new resource' },
disableClose: true
};
const dialogRef = this._dialog.open(
DialogComponent,
dialogConfig
);
dialogRef.afterClosed().subscribe(() => {
// do something
});
}
}