variableValueSelect.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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($q) {
  12. var vm = this;
  13. vm.show = function() {
  14. vm.oldVariableText = 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) { option.selected = true; }
  22. return option;
  23. });
  24. _.sortBy(vm.options, 'text');
  25. vm.selectedValues = _.filter(vm.options, {selected: true});
  26. if (!vm.tags) {
  27. vm.tags = _.map(vm.variable.tags, function(value) {
  28. return { text: value, selected: false };
  29. });
  30. }
  31. vm.search = {query: '', options: vm.options};
  32. vm.dropdownVisible = true;
  33. };
  34. vm.updateLinkText = function() {
  35. var current = vm.variable.current;
  36. var currentValues = current.value;
  37. if (_.isArray(currentValues) && current.tags.length) {
  38. // filer out values that are in selected tags
  39. currentValues = _.filter(currentValues, function(test) {
  40. for (var i = 0; i < current.tags.length; i++) {
  41. if (_.indexOf(current.tags[i].values, test) !== -1) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. });
  47. // convert values to text
  48. var currentTexts = _.map(currentValues, function(value) {
  49. for (var i = 0; i < vm.variable.options.length; i++) {
  50. var option = vm.variable.options[i];
  51. if (option.value === value) {
  52. return option.text;
  53. }
  54. }
  55. return value;
  56. });
  57. // join texts
  58. vm.linkText = currentTexts.join(' + ');
  59. if (vm.linkText.length > 0) {
  60. vm.linkText += ' + ';
  61. }
  62. } else {
  63. vm.linkText = vm.variable.current.text;
  64. }
  65. };
  66. vm.clearSelections = function() {
  67. _.each(vm.options, function(option) {
  68. option.selected = false;
  69. });
  70. vm.selectionsChanged(false);
  71. };
  72. vm.selectTag = function(tag) {
  73. tag.selected = !tag.selected;
  74. var tagValuesPromise;
  75. if (!tag.values) {
  76. tagValuesPromise = vm.getValuesForTag({tagKey: tag.text});
  77. } else {
  78. tagValuesPromise = $q.when(tag.values);
  79. }
  80. tagValuesPromise.then(function(values) {
  81. tag.values = values;
  82. tag.valuesText = values.join(', ');
  83. _.each(vm.options, function(option) {
  84. if (_.indexOf(tag.values, option.value) !== -1) {
  85. option.selected = tag.selected;
  86. }
  87. });
  88. vm.selectionsChanged(false);
  89. });
  90. };
  91. vm.keyDown = function (evt) {
  92. if (evt.keyCode === 27) {
  93. vm.hide();
  94. }
  95. if (evt.keyCode === 40) {
  96. vm.moveHighlight(1);
  97. }
  98. if (evt.keyCode === 38) {
  99. vm.moveHighlight(-1);
  100. }
  101. if (evt.keyCode === 13) {
  102. vm.optionSelected(vm.search.options[vm.highlightIndex], {}, true, false);
  103. }
  104. if (evt.keyCode === 32) {
  105. vm.optionSelected(vm.search.options[vm.highlightIndex], {}, false, false);
  106. }
  107. };
  108. vm.moveHighlight = function(direction) {
  109. vm.highlightIndex = (vm.highlightIndex + direction) % vm.search.options.length;
  110. };
  111. vm.selectValue = function(option, event, commitChange, excludeOthers) {
  112. if (!option) { return; }
  113. option.selected = !option.selected;
  114. commitChange = commitChange || false;
  115. excludeOthers = excludeOthers || false;
  116. var setAllExceptCurrentTo = function(newValue) {
  117. _.each(vm.options, function(other) {
  118. if (option !== other) { other.selected = newValue; }
  119. });
  120. };
  121. // commit action (enter key), should not deselect it
  122. if (commitChange) {
  123. option.selected = true;
  124. }
  125. if (option.text === 'All' || excludeOthers) {
  126. setAllExceptCurrentTo(false);
  127. commitChange = true;
  128. }
  129. else if (!vm.variable.multi) {
  130. setAllExceptCurrentTo(false);
  131. commitChange = true;
  132. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  133. commitChange = true;
  134. setAllExceptCurrentTo(false);
  135. }
  136. vm.selectionsChanged(commitChange);
  137. };
  138. vm.selectionsChanged = function(commitChange) {
  139. vm.selectedValues = _.filter(vm.options, {selected: true});
  140. if (vm.selectedValues.length > 1 && vm.selectedValues.length !== vm.options.length) {
  141. if (vm.selectedValues[0].text === 'All') {
  142. vm.selectedValues[0].selected = false;
  143. vm.selectedValues = vm.selectedValues.slice(1, vm.selectedValues.length);
  144. }
  145. }
  146. // validate selected tags
  147. _.each(vm.tags, function(tag) {
  148. if (tag.selected) {
  149. _.each(tag.values, function(value) {
  150. if (!_.findWhere(vm.selectedValues, {value: value})) {
  151. tag.selected = false;
  152. }
  153. });
  154. }
  155. });
  156. vm.selectedTags = _.filter(vm.tags, {selected: true});
  157. vm.variable.current.value = _.pluck(vm.selectedValues, 'value');
  158. vm.variable.current.text = _.pluck(vm.selectedValues, 'text').join(' + ');
  159. vm.variable.current.tags = vm.selectedTags;
  160. // only single value
  161. if (vm.selectedValues.length === 1) {
  162. vm.variable.current.value = vm.selectedValues[0].value;
  163. }
  164. if (commitChange) {
  165. vm.commitChanges();
  166. }
  167. };
  168. vm.commitChanges = function() {
  169. // make sure one option is selected
  170. if (vm.selectedValues.length === 0) {
  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. };
  186. vm.init = function() {
  187. vm.selectedTags = vm.variable.current.tags || [];
  188. vm.updateLinkText();
  189. };
  190. });
  191. angular
  192. .module('grafana.directives')
  193. .directive('variableValueSelect', function($compile, $window, $timeout) {
  194. return {
  195. scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" },
  196. templateUrl: 'app/features/dashboard/partials/variableValueSelect.html',
  197. controller: 'SelectDropdownCtrl',
  198. controllerAs: 'vm',
  199. bindToController: true,
  200. link: function(scope, elem) {
  201. var bodyEl = angular.element($window.document.body);
  202. var linkEl = elem.find('.variable-value-link');
  203. var inputEl = elem.find('input');
  204. function openDropdown() {
  205. inputEl.css('width', Math.max(linkEl.width(), 30) + 'px');
  206. inputEl.show();
  207. linkEl.hide();
  208. inputEl.focus();
  209. $timeout(function() { bodyEl.on('click', bodyOnClick); }, 0, false);
  210. }
  211. function switchToLink() {
  212. inputEl.hide();
  213. linkEl.show();
  214. bodyEl.off('click', bodyOnClick);
  215. }
  216. function bodyOnClick (e) {
  217. if (elem.has(e.target).length === 0) {
  218. scope.$apply(function() {
  219. scope.vm.commitChanges();
  220. });
  221. }
  222. }
  223. scope.$watch('vm.dropdownVisible', function(newValue) {
  224. if (newValue) {
  225. openDropdown();
  226. } else {
  227. switchToLink();
  228. }
  229. });
  230. scope.vm.init();
  231. },
  232. };
  233. });
  234. });