Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 -- YANG Model table view |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
| 24 | // injected refs |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 25 | var $log, $scope, fs, mast, ps, wss, is; |
| 26 | |
| 27 | // constants |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 28 | var topPdg = 20, |
| 29 | hFudge = 12, |
| 30 | srcFudge = 130, |
| 31 | pWidth = 800; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 32 | |
| 33 | // internal state |
| 34 | var detailsPanel, |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 35 | pStartY, |
| 36 | pHeight, |
| 37 | wSize, |
| 38 | top, |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 39 | bottom, |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 40 | iconDiv, |
| 41 | srcFrame, |
| 42 | srcDiv; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 43 | |
| 44 | // constants |
| 45 | var pName = 'yang-model-details-panel', |
| 46 | detailsReq = 'yangModelDetailsRequest', |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 47 | detailsResp = 'yangModelDetailsResponse'; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 49 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 50 | function createDetailsPanel() { |
| 51 | detailsPanel = ps.createPanel(pName, { |
| 52 | width: pWidth, |
| 53 | margin: 0, |
| 54 | hideMargin: 0 |
| 55 | }); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 56 | detailsPanel.el().style({ top: pStartY + 'px'}); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 57 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
| 58 | detailsPanel.hide(); |
| 59 | } |
| 60 | |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 61 | function fixHeight(d, h) { |
| 62 | d.style({ height: h + 'px'}); |
| 63 | } |
| 64 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 65 | function populateDetails(details) { |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 66 | setUpPanel(); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 67 | populateTop(details); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 68 | populateBottom(details.source); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 69 | |
| 70 | detailsPanel.height(pHeight); |
| 71 | // also have to fix the height of the source frame to make scroll work |
| 72 | fixHeight(srcFrame, pHeight - srcFudge); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | function setUpPanel() { |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 76 | var container, closeBtn; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 77 | detailsPanel.empty(); |
| 78 | |
| 79 | container = detailsPanel.append('div').classed('container', true); |
| 80 | |
| 81 | top = container.append('div').classed('top', true); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 82 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 83 | closeBtn = top.append('div').classed('close-btn', true); |
| 84 | addCloseBtn(closeBtn); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 85 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 86 | iconDiv = top.append('div').classed('dev-icon', true); |
| 87 | top.append('h2'); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 88 | top.append('hr'); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 89 | |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 90 | bottom = container.append('div').classed('bottom', true); |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 91 | bottom.append('h2').text('YANG Source'); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 92 | |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 93 | srcFrame = bottom.append('div').classed('src-frame', true); |
| 94 | srcDiv = srcFrame.append('div').classed('module-source', true); |
| 95 | srcDiv.append('pre'); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | function populateTop(details) { |
| 99 | is.loadEmbeddedIcon(iconDiv, 'nav_yang', 40); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 100 | top.select('h2') |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 101 | .text('Module ' + details.id + ' (' + details.revision + ')'); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 104 | function populateBottom(source) { |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 105 | var src = srcDiv.select('pre'); |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 106 | src.text(source.join('\n')); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | function closePanel() { |
| 110 | if (detailsPanel.isVisible()) { |
| 111 | $scope.selId = null; |
| 112 | detailsPanel.hide(); |
| 113 | return true; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | function addCloseBtn(div) { |
| 119 | is.loadEmbeddedIcon(div, 'close', 20); |
| 120 | div.on('click', closePanel); |
| 121 | } |
| 122 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 123 | // callback invoked when data from a details request returns from server |
| 124 | function respDetailsCb(data) { |
| 125 | $scope.panelData = data.details; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 126 | $scope.$apply(); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 127 | $log.debug('YANG_MODEL>', detailsResp, data); |
| 128 | } |
| 129 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 130 | function handleEscape() { |
| 131 | return closePanel(); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // defines view controller |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 136 | angular.module('ovYangModel', []) |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 137 | .controller('OvYangModelCtrl', [ |
| 138 | '$log', '$scope', 'TableBuilderService', 'TableDetailService', |
| 139 | 'FnService', 'MastService', 'PanelService', 'WebSocketService', |
| 140 | 'IconService', |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 141 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 142 | function (_$log_, _$scope_, tbs, tds, _fs_, _mast_, _ps_, _wss_, _is_) { |
| 143 | var handlers = {}; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 144 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 145 | $log = _$log_; |
| 146 | $scope = _$scope_; |
| 147 | fs = _fs_; |
| 148 | mast = _mast_; |
| 149 | ps = _ps_; |
| 150 | wss = _wss_; |
| 151 | is = _is_; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 152 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 153 | $scope.panelData = {}; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 154 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 155 | // register response handler |
| 156 | handlers[detailsResp] = respDetailsCb; |
| 157 | wss.bindHandlers(handlers); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 158 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 159 | // row selection callback |
| 160 | function selCb($event, row) { |
| 161 | if ($scope.selId) { |
Thomas Vachuska | 401a1d3 | 2017-03-31 09:58:11 -0700 | [diff] [blame] | 162 | wss.sendEvent(detailsReq, { modelId: row.modelId, id: row.id }); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 163 | } else { |
| 164 | $scope.hidePanel(); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 165 | } |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 166 | $log.debug('Got a click on:', row); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 167 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 168 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 169 | tbs.buildTable({ |
| 170 | scope: $scope, |
| 171 | tag: 'yangModel', |
| 172 | selCb: selCb |
| 173 | }); |
| 174 | |
| 175 | $scope.$on('$destroy', function () { |
| 176 | wss.unbindHandlers(handlers); |
| 177 | }); |
| 178 | |
| 179 | $log.log('OvYangModelCtrl has been created'); |
| 180 | } |
| 181 | ]) |
| 182 | |
| 183 | .directive('yangModelDetailsPanel', [ |
| 184 | '$rootScope', '$window', '$timeout', 'KeyService', |
| 185 | |
| 186 | function ($rootScope, $window, $timeout, ks) { |
| 187 | return function (scope) { |
| 188 | var unbindWatch; |
| 189 | |
| 190 | function heightCalc() { |
| 191 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 192 | + mast.mastHeight() + topPdg; |
| 193 | wSize = fs.windowSize(pStartY); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 194 | pHeight = wSize.height - hFudge; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | function initPanel() { |
| 198 | heightCalc(); |
| 199 | createDetailsPanel(); |
| 200 | } |
| 201 | |
| 202 | // Safari has a bug where it renders the fixed-layout table wrong |
| 203 | // if you ask for the window's size too early |
| 204 | if (scope.onos.browser === 'safari') { |
| 205 | $timeout(initPanel); |
| 206 | } else { |
| 207 | initPanel(); |
| 208 | } |
| 209 | |
| 210 | // create key bindings to handle panel |
| 211 | ks.keyBindings({ |
| 212 | esc: [handleEscape, 'Close the details panel'], |
| 213 | _helpFormat: ['esc'] |
| 214 | }); |
| 215 | ks.gestureNotes([ |
| 216 | ['click', 'Select a row to show YANG model details'], |
| 217 | ['scroll down', 'See more models'] |
| 218 | ]); |
| 219 | |
| 220 | // if the panelData changes |
| 221 | scope.$watch('panelData', function () { |
| 222 | if (!fs.isEmptyObject(scope.panelData)) { |
| 223 | populateDetails(scope.panelData); |
| 224 | detailsPanel.show(); |
| 225 | } |
| 226 | }); |
| 227 | |
| 228 | // if the window size changes |
| 229 | unbindWatch = $rootScope.$watchCollection( |
| 230 | function () { |
| 231 | return { |
| 232 | h: $window.innerHeight, |
| 233 | w: $window.innerWidth |
| 234 | }; |
| 235 | }, function () { |
| 236 | if (!fs.isEmptyObject(scope.panelData)) { |
| 237 | heightCalc(); |
| 238 | populateDetails(scope.panelData); |
| 239 | } |
| 240 | } |
| 241 | ); |
| 242 | |
| 243 | scope.$on('$destroy', function () { |
| 244 | unbindWatch(); |
| 245 | ks.unbindKeys(); |
| 246 | ps.destroyPanel(pName); |
| 247 | }); |
| 248 | }; |
| 249 | }]); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 250 | |
| 251 | }()); |