variableValueSelect.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. if (scope.selectorOpen) {
  23. return;
  24. }
  25. scope.selectorOpen = true;
  26. scope.giveFocus = 1;
  27. scope.oldCurrentText = variable.current.text;
  28. scope.highlightIndex = -1;
  29. var currentValues = variable.current.value;
  30. if (_.isString(currentValues)) {
  31. currentValues = [currentValues];
  32. }
  33. scope.options = _.map(variable.options, function(option) {
  34. if (_.indexOf(currentValues, option.value) >= 0) {
  35. option.selected = true;
  36. }
  37. return option;
  38. });
  39. scope.search = {query: '', options: scope.options};
  40. $timeout(function() {
  41. bodyEl.on('click', scope.bodyOnClick);
  42. }, 0, false);
  43. };
  44. scope.queryChanged = function() {
  45. scope.highlightIndex = -1;
  46. scope.search.options = _.filter(scope.options, function(option) {
  47. return option.text.toLowerCase().indexOf(scope.search.query.toLowerCase()) !== -1;
  48. });
  49. };
  50. scope.keyDown = function (evt) {
  51. if (evt.keyCode === 27) {
  52. scope.hide();
  53. }
  54. if (evt.keyCode === 40) {
  55. scope.moveHighlight(1);
  56. }
  57. if (evt.keyCode === 38) {
  58. scope.moveHighlight(-1);
  59. }
  60. if (evt.keyCode === 13) {
  61. scope.optionSelected(scope.search.options[scope.highlightIndex], {});
  62. }
  63. };
  64. scope.moveHighlight = function(direction) {
  65. scope.highlightIndex = (scope.highlightIndex + direction) % scope.search.options.length;
  66. };
  67. scope.optionSelected = function(option, event) {
  68. option.selected = !option.selected;
  69. var hideAfter = true;
  70. var setAllExceptCurrentTo = function(newValue) {
  71. _.each(scope.options, function(other) {
  72. if (option !== other) { other.selected = newValue; }
  73. });
  74. };
  75. if (option.text === 'All') {
  76. setAllExceptCurrentTo(false);
  77. }
  78. else if (!variable.multi) {
  79. setAllExceptCurrentTo(false);
  80. } else {
  81. if (event.ctrlKey || event.metaKey || event.shiftKey) {
  82. hideAfter = false;
  83. }
  84. else {
  85. setAllExceptCurrentTo(false);
  86. }
  87. }
  88. var selected = _.filter(scope.options, {selected: true});
  89. if (selected.length === 0) {
  90. option.selected = true;
  91. selected = [option];
  92. }
  93. if (selected.length > 1 && selected.length !== scope.options.length) {
  94. if (selected[0].text === 'All') {
  95. selected[0].selected = false;
  96. selected = selected.slice(1, selected.length);
  97. }
  98. }
  99. variable.current = {
  100. text: _.pluck(selected, 'text').join(', '),
  101. value: _.pluck(selected, 'value'),
  102. };
  103. // only single value
  104. if (variable.current.value.length === 1) {
  105. variable.current.value = selected[0].value;
  106. }
  107. scope.updateLinkText();
  108. scope.onUpdated();
  109. if (hideAfter) {
  110. scope.hide();
  111. }
  112. };
  113. scope.hide = function() {
  114. scope.selectorOpen = false;
  115. bodyEl.off('click', scope.bodyOnClick);
  116. };
  117. scope.bodyOnClick = function(e) {
  118. var dropdown = elem.find('.variable-value-dropdown');
  119. if (dropdown.has(e.target).length === 0) {
  120. scope.$apply(scope.hide);
  121. }
  122. };
  123. scope.updateLinkText = function() {
  124. scope.labelText = variable.label || '$' + variable.name;
  125. scope.linkText = variable.current.text;
  126. };
  127. scope.$watchGroup(['variable.hideLabel', 'variable.name', 'variable.label', 'variable.current.text'], function() {
  128. scope.updateLinkText();
  129. });
  130. },
  131. };
  132. });
  133. });