blob: 11473b651cf9464a23665d65662f5b40016b4d83 [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: {
80 sortCol: 'state',
81 sortDir: 'desc'
82 }
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070083 });
84
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070085 // TODO: reexamine where keybindings should be - directive or controller?
86 ks.keyBindings({
87 esc: [$scope.selectCallback, 'Deselect app'],
88 _helpFormat: ['esc']
89 });
90 ks.gestureNotes([
91 ['click row', 'Select / deselect app'],
92 ['scroll down', 'See more apps']
93 ]);
94
Simon Hunt8d28a552016-01-11 14:01:02 -080095
Simon Hunt3f92c432016-01-12 17:34:23 -080096 function createConfirmationText(action, itemId) {
Simon Hunt8d28a552016-01-11 14:01:02 -080097 var content = ds.createDiv();
Simon Hunt3f92c432016-01-12 17:34:23 -080098 content.append('p').text(action + ' ' + itemId);
99 if (strongWarning[itemId]) {
100 content.append('p').text(discouragement).classed('strong', true);
101 }
Simon Hunt8d28a552016-01-11 14:01:02 -0800102 return content;
103 }
104
105 function confirmAction(action) {
Simon Hunt3f92c432016-01-12 17:34:23 -0800106 var itemId = $scope.selId,
Simon Hunt8d28a552016-01-11 14:01:02 -0800107 spar = $scope.sortParams;
108
109 function dOk() {
Simon Hunt3f92c432016-01-12 17:34:23 -0800110 $log.debug('Initiating', action, 'of', itemId);
Simon Hunt8d28a552016-01-11 14:01:02 -0800111 wss.sendEvent(appMgmtReq, {
112 action: action,
Simon Hunt3f92c432016-01-12 17:34:23 -0800113 name: itemId,
Simon Hunt8d28a552016-01-11 14:01:02 -0800114 sortCol: spar.sortCol,
115 sortDir: spar.sortDir
116 });
117 }
118
119 function dCancel() {
Simon Hunt3f92c432016-01-12 17:34:23 -0800120 $log.debug('Canceling', action, 'of', itemId);
Simon Hunt8d28a552016-01-11 14:01:02 -0800121 }
122
123 ds.openDialog(dialogId, dialogOpts)
124 .setTitle('Confirm Action')
Simon Hunt3f92c432016-01-12 17:34:23 -0800125 .addContent(createConfirmationText(action, itemId))
Simon Hunt8d28a552016-01-11 14:01:02 -0800126 .addButton('OK', dOk)
127 .addButton('Cancel', dCancel);
128 }
129
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700130 $scope.appAction = function (action) {
131 if ($scope.ctrlBtnState.selection) {
Simon Hunt8d28a552016-01-11 14:01:02 -0800132 confirmAction(action);
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700133 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -0700134 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -0700135
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700136 $scope.$on('FileChanged', function () {
137 var formData = new FormData();
138 if ($scope.appFile) {
139 formData.append('file', $scope.appFile);
Simon Hunt8d28a552016-01-11 14:01:02 -0800140 $http.post(ufs.rsUrl(fileUploadUrl), formData, {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700141 transformRequest: angular.identity,
142 headers: {
143 'Content-Type': undefined
144 }
145 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700146 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700147 $scope.sortCallback($scope.sortParams);
148 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700149 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700150 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700151 });
152
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700153 $scope.$on('$destroy', function () {
154 ks.unbindKeys();
155 });
156
Thomas Vachuska619c5382015-04-02 13:41:47 -0700157 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700158 }])
159
160 // triggers the input form to appear when button is clicked
161 .directive('triggerForm', function () {
162 return {
163 restrict: 'A',
164 link: function (scope, elem) {
165 elem.bind('click', function () {
166 document.getElementById('uploadFile')
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -0700167 .dispatchEvent(new MouseEvent('click'));
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700168 });
169 }
170 };
171 })
172
173 // binds the model file to the scope in scope.appFile
174 // sends upload request to the server
175 .directive('fileModel', ['$parse',
176 function ($parse) {
177 return {
178 restrict: 'A',
179 link: function (scope, elem, attrs) {
180 var model = $parse(attrs.fileModel),
181 modelSetter = model.assign;
182
183 elem.bind('change', function () {
184 scope.$apply(function () {
185 modelSetter(scope, elem[0].files[0]);
186 });
187 scope.$emit('FileChanged');
188 });
189 }
190 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700191 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700192}());