blob: 3b27c38ec9dd8a81a02aa19d86249accd5b171cd [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,
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070071 respCb: refreshCtrls
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070072 });
73
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070074 // TODO: reexamine where keybindings should be - directive or controller?
75 ks.keyBindings({
76 esc: [$scope.selectCallback, 'Deselect app'],
77 _helpFormat: ['esc']
78 });
79 ks.gestureNotes([
80 ['click row', 'Select / deselect app'],
81 ['scroll down', 'See more apps']
82 ]);
83
Simon Hunt8d28a552016-01-11 14:01:02 -080084
85 function createConfirmationText(action, sid) {
86 var content = ds.createDiv();
87 content.append('p').text(action + ' ' + sid);
88 return content;
89 }
90
91 function confirmAction(action) {
92 var sid = $scope.selId,
93 spar = $scope.sortParams;
94
95 function dOk() {
96 $log.debug('Initiating', action, 'of', sid);
97 wss.sendEvent(appMgmtReq, {
98 action: action,
99 name: sid,
100 sortCol: spar.sortCol,
101 sortDir: spar.sortDir
102 });
103 }
104
105 function dCancel() {
106 $log.debug('Canceling', action, 'of', sid);
107 }
108
109 ds.openDialog(dialogId, dialogOpts)
110 .setTitle('Confirm Action')
111 .addContent(createConfirmationText(action, sid))
112 .addButton('OK', dOk)
113 .addButton('Cancel', dCancel);
114 }
115
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700116 $scope.appAction = function (action) {
117 if ($scope.ctrlBtnState.selection) {
Simon Hunt8d28a552016-01-11 14:01:02 -0800118 confirmAction(action);
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700119 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -0700120 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -0700121
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700122 $scope.$on('FileChanged', function () {
123 var formData = new FormData();
124 if ($scope.appFile) {
125 formData.append('file', $scope.appFile);
Simon Hunt8d28a552016-01-11 14:01:02 -0800126 $http.post(ufs.rsUrl(fileUploadUrl), formData, {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700127 transformRequest: angular.identity,
128 headers: {
129 'Content-Type': undefined
130 }
131 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700132 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700133 $scope.sortCallback($scope.sortParams);
134 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700135 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700136 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700137 });
138
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700139 $scope.$on('$destroy', function () {
140 ks.unbindKeys();
141 });
142
Thomas Vachuska619c5382015-04-02 13:41:47 -0700143 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700144 }])
145
146 // triggers the input form to appear when button is clicked
147 .directive('triggerForm', function () {
148 return {
149 restrict: 'A',
150 link: function (scope, elem) {
151 elem.bind('click', function () {
152 document.getElementById('uploadFile')
Bri Prebilic Cole6c82ade2015-08-05 11:12:30 -0700153 .dispatchEvent(new MouseEvent('click'));
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700154 });
155 }
156 };
157 })
158
159 // binds the model file to the scope in scope.appFile
160 // sends upload request to the server
161 .directive('fileModel', ['$parse',
162 function ($parse) {
163 return {
164 restrict: 'A',
165 link: function (scope, elem, attrs) {
166 var model = $parse(attrs.fileModel),
167 modelSetter = model.assign;
168
169 elem.bind('change', function () {
170 scope.$apply(function () {
171 modelSetter(scope, elem[0].files[0]);
172 });
173 scope.$emit('FileChanged');
174 });
175 }
176 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700177 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700178}());