mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-01 05:22:21 +02:00
Abstract various methods
This commit is contained in:
parent
920d41d0f5
commit
30c31a406c
|
@ -120,9 +120,9 @@ class Choices {
|
|||
this.presetItems = this.config.items;
|
||||
|
||||
// Then add any values passed from attribute
|
||||
if (this.passedElement.element.value) {
|
||||
if (this.passedElement.getValue()) {
|
||||
this.presetItems = this.presetItems.concat(
|
||||
this.passedElement.element.value.split(this.config.delimiter),
|
||||
this.passedElement.getValue().split(this.config.delimiter),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -218,15 +218,7 @@ class Choices {
|
|||
// Remove all event listeners
|
||||
this._removeEventListeners();
|
||||
this.passedElement.reveal();
|
||||
|
||||
// Move passed element back to original position
|
||||
this.containerOuter.element.parentNode.insertBefore(
|
||||
this.passedElement.element,
|
||||
this.containerOuter.element,
|
||||
);
|
||||
|
||||
// Remove added elements
|
||||
this.containerOuter.element.parentNode.removeChild(this.containerOuter.element);
|
||||
this.containerOuter.revert(this.passedElement.element);
|
||||
|
||||
// Clear data store
|
||||
this.clearStore();
|
||||
|
@ -1446,7 +1438,7 @@ class Choices {
|
|||
this.canSearch = false;
|
||||
if (
|
||||
this.config.removeItems &&
|
||||
!this.input.element.value &&
|
||||
!this.input.getValue() &&
|
||||
this.input.element === document.activeElement
|
||||
) {
|
||||
// Highlight items
|
||||
|
@ -1458,7 +1450,7 @@ class Choices {
|
|||
const onEnterKey = () => {
|
||||
// If enter key is pressed and the input has a value
|
||||
if (this.isTextElement && target.value) {
|
||||
const value = this.input.element.value;
|
||||
const value = this.input.getValue();
|
||||
const canAddItem = this._canAddItem(activeItems, value);
|
||||
|
||||
// All is good, add
|
||||
|
@ -1585,7 +1577,7 @@ class Choices {
|
|||
return;
|
||||
}
|
||||
|
||||
const value = this.input.element.value;
|
||||
const value = this.input.getValue();
|
||||
const activeItems = this.store.getItemsFilteredByActive();
|
||||
const canAddItem = this._canAddItem(activeItems, value);
|
||||
|
||||
|
@ -1620,7 +1612,7 @@ class Choices {
|
|||
);
|
||||
}
|
||||
} else if (this.canSearch && canAddItem.response) {
|
||||
this._handleSearch(this.input.element.value);
|
||||
this._handleSearch(this.input.getValue());
|
||||
}
|
||||
}
|
||||
// Re-establish canSearch value from changes in _onKeyDown
|
||||
|
|
|
@ -13,6 +13,10 @@ export default class Container {
|
|||
this.onBlur = this.onBlur.bind(this);
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add event listeners
|
||||
*/
|
||||
|
@ -155,6 +159,16 @@ export default class Container {
|
|||
this.isDisabled = true;
|
||||
}
|
||||
|
||||
revert(originalElement) {
|
||||
// Move passed element back to original position
|
||||
this.element.parentNode.insertBefore(
|
||||
originalElement,
|
||||
this.element,
|
||||
);
|
||||
// Remove container
|
||||
this.element.parentNode.removeChild(this.element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add loading state to element
|
||||
*/
|
||||
|
|
|
@ -8,6 +8,10 @@ export default class Dropdown {
|
|||
this.isActive = false;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine how far the top of our element is from
|
||||
* the top of the window
|
||||
|
|
|
@ -15,6 +15,10 @@ export default class Input {
|
|||
this.onBlur = this.onBlur.bind(this);
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
addEventListeners() {
|
||||
this.element.addEventListener('input', this.onInput);
|
||||
this.element.addEventListener('paste', this.onPaste);
|
||||
|
|
|
@ -8,6 +8,10 @@ export default class List {
|
|||
this.hasChildren = !!this.element.children;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear List contents
|
||||
*/
|
||||
|
|
|
@ -8,6 +8,14 @@ export default class WrappedElement {
|
|||
this.isDisabled = false;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return this.element.value;
|
||||
}
|
||||
|
||||
conceal() {
|
||||
// Hide passed input
|
||||
this.element.classList.add(
|
||||
|
|
|
@ -8,6 +8,10 @@ export default class WrappedInput extends WrappedElement {
|
|||
this.classNames = classNames;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
super.getElement();
|
||||
}
|
||||
|
||||
conceal() {
|
||||
super.conceal();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,10 @@ export default class WrappedSelect extends WrappedElement {
|
|||
this.classNames = classNames;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
super.getElement();
|
||||
}
|
||||
|
||||
conceal() {
|
||||
super.conceal();
|
||||
}
|
||||
|
@ -40,5 +44,4 @@ export default class WrappedSelect extends WrappedElement {
|
|||
getOptionGroups() {
|
||||
return Array.from(this.element.getElementsByTagName('OPTGROUP'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue