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