blob: f23fa06613fa5411f370864dc7fa59482dc91432 [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 Scandoloc3804aa2017-08-09 16:00:43 -070023}
24
25export interface IXosDebugService {
26 status: IXosDebugStatus;
27 setupShortcuts(): void;
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070028 toggleDebug(type: 'global' | 'events' | 'modelsTab'): void;
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070029}
30
31export class XosDebugService implements IXosDebugService {
32
33 static $inject = ['$log', '$rootScope', 'XosKeyboardShortcut'];
34
35 public status: IXosDebugStatus = {
36 global: false,
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070037 events: false,
38 modelsTab: false
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070039 };
40
41 constructor (
42 private $log: ng.ILogService,
43 private $scope: ng.IScope,
44 private XosKeyboardShortcut: IXosKeyboardShortcutService
45 ) {
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070046 const debug = window.localStorage.getItem('debug-global');
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070047 this.status.global = (debug === 'true');
48
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070049 const debugEvent = window.localStorage.getItem('debug-events');
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070050 this.status.events = (debugEvent === 'true');
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070051
52 const debugModelsTab = window.localStorage.getItem('debug-modelsTab');
53 this.status.modelsTab = (debugModelsTab === 'true');
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070054 }
55
56 public setupShortcuts(): void {
57 this.XosKeyboardShortcut.registerKeyBinding({
58 key: 'D',
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070059 cb: () => this.toggleDebug('global'),
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070060 description: 'Toggle debug messages in browser console'
61 }, 'global');
62
63 this.XosKeyboardShortcut.registerKeyBinding({
64 key: 'E',
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070065 cb: () => this.toggleDebug('events'),
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070066 description: 'Toggle debug messages for WS events in browser console'
67 }, 'global');
68 }
69
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070070 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 Scandoloc3804aa2017-08-09 16:00:43 -070075 }
76 else {
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070077 this.$log.info(`[XosDebug] Enabling ${type} debug`);
78 window.localStorage.setItem(`debug-${type}`, 'true');
79 this.status[type] = true;
Matteo Scandoloc3804aa2017-08-09 16:00:43 -070080 }
81 this.$scope.$broadcast('xos.debug.status', this.status);
82 }
83}