Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -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 | 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 |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 25 | var $log, $scope, 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, |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 39 | propOrder = ['fqComponent', 'prop', 'type', 'value', 'defValue', 'desc'], |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 40 | friendlyProps = [ |
Thomas Vachuska | 6af7f15 | 2017-09-12 14:56:44 -0700 | [diff] [blame] | 41 | 'Component', 'Property', 'Type', 'Value', 'Default', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 42 | 'Description', |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 43 | ]; |
| 44 | |
| 45 | function createDetailsPanel() { |
| 46 | detailsPanel = ps.createPanel(pName, { |
| 47 | width: wSize.width, |
| 48 | margin: 0, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 49 | hideMargin: 0, |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 50 | }); |
| 51 | |
| 52 | detailsPanel.el().style({ |
| 53 | position: 'absolute', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 54 | top: pStartY + 'px', |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 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) { |
Thomas Vachuska | a441074 | 2017-09-14 09:59:16 -0700 | [diff] [blame] | 71 | is.loadEmbeddedIcon(div, 'close', 20); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 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) { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 90 | var d = div.append('div').classed(cls, true); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 91 | if (addTable) { |
| 92 | d.append('table'); |
| 93 | } |
| 94 | } |
| 95 | |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 96 | ndiv('settings-title-1'); |
| 97 | ndiv('settings-title-2'); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 98 | ndiv('settings-props', true); |
| 99 | } |
| 100 | |
| 101 | function addProp(tbody, index, value) { |
| 102 | var tr = tbody.append('tr'); |
| 103 | |
| 104 | function addCell(cls, txt) { |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 105 | tr.append('td').attr('class', cls).text(txt); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | addCell('label', friendlyProps[index] + ':'); |
| 109 | addCell('value', value); |
| 110 | } |
| 111 | |
| 112 | function populateTop(details) { |
Thomas Vachuska | 6af7f15 | 2017-09-12 14:56:44 -0700 | [diff] [blame] | 113 | var propsBody = top.select('.settings-props table').append('tbody'); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 114 | |
Simon Hunt | 50e80ea | 2016-06-23 01:40:30 -0700 | [diff] [blame] | 115 | top.select('.settings-title-1').text(details.component); |
| 116 | top.select('.settings-title-2').text(details.prop); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 117 | |
| 118 | propOrder.forEach(function (prop, i) { |
| 119 | addProp(propsBody, i, details[prop]); |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | function populateDetails() { |
| 124 | setUpPanel(); |
| 125 | populateTop(panelData); |
| 126 | detailsPanel.height(pHeight); |
| 127 | } |
| 128 | |
| 129 | angular.module('ovSettings', []) |
| 130 | .controller('OvSettingsCtrl', |
| 131 | ['$log', '$scope', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 132 | 'FnService', 'KeyService', 'PanelService', |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 133 | 'IconService', 'TableBuilderService', |
| 134 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 135 | function (_$log_, _$scope_, _fs_, _ks_, _ps_, _is_, tbs) { |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 136 | $log = _$log_; |
| 137 | $scope = _$scope_; |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 138 | fs = _fs_; |
| 139 | ks = _ks_; |
| 140 | ps = _ps_; |
| 141 | is = _is_; |
| 142 | $scope.panelData = {}; |
| 143 | |
| 144 | function selCb($event, row) { |
| 145 | if ($scope.selId) { |
| 146 | panelData = row; |
| 147 | populateDetails(); |
| 148 | detailsPanel.show(); |
| 149 | } else { |
| 150 | panelData = null; |
| 151 | detailsPanel.hide(); |
| 152 | } |
| 153 | } |
| 154 | |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 155 | tbs.buildTable({ |
| 156 | scope: $scope, |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 157 | tag: 'setting', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 158 | selCb: selCb, |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 159 | }); |
| 160 | |
| 161 | ks.keyBindings({ |
| 162 | esc: [$scope.selectCallback, 'Deselect property'], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 163 | _helpFormat: ['esc'], |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 164 | }); |
| 165 | ks.gestureNotes([ |
| 166 | ['click row', 'Select / deselect settings property'], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 167 | ['scroll down', 'See more settings'], |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 168 | ]); |
| 169 | |
| 170 | $scope.$on('$destroy', function () { |
| 171 | ks.unbindKeys(); |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 172 | }); |
| 173 | |
| 174 | $log.log('OvSettingsCtrl has been created'); |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 175 | }]) |
| 176 | |
| 177 | .directive('settingsDetailsPanel', |
| 178 | ['$rootScope', '$window', '$timeout', 'KeyService', |
| 179 | function ($rootScope, $window, $timeout, ks) { |
| 180 | return function (scope) { |
| 181 | var unbindWatch; |
| 182 | |
| 183 | function heightCalc() { |
| 184 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 185 | + topPdg; |
| 186 | wSize = fs.windowSize(pStartY); |
| 187 | pHeight = wSize.height; |
| 188 | } |
| 189 | |
| 190 | function initPanel() { |
| 191 | heightCalc(); |
| 192 | createDetailsPanel(); |
| 193 | $log.debug('start to initialize panel!'); |
| 194 | } |
| 195 | |
| 196 | // Safari has a bug where it renders the fixed-layout table wrong |
| 197 | // if you ask for the window's size too early |
| 198 | if (scope.onos.browser === 'safari') { |
| 199 | $timeout(initPanel); |
| 200 | } else { |
| 201 | initPanel(); |
| 202 | } |
| 203 | // create key bindings to handle panel |
| 204 | ks.keyBindings({ |
| 205 | esc: [closePanel, 'Close the details panel'], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 206 | _helpFormat: ['esc'], |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 207 | }); |
| 208 | ks.gestureNotes([ |
| 209 | ['click', 'Select a row to show property details'], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 210 | ['scroll down', 'See more properties'], |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 211 | ]); |
| 212 | |
| 213 | // if the window size changes |
| 214 | unbindWatch = $rootScope.$watchCollection( |
| 215 | function () { |
| 216 | return { |
| 217 | h: $window.innerHeight, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 218 | w: $window.innerWidth, |
Simon Hunt | d721f29 | 2016-06-22 21:42:23 -0700 | [diff] [blame] | 219 | }; |
| 220 | }, function () { |
| 221 | if (panelData) { |
| 222 | heightCalc(); |
| 223 | populateDetails(); |
| 224 | } |
| 225 | } |
| 226 | ); |
| 227 | |
| 228 | scope.$on('$destroy', function () { |
| 229 | panelData = null; |
| 230 | unbindWatch(); |
| 231 | ks.unbindKeys(); |
| 232 | ps.destroyPanel(pName); |
| 233 | }); |
| 234 | }; |
| 235 | }]); |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 236 | }()); |