blob: ad8dfa940e45301bb27dd1498d49b6d3ae516782 [file] [log] [blame]
Steven Burrowsaf96a212016-12-28 12:57:02 +00001/*
2 * Copyright 2016-present Open Networking Laboratory
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/*
18 ONOS GUI -- View Controller.
19 A base class for view controllers to extend from
20 */
21
22(function () {
23 'use strict';
24
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000025 var flash, ps;
26
Steven Burrowsaf96a212016-12-28 12:57:02 +000027 function ViewController(options) {
28 this.initialize.apply(this, arguments);
29 }
30
31 ViewController.prototype = {
Steven Burrowsaf96a212016-12-28 12:57:02 +000032
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000033 id: null,
34 displayName: 'View',
35
36 initialize: function () {
37 this.name = this.displayName.toLowerCase().replace(/ /g,"_");
38 this.prefs = {
39 visible: this.name + '_visible'
40 }
41 },
42 node: function() {
43 return d3.select('#' + this.id);
44 },
45 enabled: function () {
Simon Hunt95f4b422017-03-03 13:49:05 -080046 return ps.getPrefs('topo2_prefs')[this.prefs.visible];
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000047 },
48 isVisible: function () {
49 return this.node().style('visibility') === 'visible';
50 },
51 hide: function () {
52 var node = this.node();
53
54 if (this.isVisible()) {
55 node
56 .transition()
57 .duration(400)
58 .style('opacity', 0)
59 .each('end', function () {
60 node.style('visibility', 'hidden')
61 });
62 }
63 },
64 show: function () {
65 var node = this.node();
66
67 if (!this.isVisible()) {
68 node
69 .style('visibility', 'visible')
70 .transition()
71 .duration(400)
72 .style('opacity', 1)
73 }
74 },
75 toggle: function () {
76 var on = !Boolean(this.isVisible()),
77 verb = on ? 'Show' : 'Hide';
78
79 on ? this.show() : this.hide();
80 flash.flash(verb + ' ' + this.displayName);
81 this.updatePrefState(this.prefs.visible, on);
82 },
83 updatePrefState: function (key, value) {
Simon Hunt95f4b422017-03-03 13:49:05 -080084 var state = ps.getPrefs('topo2_prefs');
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000085 state[key] = value ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -080086 ps.setPrefs('topo2_prefs', state);
Steven Burrowsaf96a212016-12-28 12:57:02 +000087 }
88 };
89
90 angular.module('ovTopo2')
91 .factory('Topo2ViewController', [
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000092 'FnService', 'FlashService', 'PrefsService',
93 function (fn, _flash_, _ps_) {
94
95 flash = _flash_;
96 ps = _ps_;
97
Steven Burrowsaf96a212016-12-28 12:57:02 +000098 ViewController.extend = fn.extend;
99 return ViewController;
100 }
101 ]);
102})();