Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 3 | * |
| 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 Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 16 | import { Component, OnInit, AfterViewInit, OnDestroy } from '@angular/core'; |
| 17 | import { Observable, Subscription, fromEvent } from 'rxjs'; |
| 18 | import { map, filter } from 'rxjs/operators'; |
| 19 | |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 20 | import { |
| 21 | LionService, |
| 22 | LogService, |
| 23 | ThemeService, |
| 24 | GlyphService, |
| 25 | WebSocketService, |
| 26 | WsOptions |
| 27 | } from 'gui2-fw-lib'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 28 | import { OnosService, View } from './onos.service'; |
| 29 | |
| 30 | // secret sauce |
| 31 | const sauce: string[] = [ |
| 32 | '6:69857666', |
| 33 | '9:826970', |
| 34 | '22:8069828667', |
| 35 | '6:698570688669887967', |
| 36 | '7:6971806889847186', |
| 37 | '22:8369867682', |
| 38 | '13:736583', |
| 39 | '7:667186698384', |
| 40 | '1:857780888778876787', |
| 41 | '20:70717066', |
| 42 | '24:886774868469', |
| 43 | '17:7487696973687580739078', |
| 44 | '14:70777086', |
| 45 | '17:7287687967', |
| 46 | '11:65678869706783687184', |
| 47 | '1:80777778', |
| 48 | '9:72696982', |
| 49 | '7:857165828967', |
| 50 | '11:8867696882678869759071' |
| 51 | // Add more sauce... |
| 52 | ]; |
| 53 | |
| 54 | function cap(s: string): string { |
| 55 | return s ? s[0].toUpperCase() + s.slice(1) : s; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * The main ONOS Component - the root of the whole user interface |
| 60 | */ |
| 61 | @Component({ |
| 62 | selector: 'onos-root', |
| 63 | templateUrl: './onos.component.html', |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 64 | styleUrls: ['./onos.component.css', './onos.common.css'] |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 65 | }) |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 66 | export class OnosComponent implements OnInit, AfterViewInit, OnDestroy { |
| 67 | private quickHelpSub: Subscription; |
| 68 | private quickHelpHandler: Observable<string>; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 69 | |
| 70 | // view ID to help page url map.. injected via the servlet |
| 71 | viewMap: View[] = [ |
| 72 | // {INJECTED-VIEW-DATA-START} |
| 73 | // {INJECTED-VIEW-DATA-END} |
| 74 | ]; |
| 75 | |
| 76 | defaultView = 'topo'; |
| 77 | // TODO Move this to OnosModule - warning servlet will have to be updated too |
| 78 | viewDependencies: string[] = []; |
| 79 | |
| 80 | constructor ( |
| 81 | private lion: LionService, |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 82 | private ts: ThemeService, |
| 83 | private gs: GlyphService, |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 84 | public wss: WebSocketService, |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 85 | private log: LogService, |
| 86 | private onos: OnosService |
| 87 | ) { |
| 88 | |
| 89 | // This is not like onos.js of AngularJS 1.x In this new structure modules are |
| 90 | // imported instead in the OnosModule. view dependencies should be there too |
| 91 | // const moduleDependencies = coreDependencies.concat(this.viewDependencies); |
| 92 | |
| 93 | // Testing of debugging |
| 94 | log.debug('OnosComponent: testing logger.debug()'); |
| 95 | log.info('OnosComponent: testing logger.info()'); |
| 96 | log.warn('OnosComponent: testing logger.warn()'); |
| 97 | log.error('OnosComponent: testing logger.error()'); |
| 98 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 99 | this.wss.createWebSocket(<WsOptions>{ wsport: 8181}); |
| 100 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 101 | log.debug('OnosComponent constructed'); |
| 102 | } |
| 103 | |
| 104 | ngOnInit() { |
| 105 | this.viewMap.forEach(view => |
| 106 | this.viewDependencies.push('ov' + cap(view.id))); |
| 107 | |
| 108 | this.onos.viewMap = this.viewMap; |
| 109 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 110 | // TODO: Enable this this.saucy(this.ee, this.ks); |
| 111 | this.log.debug('OnosComponent initialized'); |
| 112 | } |
| 113 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 114 | /** |
| 115 | * Start the listener for keystrokes for QuickHelp |
| 116 | * |
| 117 | * This creates an observable that listens to the '/','\' and Esc keystrokes |
| 118 | * anywhere in the web page - it strips the keyCode out of the keystroke |
| 119 | * and passes this to another observable that filters only for these keystrokes |
| 120 | * and finally maps these key code to text literals to drive the |
| 121 | * quick help feature |
| 122 | */ |
| 123 | ngAfterViewInit() { |
| 124 | const keyStrokeHandler = |
| 125 | fromEvent(document, 'keyup').pipe(map((x: KeyboardEvent) => x.keyCode)); |
| 126 | this.quickHelpHandler = keyStrokeHandler.pipe( |
| 127 | filter(x => { |
| 128 | return [27, 191, 220].includes(x); |
| 129 | }) |
| 130 | ).pipe( |
| 131 | map(x => { |
| 132 | let direction; |
| 133 | switch (x) { |
| 134 | case 27: |
| 135 | direction = 'esc'; |
| 136 | break; |
| 137 | case 191: |
| 138 | direction = 'fwdslash'; |
| 139 | break; |
| 140 | case 220: |
| 141 | direction = 'backslash'; |
| 142 | break; |
| 143 | default: |
| 144 | direction = 'esc'; |
| 145 | } |
| 146 | return direction; |
| 147 | }) |
| 148 | ); |
| 149 | |
| 150 | // TODO: Make a Quick Help component popup |
| 151 | this.quickHelpSub = this.quickHelpHandler.subscribe((keyname) => { |
| 152 | this.log.debug('Keystroke', keyname); |
| 153 | }); |
| 154 | } |
| 155 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 156 | ngOnDestroy() { |
| 157 | if (this.wss.isConnected()) { |
| 158 | this.log.debug('Stopping Web Socket connection'); |
| 159 | this.wss.closeWebSocket(); |
| 160 | } |
| 161 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 162 | this.quickHelpSub.unsubscribe(); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 163 | this.log.debug('OnosComponent destroyed'); |
| 164 | } |
| 165 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 166 | saucy(ee, ks) { |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 167 | const map1 = ee.genMap(sauce); |
| 168 | Object.keys(map1).forEach(function (k) { |
| 169 | ks.addSeq(k, map1[k]); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 170 | }); |
| 171 | } |
| 172 | } |