blob: cc316a22c673b97d39346c3e52e0627cf5a8e91b [file] [log] [blame]
Matteo Scandolo8cf33a32017-11-14 15:52:29 -08001/*
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
17import * as $ from 'jquery';
18import {IXosKeyboardShortcutService} from '../../core/services/keyboard-shortcut';
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080019import {IXosGraphStore} from './graph.store';
Matteo Scandolo5bf51732018-01-10 15:51:33 -080020import {IXosSidePanelService} from '../../core/side-panel/side-panel.service';
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080021
22export interface IXosGraphConfig {
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080023 setupKeyboardShortcuts(): void;
24 toggleFullscreen(): void;
25}
26
27export class XosGraphConfig {
28
29 static $inject = [
30 '$log',
31 '$cookies',
32 '$rootScope',
33 '$timeout',
Matteo Scandolo5bf51732018-01-10 15:51:33 -080034 'XosSidePanel',
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080035 'XosKeyboardShortcut',
36 'XosGraphStore'
37 ];
38
Matteo Scandolo1888b2a2018-01-08 16:49:06 -080039 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 Scandolo8cf33a32017-11-14 15:52:29 -080066 constructor (
67 private $log: ng.ILogService,
68 private $cookies: ng.cookies.ICookiesService,
69 private $rootScope: ng.IRootScopeService,
70 private $timeout: ng.ITimeoutService,
Matteo Scandolo5bf51732018-01-10 15:51:33 -080071 private XosSidePanel: IXosSidePanelService,
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080072 private XosKeyboardShortcut: IXosKeyboardShortcutService,
73 private XosGraphStore: IXosGraphStore
74 ) {
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080075
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 Scandolo5bf51732018-01-10 15:51:33 -080084 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 Scandolo8cf33a32017-11-14 15:52:29 -080094 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 Scandolo8cf33a32017-11-14 15:52:29 -0800107 this.XosGraphStore.toggleServiceInstances();
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800108 this.toggleInstanceShortcuts();
Matteo Scandolo8cf33a32017-11-14 15:52:29 -0800109 },
110 label: 's',
111 description: 'Toggle ServiceInstances'
112 });
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800113
Matteo Scandolo8cf33a32017-11-14 15:52:29 -0800114 }
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 Scandolo1888b2a2018-01-08 16:49:06 -0800123
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 Scandolo8cf33a32017-11-14 15:52:29 -0800145}