variableValueSelect.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.selectedValuesCount = currentValues.length;
  78. scope.openDropdown();
  79. };
  80. scope.queryChanged = function() {
  81. scope.highlightIndex = -1;
  82. scope.search.options = _.filter(scope.options, function(option) {
  83. return option.text.toLowerCase().indexOf(scope.search.query.toLowerCase()) !== -1;
  84. });
  85. };
  86. scope.keyDown = function (evt) {
  87. if (evt.keyCode === 27) {
  88. scope.hide();
  89. }
  90. if (evt.keyCode === 40) {
  91. scope.moveHighlight(1);
  92. }
  93. if (evt.keyCode === 38) {
  94. scope.moveHighlight(-1);
  95. }
  96. if (evt.keyCode === 13) {
  97. scope.optionSelected(scope.search.options[scope.highlightIndex], {}, true, false);
  98. }
  99. if (evt.keyCode === 32) {
  100. scope.optionSelected(scope.search.options[scope.highlightIndex], {}, false, false);
  101. }
  102. };
  103. scope.moveHighlight = function(direction) {
  104. scope.highlightIndex = (scope.highlightIndex + direction) % scope.search.options.length;
  105. };
  106. scope.optionSelected = function(option, event, commitChange, excludeOthers) {
  107. if (!option) { return; }
  108. option.selected = !option.selected;
  109. commitChange = commitChange || false;
  110. excludeOthers = excludeOthers || false;
  111. var setAllExceptCurrentTo = function(newValue) {
  112. _.each(scope.options, function(other) {
  113. if (option !== other) { other.selected = newValue; }
  114. });
  115. };
  116. // commit action (enter key), should not deselect it
  117. if (commitChange) {
  118. option.selected = true;
  119. }
  120. if (option.text === 'All' || excludeOthers) {
  121. setAllExceptCurrentTo(false);
  122. commitChange = true;
  123. }
  124. else if (!variable.multi) {
  125. setAllExceptCurrentTo(false);
  126. commitChange = true;
  127. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  128. commitChange = true;
  129. setAllExceptCurrentTo(false);
  130. }
  131. var selected = _.filter(scope.options, {selected: true});
  132. if (selected.length === 0) {
  133. option.selected = true;
  134. selected = [option];
  135. }
  136. if (selected.length > 1 && selected.length !== scope.options.length) {
  137. if (selected[0].text === 'All') {
  138. selected[0].selected = false;
  139. selected = selected.slice(1, selected.length);
  140. }
  141. }
  142. variable.current = {
  143. text: _.pluck(selected, 'text').join(', '),
  144. value: _.pluck(selected, 'value'),
  145. };
  146. scope.selectedValuesCount = variable.current.value.length;
  147. // only single value
  148. if (scope.selectedValuesCount === 1) {
  149. variable.current.value = selected[0].value;
  150. }
  151. if (commitChange) {
  152. scope.switchToLink();
  153. }
  154. };
  155. scope.updateLinkText = function() {
  156. scope.labelText = variable.label || '$' + variable.name;
  157. scope.linkText = variable.current.text;
  158. };
  159. scope.$watchGroup(['variable.hideLabel', 'variable.name', 'variable.label', 'variable.current.text'], function() {
  160. scope.updateLinkText();
  161. });
  162. linkEl.click(scope.openDropdown);
  163. //inputEl.blur(scope.switchToLink);
  164. },
  165. };
  166. });
  167. });