blob: 24aba23518aafac1fb7fca0244f870bdd336d6f8 [file] [log] [blame]
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07001/*
Jian Lia54de5a2016-01-20 23:10:39 -08002 * Copyright 2015-2016 Open Networking Laboratory
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07003 *
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -07004 * Licensed under the Apache License, Version 2.0 (the 'License');
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07005 * 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
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070011 * distributed under the License is distributed on an 'AS IS' BASIS,
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070012 * 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 -- App View Module
19 */
20
21(function () {
22 'use strict';
23
Jian Lia54de5a2016-01-20 23:10:39 -080024 // injected refs
25 var $log, $scope, $loc, fs, ps, wss, is, ns, ks, is;
26
27 // internal state
28 var detailsPanel,
29 pStartY,
30 pHeight,
31 top,
Jian Lida253e02016-01-21 17:46:17 -080032 middle,
Jian Lia54de5a2016-01-20 23:10:39 -080033 bottom,
34 iconDiv,
35 wSize = false;
36
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070037 // constants
38 var INSTALLED = 'INSTALLED',
39 ACTIVE = 'ACTIVE',
Simon Hunt8d28a552016-01-11 14:01:02 -080040 appMgmtReq = 'appManagementRequest',
Jian Lia54de5a2016-01-20 23:10:39 -080041 topPdg = 50,
42 ctnrPdg = 24,
Jian Lida253e02016-01-21 17:46:17 -080043 tbWidth = 470,
Jian Lia54de5a2016-01-20 23:10:39 -080044 scrollSize = 17,
45 pName = 'application-details-panel',
46 detailsReq = 'appDetailsRequest',
47 detailsResp = 'appDetailsResponse',
Simon Hunt8d28a552016-01-11 14:01:02 -080048 fileUploadUrl = 'applications/upload',
Jian Lia54de5a2016-01-20 23:10:39 -080049 iconUrlPrefix = 'rs/applications/',
50 iconUrlSuffix = '/icon',
Simon Hunt8d28a552016-01-11 14:01:02 -080051 dialogId = 'app-dialog',
52 dialogOpts = {
53 edge: 'right'
Simon Hunt3f92c432016-01-12 17:34:23 -080054 },
55 strongWarning = {
56 'org.onosproject.drivers': true
57 },
58 discouragement = 'Deactivating or uninstalling this component can' +
Jian Lia54de5a2016-01-20 23:10:39 -080059 ' have serious negative consequences! Do so at your own risk!!',
60 propOrder = ['id', 'state', 'category', 'version', 'origin', 'role', 'url'],
61 friendlyProps = ['App ID', 'State', 'Category', 'Version', 'Origin', 'Role', 'URL'];
62
63 function createDetailsPane() {
64 detailsPanel = ps.createPanel(pName, {
65 width: wSize.width,
66 margin: 0,
67 hideMargin: 0
68 });
69 detailsPanel.el().style({
70 position: 'absolute',
71 top: pStartY + 'px'
72 });
73 $scope.hidePanel = function () { detailsPanel.hide(); };
74 detailsPanel.hide();
75 }
76
77 function closePanel() {
78 if (detailsPanel.isVisible()) {
79 $scope.selId = null;
80 detailsPanel.hide();
81 return true;
82 }
83 return false;
84 }
85
86 function handleEscape() {
87 return editNameCancel() || closePanel();
88 }
89
90 function addCloseBtn(div) {
91 is.loadEmbeddedIcon(div, 'plus', 30);
92 div.select('g').attr('transform', 'translate(25, 0) rotate(45)');
93 div.on('click', closePanel);
94 }
95
96 function setUpPanel() {
97 var container, closeBtn, tblDiv;
98 detailsPanel.empty();
99
100 container = detailsPanel.append('div').classed('container', true);
101
102 top = container.append('div').classed('top', true);
103 closeBtn = top.append('div').classed('close-btn', true);
104 addCloseBtn(closeBtn);
105 iconDiv = top.append('div').classed('dev-icon', true);
Jian Lia54de5a2016-01-20 23:10:39 -0800106
107 tblDiv = top.append('div').classed('top-tables', true);
108 tblDiv.append('div').classed('left', true).append('table');
109 tblDiv.append('div').classed('right', true).append('table');
Jian Lida253e02016-01-21 17:46:17 -0800110 tblDiv.append('div').classed('description', true).append('table');
Jian Lia54de5a2016-01-20 23:10:39 -0800111
112 top.append('hr');
113
Jian Lida253e02016-01-21 17:46:17 -0800114 middle = container.append('div').classed('middle', true);
115 tblDiv = middle.append('div').classed('middle-tables', true);
116 tblDiv.append('div').classed('readme', true).append('table');
117
118 middle.append('hr');
119
Jian Lia54de5a2016-01-20 23:10:39 -0800120 bottom = container.append('div').classed('bottom', true);
Jian Lida253e02016-01-21 17:46:17 -0800121 tblDiv = bottom.append('div').classed('bottom-tables', true).append('table');
122 tblDiv.append('h2').html('Features');
123 tblDiv.append('div').classed('features', true).append('table');
124 tblDiv.append('h2').html('Required Apps');
125 tblDiv.append('div').classed('required-apps', true).append('table');
126 tblDiv.append('h2').html('Permissions');
127 tblDiv.append('div').classed('permissions', true).append('table');
Jian Lia54de5a2016-01-20 23:10:39 -0800128 }
129
130 function addProp(tbody, index, value) {
131 var tr = tbody.append('tr');
132
133 function addCell(cls, txt) {
134 tr.append('td').attr('class', cls).html(txt);
135 }
136 addCell('label', friendlyProps[index] + ' :');
137 addCell('value', value);
138 }
139
140 function addIcon(tbody, value) {
141 var tr = tbody.append('tr');
142 var td = tr.append('td');
143 td.append('img').attr('src', iconUrlPrefix + value + iconUrlSuffix);
144 }
145
Jian Lida253e02016-01-21 17:46:17 -0800146 function addContent(tbody, value) {
Jian Lia54de5a2016-01-20 23:10:39 -0800147 var tr = tbody.append('tr');
148 tr.append('td').html(value);
149 }
150
151 function populateTop(tblDiv, details) {
152 var leftTbl = tblDiv.select('.left')
153 .select('table')
154 .append('tbody'),
155 rightTbl = tblDiv.select('.right')
156 .select('table')
157 .append('tbody'),
Jian Lida253e02016-01-21 17:46:17 -0800158 descriptionTbl = tblDiv.select('.description')
Jian Lia54de5a2016-01-20 23:10:39 -0800159 .select('table')
160 .append('tbody');
161
162 top.select('h2').html(details.name);
163
164 // place application icon to the left table
165 addIcon(leftTbl, details.id);
166
167 // place rest of the fields to the right table
168 propOrder.forEach(function (prop, i) {
169 addProp(rightTbl, i, details[prop]);
170 });
171
Jian Lida253e02016-01-21 17:46:17 -0800172 // place description field to the description table
173 addContent(descriptionTbl, details.desc);
174 }
175
176 function populateMiddle(tblDiv, details) {
177 var readmeTbl = tblDiv.select('.readme')
178 .select('table')
179 .append('tbody');
180
Jian Lia54de5a2016-01-20 23:10:39 -0800181 // place readme field to the readme table
Jian Lida253e02016-01-21 17:46:17 -0800182 addContent(readmeTbl, details.readme);
Jian Lia54de5a2016-01-20 23:10:39 -0800183 }
184
185 function populateName(div, name) {
186 var lab = div.select('.label'),
187 val = div.select('.value');
188 lab.html('Friendly Name:');
189 val.html(name);
190 }
191
192 function populateDetails(details) {
Jian Lida253e02016-01-21 17:46:17 -0800193 var nameDiv, topTbs, middleTbs, bottomTbs;
Jian Lia54de5a2016-01-20 23:10:39 -0800194 setUpPanel();
195
196 nameDiv = top.select('.name-div');
197 topTbs = top.select('.top-tables');
Jian Lida253e02016-01-21 17:46:17 -0800198 middleTbs = middle.select('.middle-tables');
199 bottomTbs = bottom.select('.bottom-tables');
Jian Lia54de5a2016-01-20 23:10:39 -0800200
201 populateName(nameDiv, details.name);
202 populateTop(topTbs, details);
Jian Lida253e02016-01-21 17:46:17 -0800203 populateMiddle(middleTbs, details);
204 populateBottom(bottomTbs, details);
Jian Lia54de5a2016-01-20 23:10:39 -0800205
206 detailsPanel.height(pHeight);
207 }
208
Jian Lida253e02016-01-21 17:46:17 -0800209 function addItem(tbody, item) {
210 var tr = tbody.append('tr').attr('width', tbWidth + 'px');
211 tr.append('td').attr('width', tbWidth + 'px')
212 .attr('style', 'text-align:left').html(item);
213 }
Jian Lia54de5a2016-01-20 23:10:39 -0800214
Jian Lida253e02016-01-21 17:46:17 -0800215 function addItems(table, items) {
216 var tbody = table.append('tbody');
217 items.forEach(function (item) {
218 addItem(tbody, item);
219 });
220 }
221
222 function populateBottom(tblDiv, details) {
223 var featuresTbl = tblDiv.select('.features')
224 .select('table'),
225 permissionsTbl = tblDiv.select('.permissions')
226 .select('table'),
227 requiredAppsTbl = tblDiv.select('.required-apps')
228 .select('table');
229
230 addItems(featuresTbl, details.features);
231 addItems(requiredAppsTbl, details._required_apps);
232 addItems(permissionsTbl, details.permissions);
233
234 featuresTbl.style({
Jian Lia54de5a2016-01-20 23:10:39 -0800235 width: tbWidth + 'px',
236 overflow: 'auto',
237 display: 'block'
238 });
239
Jian Lida253e02016-01-21 17:46:17 -0800240 detailsPanel.width(tbWidth + ctnrPdg);
Jian Lia54de5a2016-01-20 23:10:39 -0800241 }
242
243 function respDetailsCb(data) {
244 $scope.panelData = data.details;
245 $scope.$apply();
246 }
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700247
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700248 angular.module('ovApp', [])
249 .controller('OvAppCtrl',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700250 ['$log', '$scope', '$http',
Jian Lia54de5a2016-01-20 23:10:39 -0800251 'FnService', 'TableBuilderService', 'PanelService', 'WebSocketService',
252 'IconService', 'UrlFnService', 'KeyService', 'DialogService',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700253
Jian Lia54de5a2016-01-20 23:10:39 -0800254 function (_$log_, _$scope_, $http, _fs_, tbs, _ps_, _wss_, _is_, ufs, _ks_, ds) {
255 $log = _$log_;
256 $scope = _$scope_;
257 wss = _wss_;
258 ks = _ks_;
259 fs = _fs_;
260 ps = _ps_;
261 is = _is_;
262 $scope.panelData = {};
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -0700263 $scope.ctrlBtnState = {};
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -0700264 $scope.uploadTip = 'Upload an application (.oar file)';
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700265 $scope.activateTip = 'Activate selected application';
266 $scope.deactivateTip = 'Deactivate selected application';
267 $scope.uninstallTip = 'Uninstall selected application';
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -0700268
Jian Lia54de5a2016-01-20 23:10:39 -0800269 var handlers = {};
270
271 // details panel handlers
272 handlers[detailsResp] = respDetailsCb;
273 wss.bindHandlers(handlers);
274
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700275 function selCb($event, row) {
Simon Hunt3f92c432016-01-12 17:34:23 -0800276 // $scope.selId is set by code in tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -0700277 $scope.ctrlBtnState.selection = !!$scope.selId;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700278 refreshCtrls();
Simon Hunt3f92c432016-01-12 17:34:23 -0800279 ds.closeDialog(); // don't want dialog from previous selection
Jian Lia54de5a2016-01-20 23:10:39 -0800280
281 if ($scope.selId) {
282 wss.sendEvent(detailsReq, { id: row.id });
283 } else {
284 $scope.hidePanel();
285 }
286 $log.debug('Got a click on:', row);
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700287 }
288
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700289 function refreshCtrls() {
290 var row, rowIdx;
291 if ($scope.ctrlBtnState.selection) {
292 rowIdx = fs.find($scope.selId, $scope.tableData);
293 row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null;
294
295 $scope.ctrlBtnState.installed = row && row.state === INSTALLED;
296 $scope.ctrlBtnState.active = row && row.state === ACTIVE;
297 } else {
298 $scope.ctrlBtnState.installed = false;
299 $scope.ctrlBtnState.active = false;
300 }
Thomas Vachuska619c5382015-04-02 13:41:47 -0700301 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700302
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -0700303 tbs.buildTable({
304 scope: $scope,
305 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700306 selCb: selCb,
Simon Hunta678b842016-01-11 17:14:18 -0800307 respCb: refreshCtrls,
308 // pre-populate sort so active apps are at the top of the list
309 sortParams: {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800310 firstCol: 'state',
311 firstDir: 'desc',
312 secondCol: 'id',
313 secondDir: 'asc'
Simon Hunta678b842016-01-11 17:14:18 -0800314 }
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -0700315 });
316
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700317 // TODO: reexamine where keybindings should be - directive or controller?
318 ks.keyBindings({
319 esc: [$scope.selectCallback, 'Deselect app'],
320 _helpFormat: ['esc']
321 });
322 ks.gestureNotes([
323 ['click row', 'Select / deselect app'],
324 ['scroll down', 'See more apps']
325 ]);
326
Simon Hunt3f92c432016-01-12 17:34:23 -0800327 function createConfirmationText(action, itemId) {
Simon Hunt8d28a552016-01-11 14:01:02 -0800328 var content = ds.createDiv();
Simon Hunt3f92c432016-01-12 17:34:23 -0800329 content.append('p').text(action + ' ' + itemId);
330 if (strongWarning[itemId]) {
331 content.append('p').text(discouragement).classed('strong', true);
332 }
Simon Hunt8d28a552016-01-11 14:01:02 -0800333 return content;
334 }
335
336 function confirmAction(action) {
Simon Hunt3f92c432016-01-12 17:34:23 -0800337 var itemId = $scope.selId,
Simon Hunt8d28a552016-01-11 14:01:02 -0800338 spar = $scope.sortParams;
339
340 function dOk() {
Simon Hunt3f92c432016-01-12 17:34:23 -0800341 $log.debug('Initiating', action, 'of', itemId);
Simon Hunt8d28a552016-01-11 14:01:02 -0800342 wss.sendEvent(appMgmtReq, {
343 action: action,
Simon Hunt3f92c432016-01-12 17:34:23 -0800344 name: itemId,
Simon Hunt8d28a552016-01-11 14:01:02 -0800345 sortCol: spar.sortCol,
346 sortDir: spar.sortDir
347 });
348 }
349
350 function dCancel() {
Simon Hunt3f92c432016-01-12 17:34:23 -0800351 $log.debug('Canceling', action, 'of', itemId);
Simon Hunt8d28a552016-01-11 14:01:02 -0800352 }
353
354 ds.openDialog(dialogId, dialogOpts)
355 .setTitle('Confirm Action')
Simon Hunt3f92c432016-01-12 17:34:23 -0800356 .addContent(createConfirmationText(action, itemId))
Simon Hunt8d28a552016-01-11 14:01:02 -0800357 .addButton('OK', dOk)
358 .addButton('Cancel', dCancel);
359 }
360
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700361 $scope.appAction = function (action) {
362 if ($scope.ctrlBtnState.selection) {
Simon Hunt8d28a552016-01-11 14:01:02 -0800363 confirmAction(action);
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700364 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -0700365 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -0700366
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700367 $scope.$on('FileChanged', function () {
368 var formData = new FormData();
369 if ($scope.appFile) {
370 formData.append('file', $scope.appFile);
Simon Hunt8d28a552016-01-11 14:01:02 -0800371 $http.post(ufs.rsUrl(fileUploadUrl), formData, {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700372 transformRequest: angular.identity,
373 headers: {
374 'Content-Type': undefined
375 }
376 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700377 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700378 $scope.sortCallback($scope.sortParams);
379 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700380 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700381 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700382 });
383
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700384 $scope.$on('$destroy', function () {
385 ks.unbindKeys();
Jian Lia54de5a2016-01-20 23:10:39 -0800386 wss.unbindHandlers(handlers);
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700387 });
388
Thomas Vachuska619c5382015-04-02 13:41:47 -0700389 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700390 }])
391
392 // triggers the input form to appear when button is clicked
393 .directive('triggerForm', function () {
394 return {
395 restrict: 'A',
396 link: function (scope, elem) {
397 elem.bind('click', function () {
398 document.getElementById('uploadFile')
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -0700399 .dispatchEvent(new MouseEvent('click'));
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700400 });
401 }
402 };
403 })
404
405 // binds the model file to the scope in scope.appFile
406 // sends upload request to the server
407 .directive('fileModel', ['$parse',
408 function ($parse) {
409 return {
410 restrict: 'A',
411 link: function (scope, elem, attrs) {
412 var model = $parse(attrs.fileModel),
413 modelSetter = model.assign;
414
415 elem.bind('change', function () {
416 scope.$apply(function () {
417 modelSetter(scope, elem[0].files[0]);
418 });
419 scope.$emit('FileChanged');
420 });
421 }
422 };
Jian Lia54de5a2016-01-20 23:10:39 -0800423 }])
424
425 .directive('applicationDetailsPanel',
426 ['$rootScope', '$window', '$timeout', 'KeyService',
427 function ($rootScope, $window, $timeout, ks) {
428 return function (scope) {
429 var unbindWatch;
430
431 function heightCalc() {
432 pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height')
433 + topPdg;
434 wSize = fs.windowSize(pStartY);
435 pHeight = wSize.height;
436 }
437
438 function initPanel() {
439 heightCalc();
440 createDetailsPane();
441 $log.debug('start to initialize panel!');
442 }
443
444 // Safari has a bug where it renders the fixed-layout table wrong
445 // if you ask for the window's size too early
446 if (scope.onos.browser === 'safari') {
447 $timeout(initPanel);
448 } else {
449 initPanel();
450 }
451 // create key bindings to handle panel
452 ks.keyBindings({
453 esc: [handleEscape, 'Close the details panel'],
454 _helpFormat: ['esc']
455 });
456 ks.gestureNotes([
457 ['click', 'Select a row to show application details'],
458 ['scroll down', 'See more application']
459 ]);
460
461 // if the panelData changes
462 scope.$watch('panelData', function () {
463 if (!fs.isEmptyObject(scope.panelData)) {
464 populateDetails(scope.panelData);
465 detailsPanel.show();
466 }
467 });
468
469 // if the window size changes
470 unbindWatch = $rootScope.$watchCollection(
471 function () {
472 return {
473 h: $window.innerHeight,
474 w: $window.innerWidth
475 };
476 }, function () {
477 if (!fs.isEmptyObject(scope.panelData)) {
478 heightCalc();
479 populateDetails(scope.panelData);
480 }
481 }
482 );
483
484 scope.$on('$destroy', function () {
485 unbindWatch();
486 ks.unbindKeys();
487 ps.destroyPanel(pName);
488 });
489 };
490 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700491}());