blob: 41e2f3e72bc9bdd5724d2fac462052d0c4ba97b5 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condonf4f54a12018-10-10 23:25:46 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Sean Condon87b78502018-09-17 20:53:24 +010016import { Component, Input, OnInit, OnDestroy, Inject, NgZone } from '@angular/core';
17import { Router } from '@angular/router';
Sean Condon83fc39f2018-04-19 18:56:13 +010018import { LionService } from '../../util/lion.service';
Sean Condon5ca00262018-09-06 17:55:25 +010019import { LogService } from '../../log.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010020import { NavService } from '../../nav/nav.service';
Sean Condon87b78502018-09-17 20:53:24 +010021import { WebSocketService } from '../../remote/websocket.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010022
23/**
24 * ONOS GUI -- Masthead Component
25 */
26@Component({
27 selector: 'onos-mast',
28 templateUrl: './mast.component.html',
29 styleUrls: ['./mast.component.css', './mast.theme.css']
30})
Sean Condon28ecc5f2018-06-25 12:50:16 +010031export class MastComponent implements OnInit, OnDestroy {
32 @Input() username: string;
33
34 lionFn; // Function
35 viewMap = new Map<string, string>([]); // A map of app names
Sean Condon87b78502018-09-17 20:53:24 +010036 confirmMessage: string = '';
37 strongWarning: string = '';
Sean Condon83fc39f2018-04-19 18:56:13 +010038
39 constructor(
Sean Condon28ecc5f2018-06-25 12:50:16 +010040 private lion: LionService,
Sean Condon83fc39f2018-04-19 18:56:13 +010041 private log: LogService,
42 public ns: NavService,
Sean Condon87b78502018-09-17 20:53:24 +010043 private wss: WebSocketService,
44 private router: Router,
45 private zone: NgZone,
Sean Condon5ca00262018-09-06 17:55:25 +010046 @Inject('Window') private window: any,
Sean Condon83fc39f2018-04-19 18:56:13 +010047 ) {
Sean Condon28ecc5f2018-06-25 12:50:16 +010048 this.viewMap.set('apps', 'https://wiki.onosproject.org/display/ONOS/GUI+Application+View');
49 this.viewMap.set('device', 'https://wiki.onosproject.org/display/ONOS/GUI+Device+View');
50 this.viewMap.set('', 'https://wiki.onosproject.org/display/ONOS/The+ONOS+Web+GUI');
Sean Condon83fc39f2018-04-19 18:56:13 +010051 }
52
53 ngOnInit() {
Sean Condon28ecc5f2018-06-25 12:50:16 +010054 if (this.lion.ubercache.length === 0) {
55 this.lionFn = this.dummyLion;
56 this.lion.loadCbs.set('mast', () => this.doLion());
57 this.log.debug('LION not available when MastComponent initialized');
58 } else {
59 this.doLion();
60 }
Sean Condon87b78502018-09-17 20:53:24 +010061
62 this.wss.bindHandlers(new Map<string, (data) => void>([
63 ['guiRemoved', (data) => this.triggerRefresh(data, false) ],
64 ['guiAdded', (data) => this.triggerRefresh(data, true) ]
65 ]));
Sean Condon83fc39f2018-04-19 18:56:13 +010066 this.log.debug('MastComponent initialized');
67 }
68
Sean Condon28ecc5f2018-06-25 12:50:16 +010069 /**
70 * Nav component should never be closed, but in case it does, it's
71 * safer to tidy up after itself
Sean Condon83fc39f2018-04-19 18:56:13 +010072 */
Sean Condon28ecc5f2018-06-25 12:50:16 +010073 ngOnDestroy() {
74 this.lion.loadCbs.delete('mast');
Sean Condon83fc39f2018-04-19 18:56:13 +010075 }
76
Sean Condon28ecc5f2018-06-25 12:50:16 +010077 /**
78 * Read the LION bundle for App and set up the lionFn
79 */
80 doLion() {
81 this.lionFn = this.lion.bundle('core.fw.Mast');
82 if (this.username === undefined) {
83 this.username = this.lionFn('unknown_user');
84 }
85 }
86
87 /**
88 * A dummy implementation of the lionFn until the response is received and the LION
89 * bundle is received from the WebSocket
90 */
91 dummyLion(key: string): string {
92 return '%' + key + '%';
93 }
94
95 directTo() {
96 const curId = this.window.location.pathname.replace('/', '');
97 let helpUrl: string = this.viewMap.get(curId);
98 if (helpUrl === undefined) {
99 helpUrl = this.viewMap.get('');
100 this.log.warn('No help file linked for view:', curId);
101 }
102 this.window.open(helpUrl);
103 }
Sean Condon87b78502018-09-17 20:53:24 +0100104
105 triggerRefresh(data: any, added: boolean): void {
106 this.confirmMessage = this.lionFn(added ? 'uicomp_added' : 'uicomp_removed');
107 this.log.debug('Refresh has been triggered - item', added ? 'added' : 'removed', ' - ', data);
108 }
109
110 /**
111 * Callback when the Confirm dialog is shown and a choice is made
112 */
113 dOk(choice: boolean) {
114 if (choice) {
115 this.ns.getUiViews();
116 this.router.navigate(['/']);
117 this.zone.runOutsideAngular(() => {
118 location.reload();
119 });
120 this.log.debug('Refresh confirmed'); // Will not be printed if page reloads
121
122 } else {
123 this.log.debug('Refresh cancelled');
124 }
125 this.confirmMessage = '';
126 this.strongWarning = '';
127 }
Sean Condon83fc39f2018-04-19 18:56:13 +0100128}