Files
grafana/public/app/features/dashboard/components/SaveModals/SaveDashboardAsModalCtrl.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import coreModule from 'app/core/core_module';
2017-12-12 11:49:01 +01:00
const template = `
2017-12-12 11:49:01 +01:00
<div class="modal-body">
<div class="modal-header">
<h2 class="modal-header-title">
<i class="fa fa-copy"></i>
<span class="p-l-1">Save As...</span>
</h2>
<a class="modal-header-close" ng-click="ctrl.dismiss();">
<i class="fa fa-remove"></i>
</a>
</div>
<form name="ctrl.saveForm" class="modal-content" novalidate>
2017-12-12 11:49:01 +01:00
<div class="p-t-2">
<div class="gf-form">
<label class="gf-form-label width-8">New name</label>
<input type="text" class="gf-form-input" ng-model="ctrl.clone.title" give-focus="true" required aria-label="Save dashboard title field">
2017-12-12 11:49:01 +01:00
</div>
<folder-picker initial-folder-id="ctrl.folderId"
2017-12-12 11:49:01 +01:00
on-change="ctrl.onFolderChange($folder)"
enter-folder-creation="ctrl.onEnterFolderCreation()"
exit-folder-creation="ctrl.onExitFolderCreation()"
enable-create-new="true"
label-class="width-8"
dashboard-id="ctrl.clone.id">
</folder-picker>
<div class="gf-form-inline">
<gf-form-switch class="gf-form" label="Copy tags" label-class="width-8" checked="ctrl.copyTags">
</gf-form-switch>
</div>
2017-12-12 11:49:01 +01:00
</div>
<div class="gf-form-button-row text-center">
<button
type="submit"
class="btn btn-primary"
ng-click="ctrl.save()"
ng-disabled="!ctrl.isValidFolderSelection"
aria-label="Save dashboard button">
Save
</button>
2017-12-12 11:49:01 +01:00
<a class="btn-text" ng-click="ctrl.dismiss();">Cancel</a>
</div>
</form>
</div>
`;
export class SaveDashboardAsModalCtrl {
clone: any;
folderId: any;
dismiss: () => void;
isValidFolderSelection = true;
copyTags: boolean;
2017-12-12 11:49:01 +01:00
/** @ngInject */
constructor(private dashboardSrv) {
const dashboard = this.dashboardSrv.getCurrent();
2017-12-12 11:49:01 +01:00
this.clone = dashboard.getSaveModelClone();
this.clone.id = null;
this.clone.uid = '';
2017-12-20 12:33:33 +01:00
this.clone.title += ' Copy';
2017-12-12 11:49:01 +01:00
this.clone.editable = true;
this.clone.hideControls = false;
this.folderId = dashboard.meta.folderId;
this.copyTags = false;
2017-12-12 11:49:01 +01:00
// remove alerts if source dashboard is already persisted
// do not want to create alert dupes
if (dashboard.id > 0) {
this.clone.panels.forEach(panel => {
2017-12-20 12:33:33 +01:00
if (panel.type === 'graph' && panel.alert) {
2017-12-12 11:49:01 +01:00
delete panel.thresholds;
}
delete panel.alert;
});
}
delete this.clone.autoUpdate;
}
save() {
if (!this.copyTags) {
this.clone.tags = [];
}
return this.dashboardSrv.save(this.clone, { folderId: this.folderId }).then(this.dismiss);
}
2017-12-12 11:49:01 +01:00
keyDown(evt) {
if (evt.keyCode === 13) {
2017-12-12 11:49:01 +01:00
this.save();
}
}
onFolderChange(folder) {
this.folderId = folder.id;
2017-12-12 11:49:01 +01:00
}
onEnterFolderCreation() {
this.isValidFolderSelection = false;
}
onExitFolderCreation() {
this.isValidFolderSelection = true;
}
2017-12-12 11:49:01 +01:00
}
export function saveDashboardAsDirective() {
return {
2017-12-20 12:33:33 +01:00
restrict: 'E',
2017-12-12 11:49:01 +01:00
template: template,
controller: SaveDashboardAsModalCtrl,
bindToController: true,
2017-12-20 12:33:33 +01:00
controllerAs: 'ctrl',
scope: { dismiss: '&' },
2017-12-12 11:49:01 +01:00
};
}
2017-12-20 12:33:33 +01:00
coreModule.directive('saveDashboardAsModal', saveDashboardAsDirective);