value_select_dropdown.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. ],
  6. function (angular, _, coreModule) {
  7. 'use strict';
  8. coreModule.default.controller('ValueSelectDropdownCtrl', function($q) {
  9. var vm = this;
  10. vm.show = function() {
  11. vm.oldVariableText = vm.variable.current.text;
  12. vm.highlightIndex = -1;
  13. vm.options = vm.variable.options;
  14. vm.selectedValues = _.filter(vm.options, {selected: true});
  15. vm.tags = _.map(vm.variable.tags, function(value) {
  16. var tag = { text: value, selected: false };
  17. _.each(vm.variable.current.tags, function(tagObj) {
  18. if (tagObj.text === value) {
  19. tag = tagObj;
  20. }
  21. });
  22. return tag;
  23. });
  24. vm.search = {
  25. query: '',
  26. options: vm.options.slice(0, Math.min(vm.options.length, 1000))
  27. };
  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. if (vm.search.options.length === 0) {
  92. vm.commitChanges();
  93. } else {
  94. vm.selectValue(vm.search.options[vm.highlightIndex], {}, true, false);
  95. }
  96. }
  97. if (evt.keyCode === 32) {
  98. vm.selectValue(vm.search.options[vm.highlightIndex], {}, false, false);
  99. }
  100. };
  101. vm.moveHighlight = function(direction) {
  102. vm.highlightIndex = (vm.highlightIndex + direction) % vm.search.options.length;
  103. };
  104. vm.selectValue = function(option, event, commitChange, excludeOthers) {
  105. if (!option) { return; }
  106. option.selected = !option.selected;
  107. commitChange = commitChange || false;
  108. excludeOthers = excludeOthers || false;
  109. var setAllExceptCurrentTo = function(newValue) {
  110. _.each(vm.options, function(other) {
  111. if (option !== other) { other.selected = newValue; }
  112. });
  113. };
  114. // commit action (enter key), should not deselect it
  115. if (commitChange) {
  116. option.selected = true;
  117. }
  118. if (option.text === 'All' || excludeOthers) {
  119. setAllExceptCurrentTo(false);
  120. commitChange = true;
  121. }
  122. else if (!vm.variable.multi) {
  123. setAllExceptCurrentTo(false);
  124. commitChange = true;
  125. } else if (event.ctrlKey || event.metaKey || event.shiftKey) {
  126. commitChange = true;
  127. setAllExceptCurrentTo(false);
  128. }
  129. vm.selectionsChanged(commitChange);
  130. };
  131. vm.selectionsChanged = function(commitChange) {
  132. vm.selectedValues = _.filter(vm.options, {selected: true});
  133. if (vm.selectedValues.length > 1) {
  134. if (vm.selectedValues[0].text === 'All') {
  135. vm.selectedValues[0].selected = false;
  136. vm.selectedValues = vm.selectedValues.slice(1, vm.selectedValues.length);
  137. }
  138. }
  139. // validate selected tags
  140. _.each(vm.tags, function(tag) {
  141. if (tag.selected) {
  142. _.each(tag.values, function(value) {
  143. if (!_.findWhere(vm.selectedValues, {value: value})) {
  144. tag.selected = false;
  145. }
  146. });
  147. }
  148. });
  149. vm.selectedTags = _.filter(vm.tags, {selected: true});
  150. vm.variable.current.value = _.pluck(vm.selectedValues, 'value');
  151. vm.variable.current.text = _.pluck(vm.selectedValues, 'text').join(' + ');
  152. vm.variable.current.tags = vm.selectedTags;
  153. // only single value
  154. if (vm.selectedValues.length === 1) {
  155. vm.variable.current.value = vm.selectedValues[0].value;
  156. }
  157. if (commitChange) {
  158. vm.commitChanges();
  159. }
  160. };
  161. vm.commitChanges = function() {
  162. // if we have a search query and no options use that
  163. if (vm.search.options.length === 0 && vm.search.query.length > 0) {
  164. vm.variable.current = {text: vm.search.query, value: vm.search.query};
  165. }
  166. else if (vm.selectedValues.length === 0) {
  167. // make sure one option is selected
  168. vm.options[0].selected = true;
  169. vm.selectionsChanged(false);
  170. }
  171. vm.dropdownVisible = false;
  172. vm.updateLinkText();
  173. if (vm.variable.current.text !== vm.oldVariableText) {
  174. vm.onUpdated();
  175. }
  176. };
  177. vm.queryChanged = function() {
  178. vm.highlightIndex = -1;
  179. vm.search.options = _.filter(vm.options, function(option) {
  180. return option.text.toLowerCase().indexOf(vm.search.query.toLowerCase()) !== -1;
  181. });
  182. vm.search.options = vm.search.options.slice(0, Math.min(vm.search.options.length, 1000));
  183. };
  184. vm.init = function() {
  185. vm.selectedTags = vm.variable.current.tags || [];
  186. vm.updateLinkText();
  187. };
  188. });
  189. coreModule.default.directive('valueSelectDropdown', function($compile, $window, $timeout, $rootScope) {
  190. return {
  191. scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" },
  192. templateUrl: 'public/app/partials/valueSelectDropdown.html',
  193. controller: 'ValueSelectDropdownCtrl',
  194. controllerAs: 'vm',
  195. bindToController: true,
  196. link: function(scope, elem) {
  197. var bodyEl = angular.element($window.document.body);
  198. var linkEl = elem.find('.variable-value-link');
  199. var inputEl = elem.find('input');
  200. function openDropdown() {
  201. inputEl.css('width', Math.max(linkEl.width(), 30) + 'px');
  202. inputEl.show();
  203. linkEl.hide();
  204. inputEl.focus();
  205. $timeout(function() { bodyEl.on('click', bodyOnClick); }, 0, false);
  206. }
  207. function switchToLink() {
  208. inputEl.hide();
  209. linkEl.show();
  210. bodyEl.off('click', bodyOnClick);
  211. }
  212. function bodyOnClick (e) {
  213. if (elem.has(e.target).length === 0) {
  214. scope.$apply(function() {
  215. scope.vm.commitChanges();
  216. });
  217. }
  218. }
  219. scope.$watch('vm.dropdownVisible', function(newValue) {
  220. if (newValue) {
  221. openDropdown();
  222. } else {
  223. switchToLink();
  224. }
  225. });
  226. var cleanUp = $rootScope.$on('template-variable-value-updated', function() {
  227. scope.vm.updateLinkText();
  228. });
  229. scope.$on("$destroy", function() {
  230. cleanUp();
  231. });
  232. scope.vm.init();
  233. },
  234. };
  235. });
  236. });