Create main html

This commit is contained in:
2021-05-18 01:02:10 +05:00
parent 3afa9557fa
commit d771cf45e5
285 changed files with 113612 additions and 2862 deletions

View File

@@ -0,0 +1,22 @@
import placements from '../methods/placements';
// Get rid of `auto` `auto-start` and `auto-end`
const validPlacements = placements.slice(3);
/**
* Given an initial placement, returns all the subsequent placements
* clockwise (or counter-clockwise).
*
* @method
* @memberof Popper.Utils
* @argument {String} placement - A valid placement (it accepts variations)
* @argument {Boolean} counter - Set to true to walk the placements counterclockwise
* @returns {Array} placements including their variations
*/
export default function clockwise(placement, counter = false) {
const index = validPlacements.indexOf(placement);
const arr = validPlacements
.slice(index + 1)
.concat(validPlacements.slice(0, index));
return counter ? arr.reverse() : arr;
}

View File

@@ -0,0 +1,74 @@
import getBoundaries from '../utils/getBoundaries';
function getArea({ width, height }) {
return width * height;
}
/**
* Utility used to transform the `auto` placement to the placement with more
* available space.
* @method
* @memberof Popper.Utils
* @argument {Object} data - The data object generated by update method
* @argument {Object} options - Modifiers configuration and options
* @returns {Object} The data object, properly modified
*/
export default function computeAutoPlacement(
placement,
refRect,
popper,
reference,
boundariesElement,
padding = 0
) {
if (placement.indexOf('auto') === -1) {
return placement;
}
const boundaries = getBoundaries(
popper,
reference,
padding,
boundariesElement
);
const rects = {
top: {
width: boundaries.width,
height: refRect.top - boundaries.top,
},
right: {
width: boundaries.right - refRect.right,
height: boundaries.height,
},
bottom: {
width: boundaries.width,
height: boundaries.bottom - refRect.bottom,
},
left: {
width: refRect.left - boundaries.left,
height: boundaries.height,
},
};
const sortedAreas = Object.keys(rects)
.map(key => ({
key,
...rects[key],
area: getArea(rects[key]),
}))
.sort((a, b) => b.area - a.area);
const filteredAreas = sortedAreas.filter(
({ width, height }) =>
width >= popper.clientWidth && height >= popper.clientHeight
);
const computedPlacement = filteredAreas.length > 0
? filteredAreas[0].key
: sortedAreas[0].key;
const variation = placement.split('-')[1];
return computedPlacement + (variation ? `-${variation}` : '');
}

View File

@@ -0,0 +1,54 @@
import isBrowser from './isBrowser';
const timeoutDuration = (function(){
const longerTimeoutBrowsers = ['Edge', 'Trident', 'Firefox'];
for (let i = 0; i < longerTimeoutBrowsers.length; i += 1) {
if (isBrowser && navigator.userAgent.indexOf(longerTimeoutBrowsers[i]) >= 0) {
return 1;
}
}
return 0;
}());
export function microtaskDebounce(fn) {
let called = false
return () => {
if (called) {
return
}
called = true
window.Promise.resolve().then(() => {
called = false
fn()
})
}
}
export function taskDebounce(fn) {
let scheduled = false;
return () => {
if (!scheduled) {
scheduled = true;
setTimeout(() => {
scheduled = false;
fn();
}, timeoutDuration);
}
};
}
const supportsMicroTasks = isBrowser && window.Promise
/**
* Create a debounced version of a method, that's asynchronously deferred
* but called in the minimum time possible.
*
* @method
* @memberof Popper.Utils
* @argument {Function} fn
* @returns {Function}
*/
export default (supportsMicroTasks
? microtaskDebounce
: taskDebounce);

View File

@@ -0,0 +1,18 @@
/**
* Mimics the `find` method of Array
* @method
* @memberof Popper.Utils
* @argument {Array} arr
* @argument prop
* @argument value
* @returns index or -1
*/
export default function find(arr, check) {
// use native find if supported
if (Array.prototype.find) {
return arr.find(check);
}
// use `filter` to obtain the same behavior of `find`
return arr.filter(check)[0];
}

View File

@@ -0,0 +1,52 @@
import isOffsetContainer from './isOffsetContainer';
import getRoot from './getRoot';
import getOffsetParent from './getOffsetParent';
/**
* Finds the offset parent common to the two provided nodes
* @method
* @memberof Popper.Utils
* @argument {Element} element1
* @argument {Element} element2
* @returns {Element} common offset parent
*/
export default function findCommonOffsetParent(element1, element2) {
// This check is needed to avoid errors in case one of the elements isn't defined for any reason
if (!element1 || !element1.nodeType || !element2 || !element2.nodeType) {
return document.documentElement;
}
// Here we make sure to give as "start" the element that comes first in the DOM
const order =
element1.compareDocumentPosition(element2) &
Node.DOCUMENT_POSITION_FOLLOWING;
const start = order ? element1 : element2;
const end = order ? element2 : element1;
// Get common ancestor container
const range = document.createRange();
range.setStart(start, 0);
range.setEnd(end, 0);
const { commonAncestorContainer } = range;
// Both nodes are inside #document
if (
(element1 !== commonAncestorContainer &&
element2 !== commonAncestorContainer) ||
start.contains(end)
) {
if (isOffsetContainer(commonAncestorContainer)) {
return commonAncestorContainer;
}
return getOffsetParent(commonAncestorContainer);
}
// one of the nodes is inside shadowDOM, find which one
const element1root = getRoot(element1);
if (element1root.host) {
return findCommonOffsetParent(element1root.host, element2);
} else {
return findCommonOffsetParent(element1, getRoot(element2).host);
}
}

