framework/js/src/common/utils/ItemList.js

161 lines
3.1 KiB
JavaScript
Raw Normal View History

class Item {
constructor(content, priority) {
this.content = content;
this.priority = priority;
}
}
/**
* The `ItemList` class collects items and then arranges them into an array
* by priority.
*/
export default class ItemList {
2015-10-29 15:18:08 +08:00
constructor() {
2015-10-30 20:15:58 +08:00
/**
* The items in the list.
*
* @type {Object}
* @public
*/
this.items = {};
2015-10-29 15:18:08 +08:00
}
/**
* Check whether the list is empty.
*
* @returns {boolean}
* @public
*/
isEmpty() {
for (const i in this.items) {
if(this.items.hasOwnProperty(i)) {
return false;
}
}
return true;
}
2015-10-29 15:18:08 +08:00
/**
2015-10-30 20:15:58 +08:00
* 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.
2015-10-29 15:18:08 +08:00
*
* @param {String} key
2015-10-30 20:15:58 +08:00
* @return {*}
2015-10-29 15:18:08 +08:00
* @public
*/
get(key) {
2015-10-30 20:15:58 +08:00
return this.items[key].content;
2015-10-29 15:18:08 +08:00
}
/**
* Add an item to the list.
*
* @param {String} key A unique key for the item.
* @param {*} content The item's content.
* @param {Integer} [priority] The priority of the item. Items with a higher
* priority will be positioned before items with a lower priority.
* @return {ItemList}
* @public
*/
2015-07-22 08:25:36 +08:00
add(key, content, priority = 0) {
2015-10-30 20:15:58 +08:00
this.items[key] = new Item(content, priority);
return this;
2015-10-29 15:18:08 +08:00
}
/**
* Replace an item in the list, only if it is already present.
*
* @param {String} key
* @param {*} [content]
* @param {Integer} [priority]
* @return {ItemList}
2015-10-29 15:18:08 +08:00
* @public
*/
replace(key, content = null, priority = null) {
2015-10-30 20:15:58 +08:00
if (this.items[key]) {
2015-10-29 15:18:08 +08:00
if (content !== null) {
2015-10-30 20:15:58 +08:00
this.items[key].content = content;
2015-10-29 15:18:08 +08:00
}
if (priority !== null) {
2015-10-30 20:15:58 +08:00
this.items[key].priority = priority;
2015-10-29 15:18:08 +08:00
}
}
return this;
2015-10-29 15:18:08 +08:00
}
/**
* Remove an item from the list.
*
* @param {String} key
* @return {ItemList}
2015-10-29 15:18:08 +08:00
* @public
*/
remove(key) {
2015-10-30 20:15:58 +08:00
delete this.items[key];
return this;
}
/**
* Merge another list's items into this one.
*
* @param {ItemList} items
* @return {ItemList}
* @public
*/
merge(items) {
2015-10-30 20:15:58 +08:00
for (const i in items.items) {
if (items.items.hasOwnProperty(i) && items.items[i] instanceof Item) {
this.items[i] = items.items[i];
}
}
return this;
}
/**
* Convert the list into an array of item content arranged by priority. Each
* item's content will be assigned an `itemName` property equal to the item's
* unique key.
*
* @return {Array}
* @public
*/
toArray() {
const items = [];
2015-10-30 20:15:58 +08:00
for (const i in this.items) {
if (this.items.hasOwnProperty(i) && this.items[i] instanceof Item) {
this.items[i].content = Object(this.items[i].content);
2015-10-30 20:15:58 +08:00
this.items[i].content.itemName = i;
items.push(this.items[i]);
this.items[i].key = items.length;
}
}
2015-07-22 08:25:36 +08:00
return items.sort((a, b) => {
if (a.priority === b.priority) {
return a.key - b.key;
} else if (a.priority > b.priority) {
return -1;
}
return 1;
}).map(item => item.content);
}
}