Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -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 -- Cluster View Module |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 24 | // injected references |
| 25 | var $log, $scope, $location, fs, tbs, ns, mast, ps, wss, is, ks; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 26 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 27 | // internal state |
| 28 | var detailsPanel, |
| 29 | pStartY, |
| 30 | pHeight, |
| 31 | top, |
| 32 | topTable, |
| 33 | bottom, |
| 34 | iconDiv, |
| 35 | nameDiv, |
| 36 | wSize; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 37 | |
| 38 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 39 | // constants |
| 40 | var topPdg = 28, |
| 41 | ctnrPdg = 24, |
| 42 | scrollSize = 17, |
| 43 | portsTblPdg = 50, |
| 44 | htPdg = 479, |
| 45 | wtPdg = 532, |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 46 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 47 | pName = 'cluster-details-panel', |
| 48 | detailsReq = 'clusterDetailsRequest', |
| 49 | detailsResp = 'clusterDetailsResponse', |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 50 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 51 | propOrder = [ |
| 52 | 'id', 'ip' |
| 53 | ], |
| 54 | friendlyProps = [ |
| 55 | 'Node ID', 'IP Address' |
| 56 | ]; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 57 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 58 | function closePanel() { |
| 59 | if (detailsPanel.isVisible()) { |
| 60 | $scope.selId = null; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 61 | detailsPanel.hide(); |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 62 | return true; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 63 | } |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 66 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 67 | function addCloseBtn(div) { |
| 68 | is.loadEmbeddedIcon(div, 'close', 20); |
| 69 | div.on('click', closePanel); |
| 70 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 71 | |
Viswanath KSP | 8601627 | 2016-10-27 09:49:46 +0530 | [diff] [blame] | 72 | function handleEscape() { |
| 73 | return closePanel(); |
| 74 | } |
| 75 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 76 | function setUpPanel() { |
| 77 | var container, closeBtn, tblDiv; |
| 78 | detailsPanel.empty(); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 79 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 80 | container = detailsPanel.append('div').classed('container', true); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 81 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 82 | top = container.append('div').classed('top', true); |
| 83 | closeBtn = top.append('div').classed('close-btn', true); |
| 84 | addCloseBtn(closeBtn); |
| 85 | iconDiv = top.append('div').classed('dev-icon', true); |
| 86 | top.append('h2'); |
| 87 | topTable = top.append('div').classed('top-content', true) |
| 88 | .append('table'); |
| 89 | top.append('hr'); |
| 90 | |
| 91 | //ToDo add more details |
| 92 | } |
| 93 | |
| 94 | function addProp(tbody, index, value) { |
| 95 | var tr = tbody.append('tr'); |
| 96 | |
| 97 | function addCell(cls, txt) { |
| 98 | tr.append('td').attr('class', cls).html(txt); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 99 | } |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 100 | addCell('label', friendlyProps[index] + ' :'); |
| 101 | addCell('value', value); |
| 102 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 103 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 104 | function populateTop(details) { |
| 105 | is.loadEmbeddedIcon(iconDiv, 'node', 40); |
| 106 | top.select('h2').html(details.id); |
| 107 | |
| 108 | var tbody = topTable.append('tbody'); |
| 109 | |
| 110 | propOrder.forEach(function (prop, i) { |
| 111 | addProp(tbody, i, details[prop]); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | function createDetailsPane() { |
| 116 | detailsPanel = ps.createPanel(pName, { |
| 117 | width: wSize.width, |
| 118 | margin: 0, |
| 119 | hideMargin: 0 |
| 120 | }); |
| 121 | detailsPanel.el().style({ |
| 122 | position: 'absolute', |
| 123 | top: pStartY + 'px' |
| 124 | }); |
| 125 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
| 126 | detailsPanel.hide(); |
| 127 | } |
| 128 | |
| 129 | function populateDetails(details) { |
| 130 | setUpPanel(); |
| 131 | populateTop(details); |
| 132 | |
| 133 | //ToDo add more details |
| 134 | detailsPanel.height(pHeight); |
| 135 | detailsPanel.width(wtPdg); |
| 136 | } |
| 137 | |
| 138 | function respDetailsCb(data) { |
| 139 | $scope.panelData = data.details; |
| 140 | $scope.$apply(); |
| 141 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 142 | |
| 143 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 144 | angular.module('ovCluster', []) |
| 145 | .controller('OvClusterCtrl', |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 146 | ['$log', '$scope', 'TableBuilderService', 'NavService', 'MastService', |
| 147 | 'PanelService', 'KeyService', 'IconService','WebSocketService', |
| 148 | 'FnService', |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 149 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 150 | function (_$log_, _$scope_, tbs, _ns_, _mast_, _ps_, _ks_, _is_, |
| 151 | _wss_, _fs_) { |
| 152 | var handlers = {}; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 153 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 154 | $log = _$log_; |
| 155 | $scope = _$scope_; |
| 156 | fs = _fs_; |
| 157 | ns = _ns_; |
| 158 | is = _is_; |
| 159 | wss = _wss_; |
| 160 | mast = _mast_; |
| 161 | ps = _ps_; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 162 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 163 | $scope.panelData = {}; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 164 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 165 | tbs.buildTable({ |
| 166 | scope: $scope, |
| 167 | selCb: selCb, |
| 168 | tag: 'cluster' |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 169 | }); |
| 170 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 171 | // details panel handlers |
| 172 | handlers[detailsResp] = respDetailsCb; |
| 173 | wss.bindHandlers(handlers); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 174 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 175 | function selCb($event, row) { |
| 176 | if ($scope.selId) { |
| 177 | wss.sendEvent(detailsReq, {id: row.id}); |
| 178 | } else { |
| 179 | $scope.hidePanel(); |
| 180 | } |
| 181 | $log.debug('Got a click on:', row); |
| 182 | } |
| 183 | |
| 184 | $scope.$on('$destroy', function () { |
| 185 | wss.unbindHandlers(handlers); |
| 186 | }); |
| 187 | |
| 188 | $log.log('OvClusterCtrl has been created'); |
| 189 | }]) |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 190 | |
Simon Hunt | a271e0a | 2016-10-26 10:59:14 -0700 | [diff] [blame] | 191 | .directive('clusterDetailsPanel', |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 192 | ['$rootScope', '$window', '$timeout', 'KeyService', |
| 193 | function ($rootScope, $window, $timeout, ks) { |
| 194 | return function (scope) { |
| 195 | var unbindWatch; |
| 196 | |
| 197 | function heightCalc() { |
| 198 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 199 | + mast.mastHeight() + topPdg; |
| 200 | wSize = fs.windowSize(pStartY); |
| 201 | pHeight = wSize.height; |
| 202 | } |
| 203 | |
| 204 | function initPanel() { |
| 205 | heightCalc(); |
| 206 | createDetailsPane(); |
| 207 | } |
| 208 | |
| 209 | // Safari has a bug where it renders the fixed-layout table wrong |
| 210 | // if you ask for the window's size too early |
| 211 | if (scope.onos.browser === 'safari') { |
| 212 | $timeout(initPanel); |
| 213 | } else { |
| 214 | initPanel(); |
| 215 | } |
Viswanath KSP | 8601627 | 2016-10-27 09:49:46 +0530 | [diff] [blame] | 216 | // create key bindings to handle panel |
| 217 | ks.keyBindings({ |
| 218 | esc: [handleEscape, 'Close the details panel'], |
| 219 | _helpFormat: ['esc'] |
| 220 | }); |
| 221 | ks.gestureNotes([ |
| 222 | ['click', 'Select a row to show cluster node details'], |
| 223 | ['scroll down', 'See available cluster nodes'] |
| 224 | ]); |
Simon Hunt | a271e0a | 2016-10-26 10:59:14 -0700 | [diff] [blame] | 225 | // if the panelData changes |
| 226 | scope.$watch('panelData', function () { |
| 227 | if (!fs.isEmptyObject(scope.panelData)) { |
| 228 | populateDetails(scope.panelData); |
| 229 | detailsPanel.show(); |
| 230 | } |
| 231 | }); |
| 232 | |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 233 | // if the window size changes |
| 234 | unbindWatch = $rootScope.$watchCollection( |
| 235 | function () { |
| 236 | return { |
| 237 | h: $window.innerHeight, |
| 238 | w: $window.innerWidth |
| 239 | }; |
| 240 | }, function () { |
| 241 | if (!fs.isEmptyObject(scope.panelData)) { |
| 242 | heightCalc(); |
| 243 | populateDetails(scope.panelData); |
| 244 | } |
| 245 | } |
| 246 | ); |
| 247 | |
| 248 | scope.$on('$destroy', function () { |
| 249 | unbindWatch(); |
| 250 | ps.destroyPanel(pName); |
| 251 | }); |
| 252 | }; |
| 253 | }]); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 254 | }()); |