variableValueSelect.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.oldLinkText = 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) {
  22. option.selected = true;
  23. }
  24. return option;
  25. });
  26. vm.search = {query: '', options: vm.options};
  27. vm.selectedValuesCount = currentValues.length;
  28. vm.selectedTags = vm.selectedTags || [];
  29. if (!vm.tags) {
  30. vm.tags = _.map(vm.variable.tags, function(value) {
  31. return { text: value, selected: false };
  32. });
  33. }
  34. vm.dropdownVisible = true;
  35. };
  36. vm.updateLinkText = function() {
  37. vm.linkText = vm.variable.current.text;
  38. if (vm.oldLinkText && vm.oldLinkText !== vm.linkText) {
  39. vm.onUpdated();
  40. }
  41. };
  42. vm.clearSelections = function() {
  43. _.each(vm.options, function(option) {
  44. option.selected = false;
  45. });
  46. vm.selectionsChanged(false);
  47. };
  48. vm.selectTag = function(tag) {
  49. tag.selected = !tag.selected;
  50. var tagValuesPromise;
  51. if (!tag.values) {
  52. tagValuesPromise = vm.getValuesForTag({tagKey: tag.text});
  53. // if (tag.text === 'backend') {
  54. // tag.values = ['backend_01', 'backend_02', 'backend_03', 'backend_04'];
  55. // } else {
  56. // tag.values = ['web_server_01', 'web_server_02', 'web_server_03', 'web_server_04'];
  57. // }
  58. } else {
  59. tagValuesPromise = $q.when(tag.values);
  60. }
  61. tagValuesPromise.then(function(values) {
  62. tag.values = values;
  63. _.each(vm.options, function(option) {
  64. if (_.indexOf(tag.values, option.value) !== -1) {
  65. option.selected = tag.selected;
  66. }
  67. });
  68. vm.selectionsChanged(false);
  69. });
  70. };
  71. vm.keyDown = function (evt) {
  72. if (evt.keyCode === 27) {
  73. vm.hide();
  74. }
  75. if (evt.keyCode === 40) {
  76. vm.moveHighlight(1);
  77. }
  78. if (evt.keyCode === 38) {
  79. vm.moveHighlight(-1);
  80. }
  81. if (evt.keyCode === 13) {
  82. vm.optionSelected(vm.search.options[vm.highlightIndex], {}, true, false);
  83. }
  84. if (evt.keyCode === 32) {
  85. vm.optionSelected(vm.search.options[vm.highlightIndex], {}, false, false);
  86. }
  87. };
  88. vm.moveHighlight = function(direction) {
  89. vm.highlightIndex = (vm.highlightIndex + direction) % vm.search.options.length;
  90. };
  91. vm.optionSelected = function(option, event, commitChange, excludeOthers) {
  92. if (!option) { return; }
  93. option.selected = !option.selected;
  94. commitChange = commitChange || false;
  95. excludeOthers = excludeOthers || false;
  96. var setAllExceptCurrentTo = function(newValue) {
  97. _.each(vm.options, function(other) {
  98. if (option !== other) { other.selected = newValue; }
  99. });
  100. };
  101. // commit action (enter key), should not deselect it
  102. if (commitChange) {
  103. option.selected = true;
  104. }
  105. if (option.text === 'All' || excludeOthers) {
  106. setAllExceptCurrentTo(false);
  107. commitChange = true;
  108. }
  109. else if (!vm.variable.multi) {
  110. setAllExceptCurrentTo(false);
  111. commitChange = true;
  112. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  113. commitChange = true;
  114. setAllExceptCurrentTo(false);
  115. }
  116. vm.selectionsChanged(commitChange);
  117. };
  118. vm.selectionsChanged = function(commitChange) {
  119. var selected = _.filter(vm.options, {selected: true});
  120. if (selected.length > 1 && selected.length !== vm.options.length) {
  121. if (selected[0].text === 'All') {
  122. selected[0].selected = false;
  123. selected = selected.slice(1, selected.length);
  124. }
  125. }
  126. // validate selected tags
  127. _.each(vm.selectedTags, function(tag) {
  128. _.each(tag.values, function(value) {
  129. if (!_.findWhere(selected, {value: value})) {
  130. tag.selected = false;
  131. }
  132. });
  133. });
  134. vm.selectedTags = _.filter(vm.tags, {selected: true});
  135. var valuesNotInTag = _.filter(selected, function(test) {
  136. for (var i = 0; i < vm.selectedTags.length; i++) {
  137. var tag = vm.selectedTags[i];
  138. if (_.indexOf(tag.values, test.value) !== -1) {
  139. return false;
  140. }
  141. }
  142. return true;
  143. });
  144. vm.variable.current.value = _.pluck(selected, 'value');
  145. vm.variable.current.text = _.pluck(valuesNotInTag, 'text').join(', ');
  146. vm.selectedValuesCount = selected.length;
  147. // only single value
  148. if (vm.selectedValuesCount === 1) {
  149. vm.variable.current.value = selected[0].value;
  150. }
  151. if (commitChange) {
  152. vm.commitChanges();
  153. }
  154. };
  155. vm.commitChanges = function() {
  156. // make sure one option is selected
  157. var selected = _.filter(vm.options, {selected: true});
  158. if (selected.length === 0) {
  159. vm.options[0].selected = true;
  160. vm.selectionsChanged(false);
  161. }
  162. vm.dropdownVisible = false;
  163. vm.updateLinkText();
  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. });