View File

@@ -0,0 +1,21 @@
import find from './find';
/**
* Return the index of the matching object
* @method
* @memberof Popper.Utils
* @argument {Array} arr
* @argument prop
* @argument value
* @returns index or -1
*/
export default function findIndex(arr, prop, value) {
// use native findIndex if supported
if (Array.prototype.findIndex) {
return arr.findIndex(cur => cur[prop] === value);
}
// use `find` + `indexOf` if `findIndex` isn't supported
const match = find(arr, obj => obj[prop] === value);
return arr.indexOf(match);
}

View File

@@ -0,0 +1,19 @@
/*
* Helper to detect borders of a given element
* @method
* @memberof Popper.Utils
* @param {CSSStyleDeclaration} styles
* Result of `getStyleComputedProperty` on the given element
* @param {String} axis - `x` or `y`
* @return {number} borders - The borders size of the given axis
*/
export default function getBordersSize(styles, axis) {
const sideA = axis === 'x' ? 'Left' : 'Top';
const sideB = sideA === 'Left' ? 'Right' : 'Bottom';
return (
parseFloat(styles[`border${sideA}Width`]) +
parseFloat(styles[`border${sideB}Width`])
);
}

View File

@@ -0,0 +1,81 @@
import getScrollParent from './getScrollParent';
import getParentNode from './getParentNode';
import getReferenceNode from './getReferenceNode';
import findCommonOffsetParent from './findCommonOffsetParent';
import getOffsetRectRelativeToArbitraryNode from './getOffsetRectRelativeToArbitraryNode';
import getViewportOffsetRectRelativeToArtbitraryNode from './getViewportOffsetRectRelativeToArtbitraryNode';
import getWindowSizes from './getWindowSizes';
import isFixed from './isFixed';
import getFixedPositionOffsetParent from './getFixedPositionOffsetParent';
/**
* Computed the boundaries limits and return them
* @method
* @memberof Popper.Utils
* @param {HTMLElement} popper
* @param {HTMLElement} reference
* @param {number} padding
* @param {HTMLElement} boundariesElement - Element used to define the boundaries
* @param {Boolean} fixedPosition - Is in fixed position mode
* @returns {Object} Coordinates of the boundaries
*/
export default function getBoundaries(
popper,
reference,
padding,
boundariesElement,
fixedPosition = false
) {
// NOTE: 1 DOM access here
let boundaries = { top: 0, left: 0 };
const offsetParent = fixedPosition ? getFixedPositionOffsetParent(popper) : findCommonOffsetParent(popper, getReferenceNode(reference));
// Handle viewport case
if (boundariesElement === 'viewport' ) {
boundaries = getViewportOffsetRectRelativeToArtbitraryNode(offsetParent, fixedPosition);
}
else {
// Handle other cases based on DOM element used as boundaries
let boundariesNode;
if (boundariesElement === 'scrollParent') {
boundariesNode = getScrollParent(getParentNode(reference));
if (boundariesNode.nodeName === 'BODY') {
boundariesNode = popper.ownerDocument.documentElement;
}
} else if (boundariesElement === 'window') {
boundariesNode = popper.ownerDocument.documentElement;
} else {
boundariesNode = boundariesElement;
}
const offsets = getOffsetRectRelativeToArbitraryNode(
boundariesNode,
offsetParent,
fixedPosition
);
// In case of HTML, we need a different computation
if (boundariesNode.nodeName === 'HTML' && !isFixed(offsetParent)) {
const { height, width } = getWindowSizes(popper.ownerDocument);
boundaries.top += offsets.top - offsets.marginTop;
boundaries.bottom = height + offsets.top;
boundaries.left += offsets.left - offsets.marginLeft;
boundaries.right = width + offsets.left;
} else {
// for all the other DOM elements, this one is good
boundaries = offsets;
}
}
// Add paddings
padding = padding || 0;
const isPaddingNumber = typeof padding === 'number';
boundaries.left += isPaddingNumber ? padding : padding.left || 0;
boundaries.top += isPaddingNumber ? padding : padding.top || 0;
boundaries.right -= isPaddingNumber ? padding : padding.right || 0;
boundaries.bottom -= isPaddingNumber ? padding : padding.bottom || 0;
return boundaries;
}

View File

