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

113 lines
2.9 KiB
React
Raw Normal View History

2017-11-21 16:50:40 +08:00
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
let enquire;
if (typeof window !== 'undefined') {
const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
return {
media: mediaQuery,
matches: false,
addListener() {
},
removeListener() {
},
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
2017-11-21 17:05:44 +08:00
enquire = require('enquire.js'); // eslint-disable-line global-require
2017-11-21 16:50:40 +08:00
}
export function getMenuItems(moduleData, locale) {
2016-08-04 10:52:09 +08:00
const menuMeta = moduleData.map(item => item.meta);
const menuItems = {};
2016-07-17 14:46:21 +08:00
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);
}
2016-10-18 12:04:09 +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`;
}
2017-07-10 22:17:52 +08:00
export function ping(callback) {
// eslint-disable-next-line
2017-09-25 14:15:06 +08:00
const url = 'https://private-a' + 'lipay' + 'objects.alip' + 'ay.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;
2016-07-17 15:25:12 +08:00
return setTimeout(() => finish('timeout'), 1500);
}
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;
}
}
export function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
export function enquireScreen(cb) {
/* eslint-disable no-unused-expressions */
// and (min-width: 320px)
2017-11-21 18:58:58 +08:00
if (!enquire) {
return;
}
enquire.register('only screen and (max-width: 768px)', {
match: () => {
cb && cb(true);
},
unmatch: () => {
cb && cb();
},
});
/* eslint-enable no-unused-expressions */
}