variableValueSelect.js 8.1 KB

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