blob: 55ccd948aedde15d8b746ebc2be5dae6f7abdff7 [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 Cole6b95a3f2015-06-04 09:15:00 -070037
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070038 function selCb($event, row) {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070039 // selId comes from tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070040 $scope.ctrlBtnState.selection = !!$scope.selId;
Thomas Vachuska619c5382015-04-02 13:41:47 -070041 $log.debug('Got a click on:', row);
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070042
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070043 refreshCtrls();
44 }
45
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070046 function refreshCtrls() {
47 var row, rowIdx;
48 if ($scope.ctrlBtnState.selection) {
49 rowIdx = fs.find($scope.selId, $scope.tableData);
50 row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null;
51
52 $scope.ctrlBtnState.installed = row && row.state === INSTALLED;
53 $scope.ctrlBtnState.active = row && row.state === ACTIVE;
54 } else {
55 $scope.ctrlBtnState.installed = false;
56 $scope.ctrlBtnState.active = false;
57 }
Thomas Vachuska619c5382015-04-02 13:41:47 -070058 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070059
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070060 tbs.buildTable({
61 scope: $scope,
62 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070063 selCb: selCb,
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070064 respCb: refreshCtrls
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070065 });
66
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070067 $scope.appAction = function (action) {
68 if ($scope.ctrlBtnState.selection) {
69 $log.debug('Initiating ' + action + ' of ' + $scope.selId);
70 wss.sendEvent(APP_MGMENT_REQ, {
71 action: action,
72 name: $scope.selId
73 });
74 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070075 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -070076
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070077 $scope.$on('FileChanged', function () {
78 var formData = new FormData();
79 if ($scope.appFile) {
80 formData.append('file', $scope.appFile);
81 $http.post(ufs.rsUrl(FILE_UPLOAD_URL), formData, {
82 transformRequest: angular.identity,
83 headers: {
84 'Content-Type': undefined
85 }
86 })
Bri Prebilic Colea7f81e52015-06-23 10:11:08 -070087 .finally(function () {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070088 $scope.sortCallback($scope.sortParams);
89 document.getElementById('inputFileForm').reset();
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070090 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070091 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070092 });
93
Thomas Vachuska619c5382015-04-02 13:41:47 -070094 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070095 }])
96
97 // triggers the input form to appear when button is clicked
98 .directive('triggerForm', function () {
99 return {
100 restrict: 'A',
101 link: function (scope, elem) {
102 elem.bind('click', function () {
103 document.getElementById('uploadFile')
104 .dispatchEvent(new Event('click'));
105 });
106 }
107 };
108 })
109
110 // binds the model file to the scope in scope.appFile
111 // sends upload request to the server
112 .directive('fileModel', ['$parse',
113 function ($parse) {
114 return {
115 restrict: 'A',
116 link: function (scope, elem, attrs) {
117 var model = $parse(attrs.fileModel),
118 modelSetter = model.assign;
119
120 elem.bind('change', function () {
121 scope.$apply(function () {
122 modelSetter(scope, elem[0].files[0]);
123 });
124 scope.$emit('FileChanged');
125 });
126 }
127 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700128 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700129}());