diff --git a/iron-multi-selectable.html b/iron-multi-selectable.html index 69c30a7..10290e0 100644 --- a/iron-multi-selectable.html +++ b/iron-multi-selectable.html @@ -50,13 +50,19 @@ ], /** - * Selects the given value. If the `multi` property is true, then the selected state of the - * `value` will be toggled; otherwise the `value` will be selected. + * Selects the given value if it's item is not disabled. + * If the `multi` property is true, then the selected state of the `value` will be toggled; + * otherwise the `value` will be selected. * * @method select * @param {string|number} value the value to select. + * @returns Returns true if `value` could be selected or toggled. */ select: function(value) { + if (this._isIneligibleForSelection(this._valueToItem(value))) { + return false; + } + if (this.multi) { if (this.selectedValues) { this._toggleSelected(value); @@ -66,6 +72,7 @@ } else { this.selected = value; } + return true; }, multiChanged: function(multi) { diff --git a/iron-selectable.html b/iron-selectable.html index 3cd73ba..e1a2188 100644 --- a/iron-selectable.html +++ b/iron-selectable.html @@ -172,34 +172,64 @@ }, /** - * Selects the given value. + * Selects the given value if it's item is not disabled. * * @method select * @param {string|number} value the value to select. + * @returns Returns true if `value` could be selected. */ select: function(value) { + if (this._isIneligibleForSelection(this._valueToItem(value))) { + return false; + } this.selected = value; + return true; }, /** - * Selects the previous item. + * Selects the previous enabled item. * * @method selectPrevious + * @returns Returns true if any item could be selected. */ selectPrevious: function() { + if (this.selected == null) { + return false; + } + var length = this.items.length; var index = (Number(this._valueToIndex(this.selected)) - 1 + length) % length; - this.selected = this._indexToValue(index); + var curr = index + 1; + while (index != curr) { + if (this.select(this._indexToValue(index))) { + break; + } + index = (index - 1 + length) % length; + } + return index != curr; }, /** - * Selects the next item. + * Selects the next enabled item. * * @method selectNext + * @returns Returns true if any item could be selected. */ selectNext: function() { - var index = (Number(this._valueToIndex(this.selected)) + 1) % this.items.length; - this.selected = this._indexToValue(index); + if (this.selected == null) { + return false; + } + + var length = this.items.length; + var index = (Number(this._valueToIndex(this.selected)) + 1 + length) % length; + var curr = index - 1; + while (index != curr) { + if (this.select(this._indexToValue(index))) { + break; + } + index = (index + 1 + length) % length; + } + return index != curr; }, /** @@ -242,10 +272,10 @@ }, _updateSelected: function() { - this._selectSelected(this.selected); + this._selectSelected(); }, - _selectSelected: function(selected) { + _selectSelected: function() { this._selection.select(this._valueToItem(this.selected)); }, @@ -337,8 +367,14 @@ {selected: value, item: item}, {cancelable: true}).defaultPrevented) { this.select(value); } - } + }, + _isIneligibleForSelection: function(item) { + if (item) { + return item.hasAttribute('disabled'); + } + return false; + } }; diff --git a/test/basic.html b/test/basic.html index d934f75..5116186 100644 --- a/test/basic.html +++ b/test/basic.html @@ -55,7 +55,7 @@