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'; |
| 19 | import {IXosSgConfig} from '../interfaces'; |
| 20 | import {IXosGraphStore} from './graph.store'; |
| 21 | |
| 22 | export interface IXosGraphConfig { |
| 23 | getUserConfig(): IXosSgConfig; |
| 24 | setupKeyboardShortcuts(): void; |
| 25 | toggleFullscreen(): void; |
| 26 | } |
| 27 | |
| 28 | export class XosGraphConfig { |
| 29 | |
| 30 | static $inject = [ |
| 31 | '$log', |
| 32 | '$cookies', |
| 33 | '$rootScope', |
| 34 | '$timeout', |
| 35 | 'XosKeyboardShortcut', |
| 36 | 'XosGraphStore' |
| 37 | ]; |
| 38 | |
| 39 | private defaultUserConfig: IXosSgConfig = { |
| 40 | labels: false |
| 41 | }; |
| 42 | |
| 43 | private userConfig: IXosSgConfig = this.defaultUserConfig; |
| 44 | |
| 45 | constructor ( |
| 46 | private $log: ng.ILogService, |
| 47 | private $cookies: ng.cookies.ICookiesService, |
| 48 | private $rootScope: ng.IRootScopeService, |
| 49 | private $timeout: ng.ITimeoutService, |
| 50 | private XosKeyboardShortcut: IXosKeyboardShortcutService, |
| 51 | private XosGraphStore: IXosGraphStore |
| 52 | ) { |
| 53 | this.userConfig = this.getUserConfig(); |
| 54 | |
| 55 | |
| 56 | } |
| 57 | |
| 58 | public setupKeyboardShortcuts() { |
| 59 | |
| 60 | this.$log.info(`[XosGraphConfig] Setting up keyboard shortcuts`); |
| 61 | |
| 62 | // Setup keyboard shortcuts |
| 63 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 64 | key: 'f', |
| 65 | modifiers: ['shift'], |
| 66 | cb: () => { |
| 67 | this.toggleFullscreen(); |
| 68 | }, |
| 69 | label: 'f', |
| 70 | description: 'Toggle graph fullscreen' |
| 71 | }); |
| 72 | |
| 73 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 74 | key: 's', |
| 75 | modifiers: ['shift'], |
| 76 | cb: () => { |
| 77 | // NOTE anytime the graph change the observable is updated, |
| 78 | // no need to manually retrigger here |
| 79 | this.XosGraphStore.toggleServiceInstances(); |
| 80 | }, |
| 81 | label: 's', |
| 82 | description: 'Toggle ServiceInstances' |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | public toggleFullscreen() { |
| 87 | $('.graph-container').toggleClass('fullscreen'); |
| 88 | this.$timeout(() => { |
| 89 | // NOTE wait for the CSS transition to complete before repositioning |
| 90 | this.$rootScope.$broadcast('xos.sg.update'); |
| 91 | }, 500); |
| 92 | } |
| 93 | |
| 94 | public getUserConfig(): IXosSgConfig { |
| 95 | let config = this.$cookies.get('xos-service-graph-user-config'); |
| 96 | if (!config || config.length === 0) { |
| 97 | this.$cookies.put('xos-service-graph-user-config', JSON.stringify(this.defaultUserConfig)); |
| 98 | config = this.$cookies.get('xos-service-graph-user-config'); |
| 99 | } |
| 100 | return JSON.parse(config); |
| 101 | } |
| 102 | } |