variableValueSelect.js 7.5 KB

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