export_modal.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: object) {
  44. const model = {
  45. object: clone,
  46. enableCopy: true,
  47. };
  48. this.$rootScope.appEvent('show-modal', {
  49. src: 'public/app/partials/edit_json.html',
  50. model: model,
  51. });
  52. this.dismiss();
  53. }
  54. }
  55. export function dashExportDirective() {
  56. return {
  57. restrict: 'E',
  58. templateUrl: 'public/app/features/dashboard/export/export_modal.html',
  59. controller: DashExportCtrl,
  60. bindToController: true,
  61. controllerAs: 'ctrl',
  62. scope: { dismiss: '&' },
  63. };
  64. }
  65. coreModule.directive('dashExportModal', dashExportDirective);