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