env.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * echarts设备环境识别
  3. *
  4. * @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
  5. * @author firede[firede@firede.us]
  6. * @desc thanks zepto.
  7. */
  8. /* global wx */
  9. var env = {};
  10. if (typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function') {
  11. // In Weixin Application
  12. env = {
  13. browser: {},
  14. os: {},
  15. node: false,
  16. wxa: true,
  17. // Weixin Application
  18. canvasSupported: true,
  19. svgSupported: false,
  20. touchEventsSupported: true,
  21. domSupported: false
  22. };
  23. } else if (typeof document === 'undefined' && typeof self !== 'undefined') {
  24. // In worker
  25. env = {
  26. browser: {},
  27. os: {},
  28. node: false,
  29. worker: true,
  30. canvasSupported: true,
  31. domSupported: false
  32. };
  33. } else if (typeof navigator === 'undefined') {
  34. // In node
  35. env = {
  36. browser: {},
  37. os: {},
  38. node: true,
  39. worker: false,
  40. // Assume canvas is supported
  41. canvasSupported: true,
  42. svgSupported: true,
  43. domSupported: false
  44. };
  45. } else {
  46. env = detect(navigator.userAgent);
  47. }
  48. var _default = env; // Zepto.js
  49. // (c) 2010-2013 Thomas Fuchs
  50. // Zepto.js may be freely distributed under the MIT license.
  51. function detect(ua) {
  52. var os = {};
  53. var browser = {}; // var webkit = ua.match(/Web[kK]it[\/]{0,1}([\d.]+)/);
  54. // var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
  55. // var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
  56. // var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
  57. // var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
  58. // var webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/);
  59. // var touchpad = webos && ua.match(/TouchPad/);
  60. // var kindle = ua.match(/Kindle\/([\d.]+)/);
  61. // var silk = ua.match(/Silk\/([\d._]+)/);
  62. // var blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/);
  63. // var bb10 = ua.match(/(BB10).*Version\/([\d.]+)/);
  64. // var rimtabletos = ua.match(/(RIM\sTablet\sOS)\s([\d.]+)/);
  65. // var playbook = ua.match(/PlayBook/);
  66. // var chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/);
  67. var firefox = ua.match(/Firefox\/([\d.]+)/); // var safari = webkit && ua.match(/Mobile\//) && !chrome;
  68. // var webview = ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/) && !chrome;
  69. var ie = ua.match(/MSIE\s([\d.]+)/) // IE 11 Trident/7.0; rv:11.0
  70. || ua.match(/Trident\/.+?rv:(([\d.]+))/);
  71. var edge = ua.match(/Edge\/([\d.]+)/); // IE 12 and 12+
  72. var weChat = /micromessenger/i.test(ua); // Todo: clean this up with a better OS/browser seperation:
  73. // - discern (more) between multiple browsers on android
  74. // - decide if kindle fire in silk mode is android or not
  75. // - Firefox on Android doesn't specify the Android version
  76. // - possibly devide in os, device and browser hashes
  77. // if (browser.webkit = !!webkit) browser.version = webkit[1];
  78. // if (android) os.android = true, os.version = android[2];
  79. // if (iphone && !ipod) os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.');
  80. // if (ipad) os.ios = os.ipad = true, os.version = ipad[2].replace(/_/g, '.');
  81. // if (ipod) os.ios = os.ipod = true, os.version = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
  82. // if (webos) os.webos = true, os.version = webos[2];
  83. // if (touchpad) os.touchpad = true;
  84. // if (blackberry) os.blackberry = true, os.version = blackberry[2];
  85. // if (bb10) os.bb10 = true, os.version = bb10[2];
  86. // if (rimtabletos) os.rimtabletos = true, os.version = rimtabletos[2];
  87. // if (playbook) browser.playbook = true;
  88. // if (kindle) os.kindle = true, os.version = kindle[1];
  89. // if (silk) browser.silk = true, browser.version = silk[1];
  90. // if (!silk && os.android && ua.match(/Kindle Fire/)) browser.silk = true;
  91. // if (chrome) browser.chrome = true, browser.version = chrome[1];
  92. if (firefox) {
  93. browser.firefox = true;
  94. browser.version = firefox[1];
  95. } // if (safari && (ua.match(/Safari/) || !!os.ios)) browser.safari = true;
  96. // if (webview) browser.webview = true;
  97. if (ie) {
  98. browser.ie = true;
  99. browser.version = ie[1];
  100. }
  101. if (edge) {
  102. browser.edge = true;
  103. browser.version = edge[1];
  104. } // It is difficult to detect WeChat in Win Phone precisely, because ua can
  105. // not be set on win phone. So we do not consider Win Phone.
  106. if (weChat) {
  107. browser.weChat = true;
  108. } // os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) ||
  109. // (firefox && ua.match(/Tablet/)) || (ie && !ua.match(/Phone/) && ua.match(/Touch/)));
  110. // os.phone = !!(!os.tablet && !os.ipod && (android || iphone || webos ||
  111. // (chrome && ua.match(/Android/)) || (chrome && ua.match(/CriOS\/([\d.]+)/)) ||
  112. // (firefox && ua.match(/Mobile/)) || (ie && ua.match(/Touch/))));
  113. return {
  114. browser: browser,
  115. os: os,
  116. node: false,
  117. // 原生canvas支持,改极端点了
  118. // canvasSupported : !(browser.ie && parseFloat(browser.version) < 9)
  119. canvasSupported: !!document.createElement('canvas').getContext,
  120. svgSupported: typeof SVGRect !== 'undefined',
  121. // works on most browsers
  122. // IE10/11 does not support touch event, and MS Edge supports them but not by
  123. // default, so we dont check navigator.maxTouchPoints for them here.
  124. touchEventsSupported: 'ontouchstart' in window && !browser.ie && !browser.edge,
  125. // <http://caniuse.com/#search=pointer%20event>.
  126. pointerEventsSupported: // (1) Firefox supports pointer but not by default, only MS browsers are reliable on pointer
  127. // events currently. So we dont use that on other browsers unless tested sufficiently.
  128. // For example, in iOS 13 Mobile Chromium 78, if the touching behavior starts page
  129. // scroll, the `pointermove` event can not be fired any more. That will break some
  130. // features like "pan horizontally to move something and pan vertically to page scroll".
  131. // The horizontal pan probably be interrupted by the casually triggered page scroll.
  132. // (2) Although IE 10 supports pointer event, it use old style and is different from the
  133. // standard. So we exclude that. (IE 10 is hardly used on touch device)
  134. 'onpointerdown' in window && (browser.edge || browser.ie && browser.version >= 11),
  135. // passiveSupported: detectPassiveSupport()
  136. domSupported: typeof document !== 'undefined'
  137. };
  138. } // See https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#feature-detection
  139. // function detectPassiveSupport() {
  140. // // Test via a getter in the options object to see if the passive property is accessed
  141. // var supportsPassive = false;
  142. // try {
  143. // var opts = Object.defineProperty({}, 'passive', {
  144. // get: function() {
  145. // supportsPassive = true;
  146. // }
  147. // });
  148. // window.addEventListener('testPassive', function() {}, opts);
  149. // } catch (e) {
  150. // }
  151. // return supportsPassive;
  152. // }
  153. module.exports = _default;