@@ -0,0 +1,66 @@
import getStyleComputedProperty from './getStyleComputedProperty';
import getBordersSize from './getBordersSize';
import getWindowSizes from './getWindowSizes';
import getScroll from './getScroll';
import getClientRect from './getClientRect';
import isIE from './isIE';
/**
* Get bounding client rect of given element
* @method
* @memberof Popper.Utils
* @param {HTMLElement} element
* @return {Object} client rect
*/
export default function getBoundingClientRect(element) {
let rect = {};
// IE10 10 FIX: Please, don't ask, the element isn't
// considered in DOM in some circumstances...
// This isn't reproducible in IE10 compatibility mode of IE11
try {
if (isIE(10)) {
rect = element.getBoundingClientRect();
const scrollTop = getScroll(element, 'top');
const scrollLeft = getScroll(element, 'left');
rect.top += scrollTop;
rect.left += scrollLeft;
rect.bottom += scrollTop;
rect.right += scrollLeft;
}
else {
rect = element.getBoundingClientRect();
}
}
catch(e){}
const result = {
left: rect.left,
top: rect.top,
width: rect.right - rect.left,
height: rect.bottom - rect.top,
};
// subtract scrollbar size from sizes
const sizes = element.nodeName === 'HTML' ? getWindowSizes(element.ownerDocument) : {};
const width =
sizes.width || element.clientWidth || result.width;
const height =
sizes.height || element.clientHeight || result.height;
let horizScrollbar = element.offsetWidth - width;
let vertScrollbar = element.offsetHeight - height;
// if an hypothetical scrollbar is detected, we must be sure it's not a `border`
// we make this check conditional for performance reasons
if (horizScrollbar || vertScrollbar) {
const styles = getStyleComputedProperty(element);
horizScrollbar -= getBordersSize(styles, 'x');
vertScrollbar -= getBordersSize(styles, 'y');
result.width -= horizScrollbar;
result.height -= vertScrollbar;
}
return getClientRect(result);
}

View File

@@ -0,0 +1,14 @@
/**
* Given element offsets, generate an output similar to getBoundingClientRect
* @method
* @memberof Popper.Utils
* @argument {Object} offsets
* @returns {Object} ClientRect like output
*/
export default function getClientRect(offsets) {
return {
...offsets,
right: offsets.left + offsets.width,
bottom: offsets.top + offsets.height,
};
}

View File

@@ -0,0 +1,22 @@
import getStyleComputedProperty from './getStyleComputedProperty';
import isIE from './isIE';
/**
* Finds the first parent of an element that has a transformed property defined
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @returns {Element} first transformed parent or documentElement
*/
export default function getFixedPositionOffsetParent(element) {
// This check is needed to avoid errors in case one of the elements isn't defined for any reason
if (!element || !element.parentElement || isIE()) {
return document.documentElement;
}
let el = element.parentElement;
while (el && getStyleComputedProperty(el, 'transform') === 'none') {
el = el.parentElement;
}
return el || document.documentElement;
}

View File

@@ -0,0 +1,40 @@
import getStyleComputedProperty from './getStyleComputedProperty';
import isIE from './isIE';
/**
* Returns the offset parent of the given element
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @returns {Element} offset parent
*/
export default function getOffsetParent(element) {
if (!element) {
return document.documentElement;
}
const noOffsetParent = isIE(10) ? document.body : null;
// NOTE: 1 DOM access here
let offsetParent = element.offsetParent || null;
// Skip hidden elements which don't have an offsetParent
while (offsetParent === noOffsetParent && element.nextElementSibling) {
offsetParent = (element = element.nextElementSibling).offsetParent;
}
const nodeName = offsetParent && offsetParent.nodeName;
if (!nodeName || nodeName === 'BODY' || nodeName === 'HTML') {
return element ? element.ownerDocument.documentElement : document.documentElement;
}
// .offsetParent will return the closest TH, TD or TABLE in case
// no offsetParent is present, I hate this job...
if (
['TH', 'TD', 'TABLE'].indexOf(offsetParent.nodeName) !== -1 &&
getStyleComputedProperty(offsetParent, 'position') === 'static'
) {
return getOffsetParent(offsetParent);
}
return offsetParent;
}

View File

@@ -0,0 +1,32 @@
import getWindowSizes from './getWindowSizes';
import getClientRect from './getClientRect';
/**
* Get the position of the given element, relative to its offset parent
* @method
* @memberof Popper.Utils
* @param {Element} element
* @return {Object} position - Coordinates of the element and its `scrollTop`
*/
export default function getOffsetRect(element) {
let elementRect;
if (element.nodeName === 'HTML') {
const { width, height } = getWindowSizes(element.ownerDocument);
elementRect = {
width,
height,
left: 0,
top: 0,
};
} else {
elementRect = {
width: element.offsetWidth,
height: element.offsetHeight,
left: element.offsetLeft,
top: element.offsetTop,
};
}
// position
return getClientRect(elementRect);
}

View File

