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', |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 27 | appMgmtReq = 'appManagementRequest', |
| 28 | fileUploadUrl = 'applications/upload', |
| 29 | dialogId = 'app-dialog', |
| 30 | dialogOpts = { |
| 31 | edge: 'right' |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 32 | }, |
| 33 | strongWarning = { |
| 34 | 'org.onosproject.drivers': true |
| 35 | }, |
| 36 | discouragement = 'Deactivating or uninstalling this component can' + |
| 37 | ' have serious negative consequences! Do so at your own risk!!'; |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 38 | |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 39 | angular.module('ovApp', []) |
| 40 | .controller('OvAppCtrl', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 41 | ['$log', '$scope', '$http', |
| 42 | 'FnService', 'TableBuilderService', 'WebSocketService', 'UrlFnService', |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 43 | 'KeyService', 'DialogService', |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 44 | |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 45 | function ($log, $scope, $http, fs, tbs, wss, ufs, ks, ds) { |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 46 | $scope.ctrlBtnState = {}; |
Bri Prebilic Cole | 6c82ade | 2015-08-05 11:12:30 -0700 | [diff] [blame] | 47 | $scope.uploadTip = 'Upload an application (.oar file)'; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame] | 48 | $scope.activateTip = 'Activate selected application'; |
| 49 | $scope.deactivateTip = 'Deactivate selected application'; |
| 50 | $scope.uninstallTip = 'Uninstall selected application'; |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 51 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 52 | function selCb($event, row) { |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 53 | // $scope.selId is set by code in tableBuilder |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 54 | $scope.ctrlBtnState.selection = !!$scope.selId; |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 55 | refreshCtrls(); |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 56 | ds.closeDialog(); // don't want dialog from previous selection |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 59 | function refreshCtrls() { |
| 60 | var row, rowIdx; |
| 61 | if ($scope.ctrlBtnState.selection) { |
| 62 | rowIdx = fs.find($scope.selId, $scope.tableData); |
| 63 | row = rowIdx >= 0 ? $scope.tableData[rowIdx] : null; |
| 64 | |
| 65 | $scope.ctrlBtnState.installed = row && row.state === INSTALLED; |
| 66 | $scope.ctrlBtnState.active = row && row.state === ACTIVE; |
| 67 | } else { |
| 68 | $scope.ctrlBtnState.installed = false; |
| 69 | $scope.ctrlBtnState.active = false; |
| 70 | } |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 71 | } |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 72 | |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 73 | tbs.buildTable({ |
| 74 | scope: $scope, |
| 75 | tag: 'app', |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 76 | selCb: selCb, |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 77 | respCb: refreshCtrls, |
| 78 | // pre-populate sort so active apps are at the top of the list |
| 79 | sortParams: { |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame^] | 80 | firstCol: 'state', |
| 81 | firstDir: 'desc', |
| 82 | secondCol: 'id', |
| 83 | secondDir: 'asc' |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 84 | } |
Bri Prebilic Cole | 6b95a3f | 2015-06-04 09:15:00 -0700 | [diff] [blame] | 85 | }); |
| 86 | |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 87 | // TODO: reexamine where keybindings should be - directive or controller? |
| 88 | ks.keyBindings({ |
| 89 | esc: [$scope.selectCallback, 'Deselect app'], |
| 90 | _helpFormat: ['esc'] |
| 91 | }); |
| 92 | ks.gestureNotes([ |
| 93 | ['click row', 'Select / deselect app'], |
| 94 | ['scroll down', 'See more apps'] |
| 95 | ]); |
| 96 | |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 97 | |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 98 | function createConfirmationText(action, itemId) { |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 99 | var content = ds.createDiv(); |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 100 | content.append('p').text(action + ' ' + itemId); |
| 101 | if (strongWarning[itemId]) { |
| 102 | content.append('p').text(discouragement).classed('strong', true); |
| 103 | } |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 104 | return content; |
| 105 | } |
| 106 | |
| 107 | function confirmAction(action) { |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 108 | var itemId = $scope.selId, |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 109 | spar = $scope.sortParams; |
| 110 | |
| 111 | function dOk() { |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 112 | $log.debug('Initiating', action, 'of', itemId); |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 113 | wss.sendEvent(appMgmtReq, { |
| 114 | action: action, |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 115 | name: itemId, |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 116 | sortCol: spar.sortCol, |
| 117 | sortDir: spar.sortDir |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | function dCancel() { |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 122 | $log.debug('Canceling', action, 'of', itemId); |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | ds.openDialog(dialogId, dialogOpts) |
| 126 | .setTitle('Confirm Action') |
Simon Hunt | 3f92c43 | 2016-01-12 17:34:23 -0800 | [diff] [blame] | 127 | .addContent(createConfirmationText(action, itemId)) |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 128 | .addButton('OK', dOk) |
| 129 | .addButton('Cancel', dCancel); |
| 130 | } |
| 131 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 132 | $scope.appAction = function (action) { |
| 133 | if ($scope.ctrlBtnState.selection) { |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 134 | confirmAction(action); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 135 | } |
Bri Prebilic Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 136 | }; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 137 | |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 138 | $scope.$on('FileChanged', function () { |
| 139 | var formData = new FormData(); |
| 140 | if ($scope.appFile) { |
| 141 | formData.append('file', $scope.appFile); |
Simon Hunt | 8d28a55 | 2016-01-11 14:01:02 -0800 | [diff] [blame] | 142 | $http.post(ufs.rsUrl(fileUploadUrl), formData, { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 143 | transformRequest: angular.identity, |
| 144 | headers: { |
| 145 | 'Content-Type': undefined |
| 146 | } |
| 147 | }) |
Bri Prebilic Cole | a7f81e5 | 2015-06-23 10:11:08 -0700 | [diff] [blame] | 148 | .finally(function () { |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 149 | $scope.sortCallback($scope.sortParams); |
| 150 | document.getElementById('inputFileForm').reset(); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 151 | }); |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 152 | } |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 153 | }); |
| 154 | |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 155 | $scope.$on('$destroy', function () { |
| 156 | ks.unbindKeys(); |
| 157 | }); |
| 158 | |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 159 | $log.log('OvAppCtrl has been created'); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 160 | }]) |
| 161 | |
| 162 | // triggers the input form to appear when button is clicked |
| 163 | .directive('triggerForm', function () { |
| 164 | return { |
| 165 | restrict: 'A', |
| 166 | link: function (scope, elem) { |
| 167 | elem.bind('click', function () { |
| 168 | document.getElementById('uploadFile') |
Bri Prebilic Cole | 6c82ade | 2015-08-05 11:12:30 -0700 | [diff] [blame] | 169 | .dispatchEvent(new MouseEvent('click')); |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 170 | }); |
| 171 | } |
| 172 | }; |
| 173 | }) |
| 174 | |
| 175 | // binds the model file to the scope in scope.appFile |
| 176 | // sends upload request to the server |
| 177 | .directive('fileModel', ['$parse', |
| 178 | function ($parse) { |
| 179 | return { |
| 180 | restrict: 'A', |
| 181 | link: function (scope, elem, attrs) { |
| 182 | var model = $parse(attrs.fileModel), |
| 183 | modelSetter = model.assign; |
| 184 | |
| 185 | elem.bind('change', function () { |
| 186 | scope.$apply(function () { |
| 187 | modelSetter(scope, elem[0].files[0]); |
| 188 | }); |
| 189 | scope.$emit('FileChanged'); |
| 190 | }); |
| 191 | } |
| 192 | }; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 193 | }]); |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 194 | }()); |