export_modal.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import angular from 'angular';
  2. import { saveAs } from 'file-saver';
  3. import coreModule from 'app/core/core_module';
  4. import { DashboardExporter } from './exporter';
  5. export class DashExportCtrl {
  6. dash: any;
  7. exporter: DashboardExporter;
  8. dismiss: () => void;
  9. shareExternally: boolean;
  10. /** @ngInject */
  11. constructor(private dashboardSrv, datasourceSrv, private $scope, private $rootScope) {
  12. this.exporter = new DashboardExporter(datasourceSrv);
  13. this.dash = this.dashboardSrv.getCurrent();
  14. }
  15. saveDashboardAsFile() {
  16. if (this.shareExternally) {
  17. this.exporter.makeExportable(this.dash).then((dashboardJson: any) => {
  18. this.$scope.$apply(() => {
  19. this.openSaveAsDialog(dashboardJson);
  20. });
  21. });
  22. } else {
  23. this.openSaveAsDialog(this.dash.getSaveModelClone());
  24. }
  25. }
  26. viewJson() {
  27. if (this.shareExternally) {
  28. this.exporter.makeExportable(this.dash).then((dashboardJson: any) => {
  29. this.$scope.$apply(() => {
  30. this.openJsonModal(dashboardJson);
  31. });
  32. });
  33. } else {
  34. this.openJsonModal(this.dash.getSaveModelClone());
  35. }
  36. }
  37. private openSaveAsDialog(dash: any) {
  38. const blob = new Blob([angular.toJson(dash, true)], {
  39. type: 'application/json;charset=utf-8',
  40. });
  41. saveAs(blob, dash.title + '-' + new Date().getTime() + '.json');
  42. }
  43. private openJsonModal(clone: any) {
  44. const editScope = this.$rootScope.$new();
  45. editScope.object = clone;
  46. editScope.enableCopy = true;
  47. this.$rootScope.appEvent('show-modal', {
  48. src: 'public/app/partials/edit_json.html',
  49. scope: editScope,
  50. });
  51. this.dismiss();
  52. }
  53. }
  54. export function dashExportDirective() {
  55. return {
  56. restrict: 'E',
  57. templateUrl: 'public/app/features/dashboard/export/export_modal.html',
  58. controller: DashExportCtrl,
  59. bindToController: true,
  60. controllerAs: 'ctrl',
  61. scope: { dismiss: '&' },
  62. };
  63. }
  64. coreModule.directive('dashExportModal', dashExportDirective);