Skip to content

Commit

Permalink
fix(core): renamed tsx -> ts, improved type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Yuferev committed Mar 20, 2019
1 parent bda9f54 commit 3d1b4dd
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type MenuTree<Item extends BaseMenuItem> = Item & {
}

export class MenuLevel<Item extends BaseMenuItem> {
constructor(public items: Item[] = [], public activeId: string = '') {}
constructor(public items: Item[], public activeId: string) {}
}

export class MenuLevelBuilder<Item extends BaseMenuItem> {
Expand All @@ -19,12 +19,11 @@ export class MenuLevelBuilder<Item extends BaseMenuItem> {
protected buildLevels(
activeId: string,
items: MenuTree<Item>[],
levels: MenuLevel<Item>[],
levels: (MenuLevel<Item> | undefined)[],
depth = 0,
parentActive = false
): boolean {
if (levels.length <= depth) levels[depth] = new MenuLevel()
const level = levels[depth]
if (levels.length <= depth) levels.push(undefined)
let activeItem: MenuTree<Item> | undefined
for (let item of items) {
const currentActive = item.id === activeId
Expand All @@ -43,24 +42,24 @@ export class MenuLevelBuilder<Item extends BaseMenuItem> {
if (childActive && !activeItem) activeItem = item
}

if (activeItem) {
level.items = items
level.activeId = activeItem.id
} else if (parentActive) {
level.items = items
level.activeId = items[0].id
if (activeItem || parentActive) {
levels[depth] = new MenuLevel(
items,
activeItem ? activeItem.id : items[0].id
)
}

return !!activeItem
}

/**
* Split MenuTree structure to levels.
* Returns array, where index is a menu level.
*
* @param activeId current menu item id
*/
levels(activeId: string): MenuLevel<Item>[] {
const result: MenuLevel<Item>[] = []
levels(activeId: string): (MenuLevel<Item> | undefined)[] {
const result: (MenuLevel<Item>[] | undefined) = []
this.buildLevels(activeId, this.items, result)
return result
}
Expand Down

0 comments on commit 3d1b4dd

Please sign in to comment.