blob: 5bcb4280093b56885a7ca04fe88db1893d1cba4d [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',
27 APP_MGMENT_REQ = 'appManagementRequest',
28 FILE_UPLOAD_URL = 'applications/upload';
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070029
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070030 angular.module('ovApp', [])
31 .controller('OvAppCtrl',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070032 ['$log', '$scope', '$http',
33 'FnService', 'TableBuilderService', 'WebSocketService', 'UrlFnService',
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070034 'KeyService',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070035
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070036 function ($log, $scope, $http, fs, tbs, wss, ufs, ks) {
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070037 $scope.ctrlBtnState = {};
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070038 $scope.uploadTip = 'Upload an application';
39 $scope.activateTip = 'Activate selected application';
40 $scope.deactivateTip = 'Deactivate selected application';
41 $scope.uninstallTip = 'Uninstall selected application';
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070042
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070043 function selCb($event, row) {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070044 // selId comes from tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070045 $scope.ctrlBtnState.selection = !!$scope.selId;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070046 refreshCtrls();
47 }
48
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070049 function refreshCtrls() {
50 var row, rowIdx;
51 if ($scope.ctrlBtnState.selection) {
52 rowIdx = fs.find($scope.selId, $scope.tableData);
53 row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null;
54
55 $scope.ctrlBtnState.installed = row && row.state === INSTALLED;
56 $scope.ctrlBtnState.active = row && row.state === ACTIVE;
57 } else {
58 $scope.ctrlBtnState.installed = false;
59 $scope.ctrlBtnState.active = false;
60 }
Thomas Vachuska619c5382015-04-02 13:41:47 -070061 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070062
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070063 tbs.buildTable({
64 scope: $scope,
65 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070066 selCb: selCb,
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070067 respCb: refreshCtrls
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070068 });
69
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070070 // TODO: reexamine where keybindings should be - directive or controller?
71 ks.keyBindings({
72 esc: [$scope.selectCallback, 'Deselect app'],
73 _helpFormat: ['esc']
74 });
75 ks.gestureNotes([
76 ['click row', 'Select / deselect app'],
77 ['scroll down', 'See more apps']
78 ]);
79
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070080 $scope.appAction = function (action) {
81 if ($scope.ctrlBtnState.selection) {
82 $log.debug('Initiating ' + action + ' of ' + $scope.selId);
83 wss.sendEvent(APP_MGMENT_REQ, {
84 action: action,
85 name: $scope.selId
86 });
87 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070088 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -070089
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070090 $scope.$on('FileChanged', function () {
91 var formData = new FormData();
92 if ($scope.appFile) {
93 formData.append('file', $scope.appFile);
94 $http.post(ufs.rsUrl(FILE_UPLOAD_URL), formData, {
95 transformRequest: angular.identity,
96 headers: {
97 'Content-Type': undefined
98 }
99 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -0700100 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700101 $scope.sortCallback($scope.sortParams);
102 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700103 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700104 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700105 });
106
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700107 $scope.$on('$destroy', function () {
108 ks.unbindKeys();
109 });
110
Thomas Vachuska619c5382015-04-02 13:41:47 -0700111 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700112 }])
113
114 // triggers the input form to appear when button is clicked
115 .directive('triggerForm', function () {
116 return {
117 restrict: 'A',
118 link: function (scope, elem) {
119 elem.bind('click', function () {
120 document.getElementById('uploadFile')
121 .dispatchEvent(new Event('click'));
122 });
123 }
124 };
125 })
126
127 // binds the model file to the scope in scope.appFile
128 // sends upload request to the server
129 .directive('fileModel', ['$parse',
130 function ($parse) {
131 return {
132 restrict: 'A',
133 link: function (scope, elem, attrs) {
134 var model = $parse(attrs.fileModel),
135 modelSetter = model.assign;
136
137 elem.bind('change', function () {
138 scope.$apply(function () {
139 modelSetter(scope, elem[0].files[0]);
140 });
141 scope.$emit('FileChanged');
142 });
143 }
144 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700145 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700146}());