mirror of
https://github.com/flarum/framework.git
synced 2025-05-22 06:39:57 +08:00
Massive JavaScript cleanup
- Use JSX for templates - Docblock/comment everything - Mostly passes ESLint (still some work to do) - Lots of renaming, refactoring, etc. CSS hasn't been updated yet.
This commit is contained in:
61
js/lib/utils/ItemList.js
Normal file
61
js/lib/utils/ItemList.js
Normal file
@ -0,0 +1,61 @@
|
||||
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 {
|
||||
/**
|
||||
* 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.
|
||||
* @public
|
||||
*/
|
||||
add(key, content, priority) {
|
||||
this[key] = new Item(content, priority);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge another list's items into this one.
|
||||
*
|
||||
* @param {ItemList} items
|
||||
* @public
|
||||
*/
|
||||
merge(items) {
|
||||
for (const i in items) {
|
||||
if (items.hasOwnProperty(i) && items[i] instanceof Item) {
|
||||
this[i] = items[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = [];
|
||||
|
||||
for (const i in this) {
|
||||
if (this.hasOwnProperty(i) && this[i] instanceof Item) {
|
||||
this[i].content.itemName = i;
|
||||
items.push(this[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return items.sort((a, b) => b.priority - a.priority).map(item => item.content);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user