Improve ordering of list items when specified key doesn't exist

This commit is contained in:
Toby Zerner 2015-06-11 18:41:13 +09:30
parent cc6113243a
commit f667313cc2

View File

@ -41,21 +41,17 @@ export default class ItemList {
addItems('push', false); addItems('push', false);
addItems('push', 'last'); addItems('push', 'last');
items = items.filter(function(item) { items.forEach(item => {
var key = item.position.before || item.position.after; var key = item.position.before || item.position.after;
var type = item.position.before ? 'before' : 'after'; var type = item.position.before ? 'before' : 'after';
if (key) { if (key) {
var index = array.indexOf(this[key]); var index = array.indexOf(this[key]);
if (index === -1) { if (index === -1) {
console.log("Can't find item with key '"+key+"' to insert "+type+", inserting at end instead"); index = type === 'before' ? 0 : array.length;
return true;
} else {
array.splice(array.indexOf(this[key]) + (type === 'after' ? 1 : 0), 0, item);
} }
array.splice(index + (type === 'after' ? 1 : 0), 0, item);
} }
}.bind(this)); });
array = array.concat(items);
return array.map((item) => item.content); return array.map((item) => item.content);
} }