UNPKG

cheetah-framework

Version:

Cheetah Framework JS used in all our applications

94 lines (80 loc) 1.97 kB
let uid = 0 class MenuElement { constructor (label, options) { this.uid = uid++ this.cache = {} this.label = label this.options = { icon: null, ...(options || {}) } } get canShow () { if (this.options.permissions?.length && !cheetahApp.user?.can(...this.options.permissions)) { return false } if (this.options.roles?.length && !cheetahApp.user?.is(...this.options.roles)) { return false } return true } } class MenuGroup extends MenuElement { /** * @param {string} label * @param {object|MenuItem[]|MenuGroup[]} options * @param {null|MenuItem[]|MenuGroup[]} items */ constructor (label, options, items = null) { if (arguments.length === 2) { [options, items] = [items, options] } super(label, options) this.items = items } get canShow () { return super.canShow && !!this.items?.filter(item => item.canShow).length } /** * Recursively get child menu items * * @returns {MenuItem[]} */ getDescendants () { if (!_.has(this.cache, 'descendants')) { this.cache.descendants = _.reduce( this.items, (childMenuItems, menuItem) => { return _.concat( childMenuItems, menuItem instanceof MenuGroup ? menuItem.getDescendants() : [menuItem] ) }, [] ) } return this.cache.descendants } } class MenuItem extends MenuElement { /** * @param {string} label * @param {object} route * @param {object} options */ constructor (label, route, options = {}) { super(label, { exact: false, ...options }) this.route = route } /** * Resolve item route * * @returns {object} */ getRoute () { if (!_.has(this.cache, 'resolvedRoute')) { this.cache.resolvedRoute = cheetahApp.$router.resolve(this.route).route } return this.cache.resolvedRoute } } export { MenuGroup, MenuItem }