@@ -0,0 +1,60 @@
import getStyleComputedProperty from './getStyleComputedProperty';
import includeScroll from './includeScroll';
import getScrollParent from './getScrollParent';
import getBoundingClientRect from './getBoundingClientRect';
import runIsIE from './isIE';
import getClientRect from './getClientRect';
export default function getOffsetRectRelativeToArbitraryNode(children, parent, fixedPosition = false) {
const isIE10 = runIsIE(10);
const isHTML = parent.nodeName === 'HTML';
const childrenRect = getBoundingClientRect(children);
const parentRect = getBoundingClientRect(parent);
const scrollParent = getScrollParent(children);
const styles = getStyleComputedProperty(parent);
const borderTopWidth = parseFloat(styles.borderTopWidth);
const borderLeftWidth = parseFloat(styles.borderLeftWidth);
// In cases where the parent is fixed, we must ignore negative scroll in offset calc
if(fixedPosition && isHTML) {
parentRect.top = Math.max(parentRect.top, 0);
parentRect.left = Math.max(parentRect.left, 0);
}
let offsets = getClientRect({
top: childrenRect.top - parentRect.top - borderTopWidth,
left: childrenRect.left - parentRect.left - borderLeftWidth,
width: childrenRect.width,
height: childrenRect.height,
});
offsets.marginTop = 0;
offsets.marginLeft = 0;
// Subtract margins of documentElement in case it's being used as parent
// we do this only on HTML because it's the only element that behaves
// differently when margins are applied to it. The margins are included in
// the box of the documentElement, in the other cases not.
if (!isIE10 && isHTML) {
const marginTop = parseFloat(styles.marginTop);
const marginLeft = parseFloat(styles.marginLeft);
offsets.top -= borderTopWidth - marginTop;
offsets.bottom -= borderTopWidth - marginTop;
offsets.left -= borderLeftWidth - marginLeft;
offsets.right -= borderLeftWidth - marginLeft;
// Attach marginTop and marginLeft because in some circumstances we may need them
offsets.marginTop = marginTop;
offsets.marginLeft = marginLeft;
}
if (
isIE10 && !fixedPosition
? parent.contains(scrollParent)
: parent === scrollParent && scrollParent.nodeName !== 'BODY'
) {
offsets = includeScroll(offsets, parent);
}
return offsets;
}

View File

@@ -0,0 +1,11 @@
/**
* Get the opposite placement of the given one
* @method
* @memberof Popper.Utils
* @argument {String} placement
* @returns {String} flipped placement
*/
export default function getOppositePlacement(placement) {
const hash = { left: 'right', right: 'left', bottom: 'top', top: 'bottom' };
return placement.replace(/left|right|bottom|top/g, matched => hash[matched]);
}

View File

@@ -0,0 +1,15 @@
/**
* Get the opposite placement variation of the given one
* @method
* @memberof Popper.Utils
* @argument {String} placement variation
* @returns {String} flipped placement variation
*/
export default function getOppositeVariation(variation) {
if (variation === 'end') {
return 'start';
} else if (variation === 'start') {
return 'end';
}
return variation;
}

View File

@@ -0,0 +1,18 @@
/**
* Get the outer sizes of the given element (offset size + margins)
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @returns {Object} object containing width and height properties
*/
export default function getOuterSizes(element) {
const window = element.ownerDocument.defaultView;
const styles = window.getComputedStyle(element);
const x = parseFloat(styles.marginTop || 0) + parseFloat(styles.marginBottom || 0);
const y = parseFloat(styles.marginLeft || 0) + parseFloat(styles.marginRight || 0);
const result = {
width: element.offsetWidth + y,
height: element.offsetHeight + x,
};
return result;
}

View File

@@ -0,0 +1,13 @@
/**
* Returns the parentNode or the host of the element
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @returns {Element} parent
*/
export default function getParentNode(element) {
if (element.nodeName === 'HTML') {
return element;
}
return element.parentNode || element.host;
}

View File

@@ -0,0 +1,46 @@
import getOuterSizes from './getOuterSizes';
import getOppositePlacement from './getOppositePlacement';
/**
* Get offsets to the popper
* @method
* @memberof Popper.Utils
* @param {Object} position - CSS position the Popper will get applied
* @param {HTMLElement} popper - the popper element
* @param {Object} referenceOffsets - the reference offsets (the popper will be relative to this)
* @param {String} placement - one of the valid placement options
* @returns {Object} popperOffsets - An object containing the offsets which will be applied to the popper
*/
export default function getPopperOffsets(popper, referenceOffsets, placement) {
placement = placement.split('-')[0];
// Get popper node sizes
const popperRect = getOuterSizes(popper);
// Add position, width and height to our offsets object
const popperOffsets = {
width: popperRect.width,
height: popperRect.height,
};
// depending by the popper placement we have to compute its offsets slightly differently
const isHoriz = ['right', 'left'].indexOf(placement) !== -1;
const mainSide = isHoriz ? 'top' : 'left';
const secondarySide = isHoriz ? 'left' : 'top';
const measurement = isHoriz ? 'height' : 'width';
const secondaryMeasurement = !isHoriz ? 'height' : 'width';
popperOffsets[mainSide] =
referenceOffsets[mainSide] +
referenceOffsets[measurement] / 2 -
popperRect[measurement] / 2;
if (placement === secondarySide) {
popperOffsets[secondarySide] =
referenceOffsets[secondarySide] - popperRect[secondaryMeasurement];
} else {
popperOffsets[secondarySide] =
referenceOffsets[getOppositePlacement(secondarySide)];
}
return popperOffsets;
}

