value_select_dropdown.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. if (commitChange) {
  154. vm.commitChanges();
  155. }
  156. };
  157. vm.commitChanges = function() {
  158. // if we have a search query and no options use that
  159. if (vm.search.options.length === 0 && vm.search.query.length > 0) {
  160. vm.variable.current = {text: vm.search.query, value: vm.search.query};
  161. }
  162. else if (vm.selectedValues.length === 0) {
  163. // make sure one option is selected
  164. vm.options[0].selected = true;
  165. vm.selectionsChanged(false);
  166. }
  167. vm.dropdownVisible = false;
  168. vm.updateLinkText();
  169. if (vm.variable.current.text !== vm.oldVariableText) {
  170. vm.onUpdated();
  171. }
  172. };
  173. vm.queryChanged = function() {
  174. vm.highlightIndex = -1;
  175. vm.search.options = _.filter(vm.options, function(option) {
  176. return option.text.toLowerCase().indexOf(vm.search.query.toLowerCase()) !== -1;
  177. });
  178. vm.search.options = vm.search.options.slice(0, Math.min(vm.search.options.length, 1000));
  179. };
  180. vm.init = function() {
  181. vm.selectedTags = vm.variable.current.tags || [];
  182. vm.updateLinkText();
  183. };
  184. });
  185. coreModule.default.directive('valueSelectDropdown', function($compile, $window, $timeout, $rootScope) {
  186. return {
  187. scope: { variable: "=", onUpdated: "&", getValuesForTag: "&" },
  188. templateUrl: 'public/app/partials/valueSelectDropdown.html',
  189. controller: 'ValueSelectDropdownCtrl',
  190. controllerAs: 'vm',
  191. bindToController: true,
  192. link: function(scope, elem) {
  193. var bodyEl = angular.element($window.document.body);
  194. var linkEl = elem.find('.variable-value-link');
  195. var inputEl = elem.find('input');
  196. function openDropdown() {
  197. inputEl.css('width', Math.max(linkEl.width(), 30) + 'px');
  198. inputEl.show();
  199. linkEl.hide();
  200. inputEl.focus();
  201. $timeout(function() { bodyEl.on('click', bodyOnClick); }, 0, false);
  202. }
  203. function switchToLink() {
  204. inputEl.hide();
  205. linkEl.show();
  206. bodyEl.off('click', bodyOnClick);
  207. }
  208. function bodyOnClick (e) {
  209. if (elem.has(e.target).length === 0) {
  210. scope.$apply(function() {
  211. scope.vm.commitChanges();
  212. });
  213. }
  214. }
  215. scope.$watch('vm.dropdownVisible', function(newValue) {
  216. if (newValue) {
  217. openDropdown();
  218. } else {
  219. switchToLink();
  220. }
  221. });
  222. var cleanUp = $rootScope.$on('template-variable-value-updated', function() {
  223. scope.vm.updateLinkText();
  224. });
  225. scope.$on("$destroy", function() {
  226. cleanUp();
  227. });
  228. scope.vm.init();
  229. },
  230. };
  231. });
  232. });