variableValueSelect.js 7.4 KB

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