View File

@@ -0,0 +1,10 @@
/**
* Returns the reference node of the reference object, or the reference object itself.
* @method
* @memberof Popper.Utils
* @param {Element|Object} reference - the reference element (the popper will be relative to this)
* @returns {Element} parent
*/
export default function getReferenceNode(reference) {
return reference && reference.referenceNode ? reference.referenceNode : reference;
}

View File

@@ -0,0 +1,19 @@
import findCommonOffsetParent from './findCommonOffsetParent';
import getOffsetRectRelativeToArbitraryNode from './getOffsetRectRelativeToArbitraryNode';
import getFixedPositionOffsetParent from './getFixedPositionOffsetParent';
import getReferenceNode from './getReferenceNode';
/**
* Get offsets to the reference element
* @method
* @memberof Popper.Utils
* @param {Object} state
* @param {Element} popper - the popper element
* @param {Element} reference - the reference element (the popper will be relative to this)
* @param {Element} fixedPosition - is in fixed position mode
* @returns {Object} An object containing the offsets which will be applied to the popper
*/
export default function getReferenceOffsets(state, popper, reference, fixedPosition = null) {
const commonOffsetParent = fixedPosition ? getFixedPositionOffsetParent(popper) : findCommonOffsetParent(popper, getReferenceNode(reference));
return getOffsetRectRelativeToArbitraryNode(reference, commonOffsetParent, fixedPosition);
}

View File

@@ -0,0 +1,14 @@
/**
* Finds the root node (document, shadowDOM root) of the given element
* @method
* @memberof Popper.Utils
* @argument {Element} node
* @returns {Element} root node
*/
export default function getRoot(node) {
if (node.parentNode !== null) {
return getRoot(node.parentNode);
}
return node;
}

View File

@@ -0,0 +1,50 @@
/**
* @function
* @memberof Popper.Utils
* @argument {Object} data - The data object generated by `update` method
* @argument {Boolean} shouldRound - If the offsets should be rounded at all
* @returns {Object} The popper's position offsets rounded
*
* The tale of pixel-perfect positioning. It's still not 100% perfect, but as
* good as it can be within reason.
* Discussion here: https://github.com/FezVrasta/popper.js/pull/715
*
* Low DPI screens cause a popper to be blurry if not using full pixels (Safari
* as well on High DPI screens).
*
* Firefox prefers no rounding for positioning and does not have blurriness on
* high DPI screens.
*
* Only horizontal placement and left/right values need to be considered.
*/
export default function getRoundedOffsets(data, shouldRound) {
const { popper, reference } = data.offsets;
const { round, floor } = Math;
const noRound = v => v;
const referenceWidth = round(reference.width);
const popperWidth = round(popper.width);
const isVertical = ['left', 'right'].indexOf(data.placement) !== -1;
const isVariation = data.placement.indexOf('-') !== -1;
const sameWidthParity = referenceWidth % 2 === popperWidth % 2;
const bothOddWidth = referenceWidth % 2 === 1 && popperWidth % 2 === 1;
const horizontalToInteger = !shouldRound
? noRound
: isVertical || isVariation || sameWidthParity
? round
: floor;
const verticalToInteger = !shouldRound ? noRound : round;
return {
left: horizontalToInteger(
bothOddWidth && !isVariation && shouldRound
? popper.left - 1
: popper.left
),
top: verticalToInteger(popper.top),
bottom: verticalToInteger(popper.bottom),
right: horizontalToInteger(popper.right),
};
}

View File

@@ -0,0 +1,20 @@
/**
* Gets the scroll value of the given element in the given side (top and left)
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @argument {String} side `top` or `left`
* @returns {number} amount of scrolled pixels
*/
export default function getScroll(element, side = 'top') {
const upperSide = side === 'top' ? 'scrollTop' : 'scrollLeft';
const nodeName = element.nodeName;
if (nodeName === 'BODY' || nodeName === 'HTML') {
const html = element.ownerDocument.documentElement;
const scrollingElement = element.ownerDocument.scrollingElement || html;
return scrollingElement[upperSide];
}
return element[upperSide];
}

View File

@@ -0,0 +1,32 @@
import getStyleComputedProperty from './getStyleComputedProperty';
import getParentNode from './getParentNode';
/**
* Returns the scrolling parent of the given element
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @returns {Element} scroll parent
*/
export default function getScrollParent(element) {
// Return body, `getScroll` will take care to get the correct `scrollTop` from it
if (!element) {
return document.body
}
switch (element.nodeName) {
case 'HTML':
case 'BODY':
return element.ownerDocument.body
case '#document':
return element.body
}
// Firefox want us to check `-x` and `-y` variations as well
const { overflow, overflowX, overflowY } = getStyleComputedProperty(element);
if (/(auto|scroll|overlay)/.test(overflow + overflowY + overflowX)) {
return element;
}
return getScrollParent(getParentNode(element));
}

