ahgittin commented on a change in pull request #293: URL: https://github.com/apache/brooklyn-ui/pull/293#discussion_r721264922
########## File path: ui-modules/utils/catalog-deleter/catalog-deleter.js ########## @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import angular from 'angular'; +import template from './catalog-deleter.html'; +import catalogApi from '../providers/catalog-api.provider'; + +const MODULE_NAME = 'brooklyn.components.catalog-deleter'; + +/** + * @ngdoc module + * @name brooklyn.components.catalog-deleter + * @requires catalogApi + * + * @description + * Adds an overlay on top of the current DOM element to upload files to the catalog. Files can either by added via + * classic file selection or drag & drop. This support multiple files to be uploaded at once. + */ +angular.module(MODULE_NAME, [catalogApi]) + .directive('brooklynCatalogDeleter', ['$compile', 'catalogApi', 'brSnackbar', catalogDeleterDirective]); + +export default MODULE_NAME; + +function catalogDeleterDirective($compile, catalogApi, brSnackbar) { + return { + restrict: 'E', + scope: { + mode: '@', + symbolicName: '<', + version: '<', + onDeleting: '&', + onDeleted: '&', + onFailed: '&', + onDeletingFinished: '&', + }, + template: template, + controller: ['$scope', catalogDeleterController], + controllerAs: 'vm', + }; + + function catalogDeleterController($scope) { + let vm = this; + + $scope.id = $scope.symbolicName + ':' + $scope.version; + + function getBundle(bundleSymbolicName, bundleVersion) { + catalogApi.getBundle(bundleSymbolicName, bundleVersion).then(data => { + $scope.bundle = data; + + }).catch(err => { + console.log("Error loading bundle: ", err); + $scope.bundleError = true; + }).finally(() => { + $scope.bundleLoading = false; + }); + } + + if ($scope.mode==='bundle') { + $scope.bundleLoading = true; + getBundle($scope.symbolicName, $scope.version); + + } else { + $scope.bundleLoading = true; + catalogApi.getType($scope.symbolicName, $scope.version).then(data => { + let bundleSymbolicName, bundleVersion; + if (data.containingBundle) { + let parts = data.containingBundle.split(':'); + if (parts.length>=1) { + bundleSymbolicName = parts[0]; + if (parts.length>=2) { + bundleVersion = parts[1]; + if (parts.length>2) { + throw 'Invalid containing bundle '+data.containingBundle; + } + } + } + } + if (!bundleSymbolicName) { + throw 'Unavailable or invalid containing bundle '+data.containingBundle; + } + + getBundle(bundleSymbolicName, bundleVersion); + + }).catch(err => { + console.log("Error loading type: ", err); + if ($scope.mode==='location') { + // don't display an error, probably it is a legacy-installed location + } else { + $scope.bundleError = true; + } + $scope.bundleLoading = false; + }); + } + + vm.delete = () => { + if ($scope.onDeleting) $scope.onDeleting(); + let promise; + if ($scope.mode==='bundle') { + promise = catalogApi.deleteBundle($scope.symbolicName, $scope.version) + } else if ($scope.mode==='location') { + promise = catalogApi.deleteLocation($scope.symbolicName, $scope.version) + } else if ($scope.mode==='type') { + // not used + throw 'deleteType not supported'; + } else { Review comment: the caller controls the button, this simply control the popup, which i think is right for maximum reusability -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@brooklyn.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org