SaveDashboardModalCtrl.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import coreModule from 'app/core/core_module';
  2. import { DashboardSrv } from '../../services/DashboardSrv';
  3. const template = `
  4. <div class="modal-body">
  5. <div class="modal-header">
  6. <h2 class="modal-header-title">
  7. <i class="fa fa-save"></i>
  8. <span class="p-l-1">Save changes</span>
  9. </h2>
  10. <a class="modal-header-close" ng-click="ctrl.dismiss();">
  11. <i class="fa fa-remove"></i>
  12. </a>
  13. </div>
  14. <form name="ctrl.saveForm" ng-submit="ctrl.save()" class="modal-content" novalidate>
  15. <div class="p-t-1">
  16. <div class="gf-form-group" ng-if="ctrl.timeChange || ctrl.variableValueChange">
  17. <gf-form-switch class="gf-form"
  18. label="Save current time range" ng-if="ctrl.timeChange" label-class="width-12" switch-class="max-width-6"
  19. checked="ctrl.saveTimerange" on-change="buildUrl()">
  20. </gf-form-switch>
  21. <gf-form-switch class="gf-form"
  22. label="Save current variables" ng-if="ctrl.variableValueChange" label-class="width-12" switch-class="max-width-6"
  23. checked="ctrl.saveVariables" on-change="buildUrl()">
  24. </gf-form-switch>
  25. </div>
  26. <div class="gf-form">
  27. <label class="gf-form-hint">
  28. <input
  29. type="text"
  30. name="message"
  31. class="gf-form-input"
  32. placeholder="Add a note to describe your changes &hellip;"
  33. give-focus="true"
  34. ng-model="ctrl.message"
  35. ng-model-options="{allowInvalid: true}"
  36. ng-maxlength="this.max"
  37. maxlength="64"
  38. autocomplete="off" />
  39. <small class="gf-form-hint-text muted" ng-cloak>
  40. <span ng-class="{'text-error': ctrl.saveForm.message.$invalid && ctrl.saveForm.message.$dirty }">
  41. {{ctrl.message.length || 0}}
  42. </span>
  43. / {{ctrl.max}} characters
  44. </small>
  45. </label>
  46. </div>
  47. </div>
  48. <div class="gf-form-button-row text-center">
  49. <button
  50. id="saveBtn"
  51. type="submit"
  52. class="btn btn-primary"
  53. ng-class="{'btn-primary--processing': ctrl.isSaving}"
  54. ng-disabled="ctrl.saveForm.$invalid || ctrl.isSaving"
  55. >
  56. <span ng-if="!ctrl.isSaving">Save</span>
  57. <span ng-if="ctrl.isSaving === true">Saving...</span>
  58. </button>
  59. <button class="btn btn-inverse" ng-click="ctrl.dismiss();">Cancel</button>
  60. </div>
  61. </form>
  62. </div>
  63. `;
  64. export class SaveDashboardModalCtrl {
  65. message: string;
  66. saveVariables = false;
  67. saveTimerange = false;
  68. time: any;
  69. originalTime: any;
  70. current: any[] = [];
  71. originalCurrent: any[] = [];
  72. max: number;
  73. saveForm: any;
  74. isSaving: boolean;
  75. dismiss: () => void;
  76. timeChange = false;
  77. variableValueChange = false;
  78. /** @ngInject */
  79. constructor(private dashboardSrv: DashboardSrv) {
  80. this.message = '';
  81. this.max = 64;
  82. this.isSaving = false;
  83. this.timeChange = this.dashboardSrv.getCurrent().hasTimeChanged();
  84. this.variableValueChange = this.dashboardSrv.getCurrent().hasVariableValuesChanged();
  85. }
  86. save() {
  87. if (!this.saveForm.$valid) {
  88. return;
  89. }
  90. const options: any = {
  91. saveVariables: this.saveVariables,
  92. saveTimerange: this.saveTimerange,
  93. message: this.message,
  94. };
  95. const dashboard = this.dashboardSrv.getCurrent();
  96. const saveModel = dashboard.getSaveModelClone(options);
  97. this.isSaving = true;
  98. return this.dashboardSrv.save(saveModel, options).then(this.postSave.bind(this, options));
  99. }
  100. postSave(options: any) {
  101. if (options.saveVariables) {
  102. this.dashboardSrv.getCurrent().resetOriginalVariables();
  103. }
  104. if (options.saveTimerange) {
  105. this.dashboardSrv.getCurrent().resetOriginalTime();
  106. }
  107. this.dismiss();
  108. }
  109. }
  110. export function saveDashboardModalDirective() {
  111. return {
  112. restrict: 'E',
  113. template: template,
  114. controller: SaveDashboardModalCtrl,
  115. bindToController: true,
  116. controllerAs: 'ctrl',
  117. scope: { dismiss: '&' },
  118. };
  119. }
  120. coreModule.directive('saveDashboardModal', saveDashboardModalDirective);