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) { |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 36 | $scope.ctrlBtnState = {}; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame] | 37 | $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 Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 41 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 42 | function selCb($event, row) { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 43 | // selId comes from tableBuilder |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 44 | $scope.ctrlBtnState.selection = !!$scope.selId; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 45 | $log.debug('Got a click on:', row); |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 46 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 47 | refreshCtrls(); |
| 48 | } |
| 49 | |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 50 | 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 Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 62 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 63 | |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 64 | tbs.buildTable({ |
| 65 | scope: $scope, |
| 66 | tag: 'app', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 67 | selCb: selCb, |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 68 | respCb: refreshCtrls |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 69 | }); |
| 70 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 71 | $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 Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 79 | }; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 80 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 81 | $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 Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 91 | .finally(function () { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 92 | $scope.sortCallback($scope.sortParams); |
| 93 | document.getElementById('inputFileForm').reset(); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 94 | }); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 95 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 96 | }); |
| 97 | |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 98 | $log.log('OvAppCtrl has been created'); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 99 | }]) |
| 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 Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 132 | }]); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 133 | }()); |