Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 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 -- Host View Module |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 24 | // injected refs |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 25 | var $log, $scope, $loc, fs, mast, ps, wss, is, ns, ks, dps; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 26 | |
| 27 | // internal state |
| 28 | var detailsPanel, |
| 29 | pStartY, |
| 30 | pHeight, |
| 31 | top, |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 32 | wSize; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 33 | |
| 34 | // constants |
| 35 | var topPdg = 28, |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 36 | pName = 'host-details-panel', |
| 37 | detailsReq = 'hostDetailsRequest', |
| 38 | detailsResp = 'hostDetailsResponse', |
| 39 | nameChangeReq = 'hostNameChangeRequest', |
| 40 | nameChangeResp = 'hostNameChangeResponse'; |
| 41 | |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 42 | var keyBindings = { |
| 43 | esc: [closePanel, 'Close the details panel'], |
| 44 | _helpFormat: ['esc'], |
| 45 | }; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 46 | |
| 47 | function closePanel() { |
| 48 | if (detailsPanel.isVisible()) { |
| 49 | $scope.selId = null; |
| 50 | detailsPanel.hide(); |
| 51 | return true; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 56 | |
| 57 | function setUpPanel() { |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 58 | |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 59 | dps.empty(); |
| 60 | dps.addContainers(); |
| 61 | dps.addCloseButton(closePanel); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 62 | |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 63 | var top = dps.top(); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 64 | |
Simon Hunt | c9e8321 | 2017-10-09 16:52:07 -0700 | [diff] [blame] | 65 | dps.addHeading('host-icon', true); |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 66 | top.append('div').classed('top-content', true); |
| 67 | |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 68 | top.append('hr'); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 69 | } |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 70 | |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 71 | function friendlyPropsList(details) { |
| 72 | return { |
| 73 | 'Host ID': details.id, |
| 74 | 'IP Address': details.ip[0], |
| 75 | 'MAC Address': details.mac, |
| 76 | 'VLAN': details.vlan, |
| 77 | 'Configured': details.configured, |
| 78 | 'Location': details.location, |
| 79 | }; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 82 | function populateTop(tblDiv, details) { |
| 83 | is.loadEmbeddedIcon(dps.select('.iconDiv'), details._iconid_type, 40); |
| 84 | dps.top().select('h2').text(details.name); |
| 85 | dps.addPropsList(tblDiv, friendlyPropsList(details)); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | function populateDetails(details) { |
| 89 | setUpPanel(); |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 90 | populateTop(dps.select('.top-content'), details); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 91 | detailsPanel.height(pHeight); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 92 | // configure width based on content.. for now hardcoded |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 93 | detailsPanel.width(400); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | function respDetailsCb(data) { |
| 97 | $scope.panelData = data.details; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 98 | $scope.$apply(); |
| 99 | } |
| 100 | |
| 101 | function respNameCb(data) { |
| 102 | if (data.warn) { |
| 103 | $log.warn(data.warn, data.id); |
| 104 | top.select('h2').text(data.id); |
| 105 | } |
| 106 | } |
| 107 | |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 108 | function createDetailsPanel() { |
| 109 | detailsPanel = dps.create(pName, { |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 110 | width: wSize.width, |
| 111 | margin: 0, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 112 | hideMargin: 0, |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 113 | scope: $scope, |
| 114 | keyBindings: keyBindings, |
| 115 | nameChangeRequest: nameChangeReq, |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 116 | }); |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 117 | |
| 118 | dps.setResponse(detailsResp, respDetailsCb); |
| 119 | |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 120 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | // Defines the Host View controller... |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 125 | angular.module('ovHost', []) |
| 126 | .controller('OvHostCtrl', |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 127 | ['$log', '$scope', |
| 128 | '$location', |
| 129 | 'TableBuilderService', |
| 130 | 'FnService', 'MastService', 'PanelService', 'WebSocketService', |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 131 | 'IconService', 'NavService', 'KeyService', 'DetailsPanelService', |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 132 | |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 133 | function (_$log_, _$scope_, _$location_, |
| 134 | tbs, |
| 135 | _fs_, _mast_, _ps_, _wss_, |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 136 | _is_, _ns_, _ks_, _dps_) { |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 137 | |
| 138 | var params, |
| 139 | handlers = {}; |
| 140 | |
| 141 | $log = _$log_; |
| 142 | $scope = _$scope_; |
| 143 | $loc = _$location_; |
| 144 | fs = _fs_; |
| 145 | mast = _mast_; |
| 146 | ps = _ps_; |
| 147 | wss = _wss_; |
| 148 | is = _is_; |
| 149 | ns = _ns_; |
| 150 | ks = _ks_; |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 151 | dps = _dps_; |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 152 | |
| 153 | params = $loc.search(); |
| 154 | |
| 155 | $scope.panelData = {}; |
| 156 | |
| 157 | // details panel handlers |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 158 | handlers[nameChangeResp] = respNameCb; |
| 159 | wss.bindHandlers(handlers); |
| 160 | |
| 161 | // query for if a certain host needs to be highlighted |
| 162 | if (params.hasOwnProperty('hostId')) { |
| 163 | $scope.selId = params['hostId']; |
| 164 | wss.sendEvent(detailsReq, { id: $scope.selId }); |
| 165 | } |
| 166 | |
| 167 | function selCb($event, row) { |
| 168 | if ($scope.selId) { |
| 169 | wss.sendEvent(detailsReq, { id: row.id }); |
| 170 | } else { |
| 171 | $scope.hidePanel(); |
| 172 | } |
| 173 | $log.debug('Got a click on:', row); |
| 174 | } |
| 175 | |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 176 | tbs.buildTable({ |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 177 | scope: $scope, |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 178 | tag: 'host', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 179 | selCb: selCb, |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 180 | }); |
| 181 | |
| 182 | $scope.nav = function (path) { |
| 183 | if ($scope.selId) { |
| 184 | ns.navTo(path, { hostId: $scope.selId }); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | $scope.$on('$destroy', function () { |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 189 | dps.destroy(); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 190 | wss.unbindHandlers(handlers); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 191 | }); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 192 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 193 | $log.log('OvHostCtrl has been created'); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 194 | }]) |
| 195 | |
| 196 | .directive('hostDetailsPanel', |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 197 | ['$rootScope', '$window', '$timeout', 'KeyService', 'DetailsPanelService', |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 198 | function ($rootScope, $window, $timeout, ks) { |
| 199 | return function (scope) { |
| 200 | var unbindWatch; |
| 201 | |
| 202 | function heightCalc() { |
| 203 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 204 | + mast.mastHeight() + topPdg; |
| 205 | wSize = fs.windowSize(pStartY); |
| 206 | pHeight = wSize.height; |
| 207 | } |
| 208 | |
| 209 | function initPanel() { |
| 210 | heightCalc(); |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 211 | createDetailsPanel(); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Safari has a bug where it renders the fixed-layout table wrong |
| 215 | // if you ask for the window's size too early |
| 216 | if (scope.onos.browser === 'safari') { |
| 217 | $timeout(initPanel); |
| 218 | } else { |
| 219 | initPanel(); |
| 220 | } |
| 221 | // create key bindings to handle panel |
Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 222 | ks.keyBindings(keyBindings); |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 223 | ks.gestureNotes([ |
| 224 | ['click', 'Select a row to show device details'], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 225 | ['scroll down', 'See more devices'], |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 226 | ]); |
| 227 | |
| 228 | // if the panelData changes |
| 229 | scope.$watch('panelData', function () { |
| 230 | if (!fs.isEmptyObject(scope.panelData)) { |
| 231 | populateDetails(scope.panelData); |
| 232 | detailsPanel.show(); |
| 233 | } |
| 234 | }); |
| 235 | |
| 236 | // if the window size changes |
| 237 | unbindWatch = $rootScope.$watchCollection( |
| 238 | function () { |
| 239 | return { |
| 240 | h: $window.innerHeight, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 241 | w: $window.innerWidth, |
Simon Hunt | e408af7 | 2017-06-14 18:32:06 -0700 | [diff] [blame] | 242 | }; |
| 243 | }, function () { |
| 244 | if (!fs.isEmptyObject(scope.panelData)) { |
| 245 | heightCalc(); |
| 246 | populateDetails(scope.panelData); |
| 247 | } |
| 248 | } |
| 249 | ); |
| 250 | |
| 251 | scope.$on('$destroy', function () { |
| 252 | unbindWatch(); |
| 253 | ks.unbindKeys(); |
| 254 | ps.destroyPanel(pName); |
| 255 | }); |
| 256 | }; |
| 257 | }]); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 258 | }()); |