valueSelectDropdown.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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('ValueSelectDropdownCtrl', function($q) {
  12. var vm = this;
  13. vm.show = function() {
  14. vm.oldVariableText = vm.variable.current.text;
  15. vm.highlightIndex = -1;
  16. vm.options = vm.variable.options;
  17. vm.selectedValues = _.filter(vm.options, {selected: true});
  18. vm.tags = _.map(vm.variable.tags, function(value) {
  19. var tag = { text: value, selected: false };
  20. _.each(vm.variable.current.tags, function(tagObj) {
  21. if (tagObj.text === value) {
  22. tag = tagObj;
  23. }
  24. });
  25. return tag;
  26. });
  27. vm.search = {
  28. query: '',
  29. options: vm.options.slice(0, Math.min(vm.options.length, 1000))
  30. };
  31. vm.dropdownVisible = true;
  32. };
  33. vm.updateLinkText = function() {
  34. var current = vm.variable.current;
  35. if (current.tags && current.tags.length) {
  36. // filer out values that are in selected tags
  37. var selectedAndNotInTag = _.filter(vm.variable.options, function(option) {
  38. if (!option.selected) { return false; }
  39. for (var i = 0; i < current.tags.length; i++) {
  40. var tag = current.tags[i];
  41. if (_.indexOf(tag.values, option.value) !== -1) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. });
  47. // convert values to text
  48. var currentTexts = _.pluck(selectedAndNotInTag, 'text');
  49. // join texts
  50. vm.linkText = currentTexts.join(' + ');
  51. if (vm.linkText.length > 0) {
  52. vm.linkText += ' + ';
  53. }
  54. } else {
  55. vm.linkText = vm.variable.current.text;
  56. }
  57. };
  58. vm.clearSelections = function() {
  59. _.each(vm.options, function(option) {
  60. option.selected = false;
  61. });
  62. vm.selectionsChanged(false);
  63. };
  64. vm.selectTag = function(tag) {
  65. tag.selected = !tag.selected;
  66. var tagValuesPromise;
  67. if (!tag.values) {
  68. tagValuesPromise = vm.getValuesForTag({tagKey: tag.text});
  69. } else {
  70. tagValuesPromise = $q.when(tag.values);
  71. }
  72. tagValuesPromise.then(function(values) {
  73. tag.values = values;
  74. tag.valuesText = values.join(' + ');
  75. _.each(vm.options, function(option) {
  76. if (_.indexOf(tag.values, option.value) !== -1) {
  77. option.selected = tag.selected;
  78. }
  79. });
  80. vm.selectionsChanged(false);
  81. });
  82. };
  83. vm.keyDown = function (evt) {
  84. if (evt.keyCode === 27) {
  85. vm.hide();
  86. }
  87. if (evt.keyCode === 40) {
  88. vm.moveHighlight(1);
  89. }
  90. if (evt.keyCode === 38) {
  91. vm.moveHighlight(-1);
  92. }
  93. if (evt.keyCode === 13) {
  94. vm.optionSelected(vm.search.options[vm.highlightIndex], {}, true, false);
  95. }
  96. if (evt.keyCode === 32) {
  97. vm.optionSelected(vm.search.options[vm.highlightIndex], {}, false, false);
  98. }
  99. };
  100. vm.moveHighlight = function(direction) {
  101. vm.highlightIndex = (vm.highlightIndex + direction) % vm.search.options.length;
  102. };
  103. vm.selectValue = 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(vm.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 (!vm.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. vm.selectionsChanged(commitChange);
  129. };
  130. vm.selectionsChanged = function(commitChange) {
  131. vm.selectedValues = _.filter(vm.options, {selected: true});
  132. if (vm.selectedValues.length > 1 && vm.selectedValues.length !== vm.options.length) {
  133. if (vm.selectedValues[0].text === 'All') {
  134. vm.selectedValues[0].selected = false;
  135. vm.selectedValues = vm.selectedValues.slice(1, vm.selectedValues.length);
  136. }
  137. }
  138. // validate selected tags
  139. _.each(vm.tags, function(tag) {
  140. if (tag.selected) {
  141. _.each(tag.values, function(value) {
  142. if (!_.findWhere(vm.selectedValues, {value: value})) {
  143. tag.selected = false;
  144. }
  145. });
  146. }
  147. });
  148. vm.selectedTags = _.filter(vm.tags, {selected: true});
  149. vm.variable.current.value = _.pluck(vm.selectedValues, 'value');
  150. vm.variable.current.text = _.pluck(vm.selectedValues, 'text').join(' + ');
  151. vm.variable.current.tags = vm.selectedTags;
  152. // only single value
  153. if (vm.selectedValues.length === 1) {
  154. vm.variable.current.value = vm.selectedValues[0].value;
  155. }
  156. if (commitChange) {
  157. vm.commitChanges();
  158. }
  159. };
  160. vm.commitChanges = function() {
  161. // make sure one option is selected
  162. if (vm.selectedValues.length === 0) {
  163. vm.options[0].selected = true;
  164. vm.selectionsChanged(false);
  165. }
  166. vm.dropdownVisible = false;
  167. vm.updateLinkText();
  168. if (vm.variable.current.text !== vm.oldVariableText) {
  169. vm.onUpdated();
  170. }
  171. };
  172. vm.queryChanged = function() {
  173. vm.highlightIndex = -1;
  174. vm.search.options = _.filter(vm.options, function(option) {
  175. return option.text.toLowerCase().indexOf(vm.search.query.toLowerCase()) !== -1;
  176. });
  177. vm.search.options = vm.search.options.slice(0, Math.min(vm.search.options.length, 1000));
  178. };
  179. vm.init = function() {
  180. vm.selectedTags = vm.variable.current.tags || [];
  181. vm.updateLinkText();
  182. };
  183. });
  184. angular
  185. .module('grafana.directives')
  186. .directive('valueSelectDropdown', function($compile, $window, $timeout, $rootScope) {
  187. return {
  188. scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" },
  189. templateUrl: 'app/partials/valueSelectDropdown.html',
  190. controller: 'ValueSelectDropdownCtrl',
  191. controllerAs: 'vm',
  192. bindToController: true,
  193. link: function(scope, elem) {
  194. var bodyEl = angular.element($window.document.body);
  195. var linkEl = elem.find('.variable-value-link');
  196. var inputEl = elem.find('input');
  197. function openDropdown() {
  198. inputEl.css('width', Math.max(linkEl.width(), 30) + 'px');
  199. inputEl.show();
  200. linkEl.hide();
  201. inputEl.focus();
  202. $timeout(function() { bodyEl.on('click', bodyOnClick); }, 0, false);
  203. }
  204. function switchToLink() {
  205. inputEl.hide();
  206. linkEl.show();
  207. bodyEl.off('click', bodyOnClick);
  208. }
  209. function bodyOnClick (e) {
  210. if (elem.has(e.target).length === 0) {
  211. scope.$apply(function() {
  212. scope.vm.commitChanges();
  213. });
  214. }
  215. }
  216. scope.$watch('vm.dropdownVisible', function(newValue) {
  217. if (newValue) {
  218. openDropdown();
  219. } else {
  220. switchToLink();
  221. }
  222. });
  223. var cleanUp = $rootScope.$on('template-variable-value-updated', function() {
  224. scope.vm.updateLinkText();
  225. });
  226. scope.$on("$destroy", function() {
  227. cleanUp();
  228. });
  229. scope.vm.init();
  230. },
  231. };
  232. });
  233. });