From 44256dcf70be52d49ccdbbb99fc543b7536bc3e0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 26 Apr 2014 01:05:25 -0500 Subject: [PATCH] add getItem and getItemIndex for List. --- README.md | 2 +- lib/widget.js | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 429942d..f664071 100644 --- a/README.md +++ b/README.md @@ -562,7 +562,7 @@ A scrollable list which can display selectable items. - inherits all from Box. - **add/addItem(text)** - add an item based on a string. - **removeItem(child)** - removes an item from the list. child can be an - element or an index. + element, index, or string. - **clearItems()** - clears all items from the list. - **setItems(items)** - sets the list items to multiple strings. - **select(index)** - select an index of an item. diff --git a/lib/widget.js b/lib/widget.js index a73d2fd..f85905b 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -4209,11 +4209,22 @@ List.prototype.appendItem = function(item) { } }; -List.prototype.removeItem = function(child) { - var i = typeof child !== 'number' - ? this.items.indexOf(child) - : child; +List.prototype.getItemIndex = function(child) { + if (typeof child === 'number') { + return child; + } else if (typeof child === 'string') { + return this.ritems.indexOf(child); + } else { + return this.items.indexOf(child); + } +}; +List.prototype.getItem = function(child) { + return this.items[this.getItemIndex(child)]; +}; + +List.prototype.removeItem = function(child) { + var i = this.getItemIndex(child); if (~i && this.items[i]) { child = this.items.splice(i, 1)[0]; this.ritems.splice(i, 1);