fix list.removeItem. add list.insertItem, list.setItem. fixes #18.

This commit is contained in:
Christopher Jeffrey 2015-05-06 17:19:56 -07:00
parent b21e535391
commit f89e6661f3
3 changed files with 169 additions and 76 deletions

View File

@ -736,14 +736,22 @@ A scrollable list which can display selectable items.
- inherits all from Box.
- __add/addItem(text)__ - add an item based on a string.
- __getItemIndex(child)__ - returns the item index from the list. child can be
an element, index, or string.
- __getItem(child)__ - returns the item element. child can be an element,
index, or string.
- __removeItem(child)__ - removes an item from the list. child can be an
element, index, or string.
- __pushItem(child)__ - push an item onto the list.
- __popItem()__ - pop an item off the list.
- __unshiftItem(child)__ - unshift an item onto the list.
- __shiftItem()__ - shift an item off the list.
- __insertItem(i, child)__ - inserts an item to the list. child can be an
element, index, or string.
- __getItem(child)__ - returns the item element. child can be an element,
index, or string.
- __setItem(child, content)__ - set item to content.
- __spliceItem(i, n, item1, ...)__ - remove and insert items to the list.
- __clearItems()__ - clears all items from the list.
- __setItems(items)__ - sets the list items to multiple strings.
- __getItemIndex(child)__ - returns the item index from the list. child can be
an element, index, or string.
- __select(index)__ - select an index of an item.
- __move(offset)__ - select item based on current offset.
- __up(amount)__ - select item above selected.

View File

@ -225,19 +225,15 @@ List.prototype.__proto__ = Box.prototype;
List.prototype.type = 'list';
List.prototype.add =
List.prototype.addItem =
List.prototype.appendItem = function(item) {
List.prototype.createItem = function(content) {
var self = this;
this.ritems.push(item);
// Note: Could potentially use Button here.
var options = {
screen: this.screen,
content: item,
content: content,
align: this.align || 'left',
top: this.items.length,
top: 0,
left: 0,
right: (this.scrollbar ? 1 : 0),
tags: this.parseTags,
@ -248,7 +244,7 @@ List.prototype.appendItem = function(item) {
};
if (!this.screen.autoPadding) {
options.top = this.itop + this.items.length;
options.top = 1;
options.left = this.ileft;
options.right = this.iright + (this.scrollbar ? 1 : 0);
}
@ -277,13 +273,6 @@ List.prototype.appendItem = function(item) {
var item = new Box(options);
this.items.push(item);
this.append(item);
if (this.items.length === 1) {
this.select(0);
}
if (this.mouse) {
item.on('click', function(data) {
self.focus();
@ -297,7 +286,156 @@ List.prototype.appendItem = function(item) {
});
}
this.emit('create item');
return item;
};
List.prototype.add =
List.prototype.addItem =
List.prototype.appendItem = function(content) {
var self = this;
content = typeof content === 'string' ? content : content.getContent();
var item = this.createItem(content);
item.position.top = this.items.length;
if (!this.screen.autoPadding) {
item.position.top = this.itop + this.items.length;
}
this.ritems.push(content);
this.items.push(item);
this.append(item);
if (this.items.length === 1) {
this.select(0);
}
this.emit('add item');
return item;
};
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);
this.remove(child);
for (var j = i; j < this.items.length; j++) {
this.items[j].position.top--;
}
if (i === this.selected) {
this.select(i - 1);
}
}
this.emit('remove item');
return child;
};
List.prototype.insertItem = function(child, content) {
content = typeof content === 'string' ? content : content.getContent();
var i = this.getItemIndex(child);
if (!~i) return;
if (i >= this.items.length) return this.appendItem(content);
var item = this.createItem(content);
for (var j = i; j < this.items.length; j++) {
this.items[j].position.top++;
}
item.position.top = i + (!this.screen.autoPadding ? 1 : 0);
this.ritems.splice(i, 0, content);
this.items.splice(i, 0, item);
this.append(item);
if (i === this.selected) {
this.select(i + 1);
}
this.emit('insert item');
};
List.prototype.getItem = function(child) {
return this.items[this.getItemIndex(child)];
};
List.prototype.setItem = function(child, content) {
content = typeof content === 'string' ? content : content.getContent();
var i = this.getItemIndex(child);
if (!~i) return;
this.items[i].setContent(content);
this.ritems[i] = content;
};
List.prototype.clearItems = function() {
return this.setItems([]);
};
List.prototype.setItems = function(items) {
var items = items.slice()
, original = this.items.slice()
, selected = this.selected
, sel = this.ritems[this.selected]
, i = 0;
this.select(0);
for (; i < items.length; i++) {
if (this.items[i]) {
this.items[i].setContent(items[i]);
} else {
this.add(items[i]);
}
}
for (; i < original.length; i++) {
this.remove(original[i]);
}
this.ritems = items;
// Try to find our old item if it still exists.
sel = items.indexOf(sel);
if (~sel) {
this.select(sel);
} else if (items.length === original.length) {
this.select(selected);
} else {
this.select(Math.min(selected, items.length - 1));
}
this.emit('set items');
};
List.prototype.pushItem = function(content) {
this.appendItem(content);
return this.items.length;
};
List.prototype.popItem = function() {
return this.removeItem(this.items.length - 1);
};
List.prototype.unshiftItem = function(content) {
this.insertItem(0, content);
return this.items.length;
};
List.prototype.shiftItem = function() {
return this.removeItem(0);
};
List.prototype.spliceItem = function(child, n) {
var self = this;
var i = this.getItemIndex(child);
if (!~i) return;
var items = Array.prototype.slice.call(arguments, 2);
var removed = [];
while (n--) {
removed.push(this.removeItem(i));
}
items.forEach(function(item) {
self.insertItem(i++, item);
});
return removed;
};
List.prototype.find =
@ -361,63 +499,6 @@ List.prototype.getItemIndex = function(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);
this.remove(child);
if (i === this.selected) {
this.select(i - 1);
}
}
this.emit('remove item');
};
List.prototype.clearItems = function() {
return this.setItems([]);
};
List.prototype.setItems = function(items) {
var items = items.slice()
, original = this.items.slice()
, selected = this.selected
, sel = this.ritems[this.selected]
, i = 0;
this.select(0);
for (; i < items.length; i++) {
if (this.items[i]) {
this.items[i].setContent(items[i]);
} else {
this.add(items[i]);
}
}
for (; i < original.length; i++) {
this.remove(original[i]);
}
this.ritems = items;
// Try to find our old item if it still exists.
sel = items.indexOf(sel);
if (~sel) {
this.select(sel);
} else if (items.length === original.length) {
this.select(selected);
} else {
this.select(Math.min(selected, items.length - 1));
}
this.emit('set items');
};
List.prototype.select = function(index) {
if (!this.interactive) {
return;

View File

@ -102,6 +102,10 @@ list.items.forEach(function(item) {
item.setHover(item.getText().trim());
});
var item = list.items[1];
list.removeItem(list.items[1]);
list.insertItem(1, item.getContent());
list.on('keypress', function(ch, key) {
if (key.name === 'up' || key.name === 'k') {
list.up();