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