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', |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 34 | 'KeyService', |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 35 | |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 36 | function ($log, $scope, $http, fs, tbs, wss, ufs, ks) { |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 37 | $scope.ctrlBtnState = {}; |
Bri Prebilic Cole | 6c82ade | 2015-08-05 11:12:30 -0700 | [diff] [blame^] | 38 | $scope.uploadTip = 'Upload an application (.oar file)'; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame] | 39 | $scope.activateTip = 'Activate selected application'; |
| 40 | $scope.deactivateTip = 'Deactivate selected application'; |
| 41 | $scope.uninstallTip = 'Uninstall selected application'; |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 42 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 43 | function selCb($event, row) { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 44 | // selId comes from tableBuilder |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 45 | $scope.ctrlBtnState.selection = !!$scope.selId; |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 46 | refreshCtrls(); |
| 47 | } |
| 48 | |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 49 | function refreshCtrls() { |
| 50 | var row, rowIdx; |
| 51 | if ($scope.ctrlBtnState.selection) { |
| 52 | rowIdx = fs.find($scope.selId, $scope.tableData); |
| 53 | row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null; |
| 54 | |
| 55 | $scope.ctrlBtnState.installed = row && row.state === INSTALLED; |
| 56 | $scope.ctrlBtnState.active = row && row.state === ACTIVE; |
| 57 | } else { |
| 58 | $scope.ctrlBtnState.installed = false; |
| 59 | $scope.ctrlBtnState.active = false; |
| 60 | } |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 61 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 62 | |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 63 | tbs.buildTable({ |
| 64 | scope: $scope, |
| 65 | tag: 'app', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 66 | selCb: selCb, |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 67 | respCb: refreshCtrls |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 68 | }); |
| 69 | |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 70 | // TODO: reexamine where keybindings should be - directive or controller? |
| 71 | ks.keyBindings({ |
| 72 | esc: [$scope.selectCallback, 'Deselect app'], |
| 73 | _helpFormat: ['esc'] |
| 74 | }); |
| 75 | ks.gestureNotes([ |
| 76 | ['click row', 'Select / deselect app'], |
| 77 | ['scroll down', 'See more apps'] |
| 78 | ]); |
| 79 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 80 | $scope.appAction = function (action) { |
| 81 | if ($scope.ctrlBtnState.selection) { |
| 82 | $log.debug('Initiating ' + action + ' of ' + $scope.selId); |
| 83 | wss.sendEvent(APP_MGMENT_REQ, { |
| 84 | action: action, |
Bri Prebilic Cole | ae85fb3 | 2015-08-03 14:19:24 -0700 | [diff] [blame] | 85 | name: $scope.selId, |
| 86 | sortCol: $scope.sortParams.sortCol, |
| 87 | sortDir: $scope.sortParams.sortDir |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 88 | }); |
| 89 | } |
Bri Prebilic Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 90 | }; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 91 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 92 | $scope.$on('FileChanged', function () { |
| 93 | var formData = new FormData(); |
| 94 | if ($scope.appFile) { |
| 95 | formData.append('file', $scope.appFile); |
| 96 | $http.post(ufs.rsUrl(FILE_UPLOAD_URL), formData, { |
| 97 | transformRequest: angular.identity, |
| 98 | headers: { |
| 99 | 'Content-Type': undefined |
| 100 | } |
| 101 | }) |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 102 | .finally(function () { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 103 | $scope.sortCallback($scope.sortParams); |
| 104 | document.getElementById('inputFileForm').reset(); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 105 | }); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 106 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 107 | }); |
| 108 | |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 109 | $scope.$on('$destroy', function () { |
| 110 | ks.unbindKeys(); |
| 111 | }); |
| 112 | |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 113 | $log.log('OvAppCtrl has been created'); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 114 | }]) |
| 115 | |
| 116 | // triggers the input form to appear when button is clicked |
| 117 | .directive('triggerForm', function () { |
| 118 | return { |
| 119 | restrict: 'A', |
| 120 | link: function (scope, elem) { |
| 121 | elem.bind('click', function () { |
| 122 | document.getElementById('uploadFile') |
Bri Prebilic Cole | 6c82ade | 2015-08-05 11:12:30 -0700 | [diff] [blame^] | 123 | .dispatchEvent(new MouseEvent('click')); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 124 | }); |
| 125 | } |
| 126 | }; |
| 127 | }) |
| 128 | |
| 129 | // binds the model file to the scope in scope.appFile |
| 130 | // sends upload request to the server |
| 131 | .directive('fileModel', ['$parse', |
| 132 | function ($parse) { |
| 133 | return { |
| 134 | restrict: 'A', |
| 135 | link: function (scope, elem, attrs) { |
| 136 | var model = $parse(attrs.fileModel), |
| 137 | modelSetter = model.assign; |
| 138 | |
| 139 | elem.bind('change', function () { |
| 140 | scope.$apply(function () { |
| 141 | modelSetter(scope, elem[0].files[0]); |
| 142 | }); |
| 143 | scope.$emit('FileChanged'); |
| 144 | }); |
| 145 | } |
| 146 | }; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 147 | }]); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 148 | }()); |