feature.js 3.8 KB

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