Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -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 -- Component Settings View Module |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame^] | 24 | // injected refs |
| 25 | var $log, $scope, wss, fs, ks, ps, is; |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 26 | |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame^] | 27 | // internal state |
| 28 | var detailsPanel, |
| 29 | panelData, |
| 30 | top, |
| 31 | pStartY, |
| 32 | pHeight, |
| 33 | wSize = false; |
| 34 | |
| 35 | // constants |
| 36 | var pName = 'settings-details-panel', |
| 37 | panelWidth = 540, |
| 38 | topPdg = 60, |
| 39 | propOrder = ['fqComponent', 'id', 'type', 'value', 'defValue', 'desc'], |
| 40 | friendlyProps = [ |
| 41 | 'Component', 'Property', 'Type', 'Value', 'Default Value', |
| 42 | 'Description' |
| 43 | ]; |
| 44 | |
| 45 | function createDetailsPanel() { |
| 46 | detailsPanel = ps.createPanel(pName, { |
| 47 | width: wSize.width, |
| 48 | margin: 0, |
| 49 | hideMargin: 0 |
| 50 | }); |
| 51 | |
| 52 | detailsPanel.el().style({ |
| 53 | position: 'absolute', |
| 54 | top: pStartY + 'px' |
| 55 | }); |
| 56 | |
| 57 | detailsPanel.hide(); |
| 58 | } |
| 59 | |
| 60 | function closePanel() { |
| 61 | if (detailsPanel.isVisible()) { |
| 62 | $scope.selId = null; |
| 63 | panelData = null; |
| 64 | detailsPanel.hide(); |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | function addCloseBtn(div) { |
| 71 | is.loadEmbeddedIcon(div, 'close', 26); |
| 72 | div.on('click', closePanel); |
| 73 | } |
| 74 | |
| 75 | function setUpPanel() { |
| 76 | var container, closeBtn, div; |
| 77 | |
| 78 | detailsPanel.empty(); |
| 79 | detailsPanel.width(panelWidth); |
| 80 | |
| 81 | container = detailsPanel.append('div').classed('container', true); |
| 82 | |
| 83 | top = container.append('div').classed('top', true); |
| 84 | closeBtn = top.append('div').classed('close-btn', true); |
| 85 | addCloseBtn(closeBtn); |
| 86 | |
| 87 | div = top.append('div').classed('top-content', true); |
| 88 | |
| 89 | function ndiv(cls, addTable) { |
| 90 | var d = div.append('div').classed(cls, true); |
| 91 | if (addTable) { |
| 92 | d.append('table'); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | ndiv('settings-title'); |
| 97 | ndiv('settings-props', true); |
| 98 | } |
| 99 | |
| 100 | function addProp(tbody, index, value) { |
| 101 | var tr = tbody.append('tr'); |
| 102 | |
| 103 | function addCell(cls, txt) { |
| 104 | tr.append('td').attr('class', cls).html(txt); |
| 105 | } |
| 106 | |
| 107 | addCell('label', friendlyProps[index] + ':'); |
| 108 | addCell('value', value); |
| 109 | } |
| 110 | |
| 111 | function populateTop(details) { |
| 112 | var propsBody = top.select('.settings-props').append('tbody'); |
| 113 | |
| 114 | top.select('.settings-title').text(details.id); |
| 115 | |
| 116 | propOrder.forEach(function (prop, i) { |
| 117 | addProp(propsBody, i, details[prop]); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | function populateDetails() { |
| 122 | setUpPanel(); |
| 123 | populateTop(panelData); |
| 124 | detailsPanel.height(pHeight); |
| 125 | } |
| 126 | |
| 127 | angular.module('ovSettings', []) |
| 128 | .controller('OvSettingsCtrl', |
| 129 | ['$log', '$scope', |
| 130 | 'WebSocketService', 'FnService', 'KeyService', 'PanelService', |
| 131 | 'IconService', 'TableBuilderService', |
| 132 | |
| 133 | function (_$log_, _$scope_, _wss_, _fs_, _ks_, _ps_, _is_, tbs) { |
| 134 | $log = _$log_; |
| 135 | $scope = _$scope_; |
| 136 | wss = _wss_; |
| 137 | fs = _fs_; |
| 138 | ks = _ks_; |
| 139 | ps = _ps_; |
| 140 | is = _is_; |
| 141 | $scope.panelData = {}; |
| 142 | |
| 143 | function selCb($event, row) { |
| 144 | if ($scope.selId) { |
| 145 | panelData = row; |
| 146 | populateDetails(); |
| 147 | detailsPanel.show(); |
| 148 | } else { |
| 149 | panelData = null; |
| 150 | detailsPanel.hide(); |
| 151 | } |
| 152 | } |
| 153 | |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 154 | tbs.buildTable({ |
| 155 | scope: $scope, |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame^] | 156 | tag: 'setting', |
| 157 | selCb: selCb |
| 158 | }); |
| 159 | |
| 160 | ks.keyBindings({ |
| 161 | esc: [$scope.selectCallback, 'Deselect property'], |
| 162 | _helpFormat: ['esc'] |
| 163 | }); |
| 164 | ks.gestureNotes([ |
| 165 | ['click row', 'Select / deselect settings property'], |
| 166 | ['scroll down', 'See more settings'] |
| 167 | ]); |
| 168 | |
| 169 | $scope.$on('$destroy', function () { |
| 170 | ks.unbindKeys(); |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 171 | }); |
| 172 | |
| 173 | $log.log('OvSettingsCtrl has been created'); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame^] | 174 | }]) |
| 175 | |
| 176 | .directive('settingsDetailsPanel', |
| 177 | ['$rootScope', '$window', '$timeout', 'KeyService', |
| 178 | function ($rootScope, $window, $timeout, ks) { |
| 179 | return function (scope) { |
| 180 | var unbindWatch; |
| 181 | |
| 182 | function heightCalc() { |
| 183 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 184 | + topPdg; |
| 185 | wSize = fs.windowSize(pStartY); |
| 186 | pHeight = wSize.height; |
| 187 | } |
| 188 | |
| 189 | function initPanel() { |
| 190 | heightCalc(); |
| 191 | createDetailsPanel(); |
| 192 | $log.debug('start to initialize panel!'); |
| 193 | } |
| 194 | |
| 195 | // Safari has a bug where it renders the fixed-layout table wrong |
| 196 | // if you ask for the window's size too early |
| 197 | if (scope.onos.browser === 'safari') { |
| 198 | $timeout(initPanel); |
| 199 | } else { |
| 200 | initPanel(); |
| 201 | } |
| 202 | // create key bindings to handle panel |
| 203 | ks.keyBindings({ |
| 204 | esc: [closePanel, 'Close the details panel'], |
| 205 | _helpFormat: ['esc'] |
| 206 | }); |
| 207 | ks.gestureNotes([ |
| 208 | ['click', 'Select a row to show property details'], |
| 209 | ['scroll down', 'See more properties'] |
| 210 | ]); |
| 211 | |
| 212 | // if the window size changes |
| 213 | unbindWatch = $rootScope.$watchCollection( |
| 214 | function () { |
| 215 | return { |
| 216 | h: $window.innerHeight, |
| 217 | w: $window.innerWidth |
| 218 | }; |
| 219 | }, function () { |
| 220 | if (panelData) { |
| 221 | heightCalc(); |
| 222 | populateDetails(); |
| 223 | } |
| 224 | } |
| 225 | ); |
| 226 | |
| 227 | scope.$on('$destroy', function () { |
| 228 | panelData = null; |
| 229 | unbindWatch(); |
| 230 | ks.unbindKeys(); |
| 231 | ps.destroyPanel(pName); |
| 232 | }); |
| 233 | }; |
| 234 | }]); |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 235 | }()); |