SaveDashboardAsModalCtrl.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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>
  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. <gf-form-switch class="gf-form" label="Preserve tags" label-class="width-8" checked="ctrl.preseveTags">
  28. </gf-form-switch>
  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. preseveTags: boolean;
  43. /** @ngInject */
  44. constructor(private dashboardSrv) {
  45. const dashboard = this.dashboardSrv.getCurrent();
  46. this.clone = dashboard.getSaveModelClone();
  47. this.clone.id = null;
  48. this.clone.uid = '';
  49. this.clone.title += ' Copy';
  50. this.clone.editable = true;
  51. this.clone.hideControls = false;
  52. this.folderId = dashboard.meta.folderId;
  53. this.preseveTags = false;
  54. // remove alerts if source dashboard is already persisted
  55. // do not want to create alert dupes
  56. if (dashboard.id > 0) {
  57. this.clone.panels.forEach(panel => {
  58. if (panel.type === 'graph' && panel.alert) {
  59. delete panel.thresholds;
  60. }
  61. delete panel.alert;
  62. });
  63. }
  64. delete this.clone.autoUpdate;
  65. }
  66. save() {
  67. if (!this.preseveTags) {
  68. this.clone.tags = [];
  69. }
  70. return this.dashboardSrv.save(this.clone, { folderId: this.folderId }).then(this.dismiss);
  71. }
  72. keyDown(evt) {
  73. if (evt.keyCode === 13) {
  74. this.save();
  75. }
  76. }
  77. onFolderChange(folder) {
  78. this.folderId = folder.id;
  79. }
  80. onEnterFolderCreation() {
  81. this.isValidFolderSelection = false;
  82. }
  83. onExitFolderCreation() {
  84. this.isValidFolderSelection = true;
  85. }
  86. }
  87. export function saveDashboardAsDirective() {
  88. return {
  89. restrict: 'E',
  90. template: template,
  91. controller: SaveDashboardAsModalCtrl,
  92. bindToController: true,
  93. controllerAs: 'ctrl',
  94. scope: { dismiss: '&' },
  95. };
  96. }
  97. coreModule.directive('saveDashboardAsModal', saveDashboardAsDirective);