Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

218 wiersze
5.6 KiB

  1. 'use strict';
  2. // ==============================
  3. // NO OP
  4. // ==============================
  5. var noop = function noop() {};
  6. // Class Name Prefixer
  7. // ==============================
  8. /**
  9. String representation of component state for styling with class names.
  10. Expects an array of strings OR a string/object pair:
  11. - className(['comp', 'comp-arg', 'comp-arg-2'])
  12. @returns 'react-select__comp react-select__comp-arg react-select__comp-arg-2'
  13. - className('comp', { some: true, state: false })
  14. @returns 'react-select__comp react-select__comp--some'
  15. */
  16. function applyPrefixToName(prefix, name) {
  17. if (!name) {
  18. return prefix;
  19. } else if (name[0] === '-') {
  20. return prefix + name;
  21. } else {
  22. return prefix + '__' + name;
  23. }
  24. }
  25. function classNames(prefix, state, className) {
  26. var arr = [className];
  27. if (state && prefix) {
  28. for (var key in state) {
  29. if (state.hasOwnProperty(key) && state[key]) {
  30. arr.push("" + applyPrefixToName(prefix, key));
  31. }
  32. }
  33. }
  34. return arr.filter(function (i) {
  35. return i;
  36. }).map(function (i) {
  37. return String(i).trim();
  38. }).join(' ');
  39. } // ==============================
  40. // Clean Value
  41. // ==============================
  42. var cleanValue = function cleanValue(value) {
  43. if (Array.isArray(value)) return value.filter(Boolean);
  44. if (typeof value === 'object' && value !== null) return [value];
  45. return [];
  46. }; // ==============================
  47. // Handle Input Change
  48. // ==============================
  49. function handleInputChange(inputValue, actionMeta, onInputChange) {
  50. if (onInputChange) {
  51. var newValue = onInputChange(inputValue, actionMeta);
  52. if (typeof newValue === 'string') return newValue;
  53. }
  54. return inputValue;
  55. } // ==============================
  56. // Scroll Helpers
  57. // ==============================
  58. function isDocumentElement(el) {
  59. return [document.documentElement, document.body, window].indexOf(el) > -1;
  60. } // Normalized Scroll Top
  61. // ------------------------------
  62. function getScrollTop(el) {
  63. if (isDocumentElement(el)) {
  64. return window.pageYOffset;
  65. }
  66. return el.scrollTop;
  67. }
  68. function scrollTo(el, top) {
  69. // with a scroll distance, we perform scroll on the element
  70. if (isDocumentElement(el)) {
  71. window.scrollTo(0, top);
  72. return;
  73. }
  74. el.scrollTop = top;
  75. } // Get Scroll Parent
  76. // ------------------------------
  77. function getScrollParent(element) {
  78. var style = getComputedStyle(element);
  79. var excludeStaticParent = style.position === 'absolute';
  80. var overflowRx = /(auto|scroll)/;
  81. var docEl = document.documentElement; // suck it, flow...
  82. if (style.position === 'fixed') return docEl;
  83. for (var parent = element; parent = parent.parentElement;) {
  84. style = getComputedStyle(parent);
  85. if (excludeStaticParent && style.position === 'static') {
  86. continue;
  87. }
  88. if (overflowRx.test(style.overflow + style.overflowY + style.overflowX)) {
  89. return parent;
  90. }
  91. }
  92. return docEl;
  93. } // Animated Scroll To
  94. // ------------------------------
  95. /**
  96. @param t: time (elapsed)
  97. @param b: initial value
  98. @param c: amount of change
  99. @param d: duration
  100. */
  101. function easeOutCubic(t, b, c, d) {
  102. return c * ((t = t / d - 1) * t * t + 1) + b;
  103. }
  104. function animatedScrollTo(element, to, duration, callback) {
  105. if (duration === void 0) {
  106. duration = 200;
  107. }
  108. if (callback === void 0) {
  109. callback = noop;
  110. }
  111. var start = getScrollTop(element);
  112. var change = to - start;
  113. var increment = 10;
  114. var currentTime = 0;
  115. function animateScroll() {
  116. currentTime += increment;
  117. var val = easeOutCubic(currentTime, start, change, duration);
  118. scrollTo(element, val);
  119. if (currentTime < duration) {
  120. window.requestAnimationFrame(animateScroll);
  121. } else {
  122. callback(element);
  123. }
  124. }
  125. animateScroll();
  126. } // Scroll Into View
  127. // ------------------------------
  128. function scrollIntoView(menuEl, focusedEl) {
  129. var menuRect = menuEl.getBoundingClientRect();
  130. var focusedRect = focusedEl.getBoundingClientRect();
  131. var overScroll = focusedEl.offsetHeight / 3;
  132. if (focusedRect.bottom + overScroll > menuRect.bottom) {
  133. scrollTo(menuEl, Math.min(focusedEl.offsetTop + focusedEl.clientHeight - menuEl.offsetHeight + overScroll, menuEl.scrollHeight));
  134. } else if (focusedRect.top - overScroll < menuRect.top) {
  135. scrollTo(menuEl, Math.max(focusedEl.offsetTop - overScroll, 0));
  136. }
  137. } // ==============================
  138. // Get bounding client object
  139. // ==============================
  140. // cannot get keys using array notation with DOMRect
  141. function getBoundingClientObj(element) {
  142. var rect = element.getBoundingClientRect();
  143. return {
  144. bottom: rect.bottom,
  145. height: rect.height,
  146. left: rect.left,
  147. right: rect.right,
  148. top: rect.top,
  149. width: rect.width
  150. };
  151. }
  152. // Touch Capability Detector
  153. // ==============================
  154. function isTouchCapable() {
  155. try {
  156. document.createEvent('TouchEvent');
  157. return true;
  158. } catch (e) {
  159. return false;
  160. }
  161. } // ==============================
  162. // Mobile Device Detector
  163. // ==============================
  164. function isMobileDevice() {
  165. try {
  166. return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  167. } catch (e) {
  168. return false;
  169. }
  170. }
  171. exports.animatedScrollTo = animatedScrollTo;
  172. exports.classNames = classNames;
  173. exports.cleanValue = cleanValue;
  174. exports.getBoundingClientObj = getBoundingClientObj;
  175. exports.getScrollParent = getScrollParent;
  176. exports.getScrollTop = getScrollTop;
  177. exports.handleInputChange = handleInputChange;
  178. exports.isDocumentElement = isDocumentElement;
  179. exports.isMobileDevice = isMobileDevice;
  180. exports.isTouchCapable = isTouchCapable;
  181. exports.noop = noop;
  182. exports.scrollIntoView = scrollIntoView;
  183. exports.scrollTo = scrollTo;