2016-09-26 11:08:27 +08:00
|
|
|
export function getMenuItems(moduleData, locale) {
|
2016-08-04 10:52:09 +08:00
|
|
|
const menuMeta = moduleData.map(item => item.meta);
|
2016-05-27 11:48:08 +08:00
|
|
|
const menuItems = {};
|
2016-07-17 14:46:21 +08:00
|
|
|
menuMeta.sort(
|
|
|
|
(a, b) => (a.order || 0) - (b.order || 0)
|
|
|
|
).forEach((meta) => {
|
2016-09-26 11:08:27 +08:00
|
|
|
const category = (meta.category && meta.category[locale]) || meta.category || 'topLevel';
|
2016-05-27 11:48:08 +08:00
|
|
|
if (!menuItems[category]) {
|
|
|
|
menuItems[category] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const type = meta.type || 'topLevel';
|
|
|
|
if (!menuItems[category][type]) {
|
|
|
|
menuItems[category][type] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItems[category][type].push(meta);
|
|
|
|
});
|
|
|
|
return menuItems;
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:02:16 +08:00
|
|
|
export function isZhCN(pathname) {
|
|
|
|
return /-cn\/?$/.test(pathname);
|
|
|
|
}
|
2016-10-18 12:04:09 +08:00
|
|
|
|
2016-12-09 13:02:16 +08:00
|
|
|
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`;
|
2016-10-18 11:18:25 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
export function ping(url, callback) {
|
|
|
|
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;
|
2016-07-17 15:25:12 +08:00
|
|
|
return setTimeout(() => finish('timeout'), 1500);
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
2017-02-21 13:36:17 +08:00
|
|
|
|
|
|
|
export function isLocalStorageNameSupported() {
|
|
|
|
const testKey = 'test';
|
|
|
|
const storage = window.localStorage;
|
|
|
|
try {
|
|
|
|
storage.setItem(testKey, '1');
|
|
|
|
storage.removeItem(testKey);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|