blob: b95ebc805e0795b0a25091d87c02cc847408a6c9 [file] [log] [blame]
Thomas Vachuskae586b792015-03-26 13:59:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskae586b792015-03-26 13:59:38 -07003 *
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 Hunte408af72017-06-14 18:32:06 -070024 // injected refs
Steven Burrowsf50a1772017-09-14 11:58:15 +010025 var $log, $scope, $loc, fs, mast, ps, wss, is, ns, ks, dps;
Simon Hunte408af72017-06-14 18:32:06 -070026
27 // internal state
28 var detailsPanel,
29 pStartY,
30 pHeight,
31 top,
Steven Burrowsf50a1772017-09-14 11:58:15 +010032 wSize;
Simon Hunte408af72017-06-14 18:32:06 -070033
34 // constants
35 var topPdg = 28,
Simon Hunte408af72017-06-14 18:32:06 -070036 pName = 'host-details-panel',
37 detailsReq = 'hostDetailsRequest',
38 detailsResp = 'hostDetailsResponse',
39 nameChangeReq = 'hostNameChangeRequest',
40 nameChangeResp = 'hostNameChangeResponse';
41
Steven Burrowsf50a1772017-09-14 11:58:15 +010042 var keyBindings = {
43 esc: [closePanel, 'Close the details panel'],
44 _helpFormat: ['esc'],
45 };
Simon Hunte408af72017-06-14 18:32:06 -070046
47 function closePanel() {
48 if (detailsPanel.isVisible()) {
49 $scope.selId = null;
50 detailsPanel.hide();
51 return true;
52 }
53 return false;
54 }
55
Simon Hunte408af72017-06-14 18:32:06 -070056
57 function setUpPanel() {
Simon Hunte408af72017-06-14 18:32:06 -070058
Steven Burrowsf50a1772017-09-14 11:58:15 +010059 dps.empty();
60 dps.addContainers();
61 dps.addCloseButton(closePanel);
Simon Hunte408af72017-06-14 18:32:06 -070062
Steven Burrowsf50a1772017-09-14 11:58:15 +010063 var top = dps.top();
Simon Hunte408af72017-06-14 18:32:06 -070064
Simon Huntc9e83212017-10-09 16:52:07 -070065 dps.addHeading('host-icon', true);
Steven Burrowsf50a1772017-09-14 11:58:15 +010066 top.append('div').classed('top-content', true);
67
Simon Hunte408af72017-06-14 18:32:06 -070068 top.append('hr');
Simon Hunt10618f62017-06-15 19:30:52 -070069 }
Simon Hunte408af72017-06-14 18:32:06 -070070
Steven Burrowsf50a1772017-09-14 11:58:15 +010071 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 Hunte408af72017-06-14 18:32:06 -070080 }
81
Steven Burrowsf50a1772017-09-14 11:58:15 +010082 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 Hunte408af72017-06-14 18:32:06 -070086 }
87
88 function populateDetails(details) {
89 setUpPanel();
Steven Burrowsf50a1772017-09-14 11:58:15 +010090 populateTop(dps.select('.top-content'), details);
Simon Hunte408af72017-06-14 18:32:06 -070091 detailsPanel.height(pHeight);
Simon Hunt86943082017-06-15 13:18:42 -070092 // configure width based on content.. for now hardcoded
Simon Hunt10618f62017-06-15 19:30:52 -070093 detailsPanel.width(400);
Simon Hunte408af72017-06-14 18:32:06 -070094 }
95
96 function respDetailsCb(data) {
97 $scope.panelData = data.details;
Simon Hunte408af72017-06-14 18:32:06 -070098 $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 Burrowsf50a1772017-09-14 11:58:15 +0100108 function createDetailsPanel() {
109 detailsPanel = dps.create(pName, {
Simon Hunte408af72017-06-14 18:32:06 -0700110 width: wSize.width,
111 margin: 0,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100112 hideMargin: 0,
Steven Burrowsf50a1772017-09-14 11:58:15 +0100113 scope: $scope,
114 keyBindings: keyBindings,
115 nameChangeRequest: nameChangeReq,
Simon Hunte408af72017-06-14 18:32:06 -0700116 });
Steven Burrowsf50a1772017-09-14 11:58:15 +0100117
118 dps.setResponse(detailsResp, respDetailsCb);
119
Simon Hunte408af72017-06-14 18:32:06 -0700120 $scope.hidePanel = function () { detailsPanel.hide(); };
Simon Hunte408af72017-06-14 18:32:06 -0700121 }
122
123
124 // Defines the Host View controller...
Thomas Vachuskae586b792015-03-26 13:59:38 -0700125 angular.module('ovHost', [])
126 .controller('OvHostCtrl',
Simon Hunte408af72017-06-14 18:32:06 -0700127 ['$log', '$scope',
128 '$location',
129 'TableBuilderService',
130 'FnService', 'MastService', 'PanelService', 'WebSocketService',
Steven Burrowsf50a1772017-09-14 11:58:15 +0100131 'IconService', 'NavService', 'KeyService', 'DetailsPanelService',
Thomas Vachuskae586b792015-03-26 13:59:38 -0700132
Simon Hunte408af72017-06-14 18:32:06 -0700133 function (_$log_, _$scope_, _$location_,
134 tbs,
135 _fs_, _mast_, _ps_, _wss_,
Steven Burrowsf50a1772017-09-14 11:58:15 +0100136 _is_, _ns_, _ks_, _dps_) {
Simon Hunte408af72017-06-14 18:32:06 -0700137
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 Burrowsf50a1772017-09-14 11:58:15 +0100151 dps = _dps_;
Simon Hunte408af72017-06-14 18:32:06 -0700152
153 params = $loc.search();
154
155 $scope.panelData = {};
156
157 // details panel handlers
Simon Hunte408af72017-06-14 18:32:06 -0700158 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 Cole864cdd62015-04-02 15:46:47 -0700176 tbs.buildTable({
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700177 scope: $scope,
Simon Hunte408af72017-06-14 18:32:06 -0700178 tag: 'host',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100179 selCb: selCb,
Simon Hunte408af72017-06-14 18:32:06 -0700180 });
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 Burrowsf50a1772017-09-14 11:58:15 +0100189 dps.destroy();
Simon Hunte408af72017-06-14 18:32:06 -0700190 wss.unbindHandlers(handlers);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700191 });
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700192
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700193 $log.log('OvHostCtrl has been created');
Simon Hunte408af72017-06-14 18:32:06 -0700194 }])
195
196 .directive('hostDetailsPanel',
Steven Burrowsf50a1772017-09-14 11:58:15 +0100197 ['$rootScope', '$window', '$timeout', 'KeyService', 'DetailsPanelService',
Simon Hunte408af72017-06-14 18:32:06 -0700198 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 Burrowsf50a1772017-09-14 11:58:15 +0100211 createDetailsPanel();
Simon Hunte408af72017-06-14 18:32:06 -0700212 }
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 Burrowsf50a1772017-09-14 11:58:15 +0100222 ks.keyBindings(keyBindings);
Simon Hunte408af72017-06-14 18:32:06 -0700223 ks.gestureNotes([
224 ['click', 'Select a row to show device details'],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100225 ['scroll down', 'See more devices'],
Simon Hunte408af72017-06-14 18:32:06 -0700226 ]);
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 Burrows1c2a9682017-07-14 16:52:46 +0100241 w: $window.innerWidth,
Simon Hunte408af72017-06-14 18:32:06 -0700242 };
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 Vachuskae586b792015-03-26 13:59:38 -0700258}());