Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 25 | var flash, ps; |
| 26 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 27 | function ViewController(options) { |
| 28 | this.initialize.apply(this, arguments); |
| 29 | } |
| 30 | |
| 31 | ViewController.prototype = { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 32 | |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 33 | id: null, |
| 34 | displayName: 'View', |
| 35 | |
| 36 | initialize: function () { |
Steven Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 37 | this.name = this.displayName.toLowerCase().replace(/ /g, '_'); |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 38 | this.prefs = { |
Steven Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 39 | visible: this.name + '_visible', |
| 40 | }; |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 41 | }, |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 42 | 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 Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 49 | node: function () { |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 50 | return d3.select('#' + this.id); |
| 51 | }, |
| 52 | enabled: function () { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 53 | return ps.getPrefs('topo2_prefs')[this.prefs.visible]; |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 54 | }, |
| 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 Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 67 | node.style('visibility', 'hidden'); |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 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) |
Steven Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 79 | .style('opacity', 1); |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 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 | }, |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 90 | lookupPrefState: function (key) { |
| 91 | // Return 0 if not defined |
| 92 | return ps.getPrefs('topo2_prefs')[key] || 0; |
| 93 | }, |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 94 | updatePrefState: function (key, value) { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 95 | var state = ps.getPrefs('topo2_prefs'); |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 96 | state[key] = value ? 1 : 0; |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 97 | ps.setPrefs('topo2_prefs', state); |
Steven Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 98 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | angular.module('ovTopo2') |
| 102 | .factory('Topo2ViewController', [ |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 103 | 'FnService', 'FlashService', 'PrefsService', |
| 104 | function (fn, _flash_, _ps_) { |
| 105 | |
| 106 | flash = _flash_; |
| 107 | ps = _ps_; |
| 108 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 109 | ViewController.extend = fn.extend; |
| 110 | return ViewController; |
Steven Burrows | 46c5f10 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 111 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 112 | ]); |
| 113 | })(); |