1
0
mirror of https://github.com/flarum/core.git synced 2025-05-05 23:15:22 +02:00

Change ItemList API

This commit is contained in:
Toby Zerner 2015-10-30 22:45:58 +10:30
parent 95e3ff8fa8
commit eb571c5595

View File

@ -11,18 +11,34 @@ class Item {
*/ */
export default class ItemList { export default class ItemList {
constructor() { constructor() {
this.list = {}; /**
* The items in the list.
*
* @type {Object}
* @public
*/
this.items = {};
} }
/** /**
* Get an item. * Check whether an item is present in the list.
*
* @param key
* @returns {boolean}
*/
has(key) {
return !!this.items[key];
}
/**
* Get the content of an item.
* *
* @param {String} key * @param {String} key
* @return {Item} * @return {*}
* @public * @public
*/ */
get(key) { get(key) {
return this.list[key]; return this.items[key].content;
} }
/** /**
@ -35,7 +51,7 @@ export default class ItemList {
* @public * @public
*/ */
add(key, content, priority = 0) { add(key, content, priority = 0) {
this.list[key] = new Item(content, priority); this.items[key] = new Item(content, priority);
} }
/** /**
@ -47,13 +63,13 @@ export default class ItemList {
* @public * @public
*/ */
replace(key, content = null, priority = null) { replace(key, content = null, priority = null) {
if (this.list[key]) { if (this.items[key]) {
if (content !== null) { if (content !== null) {
this.list[key].content = content; this.items[key].content = content;
} }
if (priority !== null) { if (priority !== null) {
this.list[key].priority = priority; this.items[key].priority = priority;
} }
} }
} }
@ -65,7 +81,7 @@ export default class ItemList {
* @public * @public
*/ */
remove(key) { remove(key) {
delete this.list[key]; delete this.items[key];
} }
/** /**
@ -75,9 +91,9 @@ export default class ItemList {
* @public * @public
*/ */
merge(items) { merge(items) {
for (const i in items.list) { for (const i in items.items) {
if (items.list.hasOwnProperty(i) && items.list[i] instanceof Item) { if (items.items.hasOwnProperty(i) && items.items[i] instanceof Item) {
this.list[i] = items.list[i]; this.items[i] = items.items[i];
} }
} }
} }
@ -93,13 +109,13 @@ export default class ItemList {
toArray() { toArray() {
const items = []; const items = [];
for (const i in this.list) { for (const i in this.items) {
if (this.list.hasOwnProperty(i) && this.list[i] instanceof Item) { if (this.items.hasOwnProperty(i) && this.items[i] instanceof Item) {
this.list[i].content = Object(this.list[i].content); this.items[i].content = Object(this.items[i].content);
this.list[i].content.itemName = i; this.items[i].content.itemName = i;
items.push(this.list[i]); items.push(this.items[i]);
this.list[i].key = items.length; this.items[i].key = items.length;
} }
} }