2
0

env.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Are we in a browser or in Node?
  3. *
  4. * @type {Boolean}
  5. */
  6. var inBrowser = exports.inBrowser =
  7. typeof window !== 'undefined' &&
  8. Object.prototype.toString.call(window) === '[object global]'
  9. /**
  10. * Defer a task to the start of the next event loop
  11. *
  12. * @param {Function} fn
  13. */
  14. var defer = inBrowser
  15. ? (window.requestAnimationFrame ||
  16. window.webkitRequestAnimationFrame ||
  17. setTimeout)
  18. : setTimeout
  19. exports.nextTick = function (fn) {
  20. return defer(fn, 0)
  21. }
  22. /**
  23. * Detect if the environment allows creating
  24. * a function from strings.
  25. *
  26. * @type {Boolean}
  27. */
  28. exports.hasEval = (function () {
  29. // chrome apps enforces CSP
  30. if (typeof chrome !== 'undefined' &&
  31. chrome.app &&
  32. chrome.app.runtime) {
  33. return false
  34. }
  35. try {
  36. var f = new Function('', 'return true;')
  37. return f()
  38. } catch (e) {
  39. return false
  40. }
  41. })()
  42. /**
  43. * Detect if we are in IE9...
  44. *
  45. * @type {Boolean}
  46. */
  47. exports.isIE9 =
  48. inBrowser &&
  49. navigator.userAgent.indexOf('MSIE 9.0') > 0
  50. /**
  51. * Detect transition and animation end events.
  52. */
  53. var testElement = inBrowser
  54. ? document.createElement('div')
  55. : null
  56. exports.transitionEndEvent = (function () {
  57. if (!inBrowser) {
  58. return null
  59. }
  60. var map = {
  61. 'webkitTransition' : 'webkitTransitionEnd',
  62. 'transition' : 'transitionend',
  63. 'mozTransition' : 'transitionend'
  64. }
  65. for (var prop in map) {
  66. if (testElement.style[prop] !== undefined) {
  67. return map[prop]
  68. }
  69. }
  70. })()
  71. exports.animationEndEvent = inBrowser
  72. ? testElement.style.animation !== undefined
  73. ? 'animationend'
  74. : 'webkitAnimationEnd'
  75. : null