blob: c4166d11e804f60e334656d6a73f4ec05e37aaea [file] [log] [blame]
Thomas Vachuska0fa583c2015-03-30 23:07:41 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070024 // constants
25 var INSTALLED = 'INSTALLED',
26 ACTIVE = 'ACTIVE',
Simon Hunt8d28a552016-01-11 14:01:02 -080027 appMgmtReq = 'appManagementRequest',
28 fileUploadUrl = 'applications/upload',
29 dialogId = 'app-dialog',
30 dialogOpts = {
31 edge: 'right'
Simon Hunt3f92c432016-01-12 17:34:23 -080032 },
33 strongWarning = {
34 'org.onosproject.drivers': true
35 },
36 discouragement = 'Deactivating or uninstalling this component can' +
37 ' have serious negative consequences! Do so at your own risk!!';
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070038
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070039 angular.module('ovApp', [])
40 .controller('OvAppCtrl',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070041 ['$log', '$scope', '$http',
42 'FnService', 'TableBuilderService', 'WebSocketService', 'UrlFnService',
Simon Hunt8d28a552016-01-11 14:01:02 -080043 'KeyService', 'DialogService',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070044
Simon Hunt8d28a552016-01-11 14:01:02 -080045 function ($log, $scope, $http, fs, tbs, wss, ufs, ks, ds) {
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070046 $scope.ctrlBtnState = {};
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -070047 $scope.uploadTip = 'Upload an application (.oar file)';
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070048 $scope.activateTip = 'Activate selected application';
49 $scope.deactivateTip = 'Deactivate selected application';
50 $scope.uninstallTip = 'Uninstall selected application';
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070051
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070052 function selCb($event, row) {
Simon Hunt3f92c432016-01-12 17:34:23 -080053 // $scope.selId is set by code in tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070054 $scope.ctrlBtnState.selection = !!$scope.selId;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070055 refreshCtrls();
Simon Hunt3f92c432016-01-12 17:34:23 -080056 ds.closeDialog(); // don't want dialog from previous selection
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070057 }
58
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070059 function refreshCtrls() {
60 var row, rowIdx;
61 if ($scope.ctrlBtnState.selection) {
62 rowIdx = fs.find($scope.selId, $scope.tableData);
63 row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null;
64
65 $scope.ctrlBtnState.installed = row && row.state === INSTALLED;
66 $scope.ctrlBtnState.active = row && row.state === ACTIVE;
67 } else {
68 $scope.ctrlBtnState.installed = false;
69 $scope.ctrlBtnState.active = false;
70 }
Thomas Vachuska619c5382015-04-02 13:41:47 -070071 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070072
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070073 tbs.buildTable({
74 scope: $scope,
75 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070076 selCb: selCb,
Simon Hunta678b842016-01-11 17:14:18 -080077 respCb: refreshCtrls,
78 // pre-populate sort so active apps are at the top of the list
79 sortParams: {
Simon Hunt051e9fa2016-01-19 15:54:22 -080080 firstCol: 'state',
81 firstDir: 'desc',
82 secondCol: 'id',
83 secondDir: 'asc'
Simon Hunta678b842016-01-11 17:14:18 -080084 }
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070085 });
86
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070087 // TODO: reexamine where keybindings should be - directive or controller?
88 ks.keyBindings({
89 esc: [$scope.selectCallback, 'Deselect app'],
90 _helpFormat: ['esc']
91 });
92 ks.gestureNotes([
93 ['click row', 'Select / deselect app'],
94 ['scroll down', 'See more apps']
95 ]);
96
Simon Hunt8d28a552016-01-11 14:01:02 -080097
Simon Hunt3f92c432016-01-12 17:34:23 -080098 function createConfirmationText(action, itemId) {
Simon Hunt8d28a552016-01-11 14:01:02 -080099 var content = ds.createDiv();
Simon Hunt3f92c432016-01-12 17:34:23 -0800100 content.append('p').text(action + ' ' + itemId);
101 if (strongWarning[itemId]) {
102 content.append('p').text(discouragement).classed('strong', true);
103 }
Simon Hunt8d28a552016-01-11 14:01:02 -0800104 return content;
105 }
106
107 function confirmAction(action) {
Simon Hunt3f92c432016-01-12 17:34:23 -0800108 var itemId = $scope.selId,
Simon Hunt8d28a552016-01-11 14:01:02 -0800109 spar = $scope.sortParams;
110
111 function dOk() {
Simon Hunt3f92c432016-01-12 17:34:23 -0800112 $log.debug('Initiating', action, 'of', itemId);
Simon Hunt8d28a552016-01-11 14:01:02 -0800113 wss.sendEvent(appMgmtReq, {
114 action: action,
Simon Hunt3f92c432016-01-12 17:34:23 -0800115 name: itemId,
Simon Hunt8d28a552016-01-11 14:01:02 -0800116 sortCol: spar.sortCol,
117 sortDir: spar.sortDir
118 });
119 }
120
121 function dCancel() {
Simon Hunt3f92c432016-01-12 17:34:23 -0800122 $log.debug('Canceling', action, 'of', itemId);
Simon Hunt8d28a552016-01-11 14:01:02 -0800123 }
124
125 ds.openDialog(dialogId, dialogOpts)
126 .setTitle('Confirm Action')
Simon Hunt3f92c432016-01-12 17:34:23 -0800127 .addContent(createConfirmationText(action, itemId))
Simon Hunt8d28a552016-01-11 14:01:02 -0800128 .addButton('OK', dOk)
129 .addButton('Cancel', dCancel);
130 }
131
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700132 $scope.appAction = function (action) {
133 if ($scope.ctrlBtnState.selection) {
Simon Hunt8d28a552016-01-11 14:01:02 -0800134 confirmAction(action);
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700135 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -0700136 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -0700137
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700138 $scope.$on('FileChanged', function () {
139 var formData = new FormData();
140 if ($scope.appFile) {
141 formData.append('file', $scope.appFile);
Simon Hunt8d28a552016-01-11 14:01:02 -0800142 $http.post(ufs.rsUrl(fileUploadUrl), formData, {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700143 transformRequest: angular.identity,
144 headers: {
145 'Content-Type': undefined
146 }
147 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700148 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700149 $scope.sortCallback($scope.sortParams);
150 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700151 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700152 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700153 });
154
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700155 $scope.$on('$destroy', function () {
156 ks.unbindKeys();
157 });
158
Thomas Vachuska619c5382015-04-02 13:41:47 -0700159 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700160 }])
161
162 // triggers the input form to appear when button is clicked
163 .directive('triggerForm', function () {
164 return {
165 restrict: 'A',
166 link: function (scope, elem) {
167 elem.bind('click', function () {
168 document.getElementById('uploadFile')
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -0700169 .dispatchEvent(new MouseEvent('click'));
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700170 });
171 }
172 };
173 })
174
175 // binds the model file to the scope in scope.appFile
176 // sends upload request to the server
177 .directive('fileModel', ['$parse',
178 function ($parse) {
179 return {
180 restrict: 'A',
181 link: function (scope, elem, attrs) {
182 var model = $parse(attrs.fileModel),
183 modelSetter = model.assign;
184
185 elem.bind('change', function () {
186 scope.$apply(function () {
187 modelSetter(scope, elem[0].files[0]);
188 });
189 scope.$emit('FileChanged');
190 });
191 }
192 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700193 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700194}());