View File

@@ -0,0 +1,16 @@
/**
* Get CSS computed property of the given element
* @method
* @memberof Popper.Utils
* @argument {Eement} element
* @argument {String} property
*/
export default function getStyleComputedProperty(element, property) {
if (element.nodeType !== 1) {
return [];
}
// NOTE: 1 DOM access here
const window = element.ownerDocument.defaultView;
const css = window.getComputedStyle(element, null);
return property ? css[property] : css;
}

View File

@@ -0,0 +1,20 @@
/**
* Get the prefixed supported property name
* @method
* @memberof Popper.Utils
* @argument {String} property (camelCase)
* @returns {String} prefixed property (camelCase or PascalCase, depending on the vendor prefix)
*/
export default function getSupportedPropertyName(property) {
const prefixes = [false, 'ms', 'Webkit', 'Moz', 'O'];
const upperProp = property.charAt(0).toUpperCase() + property.slice(1);
for (let i = 0; i < prefixes.length; i++) {
const prefix = prefixes[i];
const toCheck = prefix ? `${prefix}${upperProp}` : property;
if (typeof document.body.style[toCheck] !== 'undefined') {
return toCheck;
}
}
return null;
}

View File

@@ -0,0 +1,22 @@
import getOffsetRectRelativeToArbitraryNode from './getOffsetRectRelativeToArbitraryNode';
import getScroll from './getScroll';
import getClientRect from './getClientRect';
export default function getViewportOffsetRectRelativeToArtbitraryNode(element, excludeScroll = false) {
const html = element.ownerDocument.documentElement;
const relativeOffset = getOffsetRectRelativeToArbitraryNode(element, html);
const width = Math.max(html.clientWidth, window.innerWidth || 0);
const height = Math.max(html.clientHeight, window.innerHeight || 0);
const scrollTop = !excludeScroll ? getScroll(html) : 0;
const scrollLeft = !excludeScroll ? getScroll(html, 'left') : 0;
const offset = {
top: scrollTop - relativeOffset.top + relativeOffset.marginTop,
left: scrollLeft - relativeOffset.left + relativeOffset.marginLeft,
width,
height,
};
return getClientRect(offset);
}

View File

@@ -0,0 +1,9 @@
/**
* Get the window associated with the element
* @argument {Element} element
* @returns {Window}
*/
export default function getWindow(element) {
const ownerDocument = element.ownerDocument;
return ownerDocument ? ownerDocument.defaultView : window;
}

View File

@@ -0,0 +1,27 @@
import isIE from './isIE';
function getSize(axis, body, html, computedStyle) {
return Math.max(
body[`offset${axis}`],
body[`scroll${axis}`],
html[`client${axis}`],
html[`offset${axis}`],
html[`scroll${axis}`],
isIE(10)
? (parseInt(html[`offset${axis}`]) +
parseInt(computedStyle[`margin${axis === 'Height' ? 'Top' : 'Left'}`]) +
parseInt(computedStyle[`margin${axis === 'Height' ? 'Bottom' : 'Right'}`]))
: 0
);
}
export default function getWindowSizes(document) {
const body = document.body;
const html = document.documentElement;
const computedStyle = isIE(10) && getComputedStyle(html);
return {
height: getSize('Height', body, html, computedStyle),
width: getSize('Width', body, html, computedStyle),
};
}

View File

@@ -0,0 +1,21 @@
import getScroll from './getScroll';
/*
* Sum or subtract the element scroll values (left and top) from a given rect object
* @method
* @memberof Popper.Utils
* @param {Object} rect - Rect object you want to change
* @param {HTMLElement} element - The element from the function reads the scroll values
* @param {Boolean} subtract - set to true if you want to subtract the scroll values
* @return {Object} rect - The modifier rect object
*/
export default function includeScroll(rect, element, subtract = false) {
const scrollTop = getScroll(element, 'top');
const scrollLeft = getScroll(element, 'left');
const modifier = subtract ? -1 : 1;
rect.top += scrollTop * modifier;
rect.bottom += scrollTop * modifier;
rect.left += scrollLeft * modifier;
rect.right += scrollLeft * modifier;
return rect;
}

View File

