Early returns

This commit is contained in:
Josh Johnson 2017-08-15 16:57:29 +01:00
parent 6261962caf
commit 364aca20a6

View file

@ -293,6 +293,7 @@ class Choices {
this.config.classNames.hiddenState, this.config.classNames.hiddenState,
); );
this.passedElement.removeAttribute('tabindex'); this.passedElement.removeAttribute('tabindex');
// Recover original styles if any // Recover original styles if any
const origStyle = this.passedElement.getAttribute('data-choice-orig-style'); const origStyle = this.passedElement.getAttribute('data-choice-orig-style');
if (origStyle) { if (origStyle) {
@ -657,10 +658,7 @@ class Choices {
*/ */
highlightAll() { highlightAll() {
const items = this.store.getItems(); const items = this.store.getItems();
items.forEach((item) => { items.forEach(item => this.highlightItem(item));
this.highlightItem(item);
});
return this; return this;
} }
@ -671,10 +669,7 @@ class Choices {
*/ */
unhighlightAll() { unhighlightAll() {
const items = this.store.getItems(); const items = this.store.getItems();
items.forEach((item) => { items.forEach(item => this.unhighlightItem(item));
this.unhighlightItem(item);
});
return this; return this;
} }
@ -867,60 +862,61 @@ class Choices {
* @public * @public
*/ */
setValue(args) { setValue(args) {
if (this.initialised === true) { if (!this.initialised) {
// Convert args to an iterable array return this;
const values = [...args];
const handleValue = (item) => {
const itemType = getType(item);
if (itemType === 'Object') {
if (!item.value) {
return;
}
// If we are dealing with a select input, we need to create an option first
// that is then selected. For text inputs we can just add items normally.
if (!this.isTextElement) {
this._addChoice(
item.value,
item.label,
true,
false, -1,
item.customProperties,
item.placeholder,
);
} else {
this._addItem(
item.value,
item.label,
item.id,
undefined,
item.customProperties,
item.placeholder,
);
}
} else if (itemType === 'String') {
if (!this.isTextElement) {
this._addChoice(
item,
item,
true,
false, -1,
null,
);
} else {
this._addItem(item);
}
}
};
if (values.length > 1) {
values.forEach((value) => {
handleValue(value);
});
} else {
handleValue(values[0]);
}
} }
// Convert args to an iterable array
const values = [...args];
const handleValue = (item) => {
const itemType = getType(item);
if (itemType === 'Object') {
if (!item.value) {
return;
}
// If we are dealing with a select input, we need to create an option first
// that is then selected. For text inputs we can just add items normally.
if (!this.isTextElement) {
this._addChoice(
item.value,
item.label,
true,
false, -1,
item.customProperties,
item.placeholder,
);
} else {
this._addItem(
item.value,
item.label,
item.id,
undefined,
item.customProperties,
item.placeholder,
);
}
} else if (itemType === 'String') {
if (!this.isTextElement) {
this._addChoice(
item,
item,
true,
false, -1,
null,
);
} else {
this._addItem(item);
}
}
};
if (values.length > 1) {
values.forEach(value => handleValue(value));
} else {
handleValue(values[0]);
}
return this; return this;
} }
@ -931,35 +927,37 @@ class Choices {
* @public * @public
*/ */
setValueByChoice(value) { setValueByChoice(value) {
if (!this.isTextElement) { if (this.isTextElement) {
const choices = this.store.getChoices(); return this;
// If only one value has been passed, convert to array
const choiceValue = isType('Array', value) ? value : [value];
// Loop through each value and
choiceValue.forEach((val) => {
// Check 'value' property exists and the choice isn't already selected
const foundChoice = choices.find(choice => choice.value === val);
if (foundChoice) {
if (!foundChoice.selected) {
this._addItem(
foundChoice.value,
foundChoice.label,
foundChoice.id,
foundChoice.groupId,
foundChoice.customProperties,
foundChoice.placeholder,
foundChoice.keyCode,
);
} else if (!this.config.silent) {
console.warn('Attempting to select choice already selected');
}
} else if (!this.config.silent) {
console.warn('Attempting to select choice that does not exist');
}
});
} }
const choices = this.store.getChoices();
// If only one value has been passed, convert to array
const choiceValue = isType('Array', value) ? value : [value];
// Loop through each value and
choiceValue.forEach((val) => {
// Check 'value' property exists and the choice isn't already selected
const foundChoice = choices.find(choice => choice.value === val);
if (foundChoice) {
if (!foundChoice.selected) {
this._addItem(
foundChoice.value,
foundChoice.label,
foundChoice.id,
foundChoice.groupId,
foundChoice.customProperties,
foundChoice.placeholder,
foundChoice.keyCode,
);
} else if (!this.config.silent) {
console.warn('Attempting to select choice already selected');
}
} else if (!this.config.silent) {
console.warn('Attempting to select choice that does not exist');
}
});
return this; return this;
} }
@ -973,41 +971,45 @@ class Choices {
* @public * @public
*/ */
setChoices(choices, value, label, replaceChoices = false) { setChoices(choices, value, label, replaceChoices = false) {
if (this.initialised === true) { if (
if (this.isSelectElement) { !this.initialised ||
if (!isType('Array', choices) || !value) { !this.isSelectElement ||
return this; !isType('Array', choices) ||
} !value
// Clear choices if needed ) {
if (replaceChoices) { return this;
this._clearChoices();
}
// Add choices if passed
if (choices && choices.length) {
this.containerOuter.classList.remove(this.config.classNames.loadingState);
choices.forEach((result) => {
if (result.choices) {
this._addGroup(
result,
(result.id || null),
value,
label,
);
} else {
this._addChoice(
result[value],
result[label],
result.selected,
result.disabled,
undefined,
result.customProperties,
result.placeholder,
);
}
});
}
}
} }
// Clear choices if needed
if (replaceChoices) {
this._clearChoices();
}
// Add choices if passed
if (choices && choices.length) {
this.containerOuter.classList.remove(this.config.classNames.loadingState);
choices.forEach((result) => {
if (result.choices) {
this._addGroup(
result,
(result.id || null),
value,
label,
);
} else {
this._addChoice(
result[value],
result[label],
result.selected,
result.disabled,
undefined,
result.customProperties,
result.placeholder,
);
}
});
}
return this; return this;
} }
@ -1033,15 +1035,18 @@ class Choices {
if (this.input.value) { if (this.input.value) {
this.input.value = ''; this.input.value = '';
} }
if (!this.isSelectOneElement) { if (!this.isSelectOneElement) {
this._setInputWidth(); this._setInputWidth();
} }
if (!this.isTextElement && this.config.searchEnabled) { if (!this.isTextElement && this.config.searchEnabled) {
this.isSearching = false; this.isSearching = false;
this.store.dispatch( this.store.dispatch(
activateChoices(true), activateChoices(true),
); );
} }
return this; return this;
} }
@ -1050,22 +1055,26 @@ class Choices {
* @return {Object} Class instance * @return {Object} Class instance
*/ */
enable() { enable() {
if (this.initialised) { if (!this.initialised) {
this.passedElement.disabled = false; return this;
const isDisabled = this.containerOuter.classList.contains( }
this.config.classNames.disabledState,
); this.passedElement.disabled = false;
if (isDisabled) { const isDisabled = this.containerOuter.classList.contains(
this._addEventListeners(); this.config.classNames.disabledState,
this.passedElement.removeAttribute('disabled'); );
this.input.removeAttribute('disabled');
this.containerOuter.classList.remove(this.config.classNames.disabledState); if (isDisabled) {
this.containerOuter.removeAttribute('aria-disabled'); this._addEventListeners();
if (this.isSelectOneElement) { this.passedElement.removeAttribute('disabled');
this.containerOuter.setAttribute('tabindex', '0'); this.input.removeAttribute('disabled');
} this.containerOuter.classList.remove(this.config.classNames.disabledState);
this.containerOuter.removeAttribute('aria-disabled');
if (this.isSelectOneElement) {
this.containerOuter.setAttribute('tabindex', '0');
} }
} }
return this; return this;
} }
@ -1075,22 +1084,26 @@ class Choices {
* @public * @public
*/ */
disable() { disable() {
if (this.initialised) { if (!this.initialised) {
this.passedElement.disabled = true; return this;
const isEnabled = !this.containerOuter.classList.contains( }
this.config.classNames.disabledState,
); this.passedElement.disabled = true;
if (isEnabled) { const isEnabled = !this.containerOuter.classList.contains(
this._removeEventListeners(); this.config.classNames.disabledState,
this.passedElement.setAttribute('disabled', ''); );
this.input.setAttribute('disabled', '');
this.containerOuter.classList.add(this.config.classNames.disabledState); if (isEnabled) {
this.containerOuter.setAttribute('aria-disabled', 'true'); this._removeEventListeners();
if (this.isSelectOneElement) { this.passedElement.setAttribute('disabled', '');
this.containerOuter.setAttribute('tabindex', '-1'); this.input.setAttribute('disabled', '');
} this.containerOuter.classList.add(this.config.classNames.disabledState);
this.containerOuter.setAttribute('aria-disabled', 'true');
if (this.isSelectOneElement) {
this.containerOuter.setAttribute('tabindex', '-1');
} }
} }
return this; return this;
} }
@ -1101,16 +1114,15 @@ class Choices {
* @public * @public
*/ */
ajax(fn) { ajax(fn) {
if (this.initialised === true) { if (!this.initialised || !this.isSelectElement) {
if (this.isSelectElement) { return this;
// Show loading text
requestAnimationFrame(() => {
this._handleLoadingState(true);
});
// Run callback
fn(this._ajaxCallback());
}
} }
// Show loading text
requestAnimationFrame(() => this._handleLoadingState(true));
// Run callback
fn(this._ajaxCallback());
return this; return this;
} }