blob: f7b8bbf2b2f0e500619847673f8562b94bf45c00 [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'
32 };
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070033
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070034 angular.module('ovApp', [])
35 .controller('OvAppCtrl',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070036 ['$log', '$scope', '$http',
37 'FnService', 'TableBuilderService', 'WebSocketService', 'UrlFnService',
Simon Hunt8d28a552016-01-11 14:01:02 -080038 'KeyService', 'DialogService',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070039
Simon Hunt8d28a552016-01-11 14:01:02 -080040 function ($log, $scope, $http, fs, tbs, wss, ufs, ks, ds) {
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070041 $scope.ctrlBtnState = {};
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -070042 $scope.uploadTip = 'Upload an application (.oar file)';
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070043 $scope.activateTip = 'Activate selected application';
44 $scope.deactivateTip = 'Deactivate selected application';
45 $scope.uninstallTip = 'Uninstall selected application';
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070046
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070047 function selCb($event, row) {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070048 // selId comes from tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070049 $scope.ctrlBtnState.selection = !!$scope.selId;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070050 refreshCtrls();
51 }
52
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070053 function refreshCtrls() {
54 var row, rowIdx;
55 if ($scope.ctrlBtnState.selection) {
56 rowIdx = fs.find($scope.selId, $scope.tableData);
57 row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null;
58
59 $scope.ctrlBtnState.installed = row && row.state === INSTALLED;
60 $scope.ctrlBtnState.active = row && row.state === ACTIVE;
61 } else {
62 $scope.ctrlBtnState.installed = false;
63 $scope.ctrlBtnState.active = false;
64 }
Thomas Vachuska619c5382015-04-02 13:41:47 -070065 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070066
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070067 tbs.buildTable({
68 scope: $scope,
69 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070070 selCb: selCb,
Simon Hunta678b842016-01-11 17:14:18 -080071 respCb: refreshCtrls,
72 // pre-populate sort so active apps are at the top of the list
73 sortParams: {
74 sortCol: 'state',
75 sortDir: 'desc'
76 }
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070077 });
78
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070079 // TODO: reexamine where keybindings should be - directive or controller?
80 ks.keyBindings({
81 esc: [$scope.selectCallback, 'Deselect app'],
82 _helpFormat: ['esc']
83 });
84 ks.gestureNotes([
85 ['click row', 'Select / deselect app'],
86 ['scroll down', 'See more apps']
87 ]);
88
Simon Hunt8d28a552016-01-11 14:01:02 -080089
90 function createConfirmationText(action, sid) {
91 var content = ds.createDiv();
92 content.append('p').text(action + ' ' + sid);
93 return content;
94 }
95
96 function confirmAction(action) {
97 var sid = $scope.selId,
98 spar = $scope.sortParams;
99
100 function dOk() {
101 $log.debug('Initiating', action, 'of', sid);
102 wss.sendEvent(appMgmtReq, {
103 action: action,
104 name: sid,
105 sortCol: spar.sortCol,
106 sortDir: spar.sortDir
107 });
108 }
109
110 function dCancel() {
111 $log.debug('Canceling', action, 'of', sid);
112 }
113
114 ds.openDialog(dialogId, dialogOpts)
115 .setTitle('Confirm Action')
116 .addContent(createConfirmationText(action, sid))
117 .addButton('OK', dOk)
118 .addButton('Cancel', dCancel);
119 }
120
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700121 $scope.appAction = function (action) {
122 if ($scope.ctrlBtnState.selection) {
Simon Hunt8d28a552016-01-11 14:01:02 -0800123 confirmAction(action);
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700124 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -0700125 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -0700126
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700127 $scope.$on('FileChanged', function () {
128 var formData = new FormData();
129 if ($scope.appFile) {
130 formData.append('file', $scope.appFile);
Simon Hunt8d28a552016-01-11 14:01:02 -0800131 $http.post(ufs.rsUrl(fileUploadUrl), formData, {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700132 transformRequest: angular.identity,
133 headers: {
134 'Content-Type': undefined
135 }
136 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700137 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700138 $scope.sortCallback($scope.sortParams);
139 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700140 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700141 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700142 });
143
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700144 $scope.$on('$destroy', function () {
145 ks.unbindKeys();
146 });
147
Thomas Vachuska619c5382015-04-02 13:41:47 -0700148 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700149 }])
150
151 // triggers the input form to appear when button is clicked
152 .directive('triggerForm', function () {
153 return {
154 restrict: 'A',
155 link: function (scope, elem) {
156 elem.bind('click', function () {
157 document.getElementById('uploadFile')
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -0700158 .dispatchEvent(new MouseEvent('click'));
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700159 });
160 }
161 };
162 })
163
164 // binds the model file to the scope in scope.appFile
165 // sends upload request to the server
166 .directive('fileModel', ['$parse',
167 function ($parse) {
168 return {
169 restrict: 'A',
170 link: function (scope, elem, attrs) {
171 var model = $parse(attrs.fileModel),
172 modelSetter = model.assign;
173
174 elem.bind('change', function () {
175 scope.$apply(function () {
176 modelSetter(scope, elem[0].files[0]);
177 });
178 scope.$emit('FileChanged');
179 });
180 }
181 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700182 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700183}());