variableValueSelect.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: { variable: "=", onUpdated: "&" },
  14. templateUrl: 'app/features/dashboard/partials/variableValueSelect.html',
  15. link: function(scope, elem) {
  16. var bodyEl = angular.element($window.document.body);
  17. var linkEl = elem.find('.variable-value-link');
  18. var inputEl = elem.find('input');
  19. var variable = scope.variable;
  20. var cancelBlur = null;
  21. scope.openDropdown = function() {
  22. inputEl.show();
  23. linkEl.hide();
  24. scope.dropdownVisible = true;
  25. inputEl.css('width', (linkEl.width() + 16) + 'px');
  26. linkEl.hide();
  27. inputEl.show();
  28. inputEl.focus();
  29. $timeout(function() { bodyEl.on('click', scope.bodyOnClick); }, 0, false);
  30. };
  31. scope.switchToLink = function(now) {
  32. if (now === true || cancelBlur) {
  33. clearTimeout(cancelBlur);
  34. cancelBlur = null;
  35. inputEl.hide();
  36. linkEl.show();
  37. scope.dropdownVisible = false;
  38. scope.$digest();
  39. scope.updateLinkText();
  40. scope.onUpdated();
  41. }
  42. else {
  43. // need to have long delay because the blur
  44. // happens long before the click event on the typeahead options
  45. cancelBlur = setTimeout(scope.switchToLink, 50);
  46. }
  47. bodyEl.off('click', scope.bodyOnClick);
  48. };
  49. scope.bodyOnClick = function(e) {
  50. if (elem.has(e.target).length === 0) {
  51. scope.switchToLink();
  52. }
  53. };
  54. scope.show = function() {
  55. scope.oldCurrentText = variable.current.text;
  56. scope.highlightIndex = -1;
  57. var currentValues = variable.current.value;
  58. if (_.isString(currentValues)) {
  59. currentValues = [currentValues];
  60. }
  61. scope.options = _.map(variable.options, function(option) {
  62. if (_.indexOf(currentValues, option.value) >= 0) {
  63. option.selected = true;
  64. }
  65. return option;
  66. });
  67. scope.search = {query: '', options: scope.options};
  68. scope.selectedValuesCount = currentValues.length;
  69. scope.selectedTags = scope.selectedTag || [];
  70. if (!scope.tags) {
  71. scope.tags = _.map(variable.tags, function(value) {
  72. return { text: value, selected: false };
  73. });
  74. }
  75. scope.openDropdown();
  76. };
  77. scope.queryChanged = function() {
  78. scope.highlightIndex = -1;
  79. scope.search.options = _.filter(scope.options, function(option) {
  80. return option.text.toLowerCase().indexOf(scope.search.query.toLowerCase()) !== -1;
  81. });
  82. };
  83. scope.keyDown = function (evt) {
  84. if (evt.keyCode === 27) {
  85. scope.hide();
  86. }
  87. if (evt.keyCode === 40) {
  88. scope.moveHighlight(1);
  89. }
  90. if (evt.keyCode === 38) {
  91. scope.moveHighlight(-1);
  92. }
  93. if (evt.keyCode === 13) {
  94. scope.optionSelected(scope.search.options[scope.highlightIndex], {}, true, false);
  95. }
  96. if (evt.keyCode === 32) {
  97. scope.optionSelected(scope.search.options[scope.highlightIndex], {}, false, false);
  98. }
  99. };
  100. scope.moveHighlight = function(direction) {
  101. scope.highlightIndex = (scope.highlightIndex + direction) % scope.search.options.length;
  102. };
  103. scope.optionSelected = function(option, event, commitChange, excludeOthers) {
  104. if (!option) { return; }
  105. option.selected = !option.selected;
  106. commitChange = commitChange || false;
  107. excludeOthers = excludeOthers || false;
  108. var setAllExceptCurrentTo = function(newValue) {
  109. _.each(scope.options, function(other) {
  110. if (option !== other) { other.selected = newValue; }
  111. });
  112. };
  113. // commit action (enter key), should not deselect it
  114. if (commitChange) {
  115. option.selected = true;
  116. }
  117. if (option.text === 'All' || excludeOthers) {
  118. setAllExceptCurrentTo(false);
  119. commitChange = true;
  120. }
  121. else if (!variable.multi) {
  122. setAllExceptCurrentTo(false);
  123. commitChange = true;
  124. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  125. commitChange = true;
  126. setAllExceptCurrentTo(false);
  127. }
  128. scope.selectionsChanged(option, commitChange);
  129. };
  130. scope.selectionsChanged = function(defaultItem, commitChange) {
  131. var selected = _.filter(scope.options, {selected: true});
  132. if (selected.length === 0) {
  133. defaultItem.selected = true;
  134. selected = [defaultItem];
  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. var valuesNotInTag = _.filter(selected, function(test) {
  147. for (var i = 0; i < scope.selectedTags.length; i++) {
  148. var tag = scope.selectedTags[i];
  149. if (_.indexOf(tag.values, test.value) !== -1) {
  150. return false;
  151. }
  152. }
  153. return true;
  154. });
  155. variable.current.text = _.pluck(valuesNotInTag, 'text').join(', ');
  156. scope.selectedValuesCount = variable.current.value.length;
  157. // only single value
  158. if (scope.selectedValuesCount === 1) {
  159. variable.current.value = selected[0].value;
  160. }
  161. if (commitChange) {
  162. scope.switchToLink();
  163. }
  164. };
  165. scope.clearSelections = function() {
  166. _.each(scope.options, function(option) {
  167. option.selected = false;
  168. });
  169. scope.selectionsChanged(scope.options[0], false);
  170. };
  171. scope.selectTag = function(tag) {
  172. tag.selected = !tag.selected;
  173. if (!tag.values) {
  174. if (tag.text === 'backend') {
  175. tag.values = ['backend_01', 'backend_02', 'backend_03', 'backend_04'];
  176. } else {
  177. tag.values = ['web_server_01', 'web_server_02', 'web_server_03', 'web_server_04'];
  178. }
  179. console.log('querying for tag values');
  180. }
  181. _.each(scope.options, function(option) {
  182. if (_.indexOf(tag.values, option.value) !== -1) {
  183. option.selected = tag.selected;
  184. }
  185. });
  186. scope.selectedTags = _.filter(scope.tags, {selected: true});
  187. scope.selectionsChanged(scope.options[0], false);
  188. };
  189. scope.updateLinkText = function() {
  190. scope.labelText = variable.label || '$' + variable.name;
  191. scope.linkText = variable.current.text;
  192. };
  193. scope.$watchGroup(['variable.hideLabel', 'variable.name', 'variable.label', 'variable.current.text'], function() {
  194. scope.updateLinkText();
  195. });
  196. linkEl.click(scope.openDropdown);
  197. },
  198. };
  199. });
  200. });