env.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var Browser = (function () {
  2. function Browser() {
  3. this.firefox = false;
  4. this.ie = false;
  5. this.edge = false;
  6. this.newEdge = false;
  7. this.weChat = false;
  8. }
  9. return Browser;
  10. }());
  11. var Env = (function () {
  12. function Env() {
  13. this.browser = new Browser();
  14. this.node = false;
  15. this.wxa = false;
  16. this.worker = false;
  17. this.svgSupported = false;
  18. this.touchEventsSupported = false;
  19. this.pointerEventsSupported = false;
  20. this.domSupported = false;
  21. this.transformSupported = false;
  22. this.transform3dSupported = false;
  23. this.hasGlobalWindow = typeof window !== 'undefined';
  24. }
  25. return Env;
  26. }());
  27. var env = new Env();
  28. if (typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function') {
  29. env.wxa = true;
  30. env.touchEventsSupported = true;
  31. }
  32. else if (typeof document === 'undefined' && typeof self !== 'undefined') {
  33. env.worker = true;
  34. }
  35. else if (typeof navigator === 'undefined'
  36. || navigator.userAgent.indexOf('Node.js') === 0) {
  37. env.node = true;
  38. env.svgSupported = true;
  39. }
  40. else {
  41. detect(navigator.userAgent, env);
  42. }
  43. function detect(ua, env) {
  44. var browser = env.browser;
  45. var firefox = ua.match(/Firefox\/([\d.]+)/);
  46. var ie = ua.match(/MSIE\s([\d.]+)/)
  47. || ua.match(/Trident\/.+?rv:(([\d.]+))/);
  48. var edge = ua.match(/Edge?\/([\d.]+)/);
  49. var weChat = (/micromessenger/i).test(ua);
  50. if (firefox) {
  51. browser.firefox = true;
  52. browser.version = firefox[1];
  53. }
  54. if (ie) {
  55. browser.ie = true;
  56. browser.version = ie[1];
  57. }
  58. if (edge) {
  59. browser.edge = true;
  60. browser.version = edge[1];
  61. browser.newEdge = +edge[1].split('.')[0] > 18;
  62. }
  63. if (weChat) {
  64. browser.weChat = true;
  65. }
  66. env.svgSupported = typeof SVGRect !== 'undefined';
  67. env.touchEventsSupported = 'ontouchstart' in window && !browser.ie && !browser.edge;
  68. env.pointerEventsSupported = 'onpointerdown' in window
  69. && (browser.edge || (browser.ie && +browser.version >= 11));
  70. env.domSupported = typeof document !== 'undefined';
  71. var style = document.documentElement.style;
  72. env.transform3dSupported = ((browser.ie && 'transition' in style)
  73. || browser.edge
  74. || (('WebKitCSSMatrix' in window) && ('m11' in new WebKitCSSMatrix()))
  75. || 'MozPerspective' in style)
  76. && !('OTransition' in style);
  77. env.transformSupported = env.transform3dSupported
  78. || (browser.ie && +browser.version >= 9);
  79. }
  80. export default env;