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