SaveDashboardAsModalCtrl.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-7">New name</label>
  17. <input type="text" class="gf-form-input" ng-model="ctrl.clone.title" give-focus="true" required>
  18. </div>
  19. <div class="gf-form">
  20. <folder-picker initial-folder-id="ctrl.folderId"
  21. on-change="ctrl.onFolderChange($folder)"
  22. enter-folder-creation="ctrl.onEnterFolderCreation()"
  23. exit-folder-creation="ctrl.onExitFolderCreation()"
  24. enable-create-new="true"
  25. label-class="width-7"
  26. dashboard-id="ctrl.clone.id">
  27. </folder-picker>
  28. </div>
  29. </div>
  30. <div class="gf-form-button-row text-center">
  31. <button type="submit" class="btn btn-primary" ng-click="ctrl.save()" ng-disabled="!ctrl.isValidFolderSelection">Save</button>
  32. <a class="btn-text" ng-click="ctrl.dismiss();">Cancel</a>
  33. </div>
  34. </form>
  35. </div>
  36. `;
  37. export class SaveDashboardAsModalCtrl {
  38. clone: any;
  39. folderId: any;
  40. dismiss: () => void;
  41. isValidFolderSelection = true;
  42. /** @ngInject */
  43. constructor(private dashboardSrv) {
  44. const dashboard = this.dashboardSrv.getCurrent();
  45. this.clone = dashboard.getSaveModelClone();
  46. this.clone.id = null;
  47. this.clone.uid = '';
  48. this.clone.title += ' Copy';
  49. this.clone.editable = true;
  50. this.clone.hideControls = false;
  51. this.folderId = dashboard.meta.folderId;
  52. // remove alerts if source dashboard is already persisted
  53. // do not want to create alert dupes
  54. if (dashboard.id > 0) {
  55. this.clone.panels.forEach(panel => {
  56. if (panel.type === 'graph' && panel.alert) {
  57. delete panel.thresholds;
  58. }
  59. delete panel.alert;
  60. });
  61. }
  62. delete this.clone.autoUpdate;
  63. }
  64. save() {
  65. return this.dashboardSrv.save(this.clone, { folderId: this.folderId }).then(this.dismiss);
  66. }
  67. keyDown(evt) {
  68. if (evt.keyCode === 13) {
  69. this.save();
  70. }
  71. }
  72. onFolderChange(folder) {
  73. this.folderId = folder.id;
  74. }
  75. onEnterFolderCreation() {
  76. this.isValidFolderSelection = false;
  77. }
  78. onExitFolderCreation() {
  79. this.isValidFolderSelection = true;
  80. }
  81. }
  82. export function saveDashboardAsDirective() {
  83. return {
  84. restrict: 'E',
  85. template: template,
  86. controller: SaveDashboardAsModalCtrl,
  87. bindToController: true,
  88. controllerAs: 'ctrl',
  89. scope: { dismiss: '&' },
  90. };
  91. }
  92. coreModule.directive('saveDashboardAsModal', saveDashboardAsDirective);