blob: c5a7961f3a2428de129b6808716cf31efebc3f54 [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',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070034
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070035 function ($log, $scope, $http, fs, tbs, wss, ufs) {
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070036 $scope.ctrlBtnState = {};
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070037 $scope.uploadTip = 'Upload an application';
38 $scope.activateTip = 'Activate selected application';
39 $scope.deactivateTip = 'Deactivate selected application';
40 $scope.uninstallTip = 'Uninstall selected application';
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070041
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070042 function selCb($event, row) {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070043 // selId comes from tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070044 $scope.ctrlBtnState.selection = !!$scope.selId;
Thomas Vachuska619c5382015-04-02 13:41:47 -070045 $log.debug('Got a click on:', row);
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070046
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070047 refreshCtrls();
48 }
49
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070050 function refreshCtrls() {
51 var row, rowIdx;
52 if ($scope.ctrlBtnState.selection) {
53 rowIdx = fs.find($scope.selId, $scope.tableData);
54 row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null;
55
56 $scope.ctrlBtnState.installed = row && row.state === INSTALLED;
57 $scope.ctrlBtnState.active = row && row.state === ACTIVE;
58 } else {
59 $scope.ctrlBtnState.installed = false;
60 $scope.ctrlBtnState.active = false;
61 }
Thomas Vachuska619c5382015-04-02 13:41:47 -070062 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070063
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070064 tbs.buildTable({
65 scope: $scope,
66 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070067 selCb: selCb,
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070068 respCb: refreshCtrls
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070069 });
70
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070071 $scope.appAction = function (action) {
72 if ($scope.ctrlBtnState.selection) {
73 $log.debug('Initiating ' + action + ' of ' + $scope.selId);
74 wss.sendEvent(APP_MGMENT_REQ, {
75 action: action,
76 name: $scope.selId
77 });
78 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070079 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -070080
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070081 $scope.$on('FileChanged', function () {
82 var formData = new FormData();
83 if ($scope.appFile) {
84 formData.append('file', $scope.appFile);
85 $http.post(ufs.rsUrl(FILE_UPLOAD_URL), formData, {
86 transformRequest: angular.identity,
87 headers: {
88 'Content-Type': undefined
89 }
90 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070091 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070092 $scope.sortCallback($scope.sortParams);
93 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070094 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070095 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070096 });
97
Thomas Vachuska619c5382015-04-02 13:41:47 -070098 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070099 }])
100
101 // triggers the input form to appear when button is clicked
102 .directive('triggerForm', function () {
103 return {
104 restrict: 'A',
105 link: function (scope, elem) {
106 elem.bind('click', function () {
107 document.getElementById('uploadFile')
108 .dispatchEvent(new Event('click'));
109 });
110 }
111 };
112 })
113
114 // binds the model file to the scope in scope.appFile
115 // sends upload request to the server
116 .directive('fileModel', ['$parse',
117 function ($parse) {
118 return {
119 restrict: 'A',
120 link: function (scope, elem, attrs) {
121 var model = $parse(attrs.fileModel),
122 modelSetter = model.assign;
123
124 elem.bind('change', function () {
125 scope.$apply(function () {
126 modelSetter(scope, elem[0].files[0]);
127 });
128 scope.$emit('FileChanged');
129 });
130 }
131 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700132 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700133}());