@@ -0,0 +1,100 @@
import computeAutoPlacement from './computeAutoPlacement';
import debounce from './debounce';
import findIndex from './findIndex';
import getBordersSize from './getBordersSize';
import getBoundaries from './getBoundaries';
import getBoundingClientRect from './getBoundingClientRect';
import getClientRect from './getClientRect';
import getOffsetParent from './getOffsetParent';
import getOffsetRect from './getOffsetRect';
import getOffsetRectRelativeToArbitraryNode from './getOffsetRectRelativeToArbitraryNode';
import getOuterSizes from './getOuterSizes';
import getParentNode from './getParentNode';
import getPopperOffsets from './getPopperOffsets';
import getReferenceOffsets from './getReferenceOffsets';
import getScroll from './getScroll';
import getScrollParent from './getScrollParent';
import getStyleComputedProperty from './getStyleComputedProperty';
import getSupportedPropertyName from './getSupportedPropertyName';
import getWindowSizes from './getWindowSizes';
import isFixed from './isFixed';
import isFunction from './isFunction';
import isModifierEnabled from './isModifierEnabled';
import isModifierRequired from './isModifierRequired';
import isNumeric from './isNumeric';
import removeEventListeners from './removeEventListeners';
import runModifiers from './runModifiers';
import setAttributes from './setAttributes';
import setStyles from './setStyles';
import setupEventListeners from './setupEventListeners';
/** @namespace Popper.Utils */
export {
computeAutoPlacement,
debounce,
findIndex,
getBordersSize,
getBoundaries,
getBoundingClientRect,
getClientRect,
getOffsetParent,
getOffsetRect,
getOffsetRectRelativeToArbitraryNode,
getOuterSizes,
getParentNode,
getPopperOffsets,
getReferenceOffsets,
getScroll,
getScrollParent,
getStyleComputedProperty,
getSupportedPropertyName,
getWindowSizes,
isFixed,
isFunction,
isModifierEnabled,
isModifierRequired,
isNumeric,
removeEventListeners,
runModifiers,
setAttributes,
setStyles,
setupEventListeners,
};
// This is here just for backward compatibility with versions lower than v1.10.3
// you should import the utilities using named exports, if you want them all use:
// ```
// import * as PopperUtils from 'popper-utils';
// ```
// The default export will be removed in the next major version.
export default {
computeAutoPlacement,
debounce,
findIndex,
getBordersSize,
getBoundaries,
getBoundingClientRect,
getClientRect,
getOffsetParent,
getOffsetRect,
getOffsetRectRelativeToArbitraryNode,
getOuterSizes,
getParentNode,
getPopperOffsets,
getReferenceOffsets,
getScroll,
getScrollParent,
getStyleComputedProperty,
getSupportedPropertyName,
getWindowSizes,
isFixed,
isFunction,
isModifierEnabled,
isModifierRequired,
isNumeric,
removeEventListeners,
runModifiers,
setAttributes,
setStyles,
setupEventListeners,
};

View File

@@ -0,0 +1 @@
export default typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined';

View File

@@ -0,0 +1,25 @@
import getStyleComputedProperty from './getStyleComputedProperty';
import getParentNode from './getParentNode';
/**
* Check if the given element is fixed or is inside a fixed parent
* @method
* @memberof Popper.Utils
* @argument {Element} element
* @argument {Element} customContainer
* @returns {Boolean} answer to "isFixed?"
*/
export default function isFixed(element) {
const nodeName = element.nodeName;
if (nodeName === 'BODY' || nodeName === 'HTML') {
return false;
}
if (getStyleComputedProperty(element, 'position') === 'fixed') {
return true;
}
const parentNode = getParentNode(element);
if (!parentNode) {
return false;
}
return isFixed(parentNode);
}

View File

@@ -0,0 +1,14 @@
/**
* Check if the given variable is a function
* @method
* @memberof Popper.Utils
* @argument {Any} functionToCheck - variable to check
* @returns {Boolean} answer to: is a function?
*/
export default function isFunction(functionToCheck) {
const getType = {};
return (
functionToCheck &&
getType.toString.call(functionToCheck) === '[object Function]'
);
}

View File

@@ -0,0 +1,21 @@
import isBrowser from './isBrowser';
const isIE11 = isBrowser && !!(window.MSInputMethodContext && document.documentMode);
const isIE10 = isBrowser && /MSIE 10/.test(navigator.userAgent);
/**
* Determines if the browser is Internet Explorer
* @method
* @memberof Popper.Utils
* @param {Number} version to check
* @returns {Boolean} isIE
*/
export default function isIE(version) {
if (version === 11) {
return isIE11;
}
if (version === 10) {
return isIE10;
}
return isIE11 || isIE10;
}

View File

@@ -0,0 +1,11 @@
/**
* Helper used to know if the given modifier is enabled.
* @method
* @memberof Popper.Utils
* @returns {Boolean}
*/
export default function isModifierEnabled(modifiers, modifierName) {
return modifiers.some(
({ name, enabled }) => enabled && name === modifierName
);
}

View File

@@ -0,0 +1,38 @@
import find from './find';
/**
* Helper used to know if the given modifier depends from another one.<br />
* It checks if the needed modifier is listed and enabled.
* @method
* @memberof Popper.Utils
* @param {Array} modifiers - list of modifiers
* @param {String} requestingName - name of requesting modifier
* @param {String} requestedName - name of requested modifier
* @returns {Boolean}
*/
export default function isModifierRequired(
modifiers,
requestingName,
requestedName
) {
const requesting = find(modifiers, ({ name }) => name === requestingName);
const isRequired =
!!requesting &&
modifiers.some(modifier => {
return (
modifier.name === requestedName &&
modifier.enabled &&
modifier.order < requesting.order
);
});
if (!isRequired) {
const requesting = `\`${requestingName}\``;
const requested = `\`${requestedName}\``;
console.warn(
`${requested} modifier is required by ${requesting} modifier in order to work, be sure to include it before ${requesting}!`
);
}
return isRequired;
}

