variableValueSelect.js 7.5 KB

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