blob: 6c19fe904ad67e442bcb89a15a8e9548a19656d4 [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 },
Steven Burrowsb43c1a92017-03-07 17:13:28 +000042 appendElement: function (parent, node) {
43 var el = d3.select('#' + this.id);
44 if (el.empty()) {
45 return d3.select(parent).append(node).attr('id', this.id);
46 }
47 return el;
48 },
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000049 node: function() {
50 return d3.select('#' + this.id);
51 },
52 enabled: function () {
Simon Hunt95f4b422017-03-03 13:49:05 -080053 return ps.getPrefs('topo2_prefs')[this.prefs.visible];
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000054 },
55 isVisible: function () {
56 return this.node().style('visibility') === 'visible';
57 },
58 hide: function () {
59 var node = this.node();
60
61 if (this.isVisible()) {
62 node
63 .transition()
64 .duration(400)
65 .style('opacity', 0)
66 .each('end', function () {
67 node.style('visibility', 'hidden')
68 });
69 }
70 },
71 show: function () {
72 var node = this.node();
73
74 if (!this.isVisible()) {
75 node
76 .style('visibility', 'visible')
77 .transition()
78 .duration(400)
79 .style('opacity', 1)
80 }
81 },
82 toggle: function () {
83 var on = !Boolean(this.isVisible()),
84 verb = on ? 'Show' : 'Hide';
85
86 on ? this.show() : this.hide();
87 flash.flash(verb + ' ' + this.displayName);
88 this.updatePrefState(this.prefs.visible, on);
89 },
90 updatePrefState: function (key, value) {
Simon Hunt95f4b422017-03-03 13:49:05 -080091 var state = ps.getPrefs('topo2_prefs');
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000092 state[key] = value ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -080093 ps.setPrefs('topo2_prefs', state);
Steven Burrowsaf96a212016-12-28 12:57:02 +000094 }
95 };
96
97 angular.module('ovTopo2')
98 .factory('Topo2ViewController', [
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000099 'FnService', 'FlashService', 'PrefsService',
100 function (fn, _flash_, _ps_) {
101
102 flash = _flash_;
103 ps = _ps_;
104
Steven Burrowsaf96a212016-12-28 12:57:02 +0000105 ViewController.extend = fn.extend;
106 return ViewController;
107 }
108 ]);
109})();