grafana/public/app/features/dashboard/export/export_modal.ts

78 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import angular from 'angular';
import { saveAs } from 'file-saver';
2017-12-20 05:33:33 -06:00
import coreModule from 'app/core/core_module';
import { DashboardExporter } from './exporter';
export class DashExportCtrl {
dash: any;
exporter: DashboardExporter;
dismiss: () => void;
shareExternally: boolean;
/** @ngInject */
constructor(private dashboardSrv, datasourceSrv, private $scope, private $rootScope) {
this.exporter = new DashboardExporter(datasourceSrv);
this.dash = this.dashboardSrv.getCurrent();
}
saveDashboardAsFile() {
if (this.shareExternally) {
this.exporter.makeExportable(this.dash).then((dashboardJson: any) => {
this.$scope.$apply(() => {
2018-11-06 02:00:17 -06:00
this.openSaveAsDialog(dashboardJson);
});
});
} else {
2018-11-06 02:00:17 -06:00
this.openSaveAsDialog(this.dash.getSaveModelClone());
}
}
viewJson() {
if (this.shareExternally) {
this.exporter.makeExportable(this.dash).then((dashboardJson: any) => {
this.$scope.$apply(() => {
2018-11-06 02:00:17 -06:00
this.openJsonModal(dashboardJson);
});
});
} else {
2018-11-06 02:00:17 -06:00
this.openJsonModal(this.dash.getSaveModelClone());
}
}
2018-11-06 02:00:17 -06:00
private openSaveAsDialog(dash: any) {
const blob = new Blob([angular.toJson(dash, true)], {
2017-12-20 05:33:33 -06:00
type: 'application/json;charset=utf-8',
});
saveAs(blob, dash.title + '-' + new Date().getTime() + '.json');
2016-05-17 04:17:11 -05:00
}
private openJsonModal(clone: object) {
const model = {
object: clone,
enableCopy: true,
};
2016-05-17 04:17:11 -05:00
2017-12-20 05:33:33 -06:00
this.$rootScope.appEvent('show-modal', {
src: 'public/app/partials/edit_json.html',
model: model,
});
this.dismiss();
}
}
export function dashExportDirective() {
return {
2017-12-20 05:33:33 -06:00
restrict: 'E',
templateUrl: 'public/app/features/dashboard/export/export_modal.html',
controller: DashExportCtrl,
bindToController: true,
2017-12-20 05:33:33 -06:00
controllerAs: 'ctrl',
scope: { dismiss: '&' },
};
}
2017-12-20 05:33:33 -06:00
coreModule.directive('dashExportModal', dashExportDirective);