2015-07-15 12:30:11 +08:00
|
|
|
import Separator from 'flarum/components/Separator';
|
2015-07-17 13:17:49 +08:00
|
|
|
import classList from 'flarum/utils/classList';
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
function isSeparator(item) {
|
|
|
|
return item && item.component === Separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
function withoutUnnecessarySeparators(items) {
|
|
|
|
const newItems = [];
|
|
|
|
let prevItem;
|
|
|
|
|
|
|
|
items.forEach((item, i) => {
|
|
|
|
if (!isSeparator(item) || (prevItem && !isSeparator(prevItem) && i !== items.length - 1)) {
|
|
|
|
prevItem = item;
|
|
|
|
newItems.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return newItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `listItems` helper wraps a collection of components in <li> tags,
|
|
|
|
* stripping out any unnecessary `Separator` components.
|
|
|
|
*
|
2015-09-04 10:46:23 +08:00
|
|
|
* @param {*} items
|
2015-07-15 12:30:11 +08:00
|
|
|
* @return {Array}
|
|
|
|
*/
|
|
|
|
export default function listItems(items) {
|
2015-09-04 10:46:23 +08:00
|
|
|
if (!(items instanceof Array)) items = [items];
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
return withoutUnnecessarySeparators(items).map(item => {
|
|
|
|
const isListItem = item.component && item.component.isListItem;
|
2015-07-17 13:17:49 +08:00
|
|
|
const active = item.component && item.component.isActive && item.component.isActive(item.props);
|
2015-07-15 12:30:11 +08:00
|
|
|
const className = item.props ? item.props.itemClassName : item.itemClassName;
|
|
|
|
|
2016-01-19 15:29:19 +08:00
|
|
|
if (isListItem) {
|
2016-01-19 16:25:57 +08:00
|
|
|
item.attrs = item.attrs || {};
|
|
|
|
item.attrs.key = item.attrs.key || item.itemName;
|
2016-01-19 15:29:19 +08:00
|
|
|
}
|
|
|
|
|
2016-01-19 16:25:57 +08:00
|
|
|
const space = new String(' ');
|
|
|
|
space.attrs = {key: '_space_'+item.itemName};
|
|
|
|
|
2015-07-17 13:17:49 +08:00
|
|
|
return [
|
|
|
|
isListItem
|
|
|
|
? item
|
|
|
|
: <li className={classList([
|
|
|
|
(item.itemName ? 'item-' + item.itemName : ''),
|
|
|
|
className,
|
|
|
|
(active ? 'active' : '')
|
2016-01-13 07:04:12 +08:00
|
|
|
])}
|
|
|
|
key={item.itemName}>
|
2015-07-17 13:17:49 +08:00
|
|
|
{item}
|
|
|
|
</li>,
|
2016-01-19 16:25:57 +08:00
|
|
|
space
|
2015-07-17 13:17:49 +08:00
|
|
|
];
|
2015-07-15 12:30:11 +08:00
|
|
|
});
|
2015-07-17 13:17:49 +08:00
|
|
|
}
|