blob: e35b8df61d325a1356e11423686e66f8c38aaf4c [file] [log] [blame]
Simon Huntcc035c52017-02-22 21:12:51 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Huntcc035c52017-02-22 21:12:51 -08003 *
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 Hunt7d5e9842017-02-23 11:37:02 -080025 var $log, $scope, fs, mast, ps, wss, is;
26
27 // constants
Simon Hunt3ee1f432017-03-31 17:18:53 -070028 var topPdg = 20,
29 hFudge = 12,
30 srcFudge = 130,
31 pWidth = 800;
Simon Huntcc035c52017-02-22 21:12:51 -080032
33 // internal state
34 var detailsPanel,
Simon Hunt7d5e9842017-02-23 11:37:02 -080035 pStartY,
36 pHeight,
37 wSize,
38 top,
Thomas Vachuskacde197c2017-03-23 17:51:08 -070039 bottom,
Simon Hunt3ee1f432017-03-31 17:18:53 -070040 iconDiv,
41 srcFrame,
42 srcDiv;
Simon Huntcc035c52017-02-22 21:12:51 -080043
44 // constants
45 var pName = 'yang-model-details-panel',
46 detailsReq = 'yangModelDetailsRequest',
Thomas Vachuskaad37e372017-08-03 12:07:01 -070047 detailsResp = 'yangModelDetailsResponse',
48 fileUploadUrl = '/onos/yang/models?modelId=';
Simon Huntcc035c52017-02-22 21:12:51 -080049
Simon Hunt7d5e9842017-02-23 11:37:02 -080050
Simon Hunt7d5e9842017-02-23 11:37:02 -080051 function createDetailsPanel() {
52 detailsPanel = ps.createPanel(pName, {
53 width: pWidth,
54 margin: 0,
55 hideMargin: 0
56 });
Simon Hunt3ee1f432017-03-31 17:18:53 -070057 detailsPanel.el().style({ top: pStartY + 'px'});
Simon Hunt7d5e9842017-02-23 11:37:02 -080058 $scope.hidePanel = function () { detailsPanel.hide(); };
59 detailsPanel.hide();
60 }
61
Simon Hunt3ee1f432017-03-31 17:18:53 -070062 function fixHeight(d, h) {
63 d.style({ height: h + 'px'});
64 }
65
Simon Hunt7d5e9842017-02-23 11:37:02 -080066 function populateDetails(details) {
Simon Hunt7d5e9842017-02-23 11:37:02 -080067 setUpPanel();
Thomas Vachuskacde197c2017-03-23 17:51:08 -070068 populateTop(details);
Thomas Vachuskae0792f12017-03-31 00:15:06 -070069 populateBottom(details.source);
Simon Hunt3ee1f432017-03-31 17:18:53 -070070
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 Hunt7d5e9842017-02-23 11:37:02 -080074 }
75
76 function setUpPanel() {
Simon Hunt3ee1f432017-03-31 17:18:53 -070077 var container, closeBtn;
Simon Hunt7d5e9842017-02-23 11:37:02 -080078 detailsPanel.empty();
79
80 container = detailsPanel.append('div').classed('container', true);
81
82 top = container.append('div').classed('top', true);
Simon Hunt3ee1f432017-03-31 17:18:53 -070083
Simon Hunt7d5e9842017-02-23 11:37:02 -080084 closeBtn = top.append('div').classed('close-btn', true);
85 addCloseBtn(closeBtn);
Simon Hunt3ee1f432017-03-31 17:18:53 -070086
Simon Hunt7d5e9842017-02-23 11:37:02 -080087 iconDiv = top.append('div').classed('dev-icon', true);
88 top.append('h2');
Thomas Vachuskacde197c2017-03-23 17:51:08 -070089 top.append('hr');
Simon Hunt7d5e9842017-02-23 11:37:02 -080090
Thomas Vachuskacde197c2017-03-23 17:51:08 -070091 bottom = container.append('div').classed('bottom', true);
Thomas Vachuska6af7f152017-09-12 14:56:44 -070092 bottom.append('h3').text('YANG Source');
Simon Hunt7d5e9842017-02-23 11:37:02 -080093
Simon Hunt3ee1f432017-03-31 17:18:53 -070094 srcFrame = bottom.append('div').classed('src-frame', true);
95 srcDiv = srcFrame.append('div').classed('module-source', true);
96 srcDiv.append('pre');
Thomas Vachuskacde197c2017-03-23 17:51:08 -070097 }
98
99 function populateTop(details) {
100 is.loadEmbeddedIcon(iconDiv, 'nav_yang', 40);
Simon Hunt3ee1f432017-03-31 17:18:53 -0700101 top.select('h2')
Simon Hunt239f09e2017-05-18 13:10:09 -0700102 .text('Module ' + details.id + ' (' + details.revision + ')');
Thomas Vachuskacde197c2017-03-23 17:51:08 -0700103 }
104
Thomas Vachuskae0792f12017-03-31 00:15:06 -0700105 function populateBottom(source) {
Simon Hunt3ee1f432017-03-31 17:18:53 -0700106 var src = srcDiv.select('pre');
Simon Hunt239f09e2017-05-18 13:10:09 -0700107 src.text(source.join('\n'));
Simon Hunt7d5e9842017-02-23 11:37:02 -0800108 }
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 Huntcc035c52017-02-22 21:12:51 -0800124 // callback invoked when data from a details request returns from server
125 function respDetailsCb(data) {
126 $scope.panelData = data.details;
Simon Huntcc035c52017-02-22 21:12:51 -0800127 $scope.$apply();
Simon Huntcc035c52017-02-22 21:12:51 -0800128 $log.debug('YANG_MODEL>', detailsResp, data);
129 }
130
Simon Hunt7d5e9842017-02-23 11:37:02 -0800131 function handleEscape() {
132 return closePanel();
133 }
134
135
136 // defines view controller
Simon Huntcc035c52017-02-22 21:12:51 -0800137 angular.module('ovYangModel', [])
Simon Hunt7d5e9842017-02-23 11:37:02 -0800138 .controller('OvYangModelCtrl', [
Thomas Vachuskaad37e372017-08-03 12:07:01 -0700139 '$log', '$scope', '$http', '$timeout',
140 'TableBuilderService', 'TableDetailService',
Simon Hunt7d5e9842017-02-23 11:37:02 -0800141 'FnService', 'MastService', 'PanelService', 'WebSocketService',
Thomas Vachuskaad37e372017-08-03 12:07:01 -0700142 'IconService', 'UrlFnService', 'FlashService',
Simon Huntcc035c52017-02-22 21:12:51 -0800143
Thomas Vachuskaad37e372017-08-03 12:07:01 -0700144 function (_$log_, _$scope_, $http, $timeout, tbs, tds, _fs_, _mast_, _ps_, _wss_, _is_, ufs, _flash_) {
Simon Hunt7d5e9842017-02-23 11:37:02 -0800145 var handlers = {};
Simon Huntcc035c52017-02-22 21:12:51 -0800146
Simon Hunt7d5e9842017-02-23 11:37:02 -0800147 $log = _$log_;
148 $scope = _$scope_;
149 fs = _fs_;
150 mast = _mast_;
151 ps = _ps_;
152 wss = _wss_;
153 is = _is_;
Simon Huntcc035c52017-02-22 21:12:51 -0800154
Simon Hunt7d5e9842017-02-23 11:37:02 -0800155 $scope.panelData = {};
Simon Huntcc035c52017-02-22 21:12:51 -0800156
Simon Hunt7d5e9842017-02-23 11:37:02 -0800157 // register response handler
158 handlers[detailsResp] = respDetailsCb;
159 wss.bindHandlers(handlers);
Simon Huntcc035c52017-02-22 21:12:51 -0800160
Simon Hunt7d5e9842017-02-23 11:37:02 -0800161 // row selection callback
162 function selCb($event, row) {
163 if ($scope.selId) {
Thomas Vachuskac307d212017-07-31 13:57:20 -0700164 wss.sendEvent(detailsReq, {
165 modelId: row.modelId,
166 id: row.id,
167 revision: row.revision
168 });
Simon Hunt7d5e9842017-02-23 11:37:02 -0800169 } else {
170 $scope.hidePanel();
Simon Huntcc035c52017-02-22 21:12:51 -0800171 }
Simon Hunt7d5e9842017-02-23 11:37:02 -0800172 $log.debug('Got a click on:', row);
Simon Huntcc035c52017-02-22 21:12:51 -0800173 }
Simon Huntcc035c52017-02-22 21:12:51 -0800174
Simon Hunt7d5e9842017-02-23 11:37:02 -0800175 tbs.buildTable({
176 scope: $scope,
177 tag: 'yangModel',
178 selCb: selCb
179 });
180
Thomas Vachuskaad37e372017-08-03 12:07:01 -0700181 $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 Hunt7d5e9842017-02-23 11:37:02 -0800220 $scope.$on('$destroy', function () {
221 wss.unbindHandlers(handlers);
222 });
223
224 $log.log('OvYangModelCtrl has been created');
225 }
226 ])
227
Thomas Vachuskaad37e372017-08-03 12:07:01 -0700228 // 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 Hunt7d5e9842017-02-23 11:37:02 -0800300 .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 Hunt3ee1f432017-03-31 17:18:53 -0700311 pHeight = wSize.height - hFudge;
Simon Hunt7d5e9842017-02-23 11:37:02 -0800312 }
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 Huntcc035c52017-02-22 21:12:51 -0800367
368}());