Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VCombobox.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Styles
  2. import '../../../src/stylus/components/_autocompletes.styl';
  3. // Extensions
  4. import VSelect from '../VSelect/VSelect';
  5. import VAutocomplete from '../VAutocomplete/VAutocomplete';
  6. // Utils
  7. import { keyCodes } from '../../util/helpers';
  8. /* @vue/component */
  9. export default {
  10. name: 'v-combobox',
  11. extends: VAutocomplete,
  12. props: {
  13. delimiters: {
  14. type: Array,
  15. default: function _default() {
  16. return [];
  17. }
  18. },
  19. returnObject: {
  20. type: Boolean,
  21. default: true
  22. }
  23. },
  24. data: function data() {
  25. return {
  26. editingIndex: -1
  27. };
  28. },
  29. computed: {
  30. counterValue: function counterValue() {
  31. return this.multiple ? this.selectedItems.length : (this.internalSearch || '').toString().length;
  32. },
  33. hasSlot: function hasSlot() {
  34. return VSelect.options.computed.hasSlot.call(this) || this.multiple;
  35. },
  36. isAnyValueAllowed: function isAnyValueAllowed() {
  37. return true;
  38. },
  39. menuCanShow: function menuCanShow() {
  40. if (!this.isFocused) return false;
  41. return this.hasDisplayedItems || !!this.$slots['no-data'] && !this.hideNoData;
  42. }
  43. },
  44. methods: {
  45. onFilteredItemsChanged: function onFilteredItemsChanged() {
  46. // nop
  47. },
  48. onInternalSearchChanged: function onInternalSearchChanged(val) {
  49. if (val && this.multiple && this.delimiters.length) {
  50. var delimiter = this.delimiters.find(function (d) {
  51. return val.endsWith(d);
  52. });
  53. if (delimiter != null) {
  54. this.internalSearch = val.slice(0, val.length - delimiter.length);
  55. this.updateTags();
  56. }
  57. }
  58. this.updateMenuDimensions();
  59. },
  60. genChipSelection: function genChipSelection(item, index) {
  61. var _this = this;
  62. var chip = VSelect.options.methods.genChipSelection.call(this, item, index);
  63. // Allow user to update an existing value
  64. if (this.multiple) {
  65. chip.componentOptions.listeners.dblclick = function () {
  66. _this.editingIndex = index;
  67. _this.internalSearch = _this.getText(item);
  68. _this.selectedIndex = -1;
  69. };
  70. }
  71. return chip;
  72. },
  73. onChipInput: function onChipInput(item) {
  74. VSelect.options.methods.onChipInput.call(this, item);
  75. this.editingIndex = -1;
  76. },
  77. // Requires a manual definition
  78. // to overwrite removal in v-autocomplete
  79. onEnterDown: function onEnterDown(e) {
  80. e.preventDefault();
  81. VSelect.options.methods.onEnterDown.call(this);
  82. // If has menu index, let v-select-list handle
  83. if (this.getMenuIndex() > -1) return;
  84. this.updateSelf();
  85. },
  86. onKeyDown: function onKeyDown(e) {
  87. var keyCode = e.keyCode;
  88. VSelect.options.methods.onKeyDown.call(this, e);
  89. // If user is at selection index of 0
  90. // create a new tag
  91. if (this.multiple && keyCode === keyCodes.left && this.$refs.input.selectionStart === 0) {
  92. this.updateSelf();
  93. }
  94. // The ordering is important here
  95. // allows new value to be updated
  96. // and then moves the index to the
  97. // proper location
  98. this.changeSelectedIndex(keyCode);
  99. },
  100. onTabDown: function onTabDown(e) {
  101. // When adding tags, if searching and
  102. // there is not a filtered options,
  103. // add the value to the tags list
  104. if (this.multiple && this.internalSearch && this.getMenuIndex() === -1) {
  105. e.preventDefault();
  106. e.stopPropagation();
  107. return this.updateTags();
  108. }
  109. VAutocomplete.options.methods.onTabDown.call(this, e);
  110. },
  111. selectItem: function selectItem(item) {
  112. // Currently only supports items:<string[]>
  113. if (this.editingIndex > -1) {
  114. this.updateEditing();
  115. } else {
  116. VSelect.options.methods.selectItem.call(this, item);
  117. }
  118. },
  119. setSelectedItems: function setSelectedItems() {
  120. if (this.internalValue == null || this.internalValue === '') {
  121. this.selectedItems = [];
  122. } else {
  123. this.selectedItems = this.multiple ? this.internalValue : [this.internalValue];
  124. }
  125. },
  126. setValue: function setValue() {
  127. var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.internalSearch;
  128. VSelect.options.methods.setValue.call(this, value);
  129. },
  130. updateEditing: function updateEditing() {
  131. var value = this.internalValue.slice();
  132. value[this.editingIndex] = this.internalSearch;
  133. this.setValue(value);
  134. this.editingIndex = -1;
  135. },
  136. updateCombobox: function updateCombobox() {
  137. var isUsingSlot = Boolean(this.$scopedSlots.selection) || this.hasChips;
  138. // If search is not dirty and is
  139. // using slot, do nothing
  140. if (isUsingSlot && !this.searchIsDirty) return;
  141. // The internal search is not matching
  142. // the internal value, update the input
  143. if (this.internalSearch !== this.getText(this.internalValue)) this.setValue();
  144. // Reset search if using slot
  145. // to avoid a double input
  146. if (isUsingSlot) this.internalSearch = undefined;
  147. },
  148. updateSelf: function updateSelf() {
  149. this.multiple ? this.updateTags() : this.updateCombobox();
  150. },
  151. updateTags: function updateTags() {
  152. var menuIndex = this.getMenuIndex();
  153. // If the user is not searching
  154. // and no menu item is selected
  155. // do nothing
  156. if (menuIndex < 0 && !this.searchIsDirty) return;
  157. if (this.editingIndex > -1) {
  158. return this.updateEditing();
  159. }
  160. var index = this.selectedItems.indexOf(this.internalSearch);
  161. // If it already exists, do nothing
  162. // this might need to change to bring
  163. // the duplicated item to the last entered
  164. if (index > -1) {
  165. var internalValue = this.internalValue.slice();
  166. internalValue.splice(index, 1);
  167. this.setValue(internalValue);
  168. }
  169. // If menu index is greater than 1
  170. // the selection is handled elsewhere
  171. // TODO: find out where
  172. if (menuIndex > -1) return this.internalSearch = null;
  173. this.selectItem(this.internalSearch);
  174. this.internalSearch = null;
  175. }
  176. }
  177. };
  178. //# sourceMappingURL=VCombobox.js.map