valueSelectDropdown.js 7.7 KB

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