Personal portfolio website created with bootstrap.
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.

2448 lines
80 KiB

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