DashExportCtrl.ts 2.1 KB

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