Better splitting up of functions

This commit is contained in:
Josh Johnson 2017-11-07 14:08:55 +00:00
parent 97e8c04536
commit b44c918022
5 changed files with 106 additions and 98 deletions

View file

@ -1,4 +1,4 @@
/*! choices.js v3.0.2 | (c) 2017 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v3.0.2 | (c) 2017 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
@ -2020,7 +2020,7 @@ var Choices = function () {
// Create required elements
this._createTemplates();
// Generate input markup
this._createInput();
this._createStructure();
// Subscribe store to render method
this.store.subscribe(this.render);
// Render any items
@ -4152,8 +4152,8 @@ var Choices = function () {
*/
}, {
key: '_createInput',
value: function _createInput() {
key: '_createStructure',
value: function _createStructure() {
var _this22 = this;
var direction = this.passedElement.element.getAttribute('dir') || 'ltr';
@ -4307,10 +4307,10 @@ module.exports = Choices;
/*!
* Fuse.js v3.2.0 - Lightweight fuzzy-search (http://fusejs.io)
*
*
* Copyright (c) 2012-2017 Kirollos Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
(function webpackUniversalModuleDefinition(root, factory) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -190,7 +190,7 @@ class Choices {
// Create required elements
this._createTemplates();
// Generate input markup
this._createInput();
this._createStructure();
// Subscribe store to render method
this.store.subscribe(this.render);
// Render any items
@ -2229,7 +2229,7 @@ class Choices {
* @return
* @private
*/
_createInput() {
_createStructure() {
const direction = this.passedElement.element.getAttribute('dir') || 'ltr';
const containerOuter = this._getTemplate('containerOuter',
direction,
@ -2284,115 +2284,123 @@ class Choices {
}
if (this.isSelectElement) {
const passedGroups = this.passedElement.getOptionGroups();
this._addPredefinedChoices();
} else if (this.isTextElement) {
this._addPredefinedItems();
}
}
this.highlightPosition = 0;
this.isSearching = false;
_addPredefinedChoices() {
const passedGroups = this.passedElement.getOptionGroups();
if (passedGroups && passedGroups.length) {
// If we have a placeholder option
const placeholderChoice = this.passedElement.getPlaceholderOption();
if (placeholderChoice && placeholderChoice.parentNode.tagName === 'SELECT') {
this._addChoice(
placeholderChoice.value,
placeholderChoice.innerHTML,
placeholderChoice.selected,
placeholderChoice.disabled,
undefined,
undefined,
this.highlightPosition = 0;
this.isSearching = false;
if (passedGroups && passedGroups.length) {
// If we have a placeholder option
const placeholderChoice = this.passedElement.getPlaceholderOption();
if (placeholderChoice && placeholderChoice.parentNode.tagName === 'SELECT') {
this._addChoice(
placeholderChoice.value,
placeholderChoice.innerHTML,
placeholderChoice.selected,
placeholderChoice.disabled,
undefined,
undefined,
/* placeholder */ true,
);
}
);
}
passedGroups.forEach((group) => {
this._addGroup(group, (group.id || null));
passedGroups.forEach((group) => {
this._addGroup(group, (group.id || null));
});
} else {
const passedOptions = this.passedElement.getOptions();
const filter = this.config.sortFilter;
const allChoices = this.presetChoices;
// Create array of options from option elements
passedOptions.forEach((o) => {
allChoices.push({
value: o.value,
label: o.innerHTML,
selected: o.selected,
disabled: o.disabled || o.parentNode.disabled,
placeholder: o.hasAttribute('placeholder'),
});
} else {
const passedOptions = this.passedElement.getOptions();
const filter = this.config.sortFilter;
const allChoices = this.presetChoices;
});
// Create array of options from option elements
passedOptions.forEach((o) => {
allChoices.push({
value: o.value,
label: o.innerHTML,
selected: o.selected,
disabled: o.disabled || o.parentNode.disabled,
placeholder: o.hasAttribute('placeholder'),
});
});
// If sorting is enabled or the user is searching, filter choices
if (this.config.shouldSort) {
allChoices.sort(filter);
}
// If sorting is enabled or the user is searching, filter choices
if (this.config.shouldSort) {
allChoices.sort(filter);
}
// Determine whether there is a selected choice
const hasSelectedChoice = allChoices.some(choice => choice.selected);
const handleChoice = (choice, index) => {
if (this.isSelectElement) {
// If the choice is actually a group
if (choice.choices) {
this._addGroup(choice, choice.id || null);
} else {
// If there is a selected choice already or the choice is not
// the first in the array, add each choice normally
// Otherwise pre-select the first choice in the array if it's a single select
const shouldPreselect = this.isSelectOneElement && !hasSelectedChoice && index === 0;
const isSelected = shouldPreselect ? true : choice.selected;
const isDisabled = shouldPreselect ? false : choice.disabled;
this._addChoice(
choice.value,
choice.label,
isSelected,
isDisabled,
undefined,
choice.customProperties,
choice.placeholder,
);
}
// Determine whether there is a selected choice
const hasSelectedChoice = allChoices.some(choice => choice.selected);
const handleChoice = (choice, index) => {
if (this.isSelectElement) {
// If the choice is actually a group
if (choice.choices) {
this._addGroup(choice, choice.id || null);
} else {
// If there is a selected choice already or the choice is not
// the first in the array, add each choice normally
// Otherwise pre-select the first choice in the array if it's a single select
const shouldPreselect = this.isSelectOneElement && !hasSelectedChoice && index === 0;
const isSelected = shouldPreselect ? true : choice.selected;
const isDisabled = shouldPreselect ? false : choice.disabled;
this._addChoice(
choice.value,
choice.label,
choice.selected,
choice.disabled,
isSelected,
isDisabled,
undefined,
choice.customProperties,
choice.placeholder,
);
}
};
// Add each choice
allChoices.forEach((choice, index) => handleChoice(choice, index));
}
} else if (this.isTextElement) {
const handlePresetItem = (item) => {
const itemType = getType(item);
if (itemType === 'Object') {
if (!item.value) {
return;
}
this._addItem(
item.value,
item.label,
item.id,
} else {
this._addChoice(
choice.value,
choice.label,
choice.selected,
choice.disabled,
undefined,
item.customProperties,
item.placeholder,
choice.customProperties,
choice.placeholder,
);
} else if (itemType === 'String') {
this._addItem(item);
}
};
this.presetItems.forEach(item => handlePresetItem(item));
// Add each choice
allChoices.forEach((choice, index) => handleChoice(choice, index));
}
}
_addPredefinedItems() {
const handlePresetItem = (item) => {
const itemType = getType(item);
if (itemType === 'Object') {
if (!item.value) {
return;
}
this._addItem(
item.value,
item.label,
item.id,
undefined,
item.customProperties,
item.placeholder,
);
} else if (itemType === 'String') {
this._addItem(item);
}
};
this.presetItems.forEach(item => handlePresetItem(item));
}
/* ===== End of Private functions ====== */
}

View file

@ -43,7 +43,7 @@ describe('choices', () => {
beforeEach(() => {
createTemplatesSpy = spy(instance, '_createTemplates');
createInputSpy = spy(instance, '_createInput');
createInputSpy = spy(instance, '_createStructure');
storeSubscribeSpy = spy(instance.store, 'subscribe');
renderSpy = spy(instance, 'render');
addEventListenersSpy = spy(instance, '_addEventListeners');
@ -1056,6 +1056,6 @@ describe('choices', () => {
describe('_addGroup', () => {});
describe('_getTemplate', () => {});
describe('_createTemplates', () => {});
describe('_createInput', () => {});
describe('_createStructure', () => {});
});
});