UNPKG

@etsoo/website

Version:

ETSOO CMS Based NextJs Website Framework

198 lines (197 loc) 6.14 kB
import { DataTypes } from '@etsoo/shared'; import enResources from '../i18n/en.json'; import zhHansResources from '../i18n/zh-Hans.json'; import zhHantResources from '../i18n/zh-Hant.json'; /** * Site utilities * 网站工具 */ export var SiteUtils; (function (SiteUtils) { /** * Check the content should be considered as no content * @param content Check content * @returns Result */ function checkNoContent(content) { if (content == null || content === '' || content === 'n/a' || content === '<p>n/a</p>') return true; else return false; } SiteUtils.checkNoContent = checkNoContent; /** * Format article URL * 格式化文章链接 * @param item Article link item * @returns Result */ function formatLink(item) { const { url, tabLayout, tabUrl } = item; if (tabLayout === 0) return tabUrl; if (tabLayout === 1) return '#'; return `${tabUrl}/${url}`; } SiteUtils.formatLink = formatLink; /** * Format tab URL * 格式化栏目地址 * @param tab Tab * @returns Result */ function formatUrl(tab) { return tab.layout === 1 ? '#' : tab.url; } SiteUtils.formatUrl = formatUrl; /** * Get JSON data * @param jsonData JSON data * @returns Result */ function getJsonData(jsonData) { if (jsonData == null) return undefined; if (typeof jsonData === 'string') { try { return JSON.parse(jsonData); } catch { return undefined; } } else if (typeof jsonData === 'object') { return jsonData; } } SiteUtils.getJsonData = getJsonData; /** * Get placement * @param p Placement * @returns Style classes */ function getPlacement(p) { switch (p) { case DataTypes.PlacementEnum.TopLeft: return 'top-0 start-0'; case DataTypes.PlacementEnum.TopCenter: return 'top-0 start-50 translate-middle-x'; case DataTypes.PlacementEnum.TopRight: return 'top-0 end-0'; case DataTypes.PlacementEnum.MiddleLeft: return 'top-50 start-0 translate-middle-y'; case DataTypes.PlacementEnum.Center: return 'top-50 start-50 translate-middle'; case DataTypes.PlacementEnum.MiddleRight: return 'top-50 end-0 translate-middle-y'; case DataTypes.PlacementEnum.BottomLeft: return 'bottom-0 start-0'; case DataTypes.PlacementEnum.BottomCenter: return 'bottom-0 start-50 translate-middle-x'; default: return 'bottom-0 end-0'; } } SiteUtils.getPlacement = getPlacement; const resources = {}; /** * Get culture resource * @param key key * @returns Resource */ function get(key) { const value = resources[key]; if (value == null) return undefined; // No strict type convertion here // Make sure the type is strictly match // Otherwise even request number, may still return the source string type return value; } SiteUtils.get = get; /** * Get string resource * @param key key * @param defaultLabel Default label * @returns Resource */ function getLabel(key, defaultLabel) { var _a, _b; return (_b = (_a = get(key)) !== null && _a !== void 0 ? _a : defaultLabel) !== null && _b !== void 0 ? _b : key; } SiteUtils.getLabel = getLabel; /** * Get multiple tring resources * @param keys Keys */ function getLabels(...keys) { const init = {}; return keys.reduce((a, v) => ({ ...a, [v]: getLabel(v) }), init); } SiteUtils.getLabels = getLabels; /** * Is site tab page props * 是否为网站栏目页面属性 * @param props Page props * @returns Result */ function isSiteTabPageProps(props) { return 'tab' in props && props['tab'] != null; } SiteUtils.isSiteTabPageProps = isSiteTabPageProps; /** * Is static tab page props * 是否为静态栏目页面属性 * @param props Page props * @returns Result */ function isStaticTabPageProps(props) { return 'articles' in props && Array.isArray(props['articles']); } SiteUtils.isStaticTabPageProps = isStaticTabPageProps; /** * Setup * @param culture Culture, like zh-Hans * @param customResources Custom resurces */ function setup(culture, customResources) { if (culture === 'zh-CN' || culture === 'zh-SG' || culture === 'zh-Hans' || culture.startsWith('zh-Hans-')) { Object.assign(resources, zhHansResources); } else if (culture.startsWith('zh-')) { Object.assign(resources, zhHantResources); } else if (culture === 'en' || culture.startsWith('en-')) { Object.assign(resources, enResources); } Object.assign(resources, customResources); } SiteUtils.setup = setup; /** * Toggle button spinner show / hide * @param button Button * @param startSide In start side or not, default false */ function toggleButtonSpinner(button, startSide = false) { let span = button.querySelector('.spinner-border'); if (span == null) { span = document.createElement('span'); span.className = `spinner-border spinner-border-sm ${startSide ? 'me-2' : 'ms-2'}`; span.role = 'status'; startSide ? button.prepend(span) : button.append(span); button.disabled = true; } else { span.remove(); button.disabled = false; } } SiteUtils.toggleButtonSpinner = toggleButtonSpinner; })(SiteUtils || (SiteUtils = {}));