blob: 1918f3aedd7b2193a04d9fbfe4554a34d904e8eb [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) {
36 var refreshCtrls;
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070037 $scope.ctrlBtnState = {};
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070038
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070039 function selCb($event, row) {
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070040 // selId comes from tableBuilder
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070041 $scope.ctrlBtnState.selection = !!$scope.selId;
Thomas Vachuska619c5382015-04-02 13:41:47 -070042 $log.debug('Got a click on:', row);
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070043
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070044 refreshCtrls = function () {
45 if ($scope.ctrlBtnState.selection) {
46 $scope.ctrlBtnState.installed = row.state === INSTALLED;
47 $scope.ctrlBtnState.active = row.state === ACTIVE;
48 } else {
49 $scope.ctrlBtnState.installed = false;
50 $scope.ctrlBtnState.active = false;
51 }
52 };
53
54 refreshCtrls();
55 }
56
57 function respCb() {
58 refreshCtrls && refreshCtrls();
Thomas Vachuska619c5382015-04-02 13:41:47 -070059 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070060
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070061 tbs.buildTable({
62 scope: $scope,
63 tag: 'app',
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070064 selCb: selCb,
65 respCb: respCb
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070066 });
67
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070068 $scope.appAction = function (action) {
69 if ($scope.ctrlBtnState.selection) {
70 $log.debug('Initiating ' + action + ' of ' + $scope.selId);
71 wss.sendEvent(APP_MGMENT_REQ, {
72 action: action,
73 name: $scope.selId
74 });
75 }
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070076 };
Thomas Vachuska530e52a2015-05-06 19:51:32 -070077
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070078 $scope.$on('FileChanged', function () {
79 var formData = new FormData();
80 if ($scope.appFile) {
81 formData.append('file', $scope.appFile);
82 $http.post(ufs.rsUrl(FILE_UPLOAD_URL), formData, {
83 transformRequest: angular.identity,
84 headers: {
85 'Content-Type': undefined
86 }
87 })
88 // TODO: look for finally function to combine lines
89 // TODO: reexamine reset input value
90 .success(function () {
91 $scope.sortCallback($scope.sortParams);
92 document.getElementById('inputFileForm').reset();
93 })
94 .error(function () {
95 $scope.sortCallback($scope.sortParams);
96 });
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070097 }
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070098 });
99
Thomas Vachuska619c5382015-04-02 13:41:47 -0700100 $log.log('OvAppCtrl has been created');
Bri Prebilic Cole522e7562015-06-22 15:56:25 -0700101 }])
102
103 // triggers the input form to appear when button is clicked
104 .directive('triggerForm', function () {
105 return {
106 restrict: 'A',
107 link: function (scope, elem) {
108 elem.bind('click', function () {
109 document.getElementById('uploadFile')
110 .dispatchEvent(new Event('click'));
111 });
112 }
113 };
114 })
115
116 // binds the model file to the scope in scope.appFile
117 // sends upload request to the server
118 .directive('fileModel', ['$parse',
119 function ($parse) {
120 return {
121 restrict: 'A',
122 link: function (scope, elem, attrs) {
123 var model = $parse(attrs.fileModel),
124 modelSetter = model.assign;
125
126 elem.bind('change', function () {
127 scope.$apply(function () {
128 modelSetter(scope, elem[0].files[0]);
129 });
130 scope.$emit('FileChanged');
131 });
132 }
133 };
Thomas Vachuska619c5382015-04-02 13:41:47 -0700134 }]);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -0700135}());