mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 07:09:55 +08:00
67 lines
1.7 KiB
JavaScript
67 lines
1.7 KiB
JavaScript
export function getMenuItems(moduleData, locale) {
|
|
const menuMeta = moduleData.map(item => item.meta);
|
|
const menuItems = {};
|
|
menuMeta.sort(
|
|
(a, b) => (a.order || 0) - (b.order || 0)
|
|
).forEach((meta) => {
|
|
const category = (meta.category && meta.category[locale]) || meta.category || 'topLevel';
|
|
if (!menuItems[category]) {
|
|
menuItems[category] = {};
|
|
}
|
|
|
|
const type = meta.type || 'topLevel';
|
|
if (!menuItems[category][type]) {
|
|
menuItems[category][type] = [];
|
|
}
|
|
|
|
menuItems[category][type].push(meta);
|
|
});
|
|
return menuItems;
|
|
}
|
|
|
|
export function isZhCN(pathname) {
|
|
return /-cn\/?$/.test(pathname);
|
|
}
|
|
|
|
export function getLocalizedPathname(path, zhCN) {
|
|
const pathname = path.startsWith('/') ? path : `/${path}`;
|
|
if (!zhCN) { // to enUS
|
|
return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
|
|
} else if (pathname === '/') {
|
|
return '/index-cn';
|
|
} else if (pathname.endsWith('/')) {
|
|
return pathname.replace(/\/$/, '-cn/');
|
|
}
|
|
return `${pathname}-cn`;
|
|
}
|
|
|
|
export function ping(callback) {
|
|
// eslint-disable-next-line
|
|
const url = 'https://private.alipay' + 'objects.com/alip' + 'ay-rmsdeploy-image/rmsportal/RKuAiriJqrUhyqW.png';
|
|
const img = new Image();
|
|
let done;
|
|
const finish = (status) => {
|
|
if (!done) {
|
|
done = true;
|
|
img.src = '';
|
|
callback(status);
|
|
}
|
|
};
|
|
img.onload = () => finish('responded');
|
|
img.onerror = () => finish('error');
|
|
img.src = url;
|
|
return setTimeout(() => finish('timeout'), 1500);
|
|
}
|
|
|
|
export function isLocalStorageNameSupported() {
|
|
const testKey = 'test';
|
|
const storage = window.localStorage;
|
|
try {
|
|
storage.setItem(testKey, '1');
|
|
storage.removeItem(testKey);
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|