templateParamSelector.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. define([
  2. 'angular',
  3. 'app',
  4. 'lodash',
  5. 'jquery',
  6. ],
  7. function (angular, app, _) {
  8. 'use strict';
  9. angular
  10. .module('grafana.directives')
  11. .directive('variableValueSelect', function($compile, $window, $timeout) {
  12. return {
  13. scope: {
  14. variable: "=",
  15. onUpdated: "&"
  16. },
  17. templateUrl: 'app/features/dashboard/partials/variableValueSelect.html',
  18. link: function(scope, elem) {
  19. var bodyEl = angular.element($window.document.body);
  20. var variable = scope.variable;
  21. scope.show = function() {
  22. scope.selectorOpen = true;
  23. scope.giveFocus = 1;
  24. scope.oldCurrentText = variable.current.text;
  25. var currentValues = variable.current.value;
  26. if (_.isString(currentValues)) {
  27. currentValues = [currentValues];
  28. }
  29. scope.options = _.map(variable.options, function(option) {
  30. if (_.indexOf(currentValues, option.value) >= 0) {
  31. option.selected = true;
  32. }
  33. return option;
  34. });
  35. $timeout(function() {
  36. bodyEl.on('click', scope.bodyOnClick);
  37. }, 0, false);
  38. };
  39. scope.optionSelected = function(option) {
  40. option.selected = !option.selected;
  41. if (!variable.multi || option.text === 'All') {
  42. _.each(scope.options, function(other) {
  43. if (option !== other) {
  44. other.selected = false;
  45. }
  46. });
  47. }
  48. var selected = _.filter(scope.options, {selected: true});
  49. // enfore the first selected if no option is selected
  50. if (selected.length === 0) {
  51. scope.options[0].selected = true;
  52. selected = [scope.options[0]];
  53. }
  54. if (selected.length > 1) {
  55. if (selected[0].text === 'All') {
  56. selected[0].selected = false;
  57. selected = selected.slice(1, selected.length);
  58. }
  59. }
  60. variable.current = {
  61. text: _.pluck(selected, 'text').join(', '),
  62. value: _.pluck(selected, 'value'),
  63. };
  64. // only single value
  65. if (variable.current.value.length === 1) {
  66. variable.current.value = selected[0].value;
  67. }
  68. scope.updateLinkText();
  69. scope.onUpdated();
  70. };
  71. scope.hide = function() {
  72. scope.selectorOpen = false;
  73. // if (scope.oldCurrentText !== variable.current.text) {
  74. // scope.onUpdated();
  75. // }
  76. bodyEl.off('click', scope.bodyOnClick);
  77. };
  78. scope.bodyOnClick = function(e) {
  79. var dropdown = elem.find('.variable-value-dropdown');
  80. if (dropdown.has(e.target).length === 0) {
  81. scope.$apply(scope.hide);
  82. }
  83. };
  84. scope.updateLinkText = function() {
  85. scope.labelText = variable.label || '$' + variable.name;
  86. scope.linkText = variable.current.text;
  87. };
  88. scope.$watchGroup(['variable.hideLabel', 'variable.name', 'variable.label', 'variable.current.text'], function() {
  89. scope.updateLinkText();
  90. });
  91. },
  92. };
  93. });
  94. });