5c8ba47891c9938dd3aef554ab4ae81562c43e26648bc3d44552cc917946c678d6adbc1e4a3d2f044ec12c59b1ea23cae55b3beecb2ac828cd22735221d3e4 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // https://gist.github.com/paulirish/1579671
  2. let lastTime = 0;
  3. let vendors = ['ms', 'moz', 'webkit', 'o'];
  4. let _requestAnimationFrame = window.requestAnimationFrame;
  5. let _cancelAnimationFrame = window.cancelAnimationFrame;
  6. for (let x = 0; x < vendors.length && !_requestAnimationFrame; ++x) {
  7. _requestAnimationFrame = window[`${vendors[x]}RequestAnimationFrame`];
  8. _cancelAnimationFrame = window[`${vendors[x]}CancelAnimationFrame`] || window[`${vendors[x]}CancelRequestAnimationFrame`];
  9. }
  10. if (!_requestAnimationFrame) {
  11. _requestAnimationFrame = function(callback) {
  12. let currTime = new Date().getTime();
  13. let timeToCall = Math.max(0, 16 - (currTime - lastTime));
  14. let id = window.setTimeout(() => {
  15. callback(currTime + timeToCall);
  16. }, timeToCall);
  17. lastTime = currTime + timeToCall;
  18. return id;
  19. };
  20. }
  21. if (!_cancelAnimationFrame) {
  22. _cancelAnimationFrame = function(id) {
  23. clearTimeout(id);
  24. };
  25. }
  26. /**
  27. * Polyfill for requestAnimationFrame
  28. *
  29. * @param {Function} callback
  30. * @returns {Number}
  31. */
  32. export function requestAnimationFrame(callback) {
  33. return _requestAnimationFrame.call(window, callback);
  34. }
  35. /**
  36. * Polyfill for cancelAnimationFrame
  37. *
  38. * @param {Number} id
  39. */
  40. export function cancelAnimationFrame(id) {
  41. _cancelAnimationFrame.call(window, id);
  42. }
  43. export function isTouchSupported() {
  44. return ('ontouchstart' in window);
  45. }
  46. /**
  47. * Checks if browser is support web components natively
  48. *
  49. * @returns {Boolean}
  50. */
  51. export function isWebComponentSupportedNatively() {
  52. var test = document.createElement('div');
  53. return !!(test.createShadowRoot && test.createShadowRoot.toString().match(/\[native code\]/));
  54. }
  55. var _hasCaptionProblem;
  56. function detectCaptionProblem() {
  57. var TABLE = document.createElement('TABLE');
  58. TABLE.style.borderSpacing = 0;
  59. TABLE.style.borderWidth = 0;
  60. TABLE.style.padding = 0;
  61. var TBODY = document.createElement('TBODY');
  62. TABLE.appendChild(TBODY);
  63. TBODY.appendChild(document.createElement('TR'));
  64. TBODY.firstChild.appendChild(document.createElement('TD'));
  65. TBODY.firstChild.firstChild.innerHTML = '<tr><td>t<br>t</td></tr>';
  66. var CAPTION = document.createElement('CAPTION');
  67. CAPTION.innerHTML = 'c<br>c<br>c<br>c';
  68. CAPTION.style.padding = 0;
  69. CAPTION.style.margin = 0;
  70. TABLE.insertBefore(CAPTION, TBODY);
  71. document.body.appendChild(TABLE);
  72. _hasCaptionProblem = (TABLE.offsetHeight < 2 * TABLE.lastChild.offsetHeight); // boolean
  73. document.body.removeChild(TABLE);
  74. }
  75. export function hasCaptionProblem() {
  76. if (_hasCaptionProblem === void 0) {
  77. detectCaptionProblem();
  78. }
  79. return _hasCaptionProblem;
  80. }
  81. let comparisonFunction;
  82. /**
  83. * Get string comparison function for sorting purposes. It supports multilingual string comparison base on Internationalization API.
  84. *
  85. * @param {String} [language]
  86. * @param {Object} [options]
  87. * @returns {*}
  88. */
  89. export function getComparisonFunction(language, options = {}) {
  90. if (comparisonFunction) {
  91. return comparisonFunction;
  92. }
  93. if (typeof Intl === 'object') {
  94. comparisonFunction = new Intl.Collator(language, options).compare;
  95. } else if (typeof String.prototype.localeCompare === 'function') {
  96. comparisonFunction = (a, b) => (`${a}`).localeCompare(b);
  97. } else {
  98. comparisonFunction = (a, b) => {
  99. if (a === b) {
  100. return 0;
  101. }
  102. return a > b ? -1 : 1;
  103. };
  104. }
  105. return comparisonFunction;
  106. }