blob: aee106430272da2fa8cff4e9d11ead32c585eb6a [file] [log] [blame]
Matteo Scandoloc3804aa2017-08-09 16:00:43 -07001/*
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 {IXosKeyboardShortcutService} from '../services/keyboard-shortcut';
18
19export interface IXosDebugStatus {
20 global: boolean;
21 events: boolean;
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070022 modelsTab: boolean;
Matteo Scandolof3717252017-10-11 15:38:54 -070023 notifications: boolean;
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070024}
25
26export interface IXosDebugService {
27 status: IXosDebugStatus;
28 setupShortcuts(): void;
Matteo Scandolof3717252017-10-11 15:38:54 -070029 toggleDebug(type: 'global' | 'events' | 'modelsTab' | 'notifications'): void;
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070030}
31
32export class XosDebugService implements IXosDebugService {
33
34 static $inject = ['$log', '$rootScope', 'XosKeyboardShortcut'];
35
36 public status: IXosDebugStatus = {
37 global: false,
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070038 events: false,
Matteo Scandolof3717252017-10-11 15:38:54 -070039 modelsTab: false,
40 notifications: true
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070041 };
42
43 constructor (
44 private $log: ng.ILogService,
45 private $scope: ng.IScope,
46 private XosKeyboardShortcut: IXosKeyboardShortcutService
47 ) {
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070048 const debug = window.localStorage.getItem('debug-global');
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070049 this.status.global = (debug === 'true');
50
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070051 const debugEvent = window.localStorage.getItem('debug-events');
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070052 this.status.events = (debugEvent === 'true');
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070053
54 const debugModelsTab = window.localStorage.getItem('debug-modelsTab');
55 this.status.modelsTab = (debugModelsTab === 'true');
Matteo Scandolof3717252017-10-11 15:38:54 -070056
57 const notifications = window.localStorage.getItem('debug-notifications');
58 this.status.notifications = (notifications !== null ? notifications === 'true' : true);
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070059 }
60
61 public setupShortcuts(): void {
62 this.XosKeyboardShortcut.registerKeyBinding({
63 key: 'D',
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070064 cb: () => this.toggleDebug('global'),
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070065 description: 'Toggle debug messages in browser console'
66 }, 'global');
67
68 this.XosKeyboardShortcut.registerKeyBinding({
69 key: 'E',
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070070 cb: () => this.toggleDebug('events'),
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070071 description: 'Toggle debug messages for WS events in browser console'
72 }, 'global');
Matteo Scandolof3717252017-10-11 15:38:54 -070073
74 this.XosKeyboardShortcut.registerKeyBinding({
75 key: 'S',
76 cb: () => this.toggleDebug('notifications'),
77 description: 'Toggle notifications'
78 }, 'global');
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070079 }
80
Matteo Scandolof3717252017-10-11 15:38:54 -070081 public toggleDebug(type: 'global' | 'events' | 'modelsTab' | 'notifications'): void {
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070082 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 Scandoloc3804aa2017-08-09 16:00:43 -070086 }
87 else {
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070088 this.$log.info(`[XosDebug] Enabling ${type} debug`);
89 window.localStorage.setItem(`debug-${type}`, 'true');
90 this.status[type] = true;
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070091 }
92 this.$scope.$broadcast('xos.debug.status', this.status);
93 }
94}