variableValueSelect.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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('SelectDropdownCtrl', function() {
  12. var vm = this;
  13. vm.show = 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. vm.dropdownVisible = true;
  35. };
  36. vm.updateLinkText = function() {
  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(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(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(commitChange);
  109. };
  110. vm.selectionsChanged = function(commitChange) {
  111. var selected = _.filter(vm.options, {selected: true});
  112. if (selected.length > 1 && selected.length !== vm.options.length) {
  113. if (selected[0].text === 'All') {
  114. selected[0].selected = false;
  115. selected = selected.slice(1, selected.length);
  116. }
  117. }
  118. vm.variable.current = {
  119. text: _.pluck(selected, 'text').join(', '),
  120. value: _.pluck(selected, 'value'),
  121. };
  122. var valuesNotInTag = _.filter(selected, function(test) {
  123. for (var i = 0; i < vm.selectedTags.length; i++) {
  124. var tag = vm.selectedTags[i];
  125. if (_.indexOf(tag.values, test.value) !== -1) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. });
  131. vm.variable.current.text = _.pluck(valuesNotInTag, 'text').join(', ');
  132. vm.selectedValuesCount = vm.variable.current.value.length;
  133. // only single value
  134. if (vm.selectedValuesCount === 1) {
  135. vm.variable.current.value = selected[0].value;
  136. }
  137. if (commitChange) {
  138. vm.commitChanges();
  139. }
  140. };
  141. vm.commitChanges = function() {
  142. // make sure one option is selected
  143. var selected = _.filter(vm.options, {selected: true});
  144. if (selected.length === 0) {
  145. vm.options[0].selected = true;
  146. vm.selectionsChanged(false);
  147. }
  148. vm.dropdownVisible = false;
  149. vm.updateLinkText();
  150. };
  151. vm.queryChanged = function() {
  152. vm.highlightIndex = -1;
  153. vm.search.options = _.filter(vm.options, function(option) {
  154. return option.text.toLowerCase().indexOf(vm.search.query.toLowerCase()) !== -1;
  155. });
  156. };
  157. vm.init = function() {
  158. vm.updateLinkText();
  159. };
  160. });
  161. angular
  162. .module('grafana.directives')
  163. .directive('variableValueSelect', function($compile, $window, $timeout) {
  164. return {
  165. scope: { variable: "=", onUpdated: "&" },
  166. templateUrl: 'app/features/dashboard/partials/variableValueSelect.html',
  167. controller: 'SelectDropdownCtrl',
  168. controllerAs: 'vm',
  169. bindToController: true,
  170. link: function(scope, elem) {
  171. var bodyEl = angular.element($window.document.body);
  172. var linkEl = elem.find('.variable-value-link');
  173. var inputEl = elem.find('input');
  174. var cancelBlur = null;
  175. function openDropdown() {
  176. inputEl.css('width', Math.max(linkEl.width(), 30) + 'px');
  177. inputEl.show();
  178. linkEl.hide();
  179. linkEl.hide();
  180. inputEl.show();
  181. inputEl.focus();
  182. $timeout(function() { bodyEl.on('click', bodyOnClick); }, 0, false);
  183. }
  184. function switchToLink(now) {
  185. if (now === true || cancelBlur) {
  186. clearTimeout(cancelBlur);
  187. cancelBlur = null;
  188. inputEl.hide();
  189. linkEl.show();
  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', bodyOnClick);
  197. }
  198. function bodyOnClick (e) {
  199. if (elem.has(e.target).length === 0) {
  200. scope.$apply(function() {
  201. scope.vm.commitChanges();
  202. });
  203. }
  204. }
  205. scope.$watch('vm.dropdownVisible', function(newValue) {
  206. if (newValue) {
  207. openDropdown();
  208. } {
  209. switchToLink();
  210. }
  211. });
  212. scope.vm.init();
  213. },
  214. };
  215. });
  216. });