Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | |
| 17 | import * as $ from 'jquery'; |
| 18 | import {IXosKeyboardShortcutService} from '../../core/services/keyboard-shortcut'; |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 19 | import {IXosGraphStore} from './graph.store'; |
Matteo Scandolo | 5bf5173 | 2018-01-10 15:51:33 -0800 | [diff] [blame] | 20 | import {IXosSidePanelService} from '../../core/side-panel/side-panel.service'; |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 21 | |
| 22 | export interface IXosGraphConfig { |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 23 | setupKeyboardShortcuts(): void; |
| 24 | toggleFullscreen(): void; |
| 25 | } |
| 26 | |
| 27 | export class XosGraphConfig { |
| 28 | |
| 29 | static $inject = [ |
| 30 | '$log', |
| 31 | '$cookies', |
| 32 | '$rootScope', |
| 33 | '$timeout', |
Matteo Scandolo | 5bf5173 | 2018-01-10 15:51:33 -0800 | [diff] [blame] | 34 | 'XosSidePanel', |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 35 | 'XosKeyboardShortcut', |
| 36 | 'XosGraphStore' |
| 37 | ]; |
| 38 | |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 39 | private instanceEnabled = false; |
| 40 | private instanceBinding = { |
| 41 | key: 'i', |
| 42 | modifiers: ['shift'], |
| 43 | cb: () => { |
| 44 | // NOTE anytime the graph change the observable is updated, |
| 45 | // no need to manually retrigger here |
| 46 | this.XosGraphStore.toggleInstances(); |
| 47 | this.toggleNetworkShortcuts(); |
| 48 | }, |
| 49 | label: 'i', |
| 50 | description: 'Toggle Instances' |
| 51 | }; |
| 52 | |
| 53 | private networkEnabled = false; |
| 54 | private networkBinding = { |
| 55 | key: 'n', |
| 56 | modifiers: ['shift'], |
| 57 | cb: () => { |
| 58 | // NOTE anytime the graph change the observable is updated, |
| 59 | // no need to manually retrigger here |
| 60 | this.XosGraphStore.toggleNetwork(); |
| 61 | }, |
| 62 | label: 'n', |
| 63 | description: 'Toggle Networks' |
| 64 | }; |
| 65 | |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 66 | constructor ( |
| 67 | private $log: ng.ILogService, |
| 68 | private $cookies: ng.cookies.ICookiesService, |
| 69 | private $rootScope: ng.IRootScopeService, |
| 70 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | 5bf5173 | 2018-01-10 15:51:33 -0800 | [diff] [blame] | 71 | private XosSidePanel: IXosSidePanelService, |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 72 | private XosKeyboardShortcut: IXosKeyboardShortcutService, |
| 73 | private XosGraphStore: IXosGraphStore |
| 74 | ) { |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 75 | |
| 76 | } |
| 77 | |
| 78 | public setupKeyboardShortcuts() { |
| 79 | |
| 80 | this.$log.info(`[XosGraphConfig] Setting up keyboard shortcuts`); |
| 81 | |
| 82 | // Setup keyboard shortcuts |
| 83 | this.XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | 5bf5173 | 2018-01-10 15:51:33 -0800 | [diff] [blame] | 84 | key: 'h', |
| 85 | modifiers: ['shift'], |
| 86 | cb: () => { |
| 87 | this.XosSidePanel.toggleComponent('xosServiceGraphLegend'); |
| 88 | }, |
| 89 | label: 'h', |
| 90 | description: 'Toggle Graph Legend' |
| 91 | }); |
| 92 | |
| 93 | this.XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 94 | key: 'f', |
| 95 | modifiers: ['shift'], |
| 96 | cb: () => { |
| 97 | this.toggleFullscreen(); |
| 98 | }, |
| 99 | label: 'f', |
| 100 | description: 'Toggle graph fullscreen' |
| 101 | }); |
| 102 | |
| 103 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 104 | key: 's', |
| 105 | modifiers: ['shift'], |
| 106 | cb: () => { |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 107 | this.XosGraphStore.toggleServiceInstances(); |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 108 | this.toggleInstanceShortcuts(); |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 109 | }, |
| 110 | label: 's', |
| 111 | description: 'Toggle ServiceInstances' |
| 112 | }); |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 113 | |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public toggleFullscreen() { |
| 117 | $('.graph-container').toggleClass('fullscreen'); |
| 118 | this.$timeout(() => { |
| 119 | // NOTE wait for the CSS transition to complete before repositioning |
| 120 | this.$rootScope.$broadcast('xos.sg.update'); |
| 121 | }, 500); |
| 122 | } |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 123 | |
| 124 | private toggleInstanceShortcuts(): void { |
| 125 | if (!this.instanceEnabled) { |
| 126 | this.XosKeyboardShortcut.registerKeyBinding(this.instanceBinding); |
| 127 | } |
| 128 | else { |
| 129 | this.XosKeyboardShortcut.removeKeyBinding(this.instanceBinding); |
| 130 | this.XosKeyboardShortcut.removeKeyBinding(this.networkBinding); |
| 131 | } |
| 132 | this.instanceEnabled = !this.instanceEnabled; |
| 133 | } |
| 134 | |
| 135 | private toggleNetworkShortcuts(): void { |
| 136 | if (!this.networkEnabled) { |
| 137 | this.XosKeyboardShortcut.registerKeyBinding(this.networkBinding); |
| 138 | } |
| 139 | else { |
| 140 | this.XosKeyboardShortcut.removeKeyBinding(this.networkBinding); |
| 141 | } |
| 142 | |
| 143 | this.networkEnabled = !this.networkEnabled; |
| 144 | } |
Matteo Scandolo | 8cf33a3 | 2017-11-14 15:52:29 -0800 | [diff] [blame] | 145 | } |