save_as_modal.ts 2.8 KB

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