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.

2433 lines
92 KiB

  1. /**!
  2. * @fileOverview Kickass library to create and place poppers near their reference elements.
  3. * @version 1.12.6
  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' ? module.exports = factory() :
  27. typeof define === 'function' && define.amd ? define(factory) :
  28. (global.Popper = factory());
  29. }(this, (function () {
  30. 'use strict';
  31. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
  32. var longerTimeoutBrowsers = ['Edge', 'Trident', 'Firefox'];
  33. var timeoutDuration = 0;
  34. for (var i = 0; i < longerTimeoutBrowsers.length; i += 1) {
  35. if (isBrowser && navigator.userAgent.indexOf(longerTimeoutBrowsers[i]) >= 0) {
  36. timeoutDuration = 1;
  37. break;
  38. }
  39. }
  40. function microtaskDebounce(fn) {
  41. var called = false;
  42. return function () {
  43. if (called) {
  44. return;
  45. }
  46. called = true;
  47. Promise.resolve().then(function () {
  48. called = false;
  49. fn();
  50. });
  51. };
  52. }
  53. function taskDebounce(fn) {
  54. var scheduled = false;
  55. return function () {
  56. if (!scheduled) {
  57. scheduled = true;
  58. setTimeout(function () {
  59. scheduled = false;
  60. fn();
  61. }, timeoutDuration);
  62. }
  63. };
  64. }
  65. var supportsMicroTasks = isBrowser && window.Promise;
  66. /**
  67. * Create a debounced version of a method, that's asynchronously deferred
  68. * but called in the minimum time possible.
  69. *
  70. * @method
  71. * @memberof Popper.Utils
  72. * @argument {Function} fn
  73. * @returns {Function}
  74. */
  75. var debounce = supportsMicroTasks ? microtaskDebounce : taskDebounce;
  76. /**
  77. * Check if the given variable is a function
  78. * @method
  79. * @memberof Popper.Utils
  80. * @argument {Any} functionToCheck - variable to check
  81. * @returns {Boolean} answer to: is a function?
  82. */
  83. function isFunction(functionToCheck) {
  84. var getType = {};
  85. return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  86. }
  87. /**
  88. * Get CSS computed property of the given element
  89. * @method
  90. * @memberof Popper.Utils
  91. * @argument {Eement} element
  92. * @argument {String} property
  93. */
  94. function getStyleComputedProperty(element, property) {
  95. if (element.nodeType !== 1) {
  96. return [];
  97. }
  98. // NOTE: 1 DOM access here
  99. var css = window.getComputedStyle(element, null);
  100. return property ? css[property] : css;
  101. }
  102. /**
  103. * Returns the parentNode or the host of the element
  104. * @method
  105. * @memberof Popper.Utils
  106. * @argument {Element} element
  107. * @returns {Element} parent
  108. */
  109. function getParentNode(element) {
  110. if (element.nodeName === 'HTML') {
  111. return element;
  112. }
  113. return element.parentNode || element.host;
  114. }
  115. /**
  116. * Returns the scrolling parent of the given element
  117. * @method
  118. * @memberof Popper.Utils
  119. * @argument {Element} element
  120. * @returns {Element} scroll parent
  121. */
  122. function getScrollParent(element) {
  123. // Return body, `getScroll` will take care to get the correct `scrollTop` from it
  124. if (!element) {
  125. return window.document.body;
  126. }
  127. switch (element.nodeName) {
  128. case 'HTML':
  129. case 'BODY':
  130. return element.ownerDocument.body;
  131. case '#document':
  132. return element.body;
  133. }
  134. // Firefox want us to check `-x` and `-y` variations as well
  135. var _getStyleComputedProp = getStyleComputedProperty(element),
  136. overflow = _getStyleComputedProp.overflow,
  137. overflowX = _getStyleComputedProp.overflowX,
  138. overflowY = _getStyleComputedProp.overflowY;
  139. if (/(auto|scroll)/.test(overflow + overflowY + overflowX)) {
  140. return element;
  141. }
  142. return getScrollParent(getParentNode(element));
  143. }
  144. /**
  145. * Returns the offset parent of the given element
  146. * @method
  147. * @memberof Popper.Utils
  148. * @argument {Element} element
  149. * @returns {Element} offset parent
  150. */
  151. function getOffsetParent(element) {
  152. // NOTE: 1 DOM access here
  153. var offsetParent = element && element.offsetParent;
  154. var nodeName = offsetParent && offsetParent.nodeName;
  155. if (!nodeName || nodeName === 'BODY' || nodeName === 'HTML') {
  156. if (element) {
  157. return element.ownerDocument.documentElement;
  158. }
  159. return window.document.documentElement;
  160. }
  161. // .offsetParent will return the closest TD or TABLE in case
  162. // no offsetParent is present, I hate this job...
  163. if (['TD', 'TABLE'].indexOf(offsetParent.nodeName) !== -1 && getStyleComputedProperty(offsetParent, 'position') === 'static') {
  164. return getOffsetParent(offsetParent);
  165. }
  166. return offsetParent;
  167. }
  168. function isOffsetContainer(element) {
  169. var nodeName = element.nodeName;
  170. if (nodeName === 'BODY') {
  171. return false;
  172. }
  173. return nodeName === 'HTML' || getOffsetParent(element.firstElementChild) === element;
  174. }
  175. /**
  176. * Finds the root node (document, shadowDOM root) of the given element
  177. * @method
  178. * @memberof Popper.Utils
  179. * @argument {Element} node
  180. * @returns {Element} root node
  181. */
  182. function getRoot(node) {
  183. if (node.parentNode !== null) {
  184. return getRoot(node.parentNode);
  185. }
  186. return node;
  187. }
  188. /**
  189. * Finds the offset parent common to the two provided nodes
  190. * @method
  191. * @memberof Popper.Utils
  192. * @argument {Element} element1
  193. * @argument {Element} element2
  194. * @returns {Element} common offset parent
  195. */
  196. function findCommonOffsetParent(element1, element2) {
  197. // This check is needed to avoid errors in case one of the elements isn't defined for any reason
  198. if (!element1 || !element1.nodeType || !element2 || !element2.nodeType) {
  199. return window.document.documentElement;
  200. }
  201. // Here we make sure to give as "start" the element that comes first in the DOM
  202. var order = element1.compareDocumentPosition(element2) & Node.DOCUMENT_POSITION_FOLLOWING;
  203. var start = order ? element1 : element2;
  204. var end = order ? element2 : element1;
  205. // Get common ancestor container
  206. var range = document.createRange();
  207. range.setStart(start, 0);
  208. range.setEnd(end, 0);
  209. var commonAncestorContainer = range.commonAncestorContainer;
  210. // Both nodes are inside #document
  211. if (element1 !== commonAncestorContainer && element2 !== commonAncestorContainer || start.contains(end)) {
  212. if (isOffsetContainer(commonAncestorContainer)) {
  213. return commonAncestorContainer;
  214. }
  215. return getOffsetParent(commonAncestorContainer);
  216. }
  217. // one of the nodes is inside shadowDOM, find which one
  218. var element1root = getRoot(element1);
  219. if (element1root.host) {
  220. return findCommonOffsetParent(element1root.host, element2);
  221. } else {
  222. return findCommonOffsetParent(element1, getRoot(element2).host);
  223. }
  224. }
  225. /**
  226. * Gets the scroll value of the given element in the given side (top and left)
  227. * @method
  228. * @memberof Popper.Utils
  229. * @argument {Element} element
  230. * @argument {String} side `top` or `left`
  231. * @returns {number} amount of scrolled pixels
  232. */
  233. function getScroll(element) {
  234. var side = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'top';
  235. var upperSide = side === 'top' ? 'scrollTop' : 'scrollLeft';
  236. var nodeName = element.nodeName;
  237. if (nodeName === 'BODY' || nodeName === 'HTML') {
  238. var html = element.ownerDocument.documentElement;
  239. var scrollingElement = element.ownerDocument.scrollingElement || html;
  240. return scrollingElement[upperSide];
  241. }
  242. return element[upperSide];
  243. }
  244. /*
  245. * Sum or subtract the element scroll values (left and top) from a given rect object
  246. * @method
  247. * @memberof Popper.Utils
  248. * @param {Object} rect - Rect object you want to change
  249. * @param {HTMLElement} element - The element from the function reads the scroll values
  250. * @param {Boolean} subtract - set to true if you want to subtract the scroll values
  251. * @return {Object} rect - The modifier rect object
  252. */
  253. function includeScroll(rect, element) {
  254. var subtract = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  255. var scrollTop = getScroll(element, 'top');
  256. var scrollLeft = getScroll(element, 'left');
  257. var modifier = subtract ? -1 : 1;
  258. rect.top += scrollTop * modifier;
  259. rect.bottom += scrollTop * modifier;
  260. rect.left += scrollLeft * modifier;
  261. rect.right += scrollLeft * modifier;
  262. return rect;
  263. }
  264. /*
  265. * Helper to detect borders of a given element
  266. * @method
  267. * @memberof Popper.Utils
  268. * @param {CSSStyleDeclaration} styles
  269. * Result of `getStyleComputedProperty` on the given element
  270. * @param {String} axis - `x` or `y`
  271. * @return {number} borders - The borders size of the given axis
  272. */
  273. function getBordersSize(styles, axis) {
  274. var sideA = axis === 'x' ? 'Left' : 'Top';
  275. var sideB = sideA === 'Left' ? 'Right' : 'Bottom';
  276. return +styles['border' + sideA + 'Width'].split('px')[0] + +styles['border' + sideB + 'Width'].split('px')[0];
  277. }
  278. /**
  279. * Tells if you are running Internet Explorer 10
  280. * @method
  281. * @memberof Popper.Utils
  282. * @returns {Boolean} isIE10
  283. */
  284. var isIE10 = undefined;
  285. var isIE10$1 = function () {
  286. if (isIE10 === undefined) {
  287. isIE10 = navigator.appVersion.indexOf('MSIE 10') !== -1;
  288. }
  289. return isIE10;
  290. };
  291. function getSize(axis, body, html, computedStyle) {
  292. return Math.max(body['offset' + axis], body['scroll' + axis], html['client' + axis], html['offset' + axis], html['scroll' + axis], isIE10$1() ? html['offset' + axis] + computedStyle['margin' + (axis === 'Height' ? 'Top' : 'Left')] + computedStyle['margin' + (axis === 'Height' ? 'Bottom' : 'Right')] : 0);
  293. }
  294. function getWindowSizes() {
  295. var body = window.document.body;
  296. var html = window.document.documentElement;
  297. var computedStyle = isIE10$1() && window.getComputedStyle(html);
  298. return {
  299. height: getSize('Height', body, html, computedStyle),
  300. width: getSize('Width', body, html, computedStyle)
  301. };
  302. }
  303. var classCallCheck = function (instance, Constructor) {
  304. if (!(instance instanceof Constructor)) {
  305. throw new TypeError("Cannot call a class as a function");
  306. }
  307. };
  308. var createClass = function () {
  309. function defineProperties(target, props) {
  310. for (var i = 0; i < props.length; i++) {
  311. var descriptor = props[i];
  312. descriptor.enumerable = descriptor.enumerable || false;
  313. descriptor.configurable = true;
  314. if ("value" in descriptor) descriptor.writable = true;
  315. Object.defineProperty(target, descriptor.key, descriptor);
  316. }
  317. }
  318. return function (Constructor, protoProps, staticProps) {
  319. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  320. if (staticProps) defineProperties(Constructor, staticProps);
  321. return Constructor;
  322. };
  323. }();
  324. var defineProperty = function (obj, key, value) {
  325. if (key in obj) {
  326. Object.defineProperty(obj, key, {
  327. value: value,
  328. enumerable: true,
  329. configurable: true,
  330. writable: true
  331. });
  332. } else {
  333. obj[key] = value;
  334. }
  335. return obj;
  336. };
  337. var _extends = Object.assign || function (target) {
  338. for (var i = 1; i < arguments.length; i++) {
  339. var source = arguments[i];
  340. for (var key in source) {
  341. if (Object.prototype.hasOwnProperty.call(source, key)) {
  342. target[key] = source[key];
  343. }
  344. }
  345. }
  346. return target;
  347. };
  348. /**
  349. * Given element offsets, generate an output similar to getBoundingClientRect
  350. * @method
  351. * @memberof Popper.Utils
  352. * @argument {Object} offsets
  353. * @returns {Object} ClientRect like output
  354. */
  355. function getClientRect(offsets) {
  356. return _extends({}, offsets, {
  357. right: offsets.left + offsets.width,
  358. bottom: offsets.top + offsets.height
  359. });
  360. }
  361. /**
  362. * Get bounding client rect of given element
  363. * @method
  364. * @memberof Popper.Utils
  365. * @param {HTMLElement} element
  366. * @return {Object} client rect
  367. */
  368. function getBoundingClientRect(element) {
  369. var rect = {};
  370. // IE10 10 FIX: Please, don't ask, the element isn't
  371. // considered in DOM in some circumstances...
  372. // This isn't reproducible in IE10 compatibility mode of IE11
  373. if (isIE10$1()) {
  374. try {
  375. rect = element.getBoundingClientRect();
  376. var scrollTop = getScroll(element, 'top');
  377. var scrollLeft = getScroll(element, 'left');
  378. rect.top += scrollTop;
  379. rect.left += scrollLeft;
  380. rect.bottom += scrollTop;
  381. rect.right += scrollLeft;
  382. } catch (err) { }
  383. } else {
  384. rect = element.getBoundingClientRect();
  385. }
  386. var result = {
  387. left: rect.left,
  388. top: rect.top,
  389. width: rect.right - rect.left,
  390. height: rect.bottom - rect.top
  391. };
  392. // subtract scrollbar size from sizes
  393. var sizes = element.nodeName === 'HTML' ? getWindowSizes() : {};
  394. var width = sizes.width || element.clientWidth || result.right - result.left;
  395. var height = sizes.height || element.clientHeight || result.bottom - result.top;
  396. var horizScrollbar = element.offsetWidth - width;
  397. var vertScrollbar = element.offsetHeight - height;
  398. // if an hypothetical scrollbar is detected, we must be sure it's not a `border`
  399. // we make this check conditional for performance reasons
  400. if (horizScrollbar || vertScrollbar) {
  401. var styles = getStyleComputedProperty(element);
  402. horizScrollbar -= getBordersSize(styles, 'x');
  403. vertScrollbar -= getBordersSize(styles, 'y');
  404. result.width -= horizScrollbar;
  405. result.height -= vertScrollbar;
  406. }
  407. return getClientRect(result);
  408. }
  409. function getOffsetRectRelativeToArbitraryNode(children, parent) {
  410. var isIE10 = isIE10$1();
  411. var isHTML = parent.nodeName === 'HTML';
  412. var childrenRect = getBoundingClientRect(children);
  413. var parentRect = getBoundingClientRect(parent);
  414. var scrollParent = getScrollParent(children);
  415. var styles = getStyleComputedProperty(parent);
  416. var borderTopWidth = +styles.borderTopWidth.split('px')[0];
  417. var borderLeftWidth = +styles.borderLeftWidth.split('px')[0];
  418. var offsets = getClientRect({
  419. top: childrenRect.top - parentRect.top - borderTopWidth,
  420. left: childrenRect.left - parentRect.left - borderLeftWidth,
  421. width: childrenRect.width,
  422. height: childrenRect.height
  423. });
  424. offsets.marginTop = 0;
  425. offsets.marginLeft = 0;
  426. // Subtract margins of documentElement in case it's being used as parent
  427. // we do this only on HTML because it's the only element that behaves
  428. // differently when margins are applied to it. The margins are included in
  429. // the box of the documentElement, in the other cases not.
  430. if (!isIE10 && isHTML) {
  431. var marginTop = +styles.marginTop.split('px')[0];
  432. var marginLeft = +styles.marginLeft.split('px')[0];
  433. offsets.top -= borderTopWidth - marginTop;
  434. offsets.bottom -= borderTopWidth - marginTop;
  435. offsets.left -= borderLeftWidth - marginLeft;
  436. offsets.right -= borderLeftWidth - marginLeft;
  437. // Attach marginTop and marginLeft because in some circumstances we may need them
  438. offsets.marginTop = marginTop;
  439. offsets.marginLeft = marginLeft;
  440. }
  441. if (isIE10 ? parent.contains(scrollParent) : parent === scrollParent && scrollParent.nodeName !== 'BODY') {
  442. offsets = includeScroll(offsets, parent);
  443. }
  444. return offsets;
  445. }
  446. function getViewportOffsetRectRelativeToArtbitraryNode(element) {
  447. var html = element.ownerDocument.documentElement;
  448. var relativeOffset = getOffsetRectRelativeToArbitraryNode(element, html);
  449. var width = Math.max(html.clientWidth, window.innerWidth || 0);
  450. var height = Math.max(html.clientHeight, window.innerHeight || 0);
  451. var scrollTop = getScroll(html);
  452. var scrollLeft = getScroll(html, 'left');
  453. var offset = {
  454. top: scrollTop - relativeOffset.top + relativeOffset.marginTop,
  455. left: scrollLeft - relativeOffset.left + relativeOffset.marginLeft,
  456. width: width,
  457. height: height
  458. };
  459. return getClientRect(offset);
  460. }
  461. /**
  462. * Check if the given element is fixed or is inside a fixed parent
  463. * @method
  464. * @memberof Popper.Utils
  465. * @argument {Element} element
  466. * @argument {Element} customContainer
  467. * @returns {Boolean} answer to "isFixed?"
  468. */
  469. function isFixed(element) {
  470. var nodeName = element.nodeName;
  471. if (nodeName === 'BODY' || nodeName === 'HTML') {
  472. return false;
  473. }
  474. if (getStyleComputedProperty(element, 'position') === 'fixed') {
  475. return true;
  476. }
  477. return isFixed(getParentNode(element));
  478. }
  479. /**
  480. * Computed the boundaries limits and return them
  481. * @method
  482. * @memberof Popper.Utils
  483. * @param {HTMLElement} popper
  484. * @param {HTMLElement} reference
  485. * @param {number} padding
  486. * @param {HTMLElement} boundariesElement - Element used to define the boundaries
  487. * @returns {Object} Coordinates of the boundaries
  488. */
  489. function getBoundaries(popper, reference, padding, boundariesElement) {
  490. // NOTE: 1 DOM access here
  491. var boundaries = { top: 0, left: 0 };
  492. var offsetParent = findCommonOffsetParent(popper, reference);
  493. // Handle viewport case
  494. if (boundariesElement === 'viewport') {
  495. boundaries = getViewportOffsetRectRelativeToArtbitraryNode(offsetParent);
  496. } else {
  497. // Handle other cases based on DOM element used as boundaries
  498. var boundariesNode = void 0;
  499. if (boundariesElement === 'scrollParent') {
  500. boundariesNode = getScrollParent(getParentNode(popper));
  501. if (boundariesNode.nodeName === 'BODY') {
  502. boundariesNode = popper.ownerDocument.documentElement;
  503. }
  504. } else if (boundariesElement === 'window') {
  505. boundariesNode = popper.ownerDocument.documentElement;
  506. } else {
  507. boundariesNode = boundariesElement;
  508. }
  509. var offsets = getOffsetRectRelativeToArbitraryNode(boundariesNode, offsetParent);
  510. // In case of HTML, we need a different computation
  511. if (boundariesNode.nodeName === 'HTML' && !isFixed(offsetParent)) {
  512. var _getWindowSizes = getWindowSizes(),
  513. height = _getWindowSizes.height,
  514. width = _getWindowSizes.width;
  515. boundaries.top += offsets.top - offsets.marginTop;
  516. boundaries.bottom = height + offsets.top;
  517. boundaries.left += offsets.left - offsets.marginLeft;
  518. boundaries.right = width + offsets.left;
  519. } else {
  520. // for all the other DOM elements, this one is good
  521. boundaries = offsets;
  522. }
  523. }
  524. // Add paddings
  525. boundaries.left += padding;
  526. boundaries.top += padding;
  527. boundaries.right -= padding;
  528. boundaries.bottom -= padding;
  529. return boundaries;
  530. }
  531. function getArea(_ref) {
  532. var width = _ref.width,
  533. height = _ref.height;
  534. return width * height;
  535. }
  536. /**
  537. * Utility used to transform the `auto` placement to the placement with more
  538. * available space.
  539. * @method
  540. * @memberof Popper.Utils
  541. * @argument {Object} data - The data object generated by update method
  542. * @argument {Object} options - Modifiers configuration and options
  543. * @returns {Object} The data object, properly modified
  544. */
  545. function computeAutoPlacement(placement, refRect, popper, reference, boundariesElement) {
  546. var padding = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
  547. if (placement.indexOf('auto') === -1) {
  548. return placement;
  549. }
  550. var boundaries = getBoundaries(popper, reference, padding, boundariesElement);
  551. var rects = {
  552. top: {
  553. width: boundaries.width,
  554. height: refRect.top - boundaries.top
  555. },
  556. right: {
  557. width: boundaries.right - refRect.right,
  558. height: boundaries.height
  559. },
  560. bottom: {
  561. width: boundaries.width,
  562. height: boundaries.bottom - refRect.bottom
  563. },
  564. left: {
  565. width: refRect.left - boundaries.left,
  566. height: boundaries.height
  567. }
  568. };
  569. var sortedAreas = Object.keys(rects).map(function (key) {
  570. return _extends({
  571. key: key
  572. }, rects[key], {
  573. area: getArea(rects[key])
  574. });
  575. }).sort(function (a, b) {
  576. return b.area - a.area;
  577. });
  578. var filteredAreas = sortedAreas.filter(function (_ref2) {
  579. var width = _ref2.width,
  580. height = _ref2.height;
  581. return width >= popper.clientWidth && height >= popper.clientHeight;
  582. });
  583. var computedPlacement = filteredAreas.length > 0 ? filteredAreas[0].key : sortedAreas[0].key;
  584. var variation = placement.split('-')[1];
  585. return computedPlacement + (variation ? '-' + variation : '');
  586. }
  587. /**
  588. * Get offsets to the reference element
  589. * @method
  590. * @memberof Popper.Utils
  591. * @param {Object} state
  592. * @param {Element} popper - the popper element
  593. * @param {Element} reference - the reference element (the popper will be relative to this)
  594. * @returns {Object} An object containing the offsets which will be applied to the popper
  595. */
  596. function getReferenceOffsets(state, popper, reference) {
  597. var commonOffsetParent = findCommonOffsetParent(popper, reference);
  598. return getOffsetRectRelativeToArbitraryNode(reference, commonOffsetParent);
  599. }
  600. /**
  601. * Get the outer sizes of the given element (offset size + margins)
  602. * @method
  603. * @memberof Popper.Utils
  604. * @argument {Element} element
  605. * @returns {Object} object containing width and height properties
  606. */
  607. function getOuterSizes(element) {
  608. var styles = window.getComputedStyle(element);
  609. var x = parseFloat(styles.marginTop) + parseFloat(styles.marginBottom);
  610. var y = parseFloat(styles.marginLeft) + parseFloat(styles.marginRight);
  611. var result = {
  612. width: element.offsetWidth + y,
  613. height: element.offsetHeight + x
  614. };
  615. return result;
  616. }
  617. /**
  618. * Get the opposite placement of the given one
  619. * @method
  620. * @memberof Popper.Utils
  621. * @argument {String} placement
  622. * @returns {String} flipped placement
  623. */
  624. function getOppositePlacement(placement) {
  625. var hash = { left: 'right', right: 'left', bottom: 'top', top: 'bottom' };
  626. return placement.replace(/left|right|bottom|top/g, function (matched) {
  627. return hash[matched];
  628. });
  629. }
  630. /**
  631. * Get offsets to the popper
  632. * @method
  633. * @memberof Popper.Utils
  634. * @param {Object} position - CSS position the Popper will get applied
  635. * @param {HTMLElement} popper - the popper element
  636. * @param {Object} referenceOffsets - the reference offsets (the popper will be relative to this)
  637. * @param {String} placement - one of the valid placement options
  638. * @returns {Object} popperOffsets - An object containing the offsets which will be applied to the popper
  639. */
  640. function getPopperOffsets(popper, referenceOffsets, placement) {
  641. placement = placement.split('-')[0];
  642. // Get popper node sizes
  643. var popperRect = getOuterSizes(popper);
  644. // Add position, width and height to our offsets object
  645. var popperOffsets = {
  646. width: popperRect.width,
  647. height: popperRect.height
  648. };
  649. // depending by the popper placement we have to compute its offsets slightly differently
  650. var isHoriz = ['right', 'left'].indexOf(placement) !== -1;
  651. var mainSide = isHoriz ? 'top' : 'left';
  652. var secondarySide = isHoriz ? 'left' : 'top';
  653. var measurement = isHoriz ? 'height' : 'width';
  654. var secondaryMeasurement = !isHoriz ? 'height' : 'width';
  655. popperOffsets[mainSide] = referenceOffsets[mainSide] + referenceOffsets[measurement] / 2 - popperRect[measurement] / 2;
  656. if (placement === secondarySide) {
  657. popperOffsets[secondarySide] = referenceOffsets[secondarySide] - popperRect[secondaryMeasurement];
  658. } else {
  659. popperOffsets[secondarySide] = referenceOffsets[getOppositePlacement(secondarySide)];
  660. }
  661. return popperOffsets;
  662. }
  663. /**
  664. * Mimics the `find` method of Array
  665. * @method
  666. * @memberof Popper.Utils
  667. * @argument {Array} arr
  668. * @argument prop
  669. * @argument value
  670. * @returns index or -1
  671. */
  672. function find(arr, check) {
  673. // use native find if supported
  674. if (Array.prototype.find) {
  675. return arr.find(check);
  676. }
  677. // use `filter` to obtain the same behavior of `find`
  678. return arr.filter(check)[0];
  679. }
  680. /**
  681. * Return the index of the matching object
  682. * @method
  683. * @memberof Popper.Utils
  684. * @argument {Array} arr
  685. * @argument prop
  686. * @argument value
  687. * @returns index or -1
  688. */
  689. function findIndex(arr, prop, value) {
  690. // use native findIndex if supported
  691. if (Array.prototype.findIndex) {
  692. return arr.findIndex(function (cur) {
  693. return cur[prop] === value;
  694. });
  695. }
  696. // use `find` + `indexOf` if `findIndex` isn't supported
  697. var match = find(arr, function (obj) {
  698. return obj[prop] === value;
  699. });
  700. return arr.indexOf(match);
  701. }
  702. /**
  703. * Loop trough the list of modifiers and run them in order,
  704. * each of them will then edit the data object.
  705. * @method
  706. * @memberof Popper.Utils
  707. * @param {dataObject} data
  708. * @param {Array} modifiers
  709. * @param {String} ends - Optional modifier name used as stopper
  710. * @returns {dataObject}
  711. */
  712. function runModifiers(modifiers, data, ends) {
  713. var modifiersToRun = ends === undefined ? modifiers : modifiers.slice(0, findIndex(modifiers, 'name', ends));
  714. modifiersToRun.forEach(function (modifier) {
  715. if (modifier['function']) {
  716. // eslint-disable-line dot-notation
  717. console.warn('`modifier.function` is deprecated, use `modifier.fn`!');
  718. }
  719. var fn = modifier['function'] || modifier.fn; // eslint-disable-line dot-notation
  720. if (modifier.enabled && isFunction(fn)) {
  721. // Add properties to offsets to make them a complete clientRect object
  722. // we do this before each modifier to make sure the previous one doesn't
  723. // mess with these values
  724. data.offsets.popper = getClientRect(data.offsets.popper);
  725. data.offsets.reference = getClientRect(data.offsets.reference);
  726. data = fn(data, modifier);
  727. }
  728. });
  729. return data;
  730. }
  731. /**
  732. * Updates the position of the popper, computing the new offsets and applying
  733. * the new style.<br />
  734. * Prefer `scheduleUpdate` over `update` because of performance reasons.
  735. * @method
  736. * @memberof Popper
  737. */
  738. function update() {
  739. // if popper is destroyed, don't perform any further update
  740. if (this.state.isDestroyed) {
  741. return;
  742. }
  743. var data = {
  744. instance: this,
  745. styles: {},
  746. arrowStyles: {},
  747. attributes: {},
  748. flipped: false,
  749. offsets: {}
  750. };
  751. // compute reference element offsets
  752. data.offsets.reference = getReferenceOffsets(this.state, this.popper, this.reference);
  753. // compute auto placement, store placement inside the data object,
  754. // modifiers will be able to edit `placement` if needed
  755. // and refer to originalPlacement to know the original value
  756. data.placement = computeAutoPlacement(this.options.placement, data.offsets.reference, this.popper, this.reference, this.options.modifiers.flip.boundariesElement, this.options.modifiers.flip.padding);
  757. // store the computed placement inside `originalPlacement`
  758. data.originalPlacement = data.placement;
  759. // compute the popper offsets
  760. data.offsets.popper = getPopperOffsets(this.popper, data.offsets.reference, data.placement);
  761. data.offsets.popper.position = 'absolute';
  762. // run the modifiers
  763. data = runModifiers(this.modifiers, data);
  764. // the first `update` will call `onCreate` callback
  765. // the other ones will call `onUpdate` callback
  766. if (!this.state.isCreated) {
  767. this.state.isCreated = true;
  768. this.options.onCreate(data);
  769. } else {
  770. this.options.onUpdate(data);
  771. }
  772. }
  773. /**
  774. * Helper used to know if the given modifier is enabled.
  775. * @method
  776. * @memberof Popper.Utils
  777. * @returns {Boolean}
  778. */
  779. function isModifierEnabled(modifiers, modifierName) {
  780. return modifiers.some(function (_ref) {
  781. var name = _ref.name,
  782. enabled = _ref.enabled;
  783. return enabled && name === modifierName;
  784. });
  785. }
  786. /**
  787. * Get the prefixed supported property name
  788. * @method
  789. * @memberof Popper.Utils
  790. * @argument {String} property (camelCase)
  791. * @returns {String} prefixed property (camelCase or PascalCase, depending on the vendor prefix)
  792. */
  793. function getSupportedPropertyName(property) {
  794. var prefixes = [false, 'ms', 'Webkit', 'Moz', 'O'];
  795. var upperProp = property.charAt(0).toUpperCase() + property.slice(1);
  796. for (var i = 0; i < prefixes.length - 1; i++) {
  797. var prefix = prefixes[i];
  798. var toCheck = prefix ? '' + prefix + upperProp : property;
  799. if (typeof window.document.body.style[toCheck] !== 'undefined') {
  800. return toCheck;
  801. }
  802. }
  803. return null;
  804. }
  805. /**
  806. * Destroy the popper
  807. * @method
  808. * @memberof Popper
  809. */
  810. function destroy() {
  811. this.state.isDestroyed = true;
  812. // touch DOM only if `applyStyle` modifier is enabled
  813. if (isModifierEnabled(this.modifiers, 'applyStyle')) {
  814. this.popper.removeAttribute('x-placement');
  815. this.popper.style.left = '';
  816. this.popper.style.position = '';
  817. this.popper.style.top = '';
  818. this.popper.style[getSupportedPropertyName('transform')] = '';
  819. }
  820. this.disableEventListeners();
  821. // remove the popper if user explicity asked for the deletion on destroy
  822. // do not use `remove` because IE11 doesn't support it
  823. if (this.options.removeOnDestroy) {
  824. this.popper.parentNode.removeChild(this.popper);
  825. }
  826. return this;
  827. }
  828. /**
  829. * Get the window associated with the element
  830. * @argument {Element} element
  831. * @returns {Window}
  832. */
  833. function getWindow(element) {
  834. var ownerDocument = element.ownerDocument;
  835. return ownerDocument ? ownerDocument.defaultView : window;
  836. }
  837. function attachToScrollParents(scrollParent, event, callback, scrollParents) {
  838. var isBody = scrollParent.nodeName === 'BODY';
  839. var target = isBody ? scrollParent.ownerDocument.defaultView : scrollParent;
  840. target.addEventListener(event, callback, { passive: true });
  841. if (!isBody) {
  842. attachToScrollParents(getScrollParent(target.parentNode), event, callback, scrollParents);
  843. }
  844. scrollParents.push(target);
  845. }
  846. /**
  847. * Setup needed event listeners used to update the popper position
  848. * @method
  849. * @memberof Popper.Utils
  850. * @private
  851. */
  852. function setupEventListeners(reference, options, state, updateBound) {
  853. // Resize event listener on window
  854. state.updateBound = updateBound;
  855. getWindow(reference).addEventListener('resize', state.updateBound, { passive: true });
  856. // Scroll event listener on scroll parents
  857. var scrollElement = getScrollParent(reference);
  858. attachToScrollParents(scrollElement, 'scroll', state.updateBound, state.scrollParents);
  859. state.scrollElement = scrollElement;
  860. state.eventsEnabled = true;
  861. return state;
  862. }
  863. /**
  864. * It will add resize/scroll events and start recalculating
  865. * position of the popper element when they are triggered.
  866. * @method
  867. * @memberof Popper
  868. */
  869. function enableEventListeners() {
  870. if (!this.state.eventsEnabled) {
  871. this.state = setupEventListeners(this.reference, this.options, this.state, this.scheduleUpdate);
  872. }
  873. }
  874. /**
  875. * Remove event listeners used to update the popper position
  876. * @method
  877. * @memberof Popper.Utils
  878. * @private
  879. */
  880. function removeEventListeners(reference, state) {
  881. // Remove resize event listener on window
  882. getWindow(reference).removeEventListener('resize', state.updateBound);
  883. // Remove scroll event listener on scroll parents
  884. state.scrollParents.forEach(function (target) {
  885. target.removeEventListener('scroll', state.updateBound);
  886. });
  887. // Reset state
  888. state.updateBound = null;
  889. state.scrollParents = [];
  890. state.scrollElement = null;
  891. state.eventsEnabled = false;
  892. return state;
  893. }
  894. /**
  895. * It will remove resize/scroll events and won't recalculate popper position
  896. * when they are triggered. It also won't trigger onUpdate callback anymore,
  897. * unless you call `update` method manually.
  898. * @method
  899. * @memberof Popper
  900. */
  901. function disableEventListeners() {
  902. if (this.state.eventsEnabled) {
  903. window.cancelAnimationFrame(this.scheduleUpdate);
  904. this.state = removeEventListeners(this.reference, this.state);
  905. }
  906. }
  907. /**
  908. * Tells if a given input is a number
  909. * @method
  910. * @memberof Popper.Utils
  911. * @param {*} input to check
  912. * @return {Boolean}
  913. */
  914. function isNumeric(n) {
  915. return n !== '' && !isNaN(parseFloat(n)) && isFinite(n);
  916. }
  917. /**
  918. * Set the style to the given popper
  919. * @method
  920. * @memberof Popper.Utils
  921. * @argument {Element} element - Element to apply the style to
  922. * @argument {Object} styles
  923. * Object with a list of properties and values which will be applied to the element
  924. */
  925. function setStyles(element, styles) {
  926. Object.keys(styles).forEach(function (prop) {
  927. var unit = '';
  928. // add unit if the value is numeric and is one of the following
  929. if (['width', 'height', 'top', 'right', 'bottom', 'left'].indexOf(prop) !== -1 && isNumeric(styles[prop])) {
  930. unit = 'px';
  931. }
  932. element.style[prop] = styles[prop] + unit;
  933. });
  934. }
  935. /**
  936. * Set the attributes to the given popper
  937. * @method
  938. * @memberof Popper.Utils
  939. * @argument {Element} element - Element to apply the attributes to
  940. * @argument {Object} styles
  941. * Object with a list of properties and values which will be applied to the element
  942. */
  943. function setAttributes(element, attributes) {
  944. Object.keys(attributes).forEach(function (prop) {
  945. var value = attributes[prop];
  946. if (value !== false) {
  947. element.setAttribute(prop, attributes[prop]);
  948. } else {
  949. element.removeAttribute(prop);
  950. }
  951. });
  952. }
  953. /**
  954. * @function
  955. * @memberof Modifiers
  956. * @argument {Object} data - The data object generated by `update` method
  957. * @argument {Object} data.styles - List of style properties - values to apply to popper element
  958. * @argument {Object} data.attributes - List of attribute properties - values to apply to popper element
  959. * @argument {Object} options - Modifiers configuration and options
  960. * @returns {Object} The same data object
  961. */
  962. function applyStyle(data) {
  963. // any property present in `data.styles` will be applied to the popper,
  964. // in this way we can make the 3rd party modifiers add custom styles to it
  965. // Be aware, modifiers could override the properties defined in the previous
  966. // lines of this modifier!
  967. setStyles(data.instance.popper, data.styles);
  968. // any property present in `data.attributes` will be applied to the popper,
  969. // they will be set as HTML attributes of the element
  970. setAttributes(data.instance.popper, data.attributes);
  971. // if arrowElement is defined and arrowStyles has some properties
  972. if (data.arrowElement && Object.keys(data.arrowStyles).length) {
  973. setStyles(data.arrowElement, data.arrowStyles);
  974. }
  975. return data;
  976. }
  977. /**
  978. * Set the x-placement attribute before everything else because it could be used
  979. * to add margins to the popper margins needs to be calculated to get the
  980. * correct popper offsets.
  981. * @method
  982. * @memberof Popper.modifiers
  983. * @param {HTMLElement} reference - The reference element used to position the popper
  984. * @param {HTMLElement} popper - The HTML element used as popper.
  985. * @param {Object} options - Popper.js options
  986. */
  987. function applyStyleOnLoad(reference, popper, options, modifierOptions, state) {
  988. // compute reference element offsets
  989. var referenceOffsets = getReferenceOffsets(state, popper, reference);
  990. // compute auto placement, store placement inside the data object,
  991. // modifiers will be able to edit `placement` if needed
  992. // and refer to originalPlacement to know the original value
  993. var placement = computeAutoPlacement(options.placement, referenceOffsets, popper, reference, options.modifiers.flip.boundariesElement, options.modifiers.flip.padding);
  994. popper.setAttribute('x-placement', placement);
  995. // Apply `position` to popper before anything else because
  996. // without the position applied we can't guarantee correct computations
  997. setStyles(popper, { position: 'absolute' });
  998. return options;
  999. }
  1000. /**
  1001. * @function
  1002. * @memberof Modifiers
  1003. * @argument {Object} data - The data object generated by `update` method
  1004. * @argument {Object} options - Modifiers configuration and options
  1005. * @returns {Object} The data object, properly modified
  1006. */
  1007. function computeStyle(data, options) {
  1008. var x = options.x,
  1009. y = options.y;
  1010. var popper = data.offsets.popper;
  1011. // Remove this legacy support in Popper.js v2
  1012. var legacyGpuAccelerationOption = find(data.instance.modifiers, function (modifier) {
  1013. return modifier.name === 'applyStyle';
  1014. }).gpuAcceleration;
  1015. if (legacyGpuAccelerationOption !== undefined) {
  1016. console.warn('WARNING: `gpuAcceleration` option moved to `computeStyle` modifier and will not be supported in future versions of Popper.js!');
  1017. }
  1018. var gpuAcceleration = legacyGpuAccelerationOption !== undefined ? legacyGpuAccelerationOption : options.gpuAcceleration;
  1019. var offsetParent = getOffsetParent(data.instance.popper);
  1020. var offsetParentRect = getBoundingClientRect(offsetParent);
  1021. // Styles
  1022. var styles = {
  1023. position: popper.position
  1024. };
  1025. // floor sides to avoid blurry text
  1026. var offsets = {
  1027. left: Math.floor(popper.left),
  1028. top: Math.floor(popper.top),
  1029. bottom: Math.floor(popper.bottom),
  1030. right: Math.floor(popper.right)
  1031. };
  1032. var sideA = x === 'bottom' ? 'top' : 'bottom';
  1033. var sideB = y === 'right' ? 'left' : 'right';
  1034. // if gpuAcceleration is set to `true` and transform is supported,
  1035. // we use `translate3d` to apply the position to the popper we
  1036. // automatically use the supported prefixed version if needed
  1037. var prefixedProperty = getSupportedPropertyName('transform');
  1038. // now, let's make a step back and look at this code closely (wtf?)
  1039. // If the content of the popper grows once it's been positioned, it
  1040. // may happen that the popper gets misplaced because of the new content
  1041. // overflowing its reference element
  1042. // To avoid this problem, we provide two options (x and y), which allow
  1043. // the consumer to define the offset origin.
  1044. // If we position a popper on top of a reference element, we can set
  1045. // `x` to `top` to make the popper grow towards its top instead of
  1046. // its bottom.
  1047. var left = void 0,
  1048. top = void 0;
  1049. if (sideA === 'bottom') {
  1050. top = -offsetParentRect.height + offsets.bottom;
  1051. } else {
  1052. top = offsets.top;
  1053. }
  1054. if (sideB === 'right') {
  1055. left = -offsetParentRect.width + offsets.right;
  1056. } else {
  1057. left = offsets.left;
  1058. }
  1059. if (gpuAcceleration && prefixedProperty) {
  1060. styles[prefixedProperty] = 'translate3d(' + left + 'px, ' + top + 'px, 0)';
  1061. styles[sideA] = 0;
  1062. styles[sideB] = 0;
  1063. styles.willChange = 'transform';
  1064. } else {
  1065. // othwerise, we use the standard `top`, `left`, `bottom` and `right` properties
  1066. var invertTop = sideA === 'bottom' ? -1 : 1;
  1067. var invertLeft = sideB === 'right' ? -1 : 1;
  1068. styles[sideA] = top * invertTop;
  1069. styles[sideB] = left * invertLeft;
  1070. styles.willChange = sideA + ', ' + sideB;
  1071. }
  1072. // Attributes
  1073. var attributes = {
  1074. 'x-placement': data.placement
  1075. };
  1076. // Update `data` attributes, styles and arrowStyles
  1077. data.attributes = _extends({}, attributes, data.attributes);
  1078. data.styles = _extends({}, styles, data.styles);
  1079. data.arrowStyles = _extends({}, data.offsets.arrow, data.arrowStyles);
  1080. return data;
  1081. }
  1082. /**
  1083. * Helper used to know if the given modifier depends from another one.<br />
  1084. * It checks if the needed modifier is listed and enabled.
  1085. * @method
  1086. * @memberof Popper.Utils
  1087. * @param {Array} modifiers - list of modifiers
  1088. * @param {String} requestingName - name of requesting modifier
  1089. * @param {String} requestedName - name of requested modifier
  1090. * @returns {Boolean}
  1091. */
  1092. function isModifierRequired(modifiers, requestingName, requestedName) {
  1093. var requesting = find(modifiers, function (_ref) {
  1094. var name = _ref.name;
  1095. return name === requestingName;
  1096. });
  1097. var isRequired = !!requesting && modifiers.some(function (modifier) {
  1098. return modifier.name === requestedName && modifier.enabled && modifier.order < requesting.order;
  1099. });
  1100. if (!isRequired) {
  1101. var _requesting = '`' + requestingName + '`';
  1102. var requested = '`' + requestedName + '`';
  1103. console.warn(requested + ' modifier is required by ' + _requesting + ' modifier in order to work, be sure to include it before ' + _requesting + '!');
  1104. }
  1105. return isRequired;
  1106. }
  1107. /**
  1108. * @function
  1109. * @memberof Modifiers
  1110. * @argument {Object} data - The data object generated by update method
  1111. * @argument {Object} options - Modifiers configuration and options
  1112. * @returns {Object} The data object, properly modified
  1113. */
  1114. function arrow(data, options) {
  1115. // arrow depends on keepTogether in order to work
  1116. if (!isModifierRequired(data.instance.modifiers, 'arrow', 'keepTogether')) {
  1117. return data;
  1118. }
  1119. var arrowElement = options.element;
  1120. // if arrowElement is a string, suppose it's a CSS selector
  1121. if (typeof arrowElement === 'string') {
  1122. arrowElement = data.instance.popper.querySelector(arrowElement);
  1123. // if arrowElement is not found, don't run the modifier
  1124. if (!arrowElement) {
  1125. return data;
  1126. }
  1127. } else {
  1128. // if the arrowElement isn't a query selector we must check that the
  1129. // provided DOM node is child of its popper node
  1130. if (!data.instance.popper.contains(arrowElement)) {
  1131. console.warn('WARNING: `arrow.element` must be child of its popper element!');
  1132. return data;
  1133. }
  1134. }
  1135. var placement = data.placement.split('-')[0];
  1136. var _data$offsets = data.offsets,
  1137. popper = _data$offsets.popper,
  1138. reference = _data$offsets.reference;
  1139. var isVertical = ['left', 'right'].indexOf(placement) !== -1;
  1140. var len = isVertical ? 'height' : 'width';
  1141. var sideCapitalized = isVertical ? 'Top' : 'Left';
  1142. var side = sideCapitalized.toLowerCase();
  1143. var altSide = isVertical ? 'left' : 'top';
  1144. var opSide = isVertical ? 'bottom' : 'right';
  1145. var arrowElementSize = getOuterSizes(arrowElement)[len];
  1146. //
  1147. // extends keepTogether behavior making sure the popper and its
  1148. // reference have enough pixels in conjuction
  1149. //
  1150. // top/left side
  1151. if (reference[opSide] - arrowElementSize < popper[side]) {
  1152. data.offsets.popper[side] -= popper[side] - (reference[opSide] - arrowElementSize);
  1153. }
  1154. // bottom/right side
  1155. if (reference[side] + arrowElementSize > popper[opSide]) {
  1156. data.offsets.popper[side] += reference[side] + arrowElementSize - popper[opSide];
  1157. }
  1158. // compute center of the popper
  1159. var center = reference[side] + reference[len] / 2 - arrowElementSize / 2;
  1160. // Compute the sideValue using the updated popper offsets
  1161. // take popper margin in account because we don't have this info available
  1162. var popperMarginSide = getStyleComputedProperty(data.instance.popper, 'margin' + sideCapitalized).replace('px', '');
  1163. var sideValue = center - getClientRect(data.offsets.popper)[side] - popperMarginSide;
  1164. // prevent arrowElement from being placed not contiguously to its popper
  1165. sideValue = Math.max(Math.min(popper[len] - arrowElementSize, sideValue), 0);
  1166. data.arrowElement = arrowElement;
  1167. data.offsets.arrow = {};
  1168. data.offsets.arrow[side] = Math.round(sideValue);
  1169. data.offsets.arrow[altSide] = ''; // make sure to unset any eventual altSide value from the DOM node
  1170. return data;
  1171. }
  1172. /**
  1173. * Get the opposite placement variation of the given one
  1174. * @method
  1175. * @memberof Popper.Utils
  1176. * @argument {String} placement variation
  1177. * @returns {String} flipped placement variation
  1178. */
  1179. function getOppositeVariation(variation) {
  1180. if (variation === 'end') {
  1181. return 'start';
  1182. } else if (variation === 'start') {
  1183. return 'end';
  1184. }
  1185. return variation;
  1186. }
  1187. /**
  1188. * List of accepted placements to use as values of the `placement` option.<br />
  1189. * Valid placements are:
  1190. * - `auto`
  1191. * - `top`
  1192. * - `right`
  1193. * - `bottom`
  1194. * - `left`
  1195. *
  1196. * Each placement can have a variation from this list:
  1197. * - `-start`
  1198. * - `-end`
  1199. *
  1200. * Variations are interpreted easily if you think of them as the left to right
  1201. * written languages. Horizontally (`top` and `bottom`), `start` is left and `end`
  1202. * is right.<br />
  1203. * Vertically (`left` and `right`), `start` is top and `end` is bottom.
  1204. *
  1205. * Some valid examples are:
  1206. * - `top-end` (on top of reference, right aligned)
  1207. * - `right-start` (on right of reference, top aligned)
  1208. * - `bottom` (on bottom, centered)
  1209. * - `auto-right` (on the side with more space available, alignment depends by placement)
  1210. *
  1211. * @static
  1212. * @type {Array}
  1213. * @enum {String}
  1214. * @readonly
  1215. * @method placements
  1216. * @memberof Popper
  1217. */
  1218. var placements = ['auto-start', 'auto', 'auto-end', 'top-start', 'top', 'top-end', 'right-start', 'right', 'right-end', 'bottom-end', 'bottom', 'bottom-start', 'left-end', 'left', 'left-start'];
  1219. // Get rid of `auto` `auto-start` and `auto-end`
  1220. var validPlacements = placements.slice(3);
  1221. /**
  1222. * Given an initial placement, returns all the subsequent placements
  1223. * clockwise (or counter-clockwise).
  1224. *
  1225. * @method
  1226. * @memberof Popper.Utils
  1227. * @argument {String} placement - A valid placement (it accepts variations)
  1228. * @argument {Boolean} counter - Set to true to walk the placements counterclockwise
  1229. * @returns {Array} placements including their variations
  1230. */
  1231. function clockwise(placement) {
  1232. var counter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1233. var index = validPlacements.indexOf(placement);
  1234. var arr = validPlacements.slice(index + 1).concat(validPlacements.slice(0, index));
  1235. return counter ? arr.reverse() : arr;
  1236. }
  1237. var BEHAVIORS = {
  1238. FLIP: 'flip',
  1239. CLOCKWISE: 'clockwise',
  1240. COUNTERCLOCKWISE: 'counterclockwise'
  1241. };
  1242. /**
  1243. * @function
  1244. * @memberof Modifiers
  1245. * @argument {Object} data - The data object generated by update method
  1246. * @argument {Object} options - Modifiers configuration and options
  1247. * @returns {Object} The data object, properly modified
  1248. */
  1249. function flip(data, options) {
  1250. // if `inner` modifier is enabled, we can't use the `flip` modifier
  1251. if (isModifierEnabled(data.instance.modifiers, 'inner')) {
  1252. return data;
  1253. }
  1254. if (data.flipped && data.placement === data.originalPlacement) {
  1255. // seems like flip is trying to loop, probably there's not enough space on any of the flippable sides
  1256. return data;
  1257. }
  1258. var boundaries = getBoundaries(data.instance.popper, data.instance.reference, options.padding, options.boundariesElement);
  1259. var placement = data.placement.split('-')[0];
  1260. var placementOpposite = getOppositePlacement(placement);
  1261. var variation = data.placement.split('-')[1] || '';
  1262. var flipOrder = [];
  1263. switch (options.behavior) {
  1264. case BEHAVIORS.FLIP:
  1265. flipOrder = [placement, placementOpposite];
  1266. break;
  1267. case BEHAVIORS.CLOCKWISE:
  1268. flipOrder = clockwise(placement);
  1269. break;
  1270. case BEHAVIORS.COUNTERCLOCKWISE:
  1271. flipOrder = clockwise(placement, true);
  1272. break;
  1273. default:
  1274. flipOrder = options.behavior;
  1275. }
  1276. flipOrder.forEach(function (step, index) {
  1277. if (placement !== step || flipOrder.length === index + 1) {
  1278. return data;
  1279. }
  1280. placement = data.placement.split('-')[0];
  1281. placementOpposite = getOppositePlacement(placement);
  1282. var popperOffsets = data.offsets.popper;
  1283. var refOffsets = data.offsets.reference;
  1284. // using floor because the reference offsets may contain decimals we are not going to consider here
  1285. var floor = Math.floor;
  1286. var overlapsRef = placement === 'left' && floor(popperOffsets.right) > floor(refOffsets.left) || placement === 'right' && floor(popperOffsets.left) < floor(refOffsets.right) || placement === 'top' && floor(popperOffsets.bottom) > floor(refOffsets.top) || placement === 'bottom' && floor(popperOffsets.top) < floor(refOffsets.bottom);
  1287. var overflowsLeft = floor(popperOffsets.left) < floor(boundaries.left);
  1288. var overflowsRight = floor(popperOffsets.right) > floor(boundaries.right);
  1289. var overflowsTop = floor(popperOffsets.top) < floor(boundaries.top);
  1290. var overflowsBottom = floor(popperOffsets.bottom) > floor(boundaries.bottom);
  1291. var overflowsBoundaries = placement === 'left' && overflowsLeft || placement === 'right' && overflowsRight || placement === 'top' && overflowsTop || placement === 'bottom' && overflowsBottom;
  1292. // flip the variation if required
  1293. var isVertical = ['top', 'bottom'].indexOf(placement) !== -1;
  1294. var flippedVariation = !!options.flipVariations && (isVertical && variation === 'start' && overflowsLeft || isVertical && variation === 'end' && overflowsRight || !isVertical && variation === 'start' && overflowsTop || !isVertical && variation === 'end' && overflowsBottom);
  1295. if (overlapsRef || overflowsBoundaries || flippedVariation) {
  1296. // this boolean to detect any flip loop
  1297. data.flipped = true;
  1298. if (overlapsRef || overflowsBoundaries) {
  1299. placement = flipOrder[index + 1];
  1300. }
  1301. if (flippedVariation) {
  1302. variation = getOppositeVariation(variation);
  1303. }
  1304. data.placement = placement + (variation ? '-' + variation : '');
  1305. // this object contains `position`, we want to preserve it along with
  1306. // any additional property we may add in the future
  1307. data.offsets.popper = _extends({}, data.offsets.popper, getPopperOffsets(data.instance.popper, data.offsets.reference, data.placement));
  1308. data = runModifiers(data.instance.modifiers, data, 'flip');
  1309. }
  1310. });
  1311. return data;
  1312. }
  1313. /**
  1314. * @function
  1315. * @memberof Modifiers
  1316. * @argument {Object} data - The data object generated by update method
  1317. * @argument {Object} options - Modifiers configuration and options
  1318. * @returns {Object} The data object, properly modified
  1319. */
  1320. function keepTogether(data) {
  1321. var _data$offsets = data.offsets,
  1322. popper = _data$offsets.popper,
  1323. reference = _data$offsets.reference;
  1324. var placement = data.placement.split('-')[0];
  1325. var floor = Math.floor;
  1326. var isVertical = ['top', 'bottom'].indexOf(placement) !== -1;
  1327. var side = isVertical ? 'right' : 'bottom';
  1328. var opSide = isVertical ? 'left' : 'top';
  1329. var measurement = isVertical ? 'width' : 'height';
  1330. if (popper[side] < floor(reference[opSide])) {
  1331. data.offsets.popper[opSide] = floor(reference[opSide]) - popper[measurement];
  1332. }
  1333. if (popper[opSide] > floor(reference[side])) {
  1334. data.offsets.popper[opSide] = floor(reference[side]);
  1335. }
  1336. return data;
  1337. }
  1338. /**
  1339. * Converts a string containing value + unit into a px value number
  1340. * @function
  1341. * @memberof {modifiers~offset}
  1342. * @private
  1343. * @argument {String} str - Value + unit string
  1344. * @argument {String} measurement - `height` or `width`
  1345. * @argument {Object} popperOffsets
  1346. * @argument {Object} referenceOffsets
  1347. * @returns {Number|String}
  1348. * Value in pixels, or original string if no values were extracted
  1349. */
  1350. function toValue(str, measurement, popperOffsets, referenceOffsets) {
  1351. // separate value from unit
  1352. var split = str.match(/((?:\-|\+)?\d*\.?\d*)(.*)/);
  1353. var value = +split[1];
  1354. var unit = split[2];
  1355. // If it's not a number it's an operator, I guess
  1356. if (!value) {
  1357. return str;
  1358. }
  1359. if (unit.indexOf('%') === 0) {
  1360. var element = void 0;
  1361. switch (unit) {
  1362. case '%p':
  1363. element = popperOffsets;
  1364. break;
  1365. case '%':
  1366. case '%r':
  1367. default:
  1368. element = referenceOffsets;
  1369. }
  1370. var rect = getClientRect(element);
  1371. return rect[measurement] / 100 * value;
  1372. } else if (unit === 'vh' || unit === 'vw') {
  1373. // if is a vh or vw, we calculate the size based on the viewport
  1374. var size = void 0;
  1375. if (unit === 'vh') {
  1376. size = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  1377. } else {
  1378. size = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  1379. }
  1380. return size / 100 * value;
  1381. } else {
  1382. // if is an explicit pixel unit, we get rid of the unit and keep the value
  1383. // if is an implicit unit, it's px, and we return just the value
  1384. return value;
  1385. }
  1386. }
  1387. /**
  1388. * Parse an `offset` string to extrapolate `x` and `y` numeric offsets.
  1389. * @function
  1390. * @memberof {modifiers~offset}
  1391. * @private
  1392. * @argument {String} offset
  1393. * @argument {Object} popperOffsets
  1394. * @argument {Object} referenceOffsets
  1395. * @argument {String} basePlacement
  1396. * @returns {Array} a two cells array with x and y offsets in numbers
  1397. */
  1398. function parseOffset(offset, popperOffsets, referenceOffsets, basePlacement) {
  1399. var offsets = [0, 0];
  1400. // Use height if placement is left or right and index is 0 otherwise use width
  1401. // in this way the first offset will use an axis and the second one
  1402. // will use the other one
  1403. var useHeight = ['right', 'left'].indexOf(basePlacement) !== -1;
  1404. // Split the offset string to obtain a list of values and operands
  1405. // The regex addresses values with the plus or minus sign in front (+10, -20, etc)
  1406. var fragments = offset.split(/(\+|\-)/).map(function (frag) {
  1407. return frag.trim();
  1408. });
  1409. // Detect if the offset string contains a pair of values or a single one
  1410. // they could be separated by comma or space
  1411. var divider = fragments.indexOf(find(fragments, function (frag) {
  1412. return frag.search(/,|\s/) !== -1;
  1413. }));
  1414. if (fragments[divider] && fragments[divider].indexOf(',') === -1) {
  1415. console.warn('Offsets separated by white space(s) are deprecated, use a comma (,) instead.');
  1416. }
  1417. // If divider is found, we divide the list of values and operands to divide
  1418. // them by ofset X and Y.
  1419. var splitRegex = /\s*,\s*|\s+/;
  1420. var ops = divider !== -1 ? [fragments.slice(0, divider).concat([fragments[divider].split(splitRegex)[0]]), [fragments[divider].split(splitRegex)[1]].concat(fragments.slice(divider + 1))] : [fragments];
  1421. // Convert the values with units to absolute pixels to allow our computations
  1422. ops = ops.map(function (op, index) {
  1423. // Most of the units rely on the orientation of the popper
  1424. var measurement = (index === 1 ? !useHeight : useHeight) ? 'height' : 'width';
  1425. var mergeWithPrevious = false;
  1426. return op
  1427. // This aggregates any `+` or `-` sign that aren't considered operators
  1428. // e.g.: 10 + +5 => [10, +, +5]
  1429. .reduce(function (a, b) {
  1430. if (a[a.length - 1] === '' && ['+', '-'].indexOf(b) !== -1) {
  1431. a[a.length - 1] = b;
  1432. mergeWithPrevious = true;
  1433. return a;
  1434. } else if (mergeWithPrevious) {
  1435. a[a.length - 1] += b;
  1436. mergeWithPrevious = false;
  1437. return a;
  1438. } else {
  1439. return a.concat(b);
  1440. }
  1441. }, [])
  1442. // Here we convert the string values into number values (in px)
  1443. .map(function (str) {
  1444. return toValue(str, measurement, popperOffsets, referenceOffsets);
  1445. });
  1446. });
  1447. // Loop trough the offsets arrays and execute the operations
  1448. ops.forEach(function (op, index) {
  1449. op.forEach(function (frag, index2) {
  1450. if (isNumeric(frag)) {
  1451. offsets[index] += frag * (op[index2 - 1] === '-' ? -1 : 1);
  1452. }
  1453. });
  1454. });
  1455. return offsets;
  1456. }
  1457. /**
  1458. * @function
  1459. * @memberof Modifiers
  1460. * @argument {Object} data - The data object generated by update method
  1461. * @argument {Object} options - Modifiers configuration and options
  1462. * @argument {Number|String} options.offset=0
  1463. * The offset value as described in the modifier description
  1464. * @returns {Object} The data object, properly modified
  1465. */
  1466. function offset(data, _ref) {
  1467. var offset = _ref.offset;
  1468. var placement = data.placement,
  1469. _data$offsets = data.offsets,
  1470. popper = _data$offsets.popper,
  1471. reference = _data$offsets.reference;
  1472. var basePlacement = placement.split('-')[0];
  1473. var offsets = void 0;
  1474. if (isNumeric(+offset)) {
  1475. offsets = [+offset, 0];
  1476. } else {
  1477. offsets = parseOffset(offset, popper, reference, basePlacement);
  1478. }
  1479. if (basePlacement === 'left') {
  1480. popper.top += offsets[0];
  1481. popper.left -= offsets[1];
  1482. } else if (basePlacement === 'right') {
  1483. popper.top += offsets[0];
  1484. popper.left += offsets[1];
  1485. } else if (basePlacement === 'top') {
  1486. popper.left += offsets[0];
  1487. popper.top -= offsets[1];
  1488. } else if (basePlacement === 'bottom') {
  1489. popper.left += offsets[0];
  1490. popper.top += offsets[1];
  1491. }
  1492. data.popper = popper;
  1493. return data;
  1494. }
  1495. /**
  1496. * @function
  1497. * @memberof Modifiers
  1498. * @argument {Object} data - The data object generated by `update` method
  1499. * @argument {Object} options - Modifiers configuration and options
  1500. * @returns {Object} The data object, properly modified
  1501. */
  1502. function preventOverflow(data, options) {
  1503. var boundariesElement = options.boundariesElement || getOffsetParent(data.instance.popper);
  1504. // If offsetParent is the reference element, we really want to
  1505. // go one step up and use the next offsetParent as reference to
  1506. // avoid to make this modifier completely useless and look like broken
  1507. if (data.instance.reference === boundariesElement) {
  1508. boundariesElement = getOffsetParent(boundariesElement);
  1509. }
  1510. var boundaries = getBoundaries(data.instance.popper, data.instance.reference, options.padding, boundariesElement);
  1511. options.boundaries = boundaries;
  1512. var order = options.priority;
  1513. var popper = data.offsets.popper;
  1514. var check = {
  1515. primary: function primary(placement) {
  1516. var value = popper[placement];
  1517. if (popper[placement] < boundaries[placement] && !options.escapeWithReference) {
  1518. value = Math.max(popper[placement], boundaries[placement]);
  1519. }
  1520. return defineProperty({}, placement, value);
  1521. },
  1522. secondary: function secondary(placement) {
  1523. var mainSide = placement === 'right' ? 'left' : 'top';
  1524. var value = popper[mainSide];
  1525. if (popper[placement] > boundaries[placement] && !options.escapeWithReference) {
  1526. value = Math.min(popper[mainSide], boundaries[placement] - (placement === 'right' ? popper.width : popper.height));
  1527. }
  1528. return defineProperty({}, mainSide, value);
  1529. }
  1530. };
  1531. order.forEach(function (placement) {
  1532. var side = ['left', 'top'].indexOf(placement) !== -1 ? 'primary' : 'secondary';
  1533. popper = _extends({}, popper, check[side](placement));
  1534. });
  1535. data.offsets.popper = popper;
  1536. return data;
  1537. }
  1538. /**
  1539. * @function
  1540. * @memberof Modifiers
  1541. * @argument {Object} data - The data object generated by `update` method
  1542. * @argument {Object} options - Modifiers configuration and options
  1543. * @returns {Object} The data object, properly modified
  1544. */
  1545. function shift(data) {
  1546. var placement = data.placement;
  1547. var basePlacement = placement.split('-')[0];
  1548. var shiftvariation = placement.split('-')[1];
  1549. // if shift shiftvariation is specified, run the modifier
  1550. if (shiftvariation) {
  1551. var _data$offsets = data.offsets,
  1552. reference = _data$offsets.reference,
  1553. popper = _data$offsets.popper;
  1554. var isVertical = ['bottom', 'top'].indexOf(basePlacement) !== -1;
  1555. var side = isVertical ? 'left' : 'top';
  1556. var measurement = isVertical ? 'width' : 'height';
  1557. var shiftOffsets = {
  1558. start: defineProperty({}, side, reference[side]),
  1559. end: defineProperty({}, side, reference[side] + reference[measurement] - popper[measurement])
  1560. };
  1561. data.offsets.popper = _extends({}, popper, shiftOffsets[shiftvariation]);
  1562. }
  1563. return data;
  1564. }
  1565. /**
  1566. * @function
  1567. * @memberof Modifiers
  1568. * @argument {Object} data - The data object generated by update method
  1569. * @argument {Object} options - Modifiers configuration and options
  1570. * @returns {Object} The data object, properly modified
  1571. */
  1572. function hide(data) {
  1573. if (!isModifierRequired(data.instance.modifiers, 'hide', 'preventOverflow')) {
  1574. return data;
  1575. }
  1576. var refRect = data.offsets.reference;
  1577. var bound = find(data.instance.modifiers, function (modifier) {
  1578. return modifier.name === 'preventOverflow';
  1579. }).boundaries;
  1580. if (refRect.bottom < bound.top || refRect.left > bound.right || refRect.top > bound.bottom || refRect.right < bound.left) {
  1581. // Avoid unnecessary DOM access if visibility hasn't changed
  1582. if (data.hide === true) {
  1583. return data;
  1584. }
  1585. data.hide = true;
  1586. data.attributes['x-out-of-boundaries'] = '';
  1587. } else {
  1588. // Avoid unnecessary DOM access if visibility hasn't changed
  1589. if (data.hide === false) {
  1590. return data;
  1591. }
  1592. data.hide = false;
  1593. data.attributes['x-out-of-boundaries'] = false;
  1594. }
  1595. return data;
  1596. }
  1597. /**
  1598. * @function
  1599. * @memberof Modifiers
  1600. * @argument {Object} data - The data object generated by `update` method
  1601. * @argument {Object} options - Modifiers configuration and options
  1602. * @returns {Object} The data object, properly modified
  1603. */
  1604. function inner(data) {
  1605. var placement = data.placement;
  1606. var basePlacement = placement.split('-')[0];
  1607. var _data$offsets = data.offsets,
  1608. popper = _data$offsets.popper,
  1609. reference = _data$offsets.reference;
  1610. var isHoriz = ['left', 'right'].indexOf(basePlacement) !== -1;
  1611. var subtractLength = ['top', 'left'].indexOf(basePlacement) === -1;
  1612. popper[isHoriz ? 'left' : 'top'] = reference[basePlacement] - (subtractLength ? popper[isHoriz ? 'width' : 'height'] : 0);
  1613. data.placement = getOppositePlacement(placement);
  1614. data.offsets.popper = getClientRect(popper);
  1615. return data;
  1616. }
  1617. /**
  1618. * Modifier function, each modifier can have a function of this type assigned
  1619. * to its `fn` property.<br />
  1620. * These functions will be called on each update, this means that you must
  1621. * make sure they are performant enough to avoid performance bottlenecks.
  1622. *
  1623. * @function ModifierFn
  1624. * @argument {dataObject} data - The data object generated by `update` method
  1625. * @argument {Object} options - Modifiers configuration and options
  1626. * @returns {dataObject} The data object, properly modified
  1627. */
  1628. /**
  1629. * Modifiers are plugins used to alter the behavior of your poppers.<br />
  1630. * Popper.js uses a set of 9 modifiers to provide all the basic functionalities
  1631. * needed by the library.
  1632. *
  1633. * Usually you don't want to override the `order`, `fn` and `onLoad` props.
  1634. * All the other properties are configurations that could be tweaked.
  1635. * @namespace modifiers
  1636. */
  1637. var modifiers = {
  1638. /**
  1639. * Modifier used to shift the popper on the start or end of its reference
  1640. * element.<br />
  1641. * It will read the variation of the `placement` property.<br />
  1642. * It can be one either `-end` or `-start`.
  1643. * @memberof modifiers
  1644. * @inner
  1645. */
  1646. shift: {
  1647. /** @prop {number} order=100 - Index used to define the order of execution */
  1648. order: 100,
  1649. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1650. enabled: true,
  1651. /** @prop {ModifierFn} */
  1652. fn: shift
  1653. },
  1654. /**
  1655. * The `offset` modifier can shift your popper on both its axis.
  1656. *
  1657. * It accepts the following units:
  1658. * - `px` or unitless, interpreted as pixels
  1659. * - `%` or `%r`, percentage relative to the length of the reference element
  1660. * - `%p`, percentage relative to the length of the popper element
  1661. * - `vw`, CSS viewport width unit
  1662. * - `vh`, CSS viewport height unit
  1663. *
  1664. * For length is intended the main axis relative to the placement of the popper.<br />
  1665. * This means that if the placement is `top` or `bottom`, the length will be the
  1666. * `width`. In case of `left` or `right`, it will be the height.
  1667. *
  1668. * You can provide a single value (as `Number` or `String`), or a pair of values
  1669. * as `String` divided by a comma or one (or more) white spaces.<br />
  1670. * The latter is a deprecated method because it leads to confusion and will be
  1671. * removed in v2.<br />
  1672. * Additionally, it accepts additions and subtractions between different units.
  1673. * Note that multiplications and divisions aren't supported.
  1674. *
  1675. * Valid examples are:
  1676. * ```
  1677. * 10
  1678. * '10%'
  1679. * '10, 10'
  1680. * '10%, 10'
  1681. * '10 + 10%'
  1682. * '10 - 5vh + 3%'
  1683. * '-10px + 5vh, 5px - 6%'
  1684. * ```
  1685. * > **NB**: If you desire to apply offsets to your poppers in a way that may make them overlap
  1686. * > with their reference element, unfortunately, you will have to disable the `flip` modifier.
  1687. * > More on this [reading this issue](https://github.com/FezVrasta/popper.js/issues/373)
  1688. *
  1689. * @memberof modifiers
  1690. * @inner
  1691. */
  1692. offset: {
  1693. /** @prop {number} order=200 - Index used to define the order of execution */
  1694. order: 200,
  1695. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1696. enabled: true,
  1697. /** @prop {ModifierFn} */
  1698. fn: offset,
  1699. /** @prop {Number|String} offset=0
  1700. * The offset value as described in the modifier description
  1701. */
  1702. offset: 0
  1703. },
  1704. /**
  1705. * Modifier used to prevent the popper from being positioned outside the boundary.
  1706. *
  1707. * An scenario exists where the reference itself is not within the boundaries.<br />
  1708. * We can say it has "escaped the boundaries" or just "escaped".<br />
  1709. * In this case we need to decide whether the popper should either:
  1710. *
  1711. * - detach from the reference and remain "trapped" in the boundaries, or
  1712. * - if it should ignore the boundary and "escape with its reference"
  1713. *
  1714. * When `escapeWithReference` is set to`true` and reference is completely
  1715. * outside its boundaries, the popper will overflow (or completely leave)
  1716. * the boundaries in order to remain attached to the edge of the reference.
  1717. *
  1718. * @memberof modifiers
  1719. * @inner
  1720. */
  1721. preventOverflow: {
  1722. /** @prop {number} order=300 - Index used to define the order of execution */
  1723. order: 300,
  1724. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1725. enabled: true,
  1726. /** @prop {ModifierFn} */
  1727. fn: preventOverflow,
  1728. /**
  1729. * @prop {Array} [priority=['left','right','top','bottom']]
  1730. * Popper will try to prevent overflow following these priorities by default,
  1731. * then, it could overflow on the left and on top of the `boundariesElement`
  1732. */
  1733. priority: ['left', 'right', 'top', 'bottom'],
  1734. /**
  1735. * @prop {number} padding=5
  1736. * Amount of pixel used to define a minimum distance between the boundaries
  1737. * and the popper this makes sure the popper has always a little padding
  1738. * between the edges of its container
  1739. */
  1740. padding: 5,
  1741. /**
  1742. * @prop {String|HTMLElement} boundariesElement='scrollParent'
  1743. * Boundaries used by the modifier, can be `scrollParent`, `window`,
  1744. * `viewport` or any DOM element.
  1745. */
  1746. boundariesElement: 'scrollParent'
  1747. },
  1748. /**
  1749. * Modifier used to make sure the reference and its popper stay near eachothers
  1750. * without leaving any gap between the two. Expecially useful when the arrow is
  1751. * enabled and you want to assure it to point to its reference element.
  1752. * It cares only about the first axis, you can still have poppers with margin
  1753. * between the popper and its reference element.
  1754. * @memberof modifiers
  1755. * @inner
  1756. */
  1757. keepTogether: {
  1758. /** @prop {number} order=400 - Index used to define the order of execution */
  1759. order: 400,
  1760. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1761. enabled: true,
  1762. /** @prop {ModifierFn} */
  1763. fn: keepTogether
  1764. },
  1765. /**
  1766. * This modifier is used to move the `arrowElement` of the popper to make
  1767. * sure it is positioned between the reference element and its popper element.
  1768. * It will read the outer size of the `arrowElement` node to detect how many
  1769. * pixels of conjuction are needed.
  1770. *
  1771. * It has no effect if no `arrowElement` is provided.
  1772. * @memberof modifiers
  1773. * @inner
  1774. */
  1775. arrow: {
  1776. /** @prop {number} order=500 - Index used to define the order of execution */
  1777. order: 500,
  1778. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1779. enabled: true,
  1780. /** @prop {ModifierFn} */
  1781. fn: arrow,
  1782. /** @prop {String|HTMLElement} element='[x-arrow]' - Selector or node used as arrow */
  1783. element: '[x-arrow]'
  1784. },
  1785. /**
  1786. * Modifier used to flip the popper's placement when it starts to overlap its
  1787. * reference element.
  1788. *
  1789. * Requires the `preventOverflow` modifier before it in order to work.
  1790. *
  1791. * **NOTE:** this modifier will interrupt the current update cycle and will
  1792. * restart it if it detects the need to flip the placement.
  1793. * @memberof modifiers
  1794. * @inner
  1795. */
  1796. flip: {
  1797. /** @prop {number} order=600 - Index used to define the order of execution */
  1798. order: 600,
  1799. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1800. enabled: true,
  1801. /** @prop {ModifierFn} */
  1802. fn: flip,
  1803. /**
  1804. * @prop {String|Array} behavior='flip'
  1805. * The behavior used to change the popper's placement. It can be one of
  1806. * `flip`, `clockwise`, `counterclockwise` or an array with a list of valid
  1807. * placements (with optional variations).
  1808. */
  1809. behavior: 'flip',
  1810. /**
  1811. * @prop {number} padding=5
  1812. * The popper will flip if it hits the edges of the `boundariesElement`
  1813. */
  1814. padding: 5,
  1815. /**
  1816. * @prop {String|HTMLElement} boundariesElement='viewport'
  1817. * The element which will define the boundaries of the popper position,
  1818. * the popper will never be placed outside of the defined boundaries
  1819. * (except if keepTogether is enabled)
  1820. */
  1821. boundariesElement: 'viewport'
  1822. },
  1823. /**
  1824. * Modifier used to make the popper flow toward the inner of the reference element.
  1825. * By default, when this modifier is disabled, the popper will be placed outside
  1826. * the reference element.
  1827. * @memberof modifiers
  1828. * @inner
  1829. */
  1830. inner: {
  1831. /** @prop {number} order=700 - Index used to define the order of execution */
  1832. order: 700,
  1833. /** @prop {Boolean} enabled=false - Whether the modifier is enabled or not */
  1834. enabled: false,
  1835. /** @prop {ModifierFn} */
  1836. fn: inner
  1837. },
  1838. /**
  1839. * Modifier used to hide the popper when its reference element is outside of the
  1840. * popper boundaries. It will set a `x-out-of-boundaries` attribute which can
  1841. * be used to hide with a CSS selector the popper when its reference is
  1842. * out of boundaries.
  1843. *
  1844. * Requires the `preventOverflow` modifier before it in order to work.
  1845. * @memberof modifiers
  1846. * @inner
  1847. */
  1848. hide: {
  1849. /** @prop {number} order=800 - Index used to define the order of execution */
  1850. order: 800,
  1851. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1852. enabled: true,
  1853. /** @prop {ModifierFn} */
  1854. fn: hide
  1855. },
  1856. /**
  1857. * Computes the style that will be applied to the popper element to gets
  1858. * properly positioned.
  1859. *
  1860. * Note that this modifier will not touch the DOM, it just prepares the styles
  1861. * so that `applyStyle` modifier can apply it. This separation is useful
  1862. * in case you need to replace `applyStyle` with a custom implementation.
  1863. *
  1864. * This modifier has `850` as `order` value to maintain backward compatibility
  1865. * with previous versions of Popper.js. Expect the modifiers ordering method
  1866. * to change in future major versions of the library.
  1867. *
  1868. * @memberof modifiers
  1869. * @inner
  1870. */
  1871. computeStyle: {
  1872. /** @prop {number} order=850 - Index used to define the order of execution */
  1873. order: 850,
  1874. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1875. enabled: true,
  1876. /** @prop {ModifierFn} */
  1877. fn: computeStyle,
  1878. /**
  1879. * @prop {Boolean} gpuAcceleration=true
  1880. * If true, it uses the CSS 3d transformation to position the popper.
  1881. * Otherwise, it will use the `top` and `left` properties.
  1882. */
  1883. gpuAcceleration: true,
  1884. /**
  1885. * @prop {string} [x='bottom']
  1886. * Where to anchor the X axis (`bottom` or `top`). AKA X offset origin.
  1887. * Change this if your popper should grow in a direction different from `bottom`
  1888. */
  1889. x: 'bottom',
  1890. /**
  1891. * @prop {string} [x='left']
  1892. * Where to anchor the Y axis (`left` or `right`). AKA Y offset origin.
  1893. * Change this if your popper should grow in a direction different from `right`
  1894. */
  1895. y: 'right'
  1896. },
  1897. /**
  1898. * Applies the computed styles to the popper element.
  1899. *
  1900. * All the DOM manipulations are limited to this modifier. This is useful in case
  1901. * you want to integrate Popper.js inside a framework or view library and you
  1902. * want to delegate all the DOM manipulations to it.
  1903. *
  1904. * Note that if you disable this modifier, you must make sure the popper element
  1905. * has its position set to `absolute` before Popper.js can do its work!
  1906. *
  1907. * Just disable this modifier and define you own to achieve the desired effect.
  1908. *
  1909. * @memberof modifiers
  1910. * @inner
  1911. */
  1912. applyStyle: {
  1913. /** @prop {number} order=900 - Index used to define the order of execution */
  1914. order: 900,
  1915. /** @prop {Boolean} enabled=true - Whether the modifier is enabled or not */
  1916. enabled: true,
  1917. /** @prop {ModifierFn} */
  1918. fn: applyStyle,
  1919. /** @prop {Function} */
  1920. onLoad: applyStyleOnLoad,
  1921. /**
  1922. * @deprecated since version 1.10.0, the property moved to `computeStyle` modifier
  1923. * @prop {Boolean} gpuAcceleration=true
  1924. * If true, it uses the CSS 3d transformation to position the popper.
  1925. * Otherwise, it will use the `top` and `left` properties.
  1926. */
  1927. gpuAcceleration: undefined
  1928. }
  1929. };
  1930. /**
  1931. * The `dataObject` is an object containing all the informations used by Popper.js
  1932. * this object get passed to modifiers and to the `onCreate` and `onUpdate` callbacks.
  1933. * @name dataObject
  1934. * @property {Object} data.instance The Popper.js instance
  1935. * @property {String} data.placement Placement applied to popper
  1936. * @property {String} data.originalPlacement Placement originally defined on init
  1937. * @property {Boolean} data.flipped True if popper has been flipped by flip modifier
  1938. * @property {Boolean} data.hide True if the reference element is out of boundaries, useful to know when to hide the popper.
  1939. * @property {HTMLElement} data.arrowElement Node used as arrow by arrow modifier
  1940. * @property {Object} data.styles Any CSS property defined here will be applied to the popper, it expects the JavaScript nomenclature (eg. `marginBottom`)
  1941. * @property {Object} data.arrowStyles Any CSS property defined here will be applied to the popper arrow, it expects the JavaScript nomenclature (eg. `marginBottom`)
  1942. * @property {Object} data.boundaries Offsets of the popper boundaries
  1943. * @property {Object} data.offsets The measurements of popper, reference and arrow elements.
  1944. * @property {Object} data.offsets.popper `top`, `left`, `width`, `height` values
  1945. * @property {Object} data.offsets.reference `top`, `left`, `width`, `height` values
  1946. * @property {Object} data.offsets.arrow] `top` and `left` offsets, only one of them will be different from 0
  1947. */
  1948. /**
  1949. * Default options provided to Popper.js constructor.<br />
  1950. * These can be overriden using the `options` argument of Popper.js.<br />
  1951. * To override an option, simply pass as 3rd argument an object with the same
  1952. * structure of this object, example:
  1953. * ```
  1954. * new Popper(ref, pop, {
  1955. * modifiers: {
  1956. * preventOverflow: { enabled: false }
  1957. * }
  1958. * })
  1959. * ```
  1960. * @type {Object}
  1961. * @static
  1962. * @memberof Popper
  1963. */
  1964. var Defaults = {
  1965. /**
  1966. * Popper's placement
  1967. * @prop {Popper.placements} placement='bottom'
  1968. */
  1969. placement: 'bottom',
  1970. /**
  1971. * Whether events (resize, scroll) are initially enabled
  1972. * @prop {Boolean} eventsEnabled=true
  1973. */
  1974. eventsEnabled: true,
  1975. /**
  1976. * Set to true if you want to automatically remove the popper when
  1977. * you call the `destroy` method.
  1978. * @prop {Boolean} removeOnDestroy=false
  1979. */
  1980. removeOnDestroy: false,
  1981. /**
  1982. * Callback called when the popper is created.<br />
  1983. * By default, is set to no-op.<br />
  1984. * Access Popper.js instance with `data.instance`.
  1985. * @prop {onCreate}
  1986. */
  1987. onCreate: function onCreate() { },
  1988. /**
  1989. * Callback called when the popper is updated, this callback is not called
  1990. * on the initialization/creation of the popper, but only on subsequent
  1991. * updates.<br />
  1992. * By default, is set to no-op.<br />
  1993. * Access Popper.js instance with `data.instance`.
  1994. * @prop {onUpdate}
  1995. */
  1996. onUpdate: function onUpdate() { },
  1997. /**
  1998. * List of modifiers used to modify the offsets before they are applied to the popper.
  1999. * They provide most of the functionalities of Popper.js
  2000. * @prop {modifiers}
  2001. */
  2002. modifiers: modifiers
  2003. };
  2004. /**
  2005. * @callback onCreate
  2006. * @param {dataObject} data
  2007. */
  2008. /**
  2009. * @callback onUpdate
  2010. * @param {dataObject} data
  2011. */
  2012. // Utils
  2013. // Methods
  2014. var Popper = function () {
  2015. /**
  2016. * Create a new Popper.js instance
  2017. * @class Popper
  2018. * @param {HTMLElement|referenceObject} reference - The reference element used to position the popper
  2019. * @param {HTMLElement} popper - The HTML element used as popper.
  2020. * @param {Object} options - Your custom options to override the ones defined in [Defaults](#defaults)
  2021. * @return {Object} instance - The generated Popper.js instance
  2022. */
  2023. function Popper(reference, popper) {
  2024. var _this = this;
  2025. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2026. classCallCheck(this, Popper);
  2027. this.scheduleUpdate = function () {
  2028. return requestAnimationFrame(_this.update);
  2029. };
  2030. // make update() debounced, so that it only runs at most once-per-tick
  2031. this.update = debounce(this.update.bind(this));
  2032. // with {} we create a new object with the options inside it
  2033. this.options = _extends({}, Popper.Defaults, options);
  2034. // init state
  2035. this.state = {
  2036. isDestroyed: false,
  2037. isCreated: false,
  2038. scrollParents: []
  2039. };
  2040. // get reference and popper elements (allow jQuery wrappers)
  2041. this.reference = reference && reference.jquery ? reference[0] : reference;
  2042. this.popper = popper && popper.jquery ? popper[0] : popper;
  2043. // Deep merge modifiers options
  2044. this.options.modifiers = {};
  2045. Object.keys(_extends({}, Popper.Defaults.modifiers, options.modifiers)).forEach(function (name) {
  2046. _this.options.modifiers[name] = _extends({}, Popper.Defaults.modifiers[name] || {}, options.modifiers ? options.modifiers[name] : {});
  2047. });
  2048. // Refactoring modifiers' list (Object => Array)
  2049. this.modifiers = Object.keys(this.options.modifiers).map(function (name) {
  2050. return _extends({
  2051. name: name
  2052. }, _this.options.modifiers[name]);
  2053. })
  2054. // sort the modifiers by order
  2055. .sort(function (a, b) {
  2056. return a.order - b.order;
  2057. });
  2058. // modifiers have the ability to execute arbitrary code when Popper.js get inited
  2059. // such code is executed in the same order of its modifier
  2060. // they could add new properties to their options configuration
  2061. // BE AWARE: don't add options to `options.modifiers.name` but to `modifierOptions`!
  2062. this.modifiers.forEach(function (modifierOptions) {
  2063. if (modifierOptions.enabled && isFunction(modifierOptions.onLoad)) {
  2064. modifierOptions.onLoad(_this.reference, _this.popper, _this.options, modifierOptions, _this.state);
  2065. }
  2066. });
  2067. // fire the first update to position the popper in the right place
  2068. this.update();
  2069. var eventsEnabled = this.options.eventsEnabled;
  2070. if (eventsEnabled) {
  2071. // setup event listeners, they will take care of update the position in specific situations
  2072. this.enableEventListeners();
  2073. }
  2074. this.state.eventsEnabled = eventsEnabled;
  2075. }
  2076. // We can't use class properties because they don't get listed in the
  2077. // class prototype and break stuff like Sinon stubs
  2078. createClass(Popper, [{
  2079. key: 'update',
  2080. value: function update$$1() {
  2081. return update.call(this);
  2082. }
  2083. }, {
  2084. key: 'destroy',
  2085. value: function destroy$$1() {
  2086. return destroy.call(this);
  2087. }
  2088. }, {
  2089. key: 'enableEventListeners',
  2090. value: function enableEventListeners$$1() {
  2091. return enableEventListeners.call(this);
  2092. }
  2093. }, {
  2094. key: 'disableEventListeners',
  2095. value: function disableEventListeners$$1() {
  2096. return disableEventListeners.call(this);
  2097. }
  2098. /**
  2099. * Schedule an update, it will run on the next UI update available
  2100. * @method scheduleUpdate
  2101. * @memberof Popper
  2102. */
  2103. /**
  2104. * Collection of utilities useful when writing custom modifiers.
  2105. * Starting from version 1.7, this method is available only if you
  2106. * include `popper-utils.js` before `popper.js`.
  2107. *
  2108. * **DEPRECATION**: This way to access PopperUtils is deprecated
  2109. * and will be removed in v2! Use the PopperUtils module directly instead.
  2110. * Due to the high instability of the methods contained in Utils, we can't
  2111. * guarantee them to follow semver. Use them at your own risk!
  2112. * @static
  2113. * @private
  2114. * @type {Object}
  2115. * @deprecated since version 1.8
  2116. * @member Utils
  2117. * @memberof Popper
  2118. */
  2119. }]);
  2120. return Popper;
  2121. }();
  2122. /**
  2123. * The `referenceObject` is an object that provides an interface compatible with Popper.js
  2124. * and lets you use it as replacement of a real DOM node.<br />
  2125. * You can use this method to position a popper relatively to a set of coordinates
  2126. * in case you don't have a DOM node to use as reference.
  2127. *
  2128. * ```
  2129. * new Popper(referenceObject, popperNode);
  2130. * ```
  2131. *
  2132. * NB: This feature isn't supported in Internet Explorer 10
  2133. * @name referenceObject
  2134. * @property {Function} data.getBoundingClientRect
  2135. * A function that returns a set of coordinates compatible with the native `getBoundingClientRect` method.
  2136. * @property {number} data.clientWidth
  2137. * An ES6 getter that will return the width of the virtual reference element.
  2138. * @property {number} data.clientHeight
  2139. * An ES6 getter that will return the height of the virtual reference element.
  2140. */
  2141. Popper.Utils = (typeof window !== 'undefined' ? window : global).PopperUtils;
  2142. Popper.placements = placements;
  2143. Popper.Defaults = Defaults;
  2144. return Popper;
  2145. })));
  2146. //# sourceMappingURL=popper.js.map