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