UNPKG

piral-menu

Version:

Plugin for the integration of menu items in Piral.

72 lines 2.36 kB
import * as actions from './actions'; import { withApi, buildName, withAll, withRootExtension } from 'piral-core'; import { DefaultContainer, DefaultItem } from './default'; import { Menu } from './Menu'; function getSettings(defaultSettings, customSettings = {}) { return { type: 'general', ...defaultSettings, ...customSettings, }; } function getMenuItems(items, defaultSettings) { const menuItems = {}; let i = 0; for (const { component, settings } of items) { menuItems[`global-${i++}`] = { pilet: undefined, component, settings: getSettings(defaultSettings, settings), }; } return menuItems; } function withMenu(menuItems) { return (state) => ({ ...state, components: { MenuContainer: DefaultContainer, MenuItem: DefaultItem, ...state.components, }, registry: { ...state.registry, menuItems, }, }); } /** * Creates new Pilet API extensions for integration of menu items. */ export function createMenuApi(config = {}) { const { items = [], defaultSettings = {} } = config; return (context) => { context.defineActions(actions); context.dispatch(withAll(withMenu(getMenuItems(items, defaultSettings)), withRootExtension('piral-menu', Menu))); return (api, target) => { const pilet = target.name; let next = 0; return { registerMenu(name, arg, settings) { if (typeof name !== 'string') { settings = arg; arg = name; name = next++; } const id = buildName(pilet, name); context.registerMenuItem(id, { pilet, component: withApi(context, arg, api, 'menu'), settings: getSettings(defaultSettings, settings), }); return () => api.unregisterMenu(name); }, unregisterMenu(name) { const id = buildName(pilet, name); context.unregisterMenuItem(id); }, }; }; }; } //# sourceMappingURL=create.js.map