save_modal.ts 3.9 KB

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