blob: 0410f87f43215daa38f35262116c9d288a376f5e [file] [log] [blame]
Sean Condonf4f54a12018-10-10 23:25:46 +01001/*
2 * Copyright 2018-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 */
Sean Condon3dd062f2020-04-14 09:25:00 +010016import { FnService, LogService, PrefsService } from 'org_onosproject_onos/web/gui2-fw-lib/public_api';
Sean Condonf4f54a12018-10-10 23:25:46 +010017
18export interface ViewControllerPrefs {
19 visible: string;
20}
21
22/*
23 ONOS GUI -- View Controller.
24 A base class for view controllers to extend from
25 */
26export abstract class ViewControllerImpl {
27 id: string;
28 displayName: string = 'View';
29 name: string;
30 prefs: ViewControllerPrefs;
31 visibility: string;
32
33 constructor(
34 protected fs: FnService,
35 protected log: LogService,
36 protected ps: PrefsService
37 ) {
38 this.log.debug('View Controller constructed');
39 }
40
41 initialize() {
42 this.name = this.displayName.toLowerCase().replace(/ /g, '_');
43 this.prefs = {
44 visible: this.name + '_visible',
45 };
46 }
47
48 enabled() {
49 return this.ps.getPrefs('topo2_prefs', null)[this.prefs.visible];
50 }
51
52 isVisible() {
53 return this.visibility;
54 }
55
56 hide() {
57 this.visibility = 'hidden';
58 }
59
60 show() {
61 this.visibility = 'visible';
62 }
63
64 toggle() {
65 if (this.visibility === 'hidden') {
66 this.visibility = 'visible';
67 } else if (this.visibility === 'visible') {
68 this.visibility = 'hidden';
69 }
70 }
71
72 lookupPrefState(key: string): number {
73 // Return 0 if not defined
74 return this.ps.getPrefs('topo2_prefs', null)[key] || 0;
75 }
76
77 updatePrefState(key: string, value: number) {
78 const state = this.ps.getPrefs('topo2_prefs', null);
79 state[key] = value ? 1 : 0;
80 this.ps.setPrefs('topo2_prefs', state);
81 }
82}