value_select_dropdown.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import coreModule from '../core_module';
  4. export class ValueSelectDropdownCtrl {
  5. dropdownVisible: any;
  6. highlightIndex: any;
  7. linkText: any;
  8. oldVariableText: any;
  9. options: any;
  10. search: any;
  11. selectedTags: any;
  12. selectedValues: any;
  13. tags: any;
  14. variable: any;
  15. hide: any;
  16. onUpdated: any;
  17. constructor(private $q) {}
  18. show() {
  19. this.oldVariableText = this.variable.current.text;
  20. this.highlightIndex = -1;
  21. this.options = this.variable.options;
  22. this.selectedValues = _.filter(this.options, { selected: true });
  23. this.tags = _.map(this.variable.tags, function(value) {
  24. let tag = { text: value, selected: false };
  25. _.each(this.variable.current.tags, function(tagObj) {
  26. if (tagObj.text === value) {
  27. tag = tagObj;
  28. }
  29. });
  30. return tag;
  31. });
  32. this.search = {
  33. query: '',
  34. options: this.options.slice(0, Math.min(this.options.length, 1000)),
  35. };
  36. this.dropdownVisible = true;
  37. }
  38. updateLinkText() {
  39. let current = this.variable.current;
  40. if (current.tags && current.tags.length) {
  41. // filer out values that are in selected tags
  42. let selectedAndNotInTag = _.filter(this.variable.options, function(option) {
  43. if (!option.selected) {
  44. return false;
  45. }
  46. for (let i = 0; i < current.tags.length; i++) {
  47. let tag = current.tags[i];
  48. if (_.indexOf(tag.values, option.value) !== -1) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. });
  54. // convert values to text
  55. let currentTexts = _.map(selectedAndNotInTag, 'text');
  56. // join texts
  57. this.linkText = currentTexts.join(' + ');
  58. if (this.linkText.length > 0) {
  59. this.linkText += ' + ';
  60. }
  61. } else {
  62. this.linkText = this.variable.current.text;
  63. }
  64. }
  65. clearSelections() {
  66. _.each(this.options, function(option) {
  67. option.selected = false;
  68. });
  69. this.selectionsChanged(false);
  70. }
  71. selectTag(tag) {
  72. tag.selected = !tag.selected;
  73. let tagValuesPromise;
  74. if (!tag.values) {
  75. tagValuesPromise = this.variable.getValuesForTag(tag.text);
  76. } else {
  77. tagValuesPromise = this.$q.when(tag.values);
  78. }
  79. tagValuesPromise.then(function(values) {
  80. tag.values = values;
  81. tag.valuesText = values.join(' + ');
  82. _.each(this.options, function(option) {
  83. if (_.indexOf(tag.values, option.value) !== -1) {
  84. option.selected = tag.selected;
  85. }
  86. });
  87. this.selectionsChanged(false);
  88. });
  89. }
  90. keyDown(evt) {
  91. if (evt.keyCode === 27) {
  92. this.hide();
  93. }
  94. if (evt.keyCode === 40) {
  95. this.moveHighlight(1);
  96. }
  97. if (evt.keyCode === 38) {
  98. this.moveHighlight(-1);
  99. }
  100. if (evt.keyCode === 13) {
  101. if (this.search.options.length === 0) {
  102. this.commitChanges();
  103. } else {
  104. this.selectValue(this.search.options[this.highlightIndex], {}, true, false);
  105. }
  106. }
  107. if (evt.keyCode === 32) {
  108. this.selectValue(this.search.options[this.highlightIndex], {}, false, false);
  109. }
  110. }
  111. moveHighlight(direction) {
  112. this.highlightIndex = (this.highlightIndex + direction) % this.search.options.length;
  113. }
  114. selectValue(option, event, commitChange, excludeOthers) {
  115. if (!option) {
  116. return;
  117. }
  118. option.selected = this.variable.multi ? !option.selected : true;
  119. commitChange = commitChange || false;
  120. excludeOthers = excludeOthers || false;
  121. let setAllExceptCurrentTo = function(newValue) {
  122. _.each(this.options, function(other) {
  123. if (option !== other) {
  124. other.selected = newValue;
  125. }
  126. });
  127. };
  128. // commit action (enter key), should not deselect it
  129. if (commitChange) {
  130. option.selected = true;
  131. }
  132. if (option.text === 'All' || excludeOthers) {
  133. setAllExceptCurrentTo(false);
  134. commitChange = true;
  135. } else if (!this.variable.multi) {
  136. setAllExceptCurrentTo(false);
  137. commitChange = true;
  138. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  139. commitChange = true;
  140. setAllExceptCurrentTo(false);
  141. }
  142. this.selectionsChanged(commitChange);
  143. }
  144. selectionsChanged(commitChange) {
  145. this.selectedValues = _.filter(this.options, { selected: true });
  146. if (this.selectedValues.length > 1) {
  147. if (this.selectedValues[0].text === 'All') {
  148. this.selectedValues[0].selected = false;
  149. this.selectedValues = this.selectedValues.slice(1, this.selectedValues.length);
  150. }
  151. }
  152. // validate selected tags
  153. _.each(this.tags, function(tag) {
  154. if (tag.selected) {
  155. _.each(tag.values, function(value) {
  156. if (!_.find(this.selectedValues, { value: value })) {
  157. tag.selected = false;
  158. }
  159. });
  160. }
  161. });
  162. this.selectedTags = _.filter(this.tags, { selected: true });
  163. this.variable.current.value = _.map(this.selectedValues, 'value');
  164. this.variable.current.text = _.map(this.selectedValues, 'text').join(' + ');
  165. this.variable.current.tags = this.selectedTags;
  166. if (!this.variable.multi) {
  167. this.variable.current.value = this.selectedValues[0].value;
  168. }
  169. if (commitChange) {
  170. this.commitChanges();
  171. }
  172. }
  173. commitChanges() {
  174. // if we have a search query and no options use that
  175. if (this.search.options.length === 0 && this.search.query.length > 0) {
  176. this.variable.current = { text: this.search.query, value: this.search.query };
  177. } else if (this.selectedValues.length === 0) {
  178. // make sure one option is selected
  179. this.options[0].selected = true;
  180. this.selectionsChanged(false);
  181. }
  182. this.dropdownVisible = false;
  183. this.updateLinkText();
  184. if (this.variable.current.text !== this.oldVariableText) {
  185. this.onUpdated();
  186. }
  187. }
  188. queryChanged() {
  189. this.highlightIndex = -1;
  190. this.search.options = _.filter(this.options, function(option) {
  191. return option.text.toLowerCase().indexOf(this.search.query.toLowerCase()) !== -1;
  192. });
  193. this.search.options = this.search.options.slice(0, Math.min(this.search.options.length, 1000));
  194. }
  195. init() {
  196. this.selectedTags = this.variable.current.tags || [];
  197. this.updateLinkText();
  198. }
  199. }
  200. export function valueSelectDropdown($compile, $window, $timeout, $rootScope) {
  201. return {
  202. scope: { variable: '=', onUpdated: '&' },
  203. templateUrl: 'public/app/partials/valueSelectDropdown.html',
  204. controller: 'ValueSelectDropdownCtrl',
  205. controllerAs: 'vm',
  206. bindToController: true,
  207. link: function(scope, elem) {
  208. let bodyEl = angular.element($window.document.body);
  209. let linkEl = elem.find('.variable-value-link');
  210. let inputEl = elem.find('input');
  211. function openDropdown() {
  212. inputEl.css('width', Math.max(linkEl.width(), 80) + 'px');
  213. inputEl.show();
  214. linkEl.hide();
  215. inputEl.focus();
  216. $timeout(
  217. function() {
  218. bodyEl.on('click', bodyOnClick);
  219. },
  220. 0,
  221. false
  222. );
  223. }
  224. function switchToLink() {
  225. inputEl.hide();
  226. linkEl.show();
  227. bodyEl.off('click', bodyOnClick);
  228. }
  229. function bodyOnClick(e) {
  230. if (elem.has(e.target).length === 0) {
  231. scope.$apply(function() {
  232. scope.vm.commitChanges();
  233. });
  234. }
  235. }
  236. scope.$watch('vm.dropdownVisible', function(newValue) {
  237. if (newValue) {
  238. openDropdown();
  239. } else {
  240. switchToLink();
  241. }
  242. });
  243. let cleanUp = $rootScope.$on('template-variable-value-updated', function() {
  244. scope.vm.updateLinkText();
  245. });
  246. scope.$on('$destroy', function() {
  247. cleanUp();
  248. });
  249. scope.vm.init();
  250. },
  251. };
  252. }
  253. coreModule.controller('ValueSelectDropdownCtrl', ValueSelectDropdownCtrl);
  254. coreModule.directive('valueSelectDropdown', valueSelectDropdown);