ant-design/site/theme/template/utils.jsx

54 lines
1.4 KiB
React
Raw Normal View History

2016-06-09 15:00:44 +08:00
export function collectDocs(docs) {
// locale copy from layout
const locale = (typeof localStorage !== 'undefined' && localStorage.getItem('locale') !== 'en-US') ?
'zh-CN' : 'en-US';
const docsList = Object.keys(docs)
.map((key) => docs[key])
.map((value) => {
if (typeof value !== 'function') {
return value[locale] || value.index[locale] || value.index;
}
2016-06-09 15:00:44 +08:00
return value;
})
.map((fn) => fn());
return docsList;
}
2016-06-09 15:00:44 +08:00
export function getMenuItems(data) {
const menuMeta = data.map((item) => item.meta);
const menuItems = {};
menuMeta.sort((a, b) => {
return parseInt(a.order, 10) - parseInt(b.order, 10);
}).forEach((meta) => {
const category = 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 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;
setTimeout(() => finish('timeout'), 1500);
}