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.passedElement.removeAttribute('tabindex');
// Recover original styles if any
const origStyle = this.passedElement.getAttribute('data-choice-orig-style');
if (origStyle) {
@ -657,10 +658,7 @@ class Choices {
*/
highlightAll() {
const items = this.store.getItems();
items.forEach((item) => {
this.highlightItem(item);
});
items.forEach(item => this.highlightItem(item));
return this;
}
@ -671,10 +669,7 @@ class Choices {
*/
unhighlightAll() {
const items = this.store.getItems();
items.forEach((item) => {
this.unhighlightItem(item);
});
items.forEach(item => this.unhighlightItem(item));
return this;
}
@ -867,7 +862,10 @@ class Choices {
* @public
*/
setValue(args) {
if (this.initialised === true) {
if (!this.initialised) {
return this;
}
// Convert args to an iterable array
const values = [...args];
const handleValue = (item) => {
@ -914,13 +912,11 @@ class Choices {
};
if (values.length > 1) {
values.forEach((value) => {
handleValue(value);
});
values.forEach(value => handleValue(value));
} else {
handleValue(values[0]);
}
}
return this;
}
@ -931,7 +927,10 @@ class Choices {
* @public
*/
setValueByChoice(value) {
if (!this.isTextElement) {
if (this.isTextElement) {
return this;
}
const choices = this.store.getChoices();
// If only one value has been passed, convert to array
const choiceValue = isType('Array', value) ? value : [value];
@ -959,7 +958,6 @@ class Choices {
console.warn('Attempting to select choice that does not exist');
}
});
}
return this;
}
@ -973,15 +971,20 @@ class Choices {
* @public
*/
setChoices(choices, value, label, replaceChoices = false) {
if (this.initialised === true) {
if (this.isSelectElement) {
if (!isType('Array', choices) || !value) {
if (
!this.initialised ||
!this.isSelectElement ||
!isType('Array', choices) ||
!value
) {
return this;
}
// Clear choices if needed
if (replaceChoices) {
this._clearChoices();
}
// Add choices if passed
if (choices && choices.length) {
this.containerOuter.classList.remove(this.config.classNames.loadingState);
@ -1006,8 +1009,7 @@ class Choices {
}
});
}
}
}
return this;
}
@ -1033,15 +1035,18 @@ class Choices {
if (this.input.value) {
this.input.value = '';
}
if (!this.isSelectOneElement) {
this._setInputWidth();
}
if (!this.isTextElement && this.config.searchEnabled) {
this.isSearching = false;
this.store.dispatch(
activateChoices(true),
);
}
return this;
}
@ -1050,11 +1055,15 @@ class Choices {
* @return {Object} Class instance
*/
enable() {
if (this.initialised) {
if (!this.initialised) {
return this;
}
this.passedElement.disabled = false;
const isDisabled = this.containerOuter.classList.contains(
this.config.classNames.disabledState,
);
if (isDisabled) {
this._addEventListeners();
this.passedElement.removeAttribute('disabled');
@ -1065,7 +1074,7 @@ class Choices {
this.containerOuter.setAttribute('tabindex', '0');
}
}
}
return this;
}
@ -1075,11 +1084,15 @@ class Choices {
* @public
*/
disable() {
if (this.initialised) {
if (!this.initialised) {
return this;
}
this.passedElement.disabled = true;
const isEnabled = !this.containerOuter.classList.contains(
this.config.classNames.disabledState,
);
if (isEnabled) {
this._removeEventListeners();
this.passedElement.setAttribute('disabled', '');
@ -1090,7 +1103,7 @@ class Choices {
this.containerOuter.setAttribute('tabindex', '-1');
}
}
}
return this;
}
@ -1101,16 +1114,15 @@ class Choices {
* @public
*/
ajax(fn) {
if (this.initialised === true) {
if (this.isSelectElement) {
if (!this.initialised || !this.isSelectElement) {
return this;
}
// Show loading text
requestAnimationFrame(() => {
this._handleLoadingState(true);
});
requestAnimationFrame(() => this._handleLoadingState(true));
// Run callback
fn(this._ajaxCallback());
}
}
return this;
}