You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /**!
  2. * @fileOverview Kickass library to create and place poppers near their reference elements.
  3. * @version 1.16.1
  4. * @license
  5. * Copyright (c) 2016 Federico Zivolo and contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /**
  26. * Get CSS computed property of the given element
  27. * @method
  28. * @memberof Popper.Utils
  29. * @argument {Eement} element
  30. * @argument {String} property
  31. */
  32. function getStyleComputedProperty(element, property) {
  33. if (element.nodeType !== 1) {
  34. return [];
  35. }
  36. // NOTE: 1 DOM access here
  37. const window = element.ownerDocument.defaultView;
  38. const css = window.getComputedStyle(element, null);
  39. return property ? css[property] : css;
  40. }
  41. /**
  42. * Returns the parentNode or the host of the element
  43. * @method
  44. * @memberof Popper.Utils
  45. * @argument {Element} element
  46. * @returns {Element} parent
  47. */
  48. function getParentNode(element) {
  49. if (element.nodeName === 'HTML') {
  50. return element;
  51. }
  52. return element.parentNode || element.host;
  53. }
  54. /**
  55. * Returns the scrolling parent of the given element
  56. * @method
  57. * @memberof Popper.Utils
  58. * @argument {Element} element
  59. * @returns {Element} scroll parent
  60. */
  61. function getScrollParent(element) {
  62. // Return body, `getScroll` will take care to get the correct `scrollTop` from it
  63. if (!element) {
  64. return document.body;
  65. }
  66. switch (element.nodeName) {
  67. case 'HTML':
  68. case 'BODY':
  69. return element.ownerDocument.body;
  70. case '#document':
  71. return element.body;
  72. }
  73. // Firefox want us to check `-x` and `-y` variations as well
  74. const {overflow, overflowX, overflowY} = getStyleComputedProperty(element);
  75. if (/(auto|scroll|overlay)/.test(overflow + overflowY + overflowX)) {
  76. return element;
  77. }
  78. return getScrollParent(getParentNode(element));
  79. }
  80. /**
  81. * Returns the reference node of the reference object, or the reference object itself.
  82. * @method
  83. * @memberof Popper.Utils
  84. * @param {Element|Object} reference - the reference element (the popper will be relative to this)
  85. * @returns {Element} parent
  86. */
  87. function getReferenceNode(reference) {
  88. return reference && reference.referenceNode ? reference.referenceNode : reference;
  89. }
  90. var isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined';
  91. const isIE11 = isBrowser && !!(window.MSInputMethodContext && document.documentMode);
  92. const isIE10 = isBrowser && /MSIE 10/.test(navigator.userAgent);
  93. /**
  94. * Determines if the browser is Internet Explorer
  95. * @method
  96. * @memberof Popper.Utils
  97. * @param {Number} version to check
  98. * @returns {Boolean} isIE
  99. */
  100. function isIE(version) {
  101. if (version === 11) {
  102. return isIE11;
  103. }
  104. if (version === 10) {
  105. return isIE10;
  106. }
  107. return isIE11 || isIE10;
  108. }
  109. /**
  110. * Returns the offset parent of the given element
  111. * @method
  112. * @memberof Popper.Utils
  113. * @argument {Element} element
  114. * @returns {Element} offset parent
  115. */
  116. function getOffsetParent(element) {
  117. if (!element) {
  118. return document.documentElement;
  119. }
  120. const noOffsetParent = isIE(10) ? document.body : null;
  121. // NOTE: 1 DOM access here
  122. let offsetParent = element.offsetParent || null;
  123. // Skip hidden elements which don't have an offsetParent
  124. while (offsetParent === noOffsetParent && element.nextElementSibling) {
  125. offsetParent = (element = element.nextElementSibling).offsetParent;
  126. }
  127. const nodeName = offsetParent && offsetParent.nodeName;
  128. if (!nodeName || nodeName === 'BODY' || nodeName === 'HTML') {
  129. return element ? element.ownerDocument.documentElement : document.documentElement;
  130. }
  131. // .offsetParent will return the closest TH, TD or TABLE in case
  132. // no offsetParent is present, I hate this job...
  133. if (['TH', 'TD', 'TABLE'].indexOf(offsetParent.nodeName) !== -1 && getStyleComputedProperty(offsetParent, 'position') === 'static') {
  134. return getOffsetParent(offsetParent);
  135. }
  136. return offsetParent;
  137. }
  138. function isOffsetContainer(element) {
  139. const {nodeName} = element;
  140. if (nodeName === 'BODY') {
  141. return false;
  142. }
  143. return nodeName === 'HTML' || getOffsetParent(element.firstElementChild) === element;
  144. }
  145. /**
  146. * Finds the root node (document, shadowDOM root) of the given element
  147. * @method
  148. * @memberof Popper.Utils
  149. * @argument {Element} node
  150. * @returns {Element} root node
  151. */
  152. function getRoot(node) {
  153. if (node.parentNode !== null) {
  154. return getRoot(node.parentNode);
  155. }
  156. return node;
  157. }
  158. /**
  159. * Finds the offset parent common to the two provided nodes
  160. * @method
  161. * @memberof Popper.Utils
  162. * @argument {Element} element1
  163. * @argument {Element} element2
  164. * @returns {Element} common offset parent
  165. */
  166. function findCommonOffsetParent(element1, element2) {
  167. // This check is needed to avoid errors in case one of the elements isn't defined for any reason
  168. if (!element1 || !element1.nodeType || !element2 || !element2.nodeType) {
  169. return document.documentElement;
  170. }
  171. // Here we make sure to give as "start" the element that comes first in the DOM
  172. const order = element1.compareDocumentPosition(element2) & Node.DOCUMENT_POSITION_FOLLOWING;
  173. const start = order ? element1 : element2;
  174. const end = order ? element2 : element1;
  175. // Get common ancestor container
  176. const range = document.createRange();
  177. range.setStart(start, 0);
  178. range.setEnd(end, 0);
  179. const {commonAncestorContainer} = range;
  180. // Both nodes are inside #document
  181. if (element1 !== commonAncestorContainer && element2 !== commonAncestorContainer || start.contains(end)) {
  182. if (isOffsetContainer(commonAncestorContainer)) {
  183. return commonAncestorContainer;
  184. }
  185. return getOffsetParent(commonAncestorContainer);
  186. }
  187. // one of the nodes is inside shadowDOM, find which one
  188. const element1root = getRoot(element1);
  189. if (element1root.host) {
  190. return findCommonOffsetParent(element1root.host, element2);
  191. } else {
  192. return findCommonOffsetParent(element1, getRoot(element2).host);
  193. }
  194. }
  195. /**
  196. * Gets the scroll value of the given element in the given side (top and left)
  197. * @method
  198. * @memberof Popper.Utils
  199. * @argument {Element} element
  200. * @argument {String} side `top` or `left`
  201. * @returns {number} amount of scrolled pixels
  202. */
  203. function getScroll(element, side = 'top') {
  204. const upperSide = side === 'top' ? 'scrollTop' : 'scrollLeft';
  205. const nodeName = element.nodeName;
  206. if (nodeName === 'BODY' || nodeName === 'HTML') {
  207. const html = element.ownerDocument.documentElement;
  208. const scrollingElement = element.ownerDocument.scrollingElement || html;
  209. return scrollingElement[upperSide];
  210. }
  211. return element[upperSide];
  212. }
  213. /*
  214. * Sum or subtract the element scroll values (left and top) from a given rect object
  215. * @method
  216. * @memberof Popper.Utils
  217. * @param {Object} rect - Rect object you want to change
  218. * @param {HTMLElement} element - The element from the function reads the scroll values
  219. * @param {Boolean} subtract - set to true if you want to subtract the scroll values
  220. * @return {Object} rect - The modifier rect object
  221. */
  222. function includeScroll(rect, element, subtract = false) {
  223. const scrollTop = getScroll(element, 'top');
  224. const scrollLeft = getScroll(element, 'left');
  225. const modifier = subtract ? -1 : 1;
  226. rect.top += scrollTop * modifier;
  227. rect.bottom += scrollTop * modifier;
  228. rect.left += scrollLeft * modifier;
  229. rect.right += scrollLeft * modifier;
  230. return rect;
  231. }
  232. /*
  233. * Helper to detect borders of a given element
  234. * @method
  235. * @memberof Popper.Utils
  236. * @param {CSSStyleDeclaration} styles
  237. * Result of `getStyleComputedProperty` on the given element
  238. * @param {String} axis - `x` or `y`
  239. * @return {number} borders - The borders size of the given axis
  240. */
  241. function getBordersSize(styles, axis) {
  242. const sideA = axis === 'x' ? 'Left' : 'Top';
  243. const sideB = sideA === 'Left' ? 'Right' : 'Bottom';
  244. return parseFloat(styles[`border${sideA}Width`]) + parseFloat(styles[`border${sideB}Width`]);
  245. }
  246. function getSize(axis, body, html, computedStyle) {
  247. return Math.max(body[`offset${axis}`], body[`scroll${axis}`], html[`client${axis}`], html[`offset${axis}`], html[`scroll${axis}`], isIE(10) ? parseInt(html[`offset${axis}`]) + parseInt(computedStyle[`margin${axis === 'Height' ? 'Top' : 'Left'}`]) + parseInt(computedStyle[`margin${axis === 'Height' ? 'Bottom' : 'Right'}`]) : 0);
  248. }
  249. function getWindowSizes(document) {
  250. const body = document.body;
  251. const html = document.documentElement;
  252. const computedStyle = isIE(10) && getComputedStyle(html);
  253. return {
  254. height: getSize('Height', body, html, computedStyle),
  255. width: getSize('Width', body, html, computedStyle)
  256. };
  257. }
  258. var _extends = Object.assign || function (target) {
  259. for (var i = 1; i < arguments.length; i++) {
  260. var source = arguments[i];
  261. for (var key in source) {
  262. if (Object.prototype.hasOwnProperty.call(source, key)) {
  263. target[key] = source[key];
  264. }
  265. }
  266. }
  267. return target;
  268. };
  269. /**
  270. * Given element offsets, generate an output similar to getBoundingClientRect
  271. * @method
  272. * @memberof Popper.Utils
  273. * @argument {Object} offsets
  274. * @returns {Object} ClientRect like output
  275. */
  276. function getClientRect(offsets) {
  277. return _extends({}, offsets, {
  278. right: offsets.left + offsets.width,
  279. bottom: offsets.top + offsets.height
  280. });
  281. }
  282. /**
  283. * Get bounding client rect of given element
  284. * @method
  285. * @memberof Popper.Utils
  286. * @param {HTMLElement} element
  287. * @return {Object} client rect
  288. */
  289. function getBoundingClientRect(element) {
  290. let rect = {};
  291. // IE10 10 FIX: Please, don't ask, the element isn't
  292. // considered in DOM in some circumstances...
  293. // This isn't reproducible in IE10 compatibility mode of IE11
  294. try {
  295. if (isIE(10)) {
  296. rect = element.getBoundingClientRect();
  297. const scrollTop = getScroll(element, 'top');
  298. const scrollLeft = getScroll(element, 'left');
  299. rect.top += scrollTop;
  300. rect.left += scrollLeft;
  301. rect.bottom += scrollTop;
  302. rect.right += scrollLeft;
  303. } else {
  304. rect = element.getBoundingClientRect();
  305. }
  306. } catch (e) {
  307. }
  308. const result = {
  309. left: rect.left,
  310. top: rect.top,
  311. width: rect.right - rect.left,
  312. height: rect.bottom - rect.top
  313. };
  314. // subtract scrollbar size from sizes
  315. const sizes = element.nodeName === 'HTML' ? getWindowSizes(element.ownerDocument) : {};
  316. const width = sizes.width || element.clientWidth || result.width;
  317. const height = sizes.height || element.clientHeight || result.height;
  318. let horizScrollbar = element.offsetWidth - width;
  319. let vertScrollbar = element.offsetHeight - height;
  320. // if an hypothetical scrollbar is detected, we must be sure it's not a `border`
  321. // we make this check conditional for performance reasons
  322. if (horizScrollbar || vertScrollbar) {
  323. const styles = getStyleComputedProperty(element);
  324. horizScrollbar -= getBordersSize(styles, 'x');
  325. vertScrollbar -= getBordersSize(styles, 'y');
  326. result.width -= horizScrollbar;
  327. result.height -= vertScrollbar;
  328. }
  329. return getClientRect(result);
  330. }
  331. function getOffsetRectRelativeToArbitraryNode(children, parent, fixedPosition = false) {
  332. const isIE10 = isIE(10);
  333. const isHTML = parent.nodeName === 'HTML';
  334. const childrenRect = getBoundingClientRect(children);
  335. const parentRect = getBoundingClientRect(parent);
  336. const scrollParent = getScrollParent(children);
  337. const styles = getStyleComputedProperty(parent);
  338. const borderTopWidth = parseFloat(styles.borderTopWidth);
  339. const borderLeftWidth = parseFloat(styles.borderLeftWidth);
  340. // In cases where the parent is fixed, we must ignore negative scroll in offset calc
  341. if (fixedPosition && isHTML) {
  342. parentRect.top = Math.max(parentRect.top, 0);
  343. parentRect.left = Math.max(parentRect.left, 0);
  344. }
  345. let offsets = getClientRect({
  346. top: childrenRect.top - parentRect.top - borderTopWidth,
  347. left: childrenRect.left - parentRect.left - borderLeftWidth,
  348. width: childrenRect.width,
  349. height: childrenRect.height
  350. });
  351. offsets.marginTop = 0;
  352. offsets.marginLeft = 0;
  353. // Subtract margins of documentElement in case it's being used as parent
  354. // we do this only on HTML because it's the only element that behaves
  355. // differently when margins are applied to it. The margins are included in
  356. // the box of the documentElement, in the other cases not.
  357. if (!isIE10 && isHTML) {
  358. const marginTop = parseFloat(styles.marginTop);
  359. const marginLeft = parseFloat(styles.marginLeft);
  360. offsets.top -= borderTopWidth - marginTop;
  361. offsets.bottom -= borderTopWidth - marginTop;
  362. offsets.left -= borderLeftWidth - marginLeft;
  363. offsets.right -= borderLeftWidth - marginLeft;
  364. // Attach marginTop and marginLeft because in some circumstances we may need them
  365. offsets.marginTop = marginTop;
  366. offsets.marginLeft = marginLeft;
  367. }
  368. if (isIE10 && !fixedPosition ? parent.contains(scrollParent) : parent === scrollParent && scrollParent.nodeName !== 'BODY') {
  369. offsets = includeScroll(offsets, parent);
  370. }
  371. return offsets;
  372. }
  373. function getViewportOffsetRectRelativeToArtbitraryNode(element, excludeScroll = false) {
  374. const html = element.ownerDocument.documentElement;
  375. const relativeOffset = getOffsetRectRelativeToArbitraryNode(element, html);
  376. const width = Math.max(html.clientWidth, window.innerWidth || 0);
  377. const height = Math.max(html.clientHeight, window.innerHeight || 0);
  378. const scrollTop = !excludeScroll ? getScroll(html) : 0;
  379. const scrollLeft = !excludeScroll ? getScroll(html, 'left') : 0;
  380. const offset = {
  381. top: scrollTop - relativeOffset.top + relativeOffset.marginTop,
  382. left: scrollLeft - relativeOffset.left + relativeOffset.marginLeft,
  383. width,
  384. height
  385. };
  386. return getClientRect(offset);
  387. }
  388. /**
  389. * Check if the given element is fixed or is inside a fixed parent
  390. * @method
  391. * @memberof Popper.Utils
  392. * @argument {Element} element
  393. * @argument {Element} customContainer
  394. * @returns {Boolean} answer to "isFixed?"
  395. */
  396. function isFixed(element) {
  397. const nodeName = element.nodeName;
  398. if (nodeName === 'BODY' || nodeName === 'HTML') {
  399. return false;
  400. }
  401. if (getStyleComputedProperty(element, 'position') === 'fixed') {
  402. return true;
  403. }
  404. const parentNode = getParentNode(element);
  405. if (!parentNode) {
  406. return false;
  407. }
  408. return isFixed(parentNode);
  409. }
  410. /**
  411. * Finds the first parent of an element that has a transformed property defined
  412. * @method
  413. * @memberof Popper.Utils
  414. * @argument {Element} element
  415. * @returns {Element} first transformed parent or documentElement
  416. */
  417. function getFixedPositionOffsetParent(element) {
  418. // This check is needed to avoid errors in case one of the elements isn't defined for any reason
  419. if (!element || !element.parentElement || isIE()) {
  420. return document.documentElement;
  421. }
  422. let el = element.parentElement;
  423. while (el && getStyleComputedProperty(el, 'transform') === 'none') {
  424. el = el.parentElement;
  425. }
  426. return el || document.documentElement;
  427. }
  428. /**
  429. * Computed the boundaries limits and return them
  430. * @method
  431. * @memberof Popper.Utils
  432. * @param {HTMLElement} popper
  433. * @param {HTMLElement} reference
  434. * @param {number} padding
  435. * @param {HTMLElement} boundariesElement - Element used to define the boundaries
  436. * @param {Boolean} fixedPosition - Is in fixed position mode
  437. * @returns {Object} Coordinates of the boundaries
  438. */
  439. function getBoundaries(popper, reference, padding, boundariesElement, fixedPosition = false) {
  440. // NOTE: 1 DOM access here
  441. let boundaries = {top: 0, left: 0};
  442. const offsetParent = fixedPosition ? getFixedPositionOffsetParent(popper) : findCommonOffsetParent(popper, getReferenceNode(reference));
  443. // Handle viewport case
  444. if (boundariesElement === 'viewport') {
  445. boundaries = getViewportOffsetRectRelativeToArtbitraryNode(offsetParent, fixedPosition);
  446. } else {
  447. // Handle other cases based on DOM element used as boundaries
  448. let boundariesNode;
  449. if (boundariesElement === 'scrollParent') {
  450. boundariesNode = getScrollParent(getParentNode(reference));
  451. if (boundariesNode.nodeName === 'BODY') {
  452. boundariesNode = popper.ownerDocument.documentElement;
  453. }
  454. } else if (boundariesElement === 'window') {
  455. boundariesNode = popper.ownerDocument.documentElement;
  456. } else {
  457. boundariesNode = boundariesElement;
  458. }
  459. const offsets = getOffsetRectRelativeToArbitraryNode(boundariesNode, offsetParent, fixedPosition);
  460. // In case of HTML, we need a different computation
  461. if (boundariesNode.nodeName === 'HTML' && !isFixed(offsetParent)) {
  462. const {height, width} = getWindowSizes(popper.ownerDocument);
  463. boundaries.top += offsets.top - offsets.marginTop;
  464. boundaries.bottom = height + offsets.top;
  465. boundaries.left += offsets.left - offsets.marginLeft;
  466. boundaries.right = width + offsets.left;
  467. } else {
  468. // for all the other DOM elements, this one is good
  469. boundaries = offsets;
  470. }
  471. }
  472. // Add paddings
  473. padding = padding || 0;
  474. const isPaddingNumber = typeof padding === 'number';
  475. boundaries.left += isPaddingNumber ? padding : padding.left || 0;
  476. boundaries.top += isPaddingNumber ? padding : padding.top || 0;
  477. boundaries.right -= isPaddingNumber ? padding : padding.right || 0;
  478. boundaries.bottom -= isPaddingNumber ? padding : padding.bottom || 0;
  479. return boundaries;
  480. }
  481. function getArea({width, height}) {
  482. return width * height;
  483. }
  484. /**
  485. * Utility used to transform the `auto` placement to the placement with more
  486. * available space.
  487. * @method
  488. * @memberof Popper.Utils
  489. * @argument {Object} data - The data object generated by update method
  490. * @argument {Object} options - Modifiers configuration and options
  491. * @returns {Object} The data object, properly modified
  492. */
  493. function computeAutoPlacement(placement, refRect, popper, reference, boundariesElement, padding = 0) {
  494. if (placement.indexOf('auto') === -1) {
  495. return placement;
  496. }
  497. const boundaries = getBoundaries(popper, reference, padding, boundariesElement);
  498. const rects = {
  499. top: {
  500. width: boundaries.width,
  501. height: refRect.top - boundaries.top
  502. },
  503. right: {
  504. width: boundaries.right - refRect.right,
  505. height: boundaries.height
  506. },
  507. bottom: {
  508. width: boundaries.width,
  509. height: boundaries.bottom - refRect.bottom
  510. },
  511. left: {
  512. width: refRect.left - boundaries.left,
  513. height: boundaries.height
  514. }
  515. };
  516. const sortedAreas = Object.keys(rects).map(key => _extends({
  517. key
  518. }, rects[key], {
  519. area: getArea(rects[key])
  520. })).sort((a, b) => b.area - a.area);
  521. const filteredAreas = sortedAreas.filter(({
  522. width,
  523. height
  524. }) => width >= popper.clientWidth && height >= popper.clientHeight);
  525. const computedPlacement = filteredAreas.length > 0 ? filteredAreas[0].key : sortedAreas[0].key;
  526. const variation = placement.split('-')[1];
  527. return computedPlacement + (variation ? `-${variation}` : '');
  528. }
  529. const timeoutDuration = function () {
  530. const longerTimeoutBrowsers = ['Edge', 'Trident', 'Firefox'];
  531. for (let i = 0; i < longerTimeoutBrowsers.length; i += 1) {
  532. if (isBrowser && navigator.userAgent.indexOf(longerTimeoutBrowsers[i]) >= 0) {
  533. return 1;
  534. }
  535. }
  536. return 0;
  537. }();
  538. function microtaskDebounce(fn) {
  539. let called = false;
  540. return () => {
  541. if (called) {
  542. return;
  543. }
  544. called = true;
  545. window.Promise.resolve().then(() => {
  546. called = false;
  547. fn();
  548. });
  549. };
  550. }
  551. function taskDebounce(fn) {
  552. let scheduled = false;
  553. return () => {
  554. if (!scheduled) {
  555. scheduled = true;
  556. setTimeout(() => {
  557. scheduled = false;
  558. fn();
  559. }, timeoutDuration);
  560. }
  561. };
  562. }
  563. const supportsMicroTasks = isBrowser && window.Promise;
  564. /**
  565. * Create a debounced version of a method, that's asynchronously deferred
  566. * but called in the minimum time possible.
  567. *
  568. * @method
  569. * @memberof Popper.Utils
  570. * @argument {Function} fn
  571. * @returns {Function}
  572. */
  573. var debounce = supportsMicroTasks ? microtaskDebounce : taskDebounce;
  574. /**
  575. * Mimics the `find` method of Array
  576. * @method
  577. * @memberof Popper.Utils
  578. * @argument {Array} arr
  579. * @argument prop
  580. * @argument value
  581. * @returns index or -1
  582. */
  583. function find(arr, check) {
  584. // use native find if supported
  585. if (Array.prototype.find) {
  586. return arr.find(check);
  587. }
  588. // use `filter` to obtain the same behavior of `find`
  589. return arr.filter(check)[0];
  590. }
  591. /**
  592. * Return the index of the matching object
  593. * @method
  594. * @memberof Popper.Utils
  595. * @argument {Array} arr
  596. * @argument prop
  597. * @argument value
  598. * @returns index or -1
  599. */
  600. function findIndex(arr, prop, value) {
  601. // use native findIndex if supported
  602. if (Array.prototype.findIndex) {
  603. return arr.findIndex(cur => cur[prop] === value);
  604. }
  605. // use `find` + `indexOf` if `findIndex` isn't supported
  606. const match = find(arr, obj => obj[prop] === value);
  607. return arr.indexOf(match);
  608. }
  609. /**
  610. * Get the position of the given element, relative to its offset parent
  611. * @method
  612. * @memberof Popper.Utils
  613. * @param {Element} element
  614. * @return {Object} position - Coordinates of the element and its `scrollTop`
  615. */
  616. function getOffsetRect(element) {
  617. let elementRect;
  618. if (element.nodeName === 'HTML') {
  619. const {width, height} = getWindowSizes(element.ownerDocument);
  620. elementRect = {
  621. width,
  622. height,
  623. left: 0,
  624. top: 0
  625. };
  626. } else {
  627. elementRect = {
  628. width: element.offsetWidth,
  629. height: element.offsetHeight,
  630. left: element.offsetLeft,
  631. top: element.offsetTop
  632. };
  633. }
  634. // position
  635. return getClientRect(elementRect);
  636. }
  637. /**
  638. * Get the outer sizes of the given element (offset size + margins)
  639. * @method
  640. * @memberof Popper.Utils
  641. * @argument {Element} element
  642. * @returns {Object} object containing width and height properties
  643. */
  644. function getOuterSizes(element) {
  645. const window = element.ownerDocument.defaultView;
  646. const styles = window.getComputedStyle(element);
  647. const x = parseFloat(styles.marginTop || 0) + parseFloat(styles.marginBottom || 0);
  648. const y = parseFloat(styles.marginLeft || 0) + parseFloat(styles.marginRight || 0);
  649. const result = {
  650. width: element.offsetWidth + y,
  651. height: element.offsetHeight + x
  652. };
  653. return result;
  654. }
  655. /**
  656. * Get the opposite placement of the given one
  657. * @method
  658. * @memberof Popper.Utils
  659. * @argument {String} placement
  660. * @returns {String} flipped placement
  661. */
  662. function getOppositePlacement(placement) {
  663. const hash = {left: 'right', right: 'left', bottom: 'top', top: 'bottom'};
  664. return placement.replace(/left|right|bottom|top/g, matched => hash[matched]);
  665. }
  666. /**
  667. * Get offsets to the popper
  668. * @method
  669. * @memberof Popper.Utils
  670. * @param {Object} position - CSS position the Popper will get applied
  671. * @param {HTMLElement} popper - the popper element
  672. * @param {Object} referenceOffsets - the reference offsets (the popper will be relative to this)
  673. * @param {String} placement - one of the valid placement options
  674. * @returns {Object} popperOffsets - An object containing the offsets which will be applied to the popper
  675. */
  676. function getPopperOffsets(popper, referenceOffsets, placement) {
  677. placement = placement.split('-')[0];
  678. // Get popper node sizes
  679. const popperRect = getOuterSizes(popper);
  680. // Add position, width and height to our offsets object
  681. const popperOffsets = {
  682. width: popperRect.width,
  683. height: popperRect.height
  684. };
  685. // depending by the popper placement we have to compute its offsets slightly differently
  686. const isHoriz = ['right', 'left'].indexOf(placement) !== -1;
  687. const mainSide = isHoriz ? 'top' : 'left';
  688. const secondarySide = isHoriz ? 'left' : 'top';
  689. const measurement = isHoriz ? 'height' : 'width';
  690. const secondaryMeasurement = !isHoriz ? 'height' : 'width';
  691. popperOffsets[mainSide] = referenceOffsets[mainSide] + referenceOffsets[measurement] / 2 - popperRect[measurement] / 2;
  692. if (placement === secondarySide) {
  693. popperOffsets[secondarySide] = referenceOffsets[secondarySide] - popperRect[secondaryMeasurement];
  694. } else {
  695. popperOffsets[secondarySide] = referenceOffsets[getOppositePlacement(secondarySide)];
  696. }
  697. return popperOffsets;
  698. }
  699. /**
  700. * Get offsets to the reference element
  701. * @method
  702. * @memberof Popper.Utils
  703. * @param {Object} state
  704. * @param {Element} popper - the popper element
  705. * @param {Element} reference - the reference element (the popper will be relative to this)
  706. * @param {Element} fixedPosition - is in fixed position mode
  707. * @returns {Object} An object containing the offsets which will be applied to the popper
  708. */
  709. function getReferenceOffsets(state, popper, reference, fixedPosition = null) {
  710. const commonOffsetParent = fixedPosition ? getFixedPositionOffsetParent(popper) : findCommonOffsetParent(popper, getReferenceNode(reference));
  711. return getOffsetRectRelativeToArbitraryNode(reference, commonOffsetParent, fixedPosition);
  712. }
  713. /**
  714. * Get the prefixed supported property name
  715. * @method
  716. * @memberof Popper.Utils
  717. * @argument {String} property (camelCase)
  718. * @returns {String} prefixed property (camelCase or PascalCase, depending on the vendor prefix)
  719. */
  720. function getSupportedPropertyName(property) {
  721. const prefixes = [false, 'ms', 'Webkit', 'Moz', 'O'];
  722. const upperProp = property.charAt(0).toUpperCase() + property.slice(1);
  723. for (let i = 0; i < prefixes.length; i++) {
  724. const prefix = prefixes[i];
  725. const toCheck = prefix ? `${prefix}${upperProp}` : property;
  726. if (typeof document.body.style[toCheck] !== 'undefined') {
  727. return toCheck;
  728. }
  729. }
  730. return null;
  731. }
  732. /**
  733. * Check if the given variable is a function
  734. * @method
  735. * @memberof Popper.Utils
  736. * @argument {Any} functionToCheck - variable to check
  737. * @returns {Boolean} answer to: is a function?
  738. */
  739. function isFunction(functionToCheck) {
  740. const getType = {};
  741. return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  742. }
  743. /**
  744. * Helper used to know if the given modifier is enabled.
  745. * @method
  746. * @memberof Popper.Utils
  747. * @returns {Boolean}
  748. */
  749. function isModifierEnabled(modifiers, modifierName) {
  750. return modifiers.some(({name, enabled}) => enabled && name === modifierName);
  751. }
  752. /**
  753. * Helper used to know if the given modifier depends from another one.<br />
  754. * It checks if the needed modifier is listed and enabled.
  755. * @method
  756. * @memberof Popper.Utils
  757. * @param {Array} modifiers - list of modifiers
  758. * @param {String} requestingName - name of requesting modifier
  759. * @param {String} requestedName - name of requested modifier
  760. * @returns {Boolean}
  761. */
  762. function isModifierRequired(modifiers, requestingName, requestedName) {
  763. const requesting = find(modifiers, ({name}) => name === requestingName);
  764. const isRequired = !!requesting && modifiers.some(modifier => {
  765. return modifier.name === requestedName && modifier.enabled && modifier.order < requesting.order;
  766. });
  767. if (!isRequired) {
  768. const requesting = `\`${requestingName}\``;
  769. const requested = `\`${requestedName}\``;
  770. console.warn(`${requested} modifier is required by ${requesting} modifier in order to work, be sure to include it before ${requesting}!`);
  771. }
  772. return isRequired;
  773. }
  774. /**
  775. * Tells if a given input is a number
  776. * @method
  777. * @memberof Popper.Utils
  778. * @param {*} input to check
  779. * @return {Boolean}
  780. */
  781. function isNumeric(n) {
  782. return n !== '' && !isNaN(parseFloat(n)) && isFinite(n);
  783. }
  784. /**
  785. * Get the window associated with the element
  786. * @argument {Element} element
  787. * @returns {Window}
  788. */
  789. function getWindow(element) {
  790. const ownerDocument = element.ownerDocument;
  791. return ownerDocument ? ownerDocument.defaultView : window;
  792. }
  793. /**
  794. * Remove event listeners used to update the popper position
  795. * @method
  796. * @memberof Popper.Utils
  797. * @private
  798. */
  799. function removeEventListeners(reference, state) {
  800. // Remove resize event listener on window
  801. getWindow(reference).removeEventListener('resize', state.updateBound);
  802. // Remove scroll event listener on scroll parents
  803. state.scrollParents.forEach(target => {
  804. target.removeEventListener('scroll', state.updateBound);
  805. });
  806. // Reset state
  807. state.updateBound = null;
  808. state.scrollParents = [];
  809. state.scrollElement = null;
  810. state.eventsEnabled = false;
  811. return state;
  812. }
  813. /**
  814. * Loop trough the list of modifiers and run them in order,
  815. * each of them will then edit the data object.
  816. * @method
  817. * @memberof Popper.Utils
  818. * @param {dataObject} data
  819. * @param {Array} modifiers
  820. * @param {String} ends - Optional modifier name used as stopper
  821. * @returns {dataObject}
  822. */
  823. function runModifiers(modifiers, data, ends) {
  824. const modifiersToRun = ends === undefined ? modifiers : modifiers.slice(0, findIndex(modifiers, 'name', ends));
  825. modifiersToRun.forEach(modifier => {
  826. if (modifier['function']) {
  827. // eslint-disable-line dot-notation
  828. console.warn('`modifier.function` is deprecated, use `modifier.fn`!');
  829. }
  830. const fn = modifier['function'] || modifier.fn; // eslint-disable-line dot-notation
  831. if (modifier.enabled && isFunction(fn)) {
  832. // Add properties to offsets to make them a complete clientRect object
  833. // we do this before each modifier to make sure the previous one doesn't
  834. // mess with these values
  835. data.offsets.popper = getClientRect(data.offsets.popper);
  836. data.offsets.reference = getClientRect(data.offsets.reference);
  837. data = fn(data, modifier);
  838. }
  839. });
  840. return data;
  841. }
  842. /**
  843. * Set the attributes to the given popper
  844. * @method
  845. * @memberof Popper.Utils
  846. * @argument {Element} element - Element to apply the attributes to
  847. * @argument {Object} styles
  848. * Object with a list of properties and values which will be applied to the element
  849. */
  850. function setAttributes(element, attributes) {
  851. Object.keys(attributes).forEach(function (prop) {
  852. const value = attributes[prop];
  853. if (value !== false) {
  854. element.setAttribute(prop, attributes[prop]);
  855. } else {
  856. element.removeAttribute(prop);
  857. }
  858. });
  859. }
  860. /**
  861. * Set the style to the given popper
  862. * @method
  863. * @memberof Popper.Utils
  864. * @argument {Element} element - Element to apply the style to
  865. * @argument {Object} styles
  866. * Object with a list of properties and values which will be applied to the element
  867. */
  868. function setStyles(element, styles) {
  869. Object.keys(styles).forEach(prop => {
  870. let unit = '';
  871. // add unit if the value is numeric and is one of the following
  872. if (['width', 'height', 'top', 'right', 'bottom', 'left'].indexOf(prop) !== -1 && isNumeric(styles[prop])) {
  873. unit = 'px';
  874. }
  875. element.style[prop] = styles[prop] + unit;
  876. });
  877. }
  878. function attachToScrollParents(scrollParent, event, callback, scrollParents) {
  879. const isBody = scrollParent.nodeName === 'BODY';
  880. const target = isBody ? scrollParent.ownerDocument.defaultView : scrollParent;
  881. target.addEventListener(event, callback, {passive: true});
  882. if (!isBody) {
  883. attachToScrollParents(getScrollParent(target.parentNode), event, callback, scrollParents);
  884. }
  885. scrollParents.push(target);
  886. }
  887. /**
  888. * Setup needed event listeners used to update the popper position
  889. * @method
  890. * @memberof Popper.Utils
  891. * @private
  892. */
  893. function setupEventListeners(reference, options, state, updateBound) {
  894. // Resize event listener on window
  895. state.updateBound = updateBound;
  896. getWindow(reference).addEventListener('resize', state.updateBound, {passive: true});
  897. // Scroll event listener on scroll parents
  898. const scrollElement = getScrollParent(reference);
  899. attachToScrollParents(scrollElement, 'scroll', state.updateBound, state.scrollParents);
  900. state.scrollElement = scrollElement;
  901. state.eventsEnabled = true;
  902. return state;
  903. }
  904. // This is here just for backward compatibility with versions lower than v1.10.3
  905. // you should import the utilities using named exports, if you want them all use:
  906. // ```
  907. // import * as PopperUtils from 'popper-utils';
  908. // ```
  909. // The default export will be removed in the next major version.
  910. var index = {
  911. computeAutoPlacement,
  912. debounce,
  913. findIndex,
  914. getBordersSize,
  915. getBoundaries,
  916. getBoundingClientRect,
  917. getClientRect,
  918. getOffsetParent,
  919. getOffsetRect,
  920. getOffsetRectRelativeToArbitraryNode,
  921. getOuterSizes,
  922. getParentNode,
  923. getPopperOffsets,
  924. getReferenceOffsets,
  925. getScroll,
  926. getScrollParent,
  927. getStyleComputedProperty,
  928. getSupportedPropertyName,
  929. getWindowSizes,
  930. isFixed,
  931. isFunction,
  932. isModifierEnabled,
  933. isModifierRequired,
  934. isNumeric,
  935. removeEventListeners,
  936. runModifiers,
  937. setAttributes,
  938. setStyles,
  939. setupEventListeners
  940. };
  941. export {
  942. computeAutoPlacement,
  943. debounce,
  944. findIndex,
  945. getBordersSize,
  946. getBoundaries,
  947. getBoundingClientRect,
  948. getClientRect,
  949. getOffsetParent,
  950. getOffsetRect,
  951. getOffsetRectRelativeToArbitraryNode,
  952. getOuterSizes,
  953. getParentNode,
  954. getPopperOffsets,
  955. getReferenceOffsets,
  956. getScroll,
  957. getScrollParent,
  958. getStyleComputedProperty,
  959. getSupportedPropertyName,
  960. getWindowSizes,
  961. isFixed,
  962. isFunction,
  963. isModifierEnabled,
  964. isModifierRequired,
  965. isNumeric,
  966. removeEventListeners,
  967. runModifiers,
  968. setAttributes,
  969. setStyles,
  970. setupEventListeners
  971. };
  972. export default index;
  973. //# sourceMappingURL=popper-utils.js.map