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