UNPKG

@redocly/theme

Version:

Shared UI components lib

94 lines 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildVersionSection = exports.buildLanguagesGroup = exports.isItemActive = exports.mapNavbarItems = void 0; exports.getMenuItemType = getMenuItemType; const utils_1 = require("../../core/utils"); const constants_1 = require("../../core/constants"); const TRANSLATION_KEYS = { version: 'mobileMenu.version', }; const mapNavbarItems = (items, defaultLocale, currentLocale, locales, location) => { return items.map((navItem) => { var _a; return (Object.assign(Object.assign(Object.assign(Object.assign({}, navItem), (navItem.items && { items: (0, exports.mapNavbarItems)(navItem.items, defaultLocale, currentLocale, locales, location), })), ('link' in navItem && { link: navItem.link || '/' })), { active: 'link' in navItem && (0, exports.isItemActive)(navItem, defaultLocale, currentLocale, locales, location), hasActiveSubItem: !!((_a = navItem.items) === null || _a === void 0 ? void 0 : _a.find((item) => (0, exports.isItemActive)(item, defaultLocale, currentLocale, locales, location))) })); }); }; exports.mapNavbarItems = mapNavbarItems; const isItemActive = (item, defaultLocale, currentLocale, locales, location) => { const pathHash = location.pathname + location.hash; const link = item.languageInsensitive ? item.link || '' : (0, utils_1.getPathnameForLocale)(item.link || '/', defaultLocale, currentLocale, locales); return pathHash === (0, utils_1.withPathPrefix)(link); }; exports.isItemActive = isItemActive; const buildLanguagesGroup = (locales, defaultLocale, currentLocale) => { if (locales.length < 2) { return; } const locale = locales.find((l) => l.code === currentLocale); return { type: 'group', label: (locale === null || locale === void 0 ? void 0 : locale.name) || (locale === null || locale === void 0 ? void 0 : locale.code), items: locales .filter((locale) => locale.code !== currentLocale) .map((locale) => { const newLangPathname = (0, utils_1.getPathnameForLocale)((0, utils_1.withoutPathPrefix)(location.pathname), defaultLocale, locale.code, locales); const newUrlWithLanguage = `${newLangPathname}${location.search}${location.hash}`; return { type: 'link', label: locale.name || locale.code || '', link: newUrlWithLanguage, active: false, hasActiveSubItem: false, languageInsensitive: true, }; }), }; }; exports.buildLanguagesGroup = buildLanguagesGroup; const buildVersionSection = (translate, versions, activeVersion) => { return [ { type: 'separator', label: translate(TRANSLATION_KEYS.version, 'Version'), }, { type: 'group', label: activeVersion === null || activeVersion === void 0 ? void 0 : activeVersion.label, items: versions .filter((version) => !version.active) .map((version) => { return { type: 'link', label: version.label, link: version.link, active: false, hasActiveSubItem: false, }; }), }, ]; }; exports.buildVersionSection = buildVersionSection; function getMenuItemType(item) { var _a, _b; if (item.type === constants_1.MenuItemType.Separator) { return constants_1.MenuItemType.Separator; } else if (item.menuStyle === 'drilldown') { return constants_1.MenuItemType.DrillDown; } else if (((_a = item.items) === null || _a === void 0 ? void 0 : _a.length) || (item.type === constants_1.MenuItemType.Group && ((_b = item.items) === null || _b === void 0 ? void 0 : _b.length))) { return constants_1.MenuItemType.Group; } else if (item.httpVerb) { return constants_1.MenuItemType.Operation; } else return constants_1.MenuItemType.Default; } //# sourceMappingURL=menu.js.map