SaveDashboardAsModalCtrl.ts 3.1 KB

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