Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the 'License'); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 5 | * 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 Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 11 | * distributed under the License is distributed on an 'AS IS' BASIS, |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 12 | * 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 Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 24 | // constants |
| 25 | var INSTALLED = 'INSTALLED', |
| 26 | ACTIVE = 'ACTIVE', |
| 27 | APP_MGMENT_REQ = 'appManagementRequest', |
| 28 | FILE_UPLOAD_URL = 'applications/upload'; |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 29 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 30 | angular.module('ovApp', []) |
| 31 | .controller('OvAppCtrl', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 32 | ['$log', '$scope', '$http', |
| 33 | 'FnService', 'TableBuilderService', 'WebSocketService', 'UrlFnService', |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 34 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 35 | function ($log, $scope, $http, fs, tbs, wss, ufs) { |
| 36 | var refreshCtrls; |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 37 | $scope.ctrlBtnState = {}; |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 38 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 39 | function selCb($event, row) { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 40 | // selId comes from tableBuilder |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 41 | $scope.ctrlBtnState.selection = !!$scope.selId; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 42 | $log.debug('Got a click on:', row); |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 43 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 44 | 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 Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 59 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 60 | |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 61 | tbs.buildTable({ |
| 62 | scope: $scope, |
| 63 | tag: 'app', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 64 | selCb: selCb, |
| 65 | respCb: respCb |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 66 | }); |
| 67 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 68 | $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 Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 76 | }; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 77 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 78 | $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 Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 97 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 98 | }); |
| 99 | |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 100 | $log.log('OvAppCtrl has been created'); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame^] | 101 | }]) |
| 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 Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 134 | }]); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 135 | }()); |