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