Ensure this is returned when checking for init

This commit is contained in:
Josh Johnson 2016-09-04 12:25:43 +01:00
parent d60bf1c86c
commit 50396b4374

View file

@ -42,6 +42,12 @@ export default class Choices {
}
}
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
this.passedElement = isType('String', element) ? document.querySelector(element) : element;
// If element has already been initalised with Choices, return it silently
if (this.passedElement.getAttribute('data-choice') === 'active') return;
const defaultConfig = {
items: [],
choices: [],
@ -126,9 +132,6 @@ export default class Choices {
// Assign preset items from passed object first
this.presetItems = this.config.items;
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
this.passedElement = isType('String', element) ? document.querySelector(element) : element;
// Then add any values passed from attribute
if (this.passedElement.value) {
this.presetItems = this.presetItems.concat(this.passedElement.value.split(this.config.delimiter));
@ -162,8 +165,6 @@ export default class Choices {
const isValidType = ['select-one', 'select-multiple', 'text'].some(type => type === this.passedElement.type);
if (isValidElement && isValidType) {
// If element has already been initalised with Choices, return it silently
if (this.passedElement.getAttribute('data-choice') === 'active') return;
// Let's go
this.init();
} else {
@ -173,12 +174,11 @@ export default class Choices {
/**
* Initialise Choices
* @return
* @return {Object} Class instance
* @public
*/
init(callback = this.config.callbackOnInit) {
if (this.initialised === true) return;
if (this.initialised === false) {
// Set initialise flag
this.initialised = true;
@ -205,15 +205,16 @@ export default class Choices {
}
}
}
return this;
}
/**
* Destroy Choices and nullify values
* @return
* @return {Object} Class instance
* @public
*/
destroy() {
if (this.initialised !== true) return;
if (this.initialised === true) {
this._removeEventListeners();
this.passedElement.classList.remove(this.config.classNames.input, this.config.classNames.hiddenState);
@ -228,6 +229,8 @@ export default class Choices {
this.config = null;
this.store = null;
}
return this;
}
/**
* Select item (a selected item can be deleted)
@ -473,8 +476,7 @@ export default class Choices {
* @public
*/
setValue(args) {
if (this.initialised !== true) return;
if (this.initialised === true) {
// Convert args to an itterable array
const values = [...args];
@ -496,7 +498,7 @@ export default class Choices {
}
}
});
}
return this;
}
@ -542,8 +544,7 @@ export default class Choices {
* @public
*/
setChoices(choices, value, label) {
if (this.initialised !== true) return;
if (this.initialised === true) {
if (this.passedElement.type === 'select-one' || this.passedElement.type === 'select-multiple') {
if (!isType('Array', choices) || !value) return;
@ -558,6 +559,7 @@ export default class Choices {
});
}
}
}
return this;
}
@ -592,7 +594,7 @@ export default class Choices {
*/
disable() {
this.passedElement.disabled = true;
if (this.initialised) {
if (this.initialised === true) {
if (!this.containerOuter.classList.contains(this.config.classNames.disabledState)) {
this._removeEventListeners();
this.passedElement.setAttribute('disabled', '');
@ -610,7 +612,7 @@ export default class Choices {
*/
enable() {
this.passedElement.disabled = false;
if (this.initialised) {
if (this.initialised === true) {
if (this.containerOuter.classList.contains(this.config.classNames.disabledState)) {
this._addEventListeners();
this.passedElement.removeAttribute('disabled');
@ -629,8 +631,7 @@ export default class Choices {
* @public
*/
ajax(fn) {
if (this.initialised !== true) return;
if (this.initialised === true) {
if (this.passedElement.type === 'select-one' || this.passedElement.type === 'select-multiple') {
this.containerOuter.classList.add(this.config.classNames.loadingState);
this.containerOuter.setAttribute('aria-busy', 'true');
@ -661,9 +662,9 @@ export default class Choices {
}
this.containerOuter.removeAttribute('aria-busy');
};
fn(callback);
}
}
return this;
}