repeat_option.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { coreModule } from 'app/core/core';
  2. import { VariableSrv } from 'app/features/templating/variable_srv';
  3. const template = `
  4. <div class="gf-form-select-wrapper max-width-18">
  5. <select class="gf-form-input" ng-model="panel.repeat" ng-options="f.value as f.text for f in variables" ng-change="optionChanged()">
  6. <option value=""></option>
  7. </div>
  8. `;
  9. /** @ngInject */
  10. function dashRepeatOptionDirective(variableSrv: VariableSrv) {
  11. return {
  12. restrict: 'E',
  13. template: template,
  14. scope: {
  15. panel: '=',
  16. },
  17. link: (scope: any, element: JQuery) => {
  18. element.css({ display: 'block', width: '100%' });
  19. scope.variables = variableSrv.variables.map((item: any) => {
  20. return { text: item.name, value: item.name };
  21. });
  22. if (scope.variables.length === 0) {
  23. scope.variables.unshift({
  24. text: 'No template variables found',
  25. value: null,
  26. });
  27. }
  28. scope.variables.unshift({ text: 'Disabled', value: null });
  29. // if repeat is set and no direction set to horizontal
  30. if (scope.panel.repeat && !scope.panel.repeatDirection) {
  31. scope.panel.repeatDirection = 'h';
  32. }
  33. scope.optionChanged = () => {
  34. if (scope.panel.repeat) {
  35. scope.panel.repeatDirection = 'h';
  36. }
  37. };
  38. },
  39. };
  40. }
  41. coreModule.directive('dashRepeatOption', dashRepeatOptionDirective);