Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [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 {IXosKeyboardShortcutService} from '../services/keyboard-shortcut'; |
| 18 | |
| 19 | export interface IXosDebugStatus { |
| 20 | global: boolean; |
| 21 | events: boolean; |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 22 | modelsTab: boolean; |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | export interface IXosDebugService { |
| 26 | status: IXosDebugStatus; |
| 27 | setupShortcuts(): void; |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 28 | toggleDebug(type: 'global' | 'events' | 'modelsTab'): void; |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | export class XosDebugService implements IXosDebugService { |
| 32 | |
| 33 | static $inject = ['$log', '$rootScope', 'XosKeyboardShortcut']; |
| 34 | |
| 35 | public status: IXosDebugStatus = { |
| 36 | global: false, |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 37 | events: false, |
| 38 | modelsTab: false |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | constructor ( |
| 42 | private $log: ng.ILogService, |
| 43 | private $scope: ng.IScope, |
| 44 | private XosKeyboardShortcut: IXosKeyboardShortcutService |
| 45 | ) { |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 46 | const debug = window.localStorage.getItem('debug-global'); |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 47 | this.status.global = (debug === 'true'); |
| 48 | |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 49 | const debugEvent = window.localStorage.getItem('debug-events'); |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 50 | this.status.events = (debugEvent === 'true'); |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 51 | |
| 52 | const debugModelsTab = window.localStorage.getItem('debug-modelsTab'); |
| 53 | this.status.modelsTab = (debugModelsTab === 'true'); |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public setupShortcuts(): void { |
| 57 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 58 | key: 'D', |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 59 | cb: () => this.toggleDebug('global'), |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 60 | description: 'Toggle debug messages in browser console' |
| 61 | }, 'global'); |
| 62 | |
| 63 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 64 | key: 'E', |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 65 | cb: () => this.toggleDebug('events'), |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 66 | description: 'Toggle debug messages for WS events in browser console' |
| 67 | }, 'global'); |
| 68 | } |
| 69 | |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 70 | public toggleDebug(type: 'global' | 'events' | 'modelsTab'): void { |
| 71 | if (window.localStorage.getItem(`debug-${type}`) === 'true') { |
| 72 | this.$log.info(`[XosDebug] Disabling ${type} debug`); |
| 73 | window.localStorage.setItem(`debug-${type}`, 'false'); |
| 74 | this.status[type] = false; |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 75 | } |
| 76 | else { |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 77 | this.$log.info(`[XosDebug] Enabling ${type} debug`); |
| 78 | window.localStorage.setItem(`debug-${type}`, 'true'); |
| 79 | this.status[type] = true; |
Matteo Scandolo | c3804aa | 2017-08-09 16:00:43 -0700 | [diff] [blame] | 80 | } |
| 81 | this.$scope.$broadcast('xos.debug.status', this.status); |
| 82 | } |
| 83 | } |