save_modal.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  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.variableChange">
  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.variableChange" 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 type="submit" class="btn btn-success" ng-disabled="ctrl.saveForm.$invalid">Save</button>
  50. <a class="btn btn-link" ng-click="ctrl.dismiss();">Cancel</a>
  51. </div>
  52. </form>
  53. </div>
  54. `;
  55. export class SaveDashboardModalCtrl {
  56. message: string;
  57. saveVariables = false;
  58. saveTimerange = false;
  59. templating: any;
  60. time: any;
  61. originalTime: any;
  62. current = [];
  63. originalCurrent = [];
  64. max: number;
  65. saveForm: any;
  66. dismiss: () => void;
  67. timeChange = false;
  68. variableChange = false;
  69. /** @ngInject */
  70. constructor(private dashboardSrv) {
  71. this.message = '';
  72. this.max = 64;
  73. this.templating = dashboardSrv.dash.templating.list;
  74. this.compareTemplating();
  75. this.compareTime();
  76. }
  77. compareTime() {
  78. if (_.isEqual(this.dashboardSrv.dash.time, this.dashboardSrv.dash.originalTime)) {
  79. this.timeChange = false;
  80. } else {
  81. this.timeChange = true;
  82. }
  83. }
  84. compareTemplating() {
  85. if (this.dashboardSrv.dash.templating.list.length > 0) {
  86. for (let i = 0; i < this.dashboardSrv.dash.templating.list.length; i++) {
  87. if (
  88. this.dashboardSrv.dash.templating.list[i].current.text !==
  89. this.dashboardSrv.dash.originalTemplating[i].current.text
  90. ) {
  91. return (this.variableChange = true);
  92. }
  93. }
  94. return (this.variableChange = false);
  95. } else {
  96. return (this.variableChange = false);
  97. }
  98. }
  99. save() {
  100. if (!this.saveForm.$valid) {
  101. return;
  102. }
  103. var options = {
  104. saveVariables: this.saveVariables,
  105. saveTimerange: this.saveTimerange,
  106. message: this.message,
  107. };
  108. var dashboard = this.dashboardSrv.getCurrent();
  109. var saveModel = dashboard.getSaveModelClone(options);
  110. return this.dashboardSrv.save(saveModel, options).then(this.dismiss);
  111. }
  112. }
  113. export function saveDashboardModalDirective() {
  114. return {
  115. restrict: 'E',
  116. template: template,
  117. controller: SaveDashboardModalCtrl,
  118. bindToController: true,
  119. controllerAs: 'ctrl',
  120. scope: { dismiss: '&' },
  121. };
  122. }
  123. coreModule.directive('saveDashboardModal', saveDashboardModalDirective);