variableValueSelect.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. function openDropdown(inputEl, linkEl) {
  13. inputEl.css('width', (linkEl.width() + 16) + 'px');
  14. linkEl.hide();
  15. inputEl.show();
  16. inputEl.focus();
  17. };
  18. return {
  19. scope: {
  20. variable: "=",
  21. onUpdated: "&"
  22. },
  23. templateUrl: 'app/features/dashboard/partials/variableValueSelect.html',
  24. link: function(scope, elem) {
  25. var bodyEl = angular.element($window.document.body);
  26. var linkEl = elem.find('.variable-value-link');
  27. var inputEl = elem.find('input');
  28. var variable = scope.variable;
  29. var cancelBlur = null;
  30. scope.openDropdown = function() {
  31. inputEl.show();
  32. linkEl.hide();
  33. scope.dropdownVisible = true;
  34. inputEl.css('width', (linkEl.width() + 16) + 'px');
  35. linkEl.hide();
  36. inputEl.show();
  37. inputEl.focus();
  38. $timeout(function() { bodyEl.on('click', scope.bodyOnClick); }, 0, false);
  39. };
  40. scope.switchToLink = function(now) {
  41. if (now === true || cancelBlur) {
  42. clearTimeout(cancelBlur);
  43. cancelBlur = null;
  44. inputEl.hide();
  45. linkEl.show();
  46. scope.dropdownVisible = false;
  47. scope.$digest();
  48. scope.updateLinkText();
  49. scope.onUpdated();
  50. }
  51. else {
  52. // need to have long delay because the blur
  53. // happens long before the click event on the typeahead options
  54. cancelBlur = setTimeout(scope.switchToLink, 50);
  55. }
  56. bodyEl.off('click', scope.bodyOnClick);
  57. };
  58. scope.bodyOnClick = function(e) {
  59. if (elem.has(e.target).length === 0) {
  60. scope.switchToLink();
  61. }
  62. };
  63. scope.show = function() {
  64. scope.oldCurrentText = variable.current.text;
  65. scope.highlightIndex = -1;
  66. var currentValues = variable.current.value;
  67. if (_.isString(currentValues)) {
  68. currentValues = [currentValues];
  69. }
  70. scope.options = _.map(variable.options, function(option) {
  71. if (_.indexOf(currentValues, option.value) >= 0) {
  72. option.selected = true;
  73. }
  74. return option;
  75. });
  76. scope.search = {query: '', options: scope.options};
  77. scope.openDropdown();
  78. };
  79. scope.queryChanged = function() {
  80. scope.highlightIndex = -1;
  81. scope.search.options = _.filter(scope.options, function(option) {
  82. return option.text.toLowerCase().indexOf(scope.search.query.toLowerCase()) !== -1;
  83. });
  84. };
  85. scope.keyDown = function (evt) {
  86. if (evt.keyCode === 27) {
  87. scope.hide();
  88. }
  89. if (evt.keyCode === 40) {
  90. scope.moveHighlight(1);
  91. }
  92. if (evt.keyCode === 38) {
  93. scope.moveHighlight(-1);
  94. }
  95. if (evt.keyCode === 13) {
  96. scope.optionSelected(scope.search.options[scope.highlightIndex], {}, true, false);
  97. }
  98. if (evt.keyCode === 32) {
  99. scope.optionSelected(scope.search.options[scope.highlightIndex], {}, false, false);
  100. }
  101. };
  102. scope.moveHighlight = function(direction) {
  103. scope.highlightIndex = (scope.highlightIndex + direction) % scope.search.options.length;
  104. };
  105. scope.optionSelected = function(option, event, commitChange, excludeOthers) {
  106. if (!option) { return; }
  107. option.selected = !option.selected;
  108. commitChange = commitChange || false;
  109. excludeOthers = excludeOthers || false;
  110. var setAllExceptCurrentTo = function(newValue) {
  111. _.each(scope.options, function(other) {
  112. if (option !== other) { other.selected = newValue; }
  113. });
  114. };
  115. // commit action (enter key), should not deselect it
  116. if (commitChange) {
  117. option.selected = true;
  118. }
  119. if (option.text === 'All' || excludeOthers) {
  120. setAllExceptCurrentTo(false);
  121. commitChange = true;
  122. }
  123. else if (!variable.multi) {
  124. setAllExceptCurrentTo(false);
  125. commitChange = true;
  126. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  127. commitChange = true;
  128. setAllExceptCurrentTo(false);
  129. }
  130. var selected = _.filter(scope.options, {selected: true});
  131. if (selected.length === 0) {
  132. option.selected = true;
  133. selected = [option];
  134. }
  135. if (selected.length > 1 && selected.length !== scope.options.length) {
  136. if (selected[0].text === 'All') {
  137. selected[0].selected = false;
  138. selected = selected.slice(1, selected.length);
  139. }
  140. }
  141. variable.current = {
  142. text: _.pluck(selected, 'text').join(', '),
  143. value: _.pluck(selected, 'value'),
  144. };
  145. // only single value
  146. if (variable.current.value.length === 1) {
  147. variable.current.value = selected[0].value;
  148. }
  149. if (commitChange) {
  150. scope.switchToLink();
  151. }
  152. };
  153. scope.updateLinkText = function() {
  154. scope.labelText = variable.label || '$' + variable.name;
  155. scope.linkText = variable.current.text;
  156. };
  157. scope.$watchGroup(['variable.hideLabel', 'variable.name', 'variable.label', 'variable.current.text'], function() {
  158. scope.updateLinkText();
  159. });
  160. linkEl.click(scope.openDropdown);
  161. //inputEl.blur(scope.switchToLink);
  162. },
  163. };
  164. });
  165. });