blob: 7387c946485b6586e76978b7fec8947d8ab1d0a7 [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 () {
Steven Burrows46c5f102017-07-14 16:52:46 +010037 this.name = this.displayName.toLowerCase().replace(/ /g, '_');
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000038 this.prefs = {
Steven Burrows46c5f102017-07-14 16:52:46 +010039 visible: this.name + '_visible',
40 };
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000041 },
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 Burrows46c5f102017-07-14 16:52:46 +010049 node: function () {
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000050 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 () {
Steven Burrows46c5f102017-07-14 16:52:46 +010067 node.style('visibility', 'hidden');
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000068 });
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)
Steven Burrows46c5f102017-07-14 16:52:46 +010079 .style('opacity', 1);
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000080 }
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 },
Steven Burrowsaea509d2017-04-12 14:17:47 -070090 lookupPrefState: function (key) {
91 // Return 0 if not defined
92 return ps.getPrefs('topo2_prefs')[key] || 0;
93 },
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000094 updatePrefState: function (key, value) {
Simon Hunt95f4b422017-03-03 13:49:05 -080095 var state = ps.getPrefs('topo2_prefs');
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000096 state[key] = value ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -080097 ps.setPrefs('topo2_prefs', state);
Steven Burrows46c5f102017-07-14 16:52:46 +010098 },
Steven Burrowsaf96a212016-12-28 12:57:02 +000099 };
100
101 angular.module('ovTopo2')
102 .factory('Topo2ViewController', [
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000103 'FnService', 'FlashService', 'PrefsService',
104 function (fn, _flash_, _ps_) {
105
106 flash = _flash_;
107 ps = _ps_;
108
Steven Burrowsaf96a212016-12-28 12:57:02 +0000109 ViewController.extend = fn.extend;
110 return ViewController;
Steven Burrows46c5f102017-07-14 16:52:46 +0100111 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000112 ]);
113})();