SaveDashboardAsModalCtrl.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import coreModule from 'app/core/core_module';
  2. import { DashboardSrv } from '../../services/DashboardSrv';
  3. import { PanelModel } from '../../state/PanelModel';
  4. const template = `
  5. <div class="modal-body">
  6. <div class="modal-header">
  7. <h2 class="modal-header-title">
  8. <i class="fa fa-copy"></i>
  9. <span class="p-l-1">Save As...</span>
  10. </h2>
  11. <a class="modal-header-close" ng-click="ctrl.dismiss();">
  12. <i class="fa fa-remove"></i>
  13. </a>
  14. </div>
  15. <form name="ctrl.saveForm" class="modal-content" novalidate>
  16. <div class="p-t-2">
  17. <div class="gf-form">
  18. <label class="gf-form-label width-8">New name</label>
  19. <input type="text" class="gf-form-input" ng-model="ctrl.clone.title" give-focus="true" required aria-label="Save dashboard title field">
  20. </div>
  21. <folder-picker initial-folder-id="ctrl.folderId"
  22. on-change="ctrl.onFolderChange($folder)"
  23. enter-folder-creation="ctrl.onEnterFolderCreation()"
  24. exit-folder-creation="ctrl.onExitFolderCreation()"
  25. enable-create-new="true"
  26. label-class="width-8"
  27. dashboard-id="ctrl.clone.id">
  28. </folder-picker>
  29. <div class="gf-form-inline">
  30. <gf-form-switch class="gf-form" label="Copy tags" label-class="width-8" checked="ctrl.copyTags">
  31. </gf-form-switch>
  32. </div>
  33. </div>
  34. <div class="gf-form-button-row text-center">
  35. <button
  36. type="submit"
  37. class="btn btn-primary"
  38. ng-click="ctrl.save()"
  39. ng-disabled="!ctrl.isValidFolderSelection"
  40. aria-label="Save dashboard button">
  41. Save
  42. </button>
  43. <a class="btn-text" ng-click="ctrl.dismiss();">Cancel</a>
  44. </div>
  45. </form>
  46. </div>
  47. `;
  48. export class SaveDashboardAsModalCtrl {
  49. clone: any;
  50. folderId: any;
  51. dismiss: () => void;
  52. isValidFolderSelection = true;
  53. copyTags: boolean;
  54. /** @ngInject */
  55. constructor(private dashboardSrv: DashboardSrv) {
  56. const dashboard = this.dashboardSrv.getCurrent();
  57. this.clone = dashboard.getSaveModelClone();
  58. this.clone.id = null;
  59. this.clone.uid = '';
  60. this.clone.title += ' Copy';
  61. this.clone.editable = true;
  62. this.clone.hideControls = false;
  63. this.folderId = dashboard.meta.folderId;
  64. this.copyTags = false;
  65. // remove alerts if source dashboard is already persisted
  66. // do not want to create alert dupes
  67. if (dashboard.id > 0) {
  68. this.clone.panels.forEach((panel: PanelModel) => {
  69. if (panel.type === 'graph' && panel.alert) {
  70. delete panel.thresholds;
  71. }
  72. delete panel.alert;
  73. });
  74. }
  75. delete this.clone.autoUpdate;
  76. }
  77. save() {
  78. if (!this.copyTags) {
  79. this.clone.tags = [];
  80. }
  81. return this.dashboardSrv.save(this.clone, { folderId: this.folderId }).then(this.dismiss);
  82. }
  83. keyDown(evt: KeyboardEvent) {
  84. if (evt.keyCode === 13) {
  85. this.save();
  86. }
  87. }
  88. onFolderChange(folder: { id: any }) {
  89. this.folderId = folder.id;
  90. }
  91. onEnterFolderCreation() {
  92. this.isValidFolderSelection = false;
  93. }
  94. onExitFolderCreation() {
  95. this.isValidFolderSelection = true;
  96. }
  97. }
  98. export function saveDashboardAsDirective() {
  99. return {
  100. restrict: 'E',
  101. template: template,
  102. controller: SaveDashboardAsModalCtrl,
  103. bindToController: true,
  104. controllerAs: 'ctrl',
  105. scope: { dismiss: '&' },
  106. };
  107. }
  108. coreModule.directive('saveDashboardAsModal', saveDashboardAsDirective);