blob: e6ddfbd146cab82993bec14ec5d665ec1b0ba83d [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';
20
21export interface IXosGraphConfig {
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080022 setupKeyboardShortcuts(): void;
23 toggleFullscreen(): void;
24}
25
26export class XosGraphConfig {
27
28 static $inject = [
29 '$log',
30 '$cookies',
31 '$rootScope',
32 '$timeout',
33 'XosKeyboardShortcut',
34 'XosGraphStore'
35 ];
36
Matteo Scandolo1888b2a2018-01-08 16:49:06 -080037 private instanceEnabled = false;
38 private instanceBinding = {
39 key: 'i',
40 modifiers: ['shift'],
41 cb: () => {
42 // NOTE anytime the graph change the observable is updated,
43 // no need to manually retrigger here
44 this.XosGraphStore.toggleInstances();
45 this.toggleNetworkShortcuts();
46 },
47 label: 'i',
48 description: 'Toggle Instances'
49 };
50
51 private networkEnabled = false;
52 private networkBinding = {
53 key: 'n',
54 modifiers: ['shift'],
55 cb: () => {
56 // NOTE anytime the graph change the observable is updated,
57 // no need to manually retrigger here
58 this.XosGraphStore.toggleNetwork();
59 },
60 label: 'n',
61 description: 'Toggle Networks'
62 };
63
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080064 constructor (
65 private $log: ng.ILogService,
66 private $cookies: ng.cookies.ICookiesService,
67 private $rootScope: ng.IRootScopeService,
68 private $timeout: ng.ITimeoutService,
69 private XosKeyboardShortcut: IXosKeyboardShortcutService,
70 private XosGraphStore: IXosGraphStore
71 ) {
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080072
73 }
74
75 public setupKeyboardShortcuts() {
76
77 this.$log.info(`[XosGraphConfig] Setting up keyboard shortcuts`);
78
79 // Setup keyboard shortcuts
80 this.XosKeyboardShortcut.registerKeyBinding({
81 key: 'f',
82 modifiers: ['shift'],
83 cb: () => {
84 this.toggleFullscreen();
85 },
86 label: 'f',
87 description: 'Toggle graph fullscreen'
88 });
89
90 this.XosKeyboardShortcut.registerKeyBinding({
91 key: 's',
92 modifiers: ['shift'],
93 cb: () => {
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080094 this.XosGraphStore.toggleServiceInstances();
Matteo Scandolo1888b2a2018-01-08 16:49:06 -080095 this.toggleInstanceShortcuts();
Matteo Scandolo8cf33a32017-11-14 15:52:29 -080096 },
97 label: 's',
98 description: 'Toggle ServiceInstances'
99 });
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800100
Matteo Scandolo8cf33a32017-11-14 15:52:29 -0800101 }
102
103 public toggleFullscreen() {
104 $('.graph-container').toggleClass('fullscreen');
105 this.$timeout(() => {
106 // NOTE wait for the CSS transition to complete before repositioning
107 this.$rootScope.$broadcast('xos.sg.update');
108 }, 500);
109 }
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800110
111 private toggleInstanceShortcuts(): void {
112 if (!this.instanceEnabled) {
113 this.XosKeyboardShortcut.registerKeyBinding(this.instanceBinding);
114 }
115 else {
116 this.XosKeyboardShortcut.removeKeyBinding(this.instanceBinding);
117 this.XosKeyboardShortcut.removeKeyBinding(this.networkBinding);
118 }
119 this.instanceEnabled = !this.instanceEnabled;
120 }
121
122 private toggleNetworkShortcuts(): void {
123 if (!this.networkEnabled) {
124 this.XosKeyboardShortcut.registerKeyBinding(this.networkBinding);
125 }
126 else {
127 this.XosKeyboardShortcut.removeKeyBinding(this.networkBinding);
128 }
129
130 this.networkEnabled = !this.networkEnabled;
131 }
Matteo Scandolo8cf33a32017-11-14 15:52:29 -0800132}