SaveDashboardAsModalCtrl.ts 3.2 KB

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