Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 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'; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 18 | import * as d3 from 'd3'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 19 | import { |
| 20 | LionService, |
| 21 | LogService, |
| 22 | ThemeService, |
| 23 | GlyphService, |
| 24 | WebSocketService, |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 25 | WsOptions, |
| 26 | KeysService |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 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 | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 84 | private ks: KeysService, |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 85 | public wss: WebSocketService, |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 86 | private log: LogService, |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 87 | public onos: OnosService |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 88 | ) { |
| 89 | |
| 90 | // This is not like onos.js of AngularJS 1.x In this new structure modules are |
| 91 | // imported instead in the OnosModule. view dependencies should be there too |
| 92 | // const moduleDependencies = coreDependencies.concat(this.viewDependencies); |
| 93 | |
| 94 | // Testing of debugging |
| 95 | log.debug('OnosComponent: testing logger.debug()'); |
| 96 | log.info('OnosComponent: testing logger.info()'); |
| 97 | log.warn('OnosComponent: testing logger.warn()'); |
| 98 | log.error('OnosComponent: testing logger.error()'); |
| 99 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 100 | this.wss.createWebSocket(<WsOptions>{ wsport: 8181}); |
| 101 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 102 | log.debug('OnosComponent constructed'); |
| 103 | } |
| 104 | |
| 105 | ngOnInit() { |
| 106 | this.viewMap.forEach(view => |
| 107 | this.viewDependencies.push('ov' + cap(view.id))); |
| 108 | |
| 109 | this.onos.viewMap = this.viewMap; |
| 110 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 111 | // TODO: Enable this this.saucy(this.ee, this.ks); |
| 112 | this.log.debug('OnosComponent initialized'); |
| 113 | } |
| 114 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 115 | /** |
| 116 | * Start the listener for keystrokes for QuickHelp |
| 117 | * |
| 118 | * This creates an observable that listens to the '/','\' and Esc keystrokes |
| 119 | * anywhere in the web page - it strips the keyCode out of the keystroke |
| 120 | * and passes this to another observable that filters only for these keystrokes |
| 121 | * and finally maps these key code to text literals to drive the |
| 122 | * quick help feature |
| 123 | */ |
| 124 | ngAfterViewInit() { |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 125 | // const keyStrokeHandler = |
| 126 | // fromEvent(document, 'keyup').pipe(map((x: KeyboardEvent) => x.keyCode)); |
| 127 | // this.quickHelpHandler = keyStrokeHandler.pipe( |
| 128 | // filter(x => { |
| 129 | // return [27, 191, 220].includes(x); |
| 130 | // }) |
| 131 | // ).pipe( |
| 132 | // map(x => { |
| 133 | // let direction; |
| 134 | // switch (x) { |
| 135 | // case 27: |
| 136 | // direction = 'esc'; |
| 137 | // break; |
| 138 | // case 191: |
| 139 | // direction = 'fwdslash'; |
| 140 | // break; |
| 141 | // case 220: |
| 142 | // direction = 'backslash'; |
| 143 | // break; |
| 144 | // default: |
| 145 | // direction = 'esc'; |
| 146 | // } |
| 147 | // return direction; |
| 148 | // }) |
| 149 | // ); |
| 150 | // |
| 151 | // // TODO: Make a Quick Help component popup |
| 152 | // this.quickHelpSub = this.quickHelpHandler.subscribe((keyname) => { |
| 153 | // this.log.debug('Keystroke', keyname); |
| 154 | // }); |
| 155 | this.ks.installOn(d3.select('body')); |
| 156 | this.log.debug('OnosComponent after view initialized'); |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 159 | ngOnDestroy() { |
| 160 | if (this.wss.isConnected()) { |
| 161 | this.log.debug('Stopping Web Socket connection'); |
| 162 | this.wss.closeWebSocket(); |
| 163 | } |
| 164 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 165 | this.quickHelpSub.unsubscribe(); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 166 | this.log.debug('OnosComponent destroyed'); |
| 167 | } |
| 168 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 169 | saucy(ee, ks) { |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 170 | const map1 = ee.genMap(sauce); |
| 171 | Object.keys(map1).forEach(function (k) { |
| 172 | ks.addSeq(k, map1[k]); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 173 | }); |
| 174 | } |
| 175 | } |