Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [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 -- 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', |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 47 | detailsResp = 'yangModelDetailsResponse', |
| 48 | fileUploadUrl = '/onos/yang/models?modelId='; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 49 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 50 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 51 | function createDetailsPanel() { |
| 52 | detailsPanel = ps.createPanel(pName, { |
| 53 | width: pWidth, |
| 54 | margin: 0, |
| 55 | hideMargin: 0 |
| 56 | }); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 57 | detailsPanel.el().style({ top: pStartY + 'px'}); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 58 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
| 59 | detailsPanel.hide(); |
| 60 | } |
| 61 | |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 62 | function fixHeight(d, h) { |
| 63 | d.style({ height: h + 'px'}); |
| 64 | } |
| 65 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 66 | function populateDetails(details) { |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 67 | setUpPanel(); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 68 | populateTop(details); |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 69 | populateBottom(details.source); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 70 | |
| 71 | detailsPanel.height(pHeight); |
| 72 | // also have to fix the height of the source frame to make scroll work |
| 73 | fixHeight(srcFrame, pHeight - srcFudge); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | function setUpPanel() { |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 77 | var container, closeBtn; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 78 | detailsPanel.empty(); |
| 79 | |
| 80 | container = detailsPanel.append('div').classed('container', true); |
| 81 | |
| 82 | top = container.append('div').classed('top', true); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 83 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 84 | closeBtn = top.append('div').classed('close-btn', true); |
| 85 | addCloseBtn(closeBtn); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 86 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 87 | iconDiv = top.append('div').classed('dev-icon', true); |
| 88 | top.append('h2'); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 89 | top.append('hr'); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 90 | |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 91 | bottom = container.append('div').classed('bottom', true); |
Thomas Vachuska | 6af7f15 | 2017-09-12 14:56:44 -0700 | [diff] [blame] | 92 | bottom.append('h3').text('YANG Source'); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 93 | |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 94 | srcFrame = bottom.append('div').classed('src-frame', true); |
| 95 | srcDiv = srcFrame.append('div').classed('module-source', true); |
| 96 | srcDiv.append('pre'); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | function populateTop(details) { |
| 100 | is.loadEmbeddedIcon(iconDiv, 'nav_yang', 40); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 101 | top.select('h2') |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 102 | .text('Module ' + details.id + ' (' + details.revision + ')'); |
Thomas Vachuska | cde197c | 2017-03-23 17:51:08 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Thomas Vachuska | e0792f1 | 2017-03-31 00:15:06 -0700 | [diff] [blame] | 105 | function populateBottom(source) { |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 106 | var src = srcDiv.select('pre'); |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 107 | src.text(source.join('\n')); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | function closePanel() { |
| 111 | if (detailsPanel.isVisible()) { |
| 112 | $scope.selId = null; |
| 113 | detailsPanel.hide(); |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | function addCloseBtn(div) { |
| 120 | is.loadEmbeddedIcon(div, 'close', 20); |
| 121 | div.on('click', closePanel); |
| 122 | } |
| 123 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 124 | // callback invoked when data from a details request returns from server |
| 125 | function respDetailsCb(data) { |
| 126 | $scope.panelData = data.details; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 127 | $scope.$apply(); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 128 | $log.debug('YANG_MODEL>', detailsResp, data); |
| 129 | } |
| 130 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 131 | function handleEscape() { |
| 132 | return closePanel(); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | // defines view controller |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 137 | angular.module('ovYangModel', []) |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 138 | .controller('OvYangModelCtrl', [ |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 139 | '$log', '$scope', '$http', '$timeout', |
| 140 | 'TableBuilderService', 'TableDetailService', |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 141 | 'FnService', 'MastService', 'PanelService', 'WebSocketService', |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 142 | 'IconService', 'UrlFnService', 'FlashService', |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 143 | |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 144 | function (_$log_, _$scope_, $http, $timeout, tbs, tds, _fs_, _mast_, _ps_, _wss_, _is_, ufs, _flash_) { |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 145 | var handlers = {}; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 146 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 147 | $log = _$log_; |
| 148 | $scope = _$scope_; |
| 149 | fs = _fs_; |
| 150 | mast = _mast_; |
| 151 | ps = _ps_; |
| 152 | wss = _wss_; |
| 153 | is = _is_; |
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 | $scope.panelData = {}; |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 156 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 157 | // register response handler |
| 158 | handlers[detailsResp] = respDetailsCb; |
| 159 | wss.bindHandlers(handlers); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 160 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 161 | // row selection callback |
| 162 | function selCb($event, row) { |
| 163 | if ($scope.selId) { |
Thomas Vachuska | c307d21 | 2017-07-31 13:57:20 -0700 | [diff] [blame] | 164 | wss.sendEvent(detailsReq, { |
| 165 | modelId: row.modelId, |
| 166 | id: row.id, |
| 167 | revision: row.revision |
| 168 | }); |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 169 | } else { |
| 170 | $scope.hidePanel(); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 171 | } |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 172 | $log.debug('Got a click on:', row); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 173 | } |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 174 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 175 | tbs.buildTable({ |
| 176 | scope: $scope, |
| 177 | tag: 'yangModel', |
| 178 | selCb: selCb |
| 179 | }); |
| 180 | |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 181 | $scope.$on('YangFileChanged', function () { |
| 182 | var formData = new FormData(), |
| 183 | url, modelId, finished = false; |
| 184 | |
| 185 | if ($scope.yangFile) { |
| 186 | modelId = $scope.yangFile.name; |
| 187 | modelId = modelId.substr(0, modelId.lastIndexOf(".")); |
| 188 | url = fileUploadUrl + modelId; |
| 189 | formData.append('file', $scope.yangFile); |
| 190 | $log.info('Compiling', $scope.yangFile); |
| 191 | d3.select('#frame').classed('dropping', false); |
| 192 | |
| 193 | // FIXME: Replace this with dialog that shows progress... |
| 194 | for (var i = 0; i < 10; i++) { |
| 195 | $timeout(function () { |
| 196 | if (!finished) _flash_.flash('Compiling ' + modelId); |
| 197 | }, i * 1100); |
| 198 | } |
| 199 | |
| 200 | $http.post(url, formData, { |
| 201 | transformRequest: angular.identity, |
| 202 | headers: { |
| 203 | 'Content-Type': undefined |
| 204 | } |
| 205 | }) |
| 206 | .finally(function () { |
| 207 | finished = true; |
| 208 | _flash_.flash('Compile completed for ' + modelId); |
| 209 | $scope.sortCallback($scope.sortParams); |
| 210 | document.getElementById('inputYangFileForm').reset(); |
| 211 | }); |
| 212 | } |
| 213 | }); |
| 214 | |
| 215 | $scope.yangDropped = function() { |
| 216 | $scope.$emit('YangFileChanged'); |
| 217 | $scope.yangFile = null; |
| 218 | }; |
| 219 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 220 | $scope.$on('$destroy', function () { |
| 221 | wss.unbindHandlers(handlers); |
| 222 | }); |
| 223 | |
| 224 | $log.log('OvYangModelCtrl has been created'); |
| 225 | } |
| 226 | ]) |
| 227 | |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 228 | // triggers the input form to appear when button is clicked |
| 229 | .directive('triggerYangForm', function () { |
| 230 | return { |
| 231 | restrict: 'A', |
| 232 | link: function (scope, elem) { |
| 233 | elem.bind('click', function () { |
| 234 | document.getElementById('uploadYangFile') |
| 235 | .dispatchEvent(new MouseEvent('click')); |
| 236 | }); |
| 237 | } |
| 238 | }; |
| 239 | }) |
| 240 | |
| 241 | // binds the model file to the scope in scope.yangFile |
| 242 | // sends upload request to the server |
| 243 | .directive('yangFileModel', ['$parse', |
| 244 | function ($parse) { |
| 245 | return { |
| 246 | restrict: 'A', |
| 247 | link: function (scope, elem, attrs) { |
| 248 | var model = $parse(attrs.yangFileModel), |
| 249 | modelSetter = model.assign; |
| 250 | |
| 251 | elem.bind('change', function () { |
| 252 | scope.$apply(function () { |
| 253 | modelSetter(scope, elem[0].files[0]); |
| 254 | }); |
| 255 | scope.$emit('YangFileChanged'); |
| 256 | }); |
| 257 | } |
| 258 | }; |
| 259 | } |
| 260 | ]) |
| 261 | |
| 262 | .directive("yangfiledrop", ['$parse', '$document', function ($parse, $document) { |
| 263 | return { |
| 264 | restrict: "A", |
| 265 | link: function (scope, element, attrs) { |
| 266 | var onYangDrop = $parse(attrs.onFileDrop); |
| 267 | |
| 268 | // When an item is dragged over the document |
| 269 | var onDragOver = function (e) { |
| 270 | d3.select('#frame').classed('dropping', true); |
| 271 | e.preventDefault(); |
| 272 | }; |
| 273 | |
| 274 | // When the user leaves the window, cancels the drag or drops the item |
| 275 | var onDragEnd = function (e) { |
| 276 | d3.select('#frame').classed('dropping', false); |
| 277 | e.preventDefault(); |
| 278 | }; |
| 279 | |
| 280 | // When a file is dropped |
| 281 | var loadFile = function (file) { |
| 282 | scope.yangFile = file; |
| 283 | scope.$apply(onYangDrop(scope)); |
| 284 | }; |
| 285 | |
| 286 | // Dragging begins on the document |
| 287 | $document.bind("dragover", onDragOver); |
| 288 | |
| 289 | // Dragging ends on the overlay, which takes the whole window |
| 290 | element.bind("dragleave", onDragEnd) |
| 291 | .bind("drop", function (e) { |
| 292 | $log.info('Drag leave', e); |
| 293 | loadFile(e.dataTransfer.files[0]); |
| 294 | onDragEnd(e); |
| 295 | }); |
| 296 | } |
| 297 | }; |
| 298 | }]) |
| 299 | |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 300 | .directive('yangModelDetailsPanel', [ |
| 301 | '$rootScope', '$window', '$timeout', 'KeyService', |
| 302 | |
| 303 | function ($rootScope, $window, $timeout, ks) { |
| 304 | return function (scope) { |
| 305 | var unbindWatch; |
| 306 | |
| 307 | function heightCalc() { |
| 308 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 309 | + mast.mastHeight() + topPdg; |
| 310 | wSize = fs.windowSize(pStartY); |
Simon Hunt | 3ee1f43 | 2017-03-31 17:18:53 -0700 | [diff] [blame] | 311 | pHeight = wSize.height - hFudge; |
Simon Hunt | 7d5e984 | 2017-02-23 11:37:02 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | function initPanel() { |
| 315 | heightCalc(); |
| 316 | createDetailsPanel(); |
| 317 | } |
| 318 | |
| 319 | // Safari has a bug where it renders the fixed-layout table wrong |
| 320 | // if you ask for the window's size too early |
| 321 | if (scope.onos.browser === 'safari') { |
| 322 | $timeout(initPanel); |
| 323 | } else { |
| 324 | initPanel(); |
| 325 | } |
| 326 | |
| 327 | // create key bindings to handle panel |
| 328 | ks.keyBindings({ |
| 329 | esc: [handleEscape, 'Close the details panel'], |
| 330 | _helpFormat: ['esc'] |
| 331 | }); |
| 332 | ks.gestureNotes([ |
| 333 | ['click', 'Select a row to show YANG model details'], |
| 334 | ['scroll down', 'See more models'] |
| 335 | ]); |
| 336 | |
| 337 | // if the panelData changes |
| 338 | scope.$watch('panelData', function () { |
| 339 | if (!fs.isEmptyObject(scope.panelData)) { |
| 340 | populateDetails(scope.panelData); |
| 341 | detailsPanel.show(); |
| 342 | } |
| 343 | }); |
| 344 | |
| 345 | // if the window size changes |
| 346 | unbindWatch = $rootScope.$watchCollection( |
| 347 | function () { |
| 348 | return { |
| 349 | h: $window.innerHeight, |
| 350 | w: $window.innerWidth |
| 351 | }; |
| 352 | }, function () { |
| 353 | if (!fs.isEmptyObject(scope.panelData)) { |
| 354 | heightCalc(); |
| 355 | populateDetails(scope.panelData); |
| 356 | } |
| 357 | } |
| 358 | ); |
| 359 | |
| 360 | scope.$on('$destroy', function () { |
| 361 | unbindWatch(); |
| 362 | ks.unbindKeys(); |
| 363 | ps.destroyPanel(pName); |
| 364 | }); |
| 365 | }; |
| 366 | }]); |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 367 | |
| 368 | }()); |