valueSelectDropdown.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if (vm.search.options.length === 0) {
  95. vm.commitChanges();
  96. } else {
  97. vm.selectValue(vm.search.options[vm.highlightIndex], {}, true, false);
  98. }
  99. }
  100. if (evt.keyCode === 32) {
  101. vm.selectValue(vm.search.options[vm.highlightIndex], {}, false, false);
  102. }
  103. };
  104. vm.moveHighlight = function(direction) {
  105. vm.highlightIndex = (vm.highlightIndex + direction) % vm.search.options.length;
  106. };
  107. vm.selectValue = function(option, event, commitChange, excludeOthers) {
  108. if (!option) { return; }
  109. option.selected = !option.selected;
  110. commitChange = commitChange || false;
  111. excludeOthers = excludeOthers || false;
  112. var setAllExceptCurrentTo = function(newValue) {
  113. _.each(vm.options, function(other) {
  114. if (option !== other) { other.selected = newValue; }
  115. });
  116. };
  117. // commit action (enter key), should not deselect it
  118. if (commitChange) {
  119. option.selected = true;
  120. }
  121. if (option.text === 'All' || excludeOthers) {
  122. setAllExceptCurrentTo(false);
  123. commitChange = true;
  124. }
  125. else if (!vm.variable.multi) {
  126. setAllExceptCurrentTo(false);
  127. commitChange = true;
  128. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  129. commitChange = true;
  130. setAllExceptCurrentTo(false);
  131. }
  132. vm.selectionsChanged(commitChange);
  133. };
  134. vm.selectionsChanged = function(commitChange) {
  135. vm.selectedValues = _.filter(vm.options, {selected: true});
  136. if (vm.selectedValues.length > 1 && vm.selectedValues.length !== vm.options.length) {
  137. if (vm.selectedValues[0].text === 'All') {
  138. vm.selectedValues[0].selected = false;
  139. vm.selectedValues = vm.selectedValues.slice(1, vm.selectedValues.length);
  140. }
  141. }
  142. // validate selected tags
  143. _.each(vm.tags, function(tag) {
  144. if (tag.selected) {
  145. _.each(tag.values, function(value) {
  146. if (!_.findWhere(vm.selectedValues, {value: value})) {
  147. tag.selected = false;
  148. }
  149. });
  150. }
  151. });
  152. vm.selectedTags = _.filter(vm.tags, {selected: true});
  153. vm.variable.current.value = _.pluck(vm.selectedValues, 'value');
  154. vm.variable.current.text = _.pluck(vm.selectedValues, 'text').join(' + ');
  155. vm.variable.current.tags = vm.selectedTags;
  156. // only single value
  157. if (vm.selectedValues.length === 1) {
  158. vm.variable.current.value = vm.selectedValues[0].value;
  159. }
  160. if (commitChange) {
  161. vm.commitChanges();
  162. }
  163. };
  164. vm.commitChanges = function() {
  165. // if we have a search query and no options use that
  166. if (vm.search.options.length === 0 && vm.search.query.length > 0) {
  167. vm.variable.current = {text: vm.search.query, value: vm.search.query};
  168. }
  169. else if (vm.selectedValues.length === 0) {
  170. // make sure one option is selected
  171. vm.options[0].selected = true;
  172. vm.selectionsChanged(false);
  173. }
  174. vm.dropdownVisible = false;
  175. vm.updateLinkText();
  176. if (vm.variable.current.text !== vm.oldVariableText) {
  177. vm.onUpdated();
  178. }
  179. };
  180. vm.queryChanged = function() {
  181. vm.highlightIndex = -1;
  182. vm.search.options = _.filter(vm.options, function(option) {
  183. return option.text.toLowerCase().indexOf(vm.search.query.toLowerCase()) !== -1;
  184. });
  185. vm.search.options = vm.search.options.slice(0, Math.min(vm.search.options.length, 1000));
  186. };
  187. vm.init = function() {
  188. vm.selectedTags = vm.variable.current.tags || [];
  189. vm.updateLinkText();
  190. };
  191. });
  192. angular
  193. .module('grafana.directives')
  194. .directive('valueSelectDropdown', function($compile, $window, $timeout, $rootScope) {
  195. return {
  196. scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" },
  197. templateUrl: 'app/partials/valueSelectDropdown.html',
  198. controller: 'ValueSelectDropdownCtrl',
  199. controllerAs: 'vm',
  200. bindToController: true,
  201. link: function(scope, elem) {
  202. var bodyEl = angular.element($window.document.body);
  203. var linkEl = elem.find('.variable-value-link');
  204. var inputEl = elem.find('input');
  205. function openDropdown() {
  206. inputEl.css('width', Math.max(linkEl.width(), 30) + 'px');
  207. inputEl.show();
  208. linkEl.hide();
  209. inputEl.focus();
  210. $timeout(function() { bodyEl.on('click', bodyOnClick); }, 0, false);
  211. }
  212. function switchToLink() {
  213. inputEl.hide();
  214. linkEl.show();
  215. bodyEl.off('click', bodyOnClick);
  216. }
  217. function bodyOnClick (e) {
  218. if (elem.has(e.target).length === 0) {
  219. scope.$apply(function() {
  220. scope.vm.commitChanges();
  221. });
  222. }
  223. }
  224. scope.$watch('vm.dropdownVisible', function(newValue) {
  225. if (newValue) {
  226. openDropdown();
  227. } else {
  228. switchToLink();
  229. }
  230. });
  231. var cleanUp = $rootScope.$on('template-variable-value-updated', function() {
  232. scope.vm.updateLinkText();
  233. });
  234. scope.$on("$destroy", function() {
  235. cleanUp();
  236. });
  237. scope.vm.init();
  238. },
  239. };
  240. });
  241. });