Advertisement
Guest User

Untitled

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