forked from beer-garden/beer-garden
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add import/export buttons & functionality
- Loading branch information
1 parent
f899c76
commit e014106
Showing
7 changed files
with
360 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
gardenConfigExportController.$inject = | ||
['$scope', '$rootScope', '$filter', 'GardenService']; | ||
|
||
/** | ||
* gardenConfigExportController - Controller for the garden config export page. | ||
* @param {Object} $scope Angular's $scope object. | ||
* @param {Object} $rootScope Angular's $rootScope object. | ||
* @param {Object} $filter Filter | ||
* @param {Object} GardenService Beer-Garden's garden service. | ||
*/ | ||
export default function gardenConfigExportController( | ||
$scope, | ||
$rootScope, | ||
$filter, | ||
GardenService, | ||
) { | ||
$scope.response = $rootScope.sysResponse; | ||
|
||
$scope.exportGardenConfig = (gardenName) => { | ||
const filename = | ||
`GardenExport_${gardenName}_` + | ||
$filter('date')(new Date(Date.now()), 'yyyyMMdd_HHmmss'); | ||
|
||
const formModel = GardenService.formToServerModel($scope.data, $scope.gardenModel); | ||
|
||
const [newConnectionInfo, newConnectionParams] = [{}, {}]; | ||
newConnectionInfo['connection_type'] = formModel['connection_type']; | ||
|
||
if (formModel['connection_params']['http']) { | ||
newConnectionParams['http'] = formModel['connection_params']['http']; | ||
} | ||
|
||
if (formModel['connection_params']['stomp']) { | ||
newConnectionParams['stomp'] = formModel['connection_params']['stomp']; | ||
} | ||
|
||
newConnectionInfo['connection_params'] = newConnectionParams; | ||
|
||
const blob = new Blob( | ||
[JSON.stringify(newConnectionInfo)], | ||
{ | ||
type: 'application/json;charset=utf-8', | ||
}, | ||
); | ||
const downloadLink = angular.element('<a></a>'); | ||
|
||
downloadLink.attr('href', window.URL.createObjectURL(blob)); | ||
downloadLink.attr('download', filename); | ||
downloadLink[0].click(); | ||
}; | ||
} |
101 changes: 101 additions & 0 deletions
101
src/ui/src/js/controllers/garden/import_garden_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import template from '../../../templates/import_garden_config.html'; | ||
|
||
gardenConfigImportController.$inject = [ | ||
'$scope', | ||
'$rootScope', | ||
'$uibModal', | ||
'$state', | ||
]; | ||
|
||
/** | ||
* gardenConfigImportController - Controller for garden config import. | ||
* @param {Object} $scope Angular's $scope object. | ||
* @param {Object} $rootScope Angular's $rootScope object. | ||
* @param {Object} $uibModal Angular UI's $uibModal object. | ||
* @param {Object} $state State | ||
*/ | ||
export function gardenConfigImportController( | ||
$scope, | ||
$rootScope, | ||
$uibModal, | ||
$state, | ||
) { | ||
$scope.response = $rootScope.sysResponse; | ||
|
||
$scope.openImportGardenConfigPopup = () => { | ||
const popupInstance = $uibModal.open({ | ||
animation: true, | ||
controller: 'GardenConfigImportModalController', | ||
template: template, | ||
}); | ||
|
||
popupInstance.result.then((resolvedResponse) => { | ||
const jsonFileContents = resolvedResponse.jsonFileContents; | ||
const newConfig = JSON.parse(jsonFileContents); | ||
|
||
$scope.updateModelFromImport(newConfig); | ||
}, angular.noop); | ||
}; | ||
} | ||
|
||
gardenConfigImportModalController.$inject = | ||
['$scope', '$window', '$uibModalInstance']; | ||
|
||
/** | ||
* gardenConfigImportModalController - Controller for the garden config import | ||
* popup window. | ||
* | ||
* @param {Object} $scope Angular's $scope object. | ||
* @param {Object} $window Object for the browser window. | ||
* @param {Object} $uibModalInstance Object for the modal popup window. | ||
*/ | ||
export function gardenConfigImportModalController( | ||
$scope, $window, $uibModalInstance) { | ||
$scope.import = {}; | ||
$scope.fileName = undefined; | ||
$scope.fileContents = undefined; | ||
$scope.fileIsGoodJson = true; | ||
|
||
$scope.inputClicker = function() { | ||
$window.document.getElementById('fileSelectHiddenControl').click(); | ||
}; | ||
|
||
$scope.doImport = function() { | ||
$scope.import['jsonFileContents'] = $scope.fileContents; | ||
$uibModalInstance.close($scope.import); | ||
}; | ||
|
||
$scope.cancelImport = function() { | ||
$uibModalInstance.dismiss('cancel'); | ||
}; | ||
|
||
function isParsableJson(string) { | ||
try { | ||
JSON.parse(string); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
$scope.onFileSelected = function(event) { | ||
const theFile = event.target.files[0]; | ||
$scope.fileName = theFile.name; | ||
|
||
const reader = new FileReader(); | ||
reader.onload = function(e) { | ||
$scope.$apply(function() { | ||
const result = reader.result; | ||
const isGoodJson = isParsableJson(result); | ||
|
||
if (isGoodJson) { | ||
$scope.fileContents = result; | ||
$scope.fileIsGoodJson = true; | ||
} else { | ||
$scope.fileIsGoodJson = false; | ||
} | ||
}); | ||
}; | ||
reader.readAsText(theFile); | ||
}; | ||
} |
Oops, something went wrong.