View File

@@ -0,0 +1,10 @@
/**
* Tells if a given input is a number
* @method
* @memberof Popper.Utils
* @param {*} input to check
* @return {Boolean}
*/
export default function isNumeric(n) {
return n !== '' && !isNaN(parseFloat(n)) && isFinite(n);
}

View File

@@ -0,0 +1,11 @@
import getOffsetParent from './getOffsetParent';
export default function isOffsetContainer(element) {
const { nodeName } = element;
if (nodeName === 'BODY') {
return false;
}
return (
nodeName === 'HTML' || getOffsetParent(element.firstElementChild) === element
);
}

View File

@@ -0,0 +1,24 @@
import getWindow from './getWindow';
/**
* Remove event listeners used to update the popper position
* @method
* @memberof Popper.Utils
* @private
*/
export default function removeEventListeners(reference, state) {
// Remove resize event listener on window
getWindow(reference).removeEventListener('resize', state.updateBound);
// Remove scroll event listener on scroll parents
state.scrollParents.forEach(target => {
target.removeEventListener('scroll', state.updateBound);
});
// Reset state
state.updateBound = null;
state.scrollParents = [];
state.scrollElement = null;
state.eventsEnabled = false;
return state;
}

View File

@@ -0,0 +1,37 @@
import isFunction from './isFunction';
import findIndex from './findIndex';
import getClientRect from '../utils/getClientRect';
/**
* Loop trough the list of modifiers and run them in order,
* each of them will then edit the data object.
* @method
* @memberof Popper.Utils
* @param {dataObject} data
* @param {Array} modifiers
* @param {String} ends - Optional modifier name used as stopper
* @returns {dataObject}
*/
export default function runModifiers(modifiers, data, ends) {
const modifiersToRun = ends === undefined
? modifiers
: modifiers.slice(0, findIndex(modifiers, 'name', ends));
modifiersToRun.forEach(modifier => {
if (modifier['function']) { // eslint-disable-line dot-notation
console.warn('`modifier.function` is deprecated, use `modifier.fn`!');
}
const fn = modifier['function'] || modifier.fn; // eslint-disable-line dot-notation
if (modifier.enabled && isFunction(fn)) {
// Add properties to offsets to make them a complete clientRect object
// we do this before each modifier to make sure the previous one doesn't
// mess with these values
data.offsets.popper = getClientRect(data.offsets.popper);
data.offsets.reference = getClientRect(data.offsets.reference);
data = fn(data, modifier);
}
});
return data;
}

View File

@@ -0,0 +1,18 @@
/**
* Set the attributes to the given popper
* @method
* @memberof Popper.Utils
* @argument {Element} element - Element to apply the attributes to
* @argument {Object} styles
* Object with a list of properties and values which will be applied to the element
*/
export default function setAttributes(element, attributes) {
Object.keys(attributes).forEach(function(prop) {
const value = attributes[prop];
if (value !== false) {
element.setAttribute(prop, attributes[prop]);
} else {
element.removeAttribute(prop);
}
});
}

View File

@@ -0,0 +1,24 @@
import isNumeric from './isNumeric';
/**
* Set the style to the given popper
* @method
* @memberof Popper.Utils
* @argument {Element} element - Element to apply the style to
* @argument {Object} styles
* Object with a list of properties and values which will be applied to the element
*/
export default function setStyles(element, styles) {
Object.keys(styles).forEach(prop => {
let unit = '';
// add unit if the value is numeric and is one of the following
if (
['width', 'height', 'top', 'right', 'bottom', 'left'].indexOf(prop) !==
-1 &&
isNumeric(styles[prop])
) {
unit = 'px';
}
element.style[prop] = styles[prop] + unit;
});
}

View File

@@ -0,0 +1,48 @@
import getScrollParent from './getScrollParent';
import getWindow from './getWindow';
function attachToScrollParents(scrollParent, event, callback, scrollParents) {
const isBody = scrollParent.nodeName === 'BODY';
const target = isBody ? scrollParent.ownerDocument.defaultView : scrollParent;
target.addEventListener(event, callback, { passive: true });
if (!isBody) {
attachToScrollParents(
getScrollParent(target.parentNode),
event,
callback,
scrollParents
);
}
scrollParents.push(target);
}
/**
* Setup needed event listeners used to update the popper position
* @method
* @memberof Popper.Utils
* @private
*/
export default function setupEventListeners(
reference,
options,
state,
updateBound
) {
// Resize event listener on window
state.updateBound = updateBound;
getWindow(reference).addEventListener('resize', state.updateBound, { passive: true });
// Scroll event listener on scroll parents
const scrollElement = getScrollParent(reference);
attachToScrollParents(
scrollElement,
'scroll',
state.updateBound,
state.scrollParents
);
state.scrollElement = scrollElement;
state.eventsEnabled = true;
return state;
}