popper-utils.js 35 KB

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