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)
|
2016-06-15 15:55:39 +08:00
|
|
|
.map(key => docs[key])
|
|
|
|
.map((value) => {
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
return value[locale] || value.index[locale] || value.index;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
})
|
|
|
|
.map(fn => fn());
|
2016-06-09 15:00:44 +08:00
|
|
|
return docsList;
|
|
|
|
}
|
2016-05-27 11:48:08 +08:00
|
|
|
|
2016-06-09 15:00:44 +08:00
|
|
|
export function getMenuItems(data) {
|
|
|
|
const menuMeta = data.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-05-27 11:48:08 +08:00
|
|
|
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;
|
2016-07-17 15:25:12 +08:00
|
|
|
return setTimeout(() => finish('timeout'), 1500);
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|