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 | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 37 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 38 | function selCb($event, row) { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 39 | // selId comes from tableBuilder |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 40 | $scope.ctrlBtnState.selection = !!$scope.selId; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 41 | $log.debug('Got a click on:', row); |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 42 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 43 | refreshCtrls(); |
| 44 | } |
| 45 | |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 46 | 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 Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 58 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 59 | |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 60 | tbs.buildTable({ |
| 61 | scope: $scope, |
| 62 | tag: 'app', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 63 | selCb: selCb, |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 64 | respCb: refreshCtrls |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 65 | }); |
| 66 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 67 | $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 Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 75 | }; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 76 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 77 | $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 Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 87 | .finally(function () { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 88 | $scope.sortCallback($scope.sortParams); |
| 89 | document.getElementById('inputFileForm').reset(); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 90 | }); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 91 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 92 | }); |
| 93 | |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 94 | $log.log('OvAppCtrl has been created'); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 95 | }]) |
| 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 Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 128 | }]); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 129 | }()); |