Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
grafluxe committed Jan 2, 2018
1 parent 0101754 commit 4cf3a89
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 14 deletions.
215 changes: 203 additions & 12 deletions dist/Dropdownizer.js
Expand Up @@ -41,20 +41,21 @@ var Dropdownizer = function () {
}

/**
* Programmatically select list items.
* Programmatically selects list items.
* @throws {Error} Throws if your search returns multiple matches.
* @throws {RangeError} Throws if the index is out of bounds.
* @param {Number|String} at The list items index or name. Note that if using a
* string, letter case is ignored
* @param {Number|String} at The list items index or name. Use a negative number to select
* from the end of the list. Note that if using a string, letter case
* is ignored.
* @returns {Dropdownizer} The Dropdownizer instance.
*/


_createClass(Dropdownizer, [{
key: "selectItem",
value: function selectItem(index) {
value: function selectItem(at) {
this._dropdowns.forEach(function (dropdown) {
return dropdown.selectItem(index);
return dropdown.selectItem(at);
});
return this;
}
Expand Down Expand Up @@ -124,6 +125,49 @@ var Dropdownizer = function () {
return this;
}

/**
* Deletes list items. Note that this method properly syncs your original select elements.
* @throws {Error} Throws if your search returns multiple matches.
* @throws {RangeError} Throws if the index is out of bounds.
* @param {Number|String} at The list items index or name. Use a negative number to select
* from the end of the list. Note that if using a string, letter case
* is ignored.
* @returns {Dropdownizer} The Dropdownizer instance.
*/

}, {
key: "removeItem",
value: function removeItem(at) {
this._dropdowns.forEach(function (dropdown) {
return dropdown.removeItem(at);
});
return this;
}

/**
* Adds list items. Note that this method properly syncs your original select elements.
* @throws {RangeError} Throws if the index is out of bounds.
* @param {String} value The items value.
* @param {Object=} attributes={} Attributes to add to the list item. The supported
* properties are 'label', 'disabled', and 'selected'.
* @param {Number=} at=NaN The index in which to insert your new list item
* (defaults to the last item if not set). Use a
* negative number to insert from the end of the list.
* @returns {Dropdownizer} The Dropdownizer instance.
*/

}, {
key: "addItem",
value: function addItem(value) {
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var at = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN;

this._dropdowns.forEach(function (dropdown) {
return dropdown.addItem(value, attributes, at);
});
return this;
}

/**
* Removes all listeners.
* @returns {Dropdownizer} The Dropdownizer instance.
Expand Down Expand Up @@ -168,7 +212,7 @@ var Dropdownizer = function () {

/**
* Removes listeners and destroys the dropdownizer instances.
* @param {Boolean} resetOriginalElement=false Whether to reset the original 'select' elements.
* @param {Boolean=} resetOriginalElement=false Whether to reset the original 'select' elements.
* @returns {Dropdownizer} The Dropdownizer instance.
*/

Expand Down Expand Up @@ -303,7 +347,7 @@ var Dropdownize = function () {

this._listItems = [];
this._lastSelectedIndex = 0;
this._options = this._el.querySelectorAll("option");
this._options = Array.from(this._el.querySelectorAll("option"));
this._longestLine = 0;

this._options.forEach(function (option, i) {
Expand Down Expand Up @@ -479,7 +523,7 @@ var Dropdownize = function () {
}, {
key: "_listSelect",
value: function _listSelect(evt) {
if (evt.target.dataset.disabled) {
if (evt.target.dataset.disabled || evt.target === this.selectedItem.selectedTarget) {
return;
}

Expand All @@ -504,11 +548,12 @@ var Dropdownize = function () {
}

/**
* Programmatically select a list item.
* Programmatically selects a list item.
* @throws {Error} Throws if your search returns multiple matches.
* @throws {RangeError} Throws if the index is out of bounds.
* @param {Number|String} at The list items index or name. Note that if using a
* string, letter case is ignored.
* @param {Number|String} at The list items index or name. Use a negative number to select
* from the end of the list. Note that if using a string, letter case
* is ignored.
* @returns {Dropdownize} The Dropdownize instance.
*/

Expand All @@ -519,6 +564,10 @@ var Dropdownize = function () {
at = this._convertToIndex(at);
}

if (at < 0) {
at = this._listItems.length + at;
}

var listItem = this._listItems[at];

if (!listItem) {
Expand Down Expand Up @@ -631,6 +680,148 @@ var Dropdownize = function () {
return this;
}

/**
* Deletes a list item. Note that this method properly syncs your original select element.
* @throws {Error} Throws if your search returns multiple matches.
* @throws {RangeError} Throws if the index is out of bounds.
* @param {Number|String} at The list items index or name. Use a negative number to select
* from the end of the list. Note that if using a string, letter case
* is ignored.
* @returns {Dropdownize} The Dropdownize instance.
*/

}, {
key: "removeItem",
value: function removeItem(at) {
if (typeof at === "string") {
at = this._convertToIndex(at);
}

if (at < 0) {
at = this._listItems.length + at;
}

var listItem = this._listItems[at];

if (!listItem) {
throw new RangeError("Your index is out of bounds.");
}

this._ui.ul.removeChild(listItem);
this._listItems.splice(at, 1);

this._el.removeChild(this._options[at]);
this._options.splice(at, 1);

if (at === this._lastSelectedIndex) {
var next = Math.max(at - 1, 0);

this._lastSelectedIndex = next;

if (this._listItems.length > 0) {
this._ui.btn.innerHTML = this._listItems[next].innerHTML;
this._listItems[next].setAttribute("data-selected", true);
} else {
this._ui.btn.innerHTML = "&nbsp;";
}

this._el.selectedIndex = this._lastSelectedIndex;
}

return this;
}

/**
* Adds a list item. Note that this method properly syncs your original select element.
* @throws {RangeError} Throws if the index is out of bounds.
* @param {String} value The items value.
* @param {Object=} attributes={} Attributes to add to the list item. The supported
* properties are 'label', 'disabled', and 'selected'.
* @param {Number=} at=NaN The index in which to insert your new list item
* (defaults to the last item if not set). Use a
* negative number to insert from the end of the list.
* @returns {Dropdownize} The Dropdownize instance.
*/

}, {
key: "addItem",
value: function addItem(value) {
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var at = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN;

if (at < 0) {
at = this._listItems.length + at;
}

if (at > this._el.childElementCount || at < 0) {
throw new RangeError("Your index is out of bounds.");
} else if (isNaN(at) || at === null) {
at = this._el.childElementCount;
}

this._addToSelect(value, attributes, at);
this._addToList(value, attributes, at);

return this;
}
}, {
key: "_addToSelect",
value: function _addToSelect(value) {
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var at = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN;

var option = document.createElement("option");

option.innerHTML = value;

this._el.insertBefore(option, this._el.childNodes[at]);
this._options.splice(at, 0, option);

if (attributes.hasOwnProperty("label")) {
option.setAttribute("label", attributes.label || true);
}

if (attributes.hasOwnProperty("disabled")) {
option.setAttribute("disabled", attributes.disabled || true);
}

if (attributes.hasOwnProperty("selected")) {
option.setAttribute("selected", attributes.selected || true);
}
}
}, {
key: "_addToList",
value: function _addToList(value) {
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var at = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN;

var li = document.createElement("li");

li.dataset.value = value;
li.innerHTML = attributes.label || value;

this._ui.ul.insertBefore(li, this._ui.ul.childNodes[at]);
this._listItems.splice(at, 0, li);

li.addEventListener("click", this._onClickListItem);

if (attributes.hasOwnProperty("label")) {
li.setAttribute("data-label", attributes.label || true);
}

if (attributes.hasOwnProperty("disabled")) {
li.setAttribute("data-disabled", attributes.disabled || true);
}

if (attributes.hasOwnProperty("selected")) {
li.setAttribute("data-selected", attributes.selected || true);

if (!attributes.hasOwnProperty("disabled")) {
this.selectItem(at);
}
}
}

/**
* Removes all listeners.
* @returns {Dropdownize} The Dropdownize instance.
Expand Down Expand Up @@ -684,7 +875,7 @@ var Dropdownize = function () {

/**
* Removes listeners and destroys the dropdownizer instance.
* @param {Boolean} resetOriginalElement=false Whether to reset the original 'select' element.
* @param {Boolean=} resetOriginalElement=false Whether to reset the original 'select' element.
* @returns {Dropdownize} The Dropdownize instance.
*/

Expand Down

0 comments on commit 4cf3a89

Please sign in to comment.