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