LaravelTest

Форк
0
/
popper-utils.js 
1151 строка · 35.8 Кб
1
/**!
2
 * @fileOverview Kickass library to create and place poppers near their reference elements.
3
 * @version 1.16.1
4
 * @license
5
 * Copyright (c) 2016 Federico Zivolo and contributors
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
(function (global, factory) {
26
	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
27
	typeof define === 'function' && define.amd ? define(['exports'], factory) :
28
	(factory((global.PopperUtils = {})));
29
}(this, (function (exports) { 'use strict';
30

31
/**
32
 * Get CSS computed property of the given element
33
 * @method
34
 * @memberof Popper.Utils
35
 * @argument {Eement} element
36
 * @argument {String} property
37
 */
38
function getStyleComputedProperty(element, property) {
39
  if (element.nodeType !== 1) {
40
    return [];
41
  }
42
  // NOTE: 1 DOM access here
43
  var window = element.ownerDocument.defaultView;
44
  var css = window.getComputedStyle(element, null);
45
  return property ? css[property] : css;
46
}
47

48
/**
49
 * Returns the parentNode or the host of the element
50
 * @method
51
 * @memberof Popper.Utils
52
 * @argument {Element} element
53
 * @returns {Element} parent
54
 */
55
function getParentNode(element) {
56
  if (element.nodeName === 'HTML') {
57
    return element;
58
  }
59
  return element.parentNode || element.host;
60
}
61

62
/**
63
 * Returns the scrolling parent of the given element
64
 * @method
65
 * @memberof Popper.Utils
66
 * @argument {Element} element
67
 * @returns {Element} scroll parent
68
 */
69
function getScrollParent(element) {
70
  // Return body, `getScroll` will take care to get the correct `scrollTop` from it
71
  if (!element) {
72
    return document.body;
73
  }
74

75
  switch (element.nodeName) {
76
    case 'HTML':
77
    case 'BODY':
78
      return element.ownerDocument.body;
79
    case '#document':
80
      return element.body;
81
  }
82

83
  // Firefox want us to check `-x` and `-y` variations as well
84

85
  var _getStyleComputedProp = getStyleComputedProperty(element),
86
      overflow = _getStyleComputedProp.overflow,
87
      overflowX = _getStyleComputedProp.overflowX,
88
      overflowY = _getStyleComputedProp.overflowY;
89

90
  if (/(auto|scroll|overlay)/.test(overflow + overflowY + overflowX)) {
91
    return element;
92
  }
93

94
  return getScrollParent(getParentNode(element));
95
}
96

97
/**
98
 * Returns the reference node of the reference object, or the reference object itself.
99
 * @method
100
 * @memberof Popper.Utils
101
 * @param {Element|Object} reference - the reference element (the popper will be relative to this)
102
 * @returns {Element} parent
103
 */
104
function getReferenceNode(reference) {
105
  return reference && reference.referenceNode ? reference.referenceNode : reference;
106
}
107

108
var isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined';
109

110
var isIE11 = isBrowser && !!(window.MSInputMethodContext && document.documentMode);
111
var isIE10 = isBrowser && /MSIE 10/.test(navigator.userAgent);
112

113
/**
114
 * Determines if the browser is Internet Explorer
115
 * @method
116
 * @memberof Popper.Utils
117
 * @param {Number} version to check
118
 * @returns {Boolean} isIE
119
 */
120
function isIE(version) {
121
  if (version === 11) {
122
    return isIE11;
123
  }
124
  if (version === 10) {
125
    return isIE10;
126
  }
127
  return isIE11 || isIE10;
128
}
129

130
/**
131
 * Returns the offset parent of the given element
132
 * @method
133
 * @memberof Popper.Utils
134
 * @argument {Element} element
135
 * @returns {Element} offset parent
136
 */
137
function getOffsetParent(element) {
138
  if (!element) {
139
    return document.documentElement;
140
  }
141

142
  var noOffsetParent = isIE(10) ? document.body : null;
143

144
  // NOTE: 1 DOM access here
145
  var offsetParent = element.offsetParent || null;
146
  // Skip hidden elements which don't have an offsetParent
147
  while (offsetParent === noOffsetParent && element.nextElementSibling) {
148
    offsetParent = (element = element.nextElementSibling).offsetParent;
149
  }
150

151
  var nodeName = offsetParent && offsetParent.nodeName;
152

153
  if (!nodeName || nodeName === 'BODY' || nodeName === 'HTML') {
154
    return element ? element.ownerDocument.documentElement : document.documentElement;
155
  }
156

157
  // .offsetParent will return the closest TH, TD or TABLE in case
158
  // no offsetParent is present, I hate this job...
159
  if (['TH', 'TD', 'TABLE'].indexOf(offsetParent.nodeName) !== -1 && getStyleComputedProperty(offsetParent, 'position') === 'static') {
160
    return getOffsetParent(offsetParent);
161
  }
162

163
  return offsetParent;
164
}
165

166
function isOffsetContainer(element) {
167
  var nodeName = element.nodeName;
168

169
  if (nodeName === 'BODY') {
170
    return false;
171
  }
172
  return nodeName === 'HTML' || getOffsetParent(element.firstElementChild) === element;
173
}
174

175
/**
176
 * Finds the root node (document, shadowDOM root) of the given element
177
 * @method
178
 * @memberof Popper.Utils
179
 * @argument {Element} node
180
 * @returns {Element} root node
181
 */
182
function getRoot(node) {
183
  if (node.parentNode !== null) {
184
    return getRoot(node.parentNode);
185
  }
186

187
  return node;
188
}
189

190
/**
191
 * Finds the offset parent common to the two provided nodes
192
 * @method
193
 * @memberof Popper.Utils
194
 * @argument {Element} element1
195
 * @argument {Element} element2
196
 * @returns {Element} common offset parent
197
 */
198
function findCommonOffsetParent(element1, element2) {
199
  // This check is needed to avoid errors in case one of the elements isn't defined for any reason
200
  if (!element1 || !element1.nodeType || !element2 || !element2.nodeType) {
201
    return document.documentElement;
202
  }
203

204
  // Here we make sure to give as "start" the element that comes first in the DOM
205
  var order = element1.compareDocumentPosition(element2) & Node.DOCUMENT_POSITION_FOLLOWING;
206
  var start = order ? element1 : element2;
207
  var end = order ? element2 : element1;
208

209
  // Get common ancestor container
210
  var range = document.createRange();
211
  range.setStart(start, 0);
212
  range.setEnd(end, 0);
213
  var commonAncestorContainer = range.commonAncestorContainer;
214

215
  // Both nodes are inside #document
216

217
  if (element1 !== commonAncestorContainer && element2 !== commonAncestorContainer || start.contains(end)) {
218
    if (isOffsetContainer(commonAncestorContainer)) {
219
      return commonAncestorContainer;
220
    }
221

222
    return getOffsetParent(commonAncestorContainer);
223
  }
224

225
  // one of the nodes is inside shadowDOM, find which one
226
  var element1root = getRoot(element1);
227
  if (element1root.host) {
228
    return findCommonOffsetParent(element1root.host, element2);
229
  } else {
230
    return findCommonOffsetParent(element1, getRoot(element2).host);
231
  }
232
}
233

234
/**
235
 * Gets the scroll value of the given element in the given side (top and left)
236
 * @method
237
 * @memberof Popper.Utils
238
 * @argument {Element} element
239
 * @argument {String} side `top` or `left`
240
 * @returns {number} amount of scrolled pixels
241
 */
242
function getScroll(element) {
243
  var side = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'top';
244

245
  var upperSide = side === 'top' ? 'scrollTop' : 'scrollLeft';
246
  var nodeName = element.nodeName;
247

248
  if (nodeName === 'BODY' || nodeName === 'HTML') {
249
    var html = element.ownerDocument.documentElement;
250
    var scrollingElement = element.ownerDocument.scrollingElement || html;
251
    return scrollingElement[upperSide];
252
  }
253

254
  return element[upperSide];
255
}
256

257
/*
258
 * Sum or subtract the element scroll values (left and top) from a given rect object
259
 * @method
260
 * @memberof Popper.Utils
261
 * @param {Object} rect - Rect object you want to change
262
 * @param {HTMLElement} element - The element from the function reads the scroll values
263
 * @param {Boolean} subtract - set to true if you want to subtract the scroll values
264
 * @return {Object} rect - The modifier rect object
265
 */
266
function includeScroll(rect, element) {
267
  var subtract = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
268

269
  var scrollTop = getScroll(element, 'top');
270
  var scrollLeft = getScroll(element, 'left');
271
  var modifier = subtract ? -1 : 1;
272
  rect.top += scrollTop * modifier;
273
  rect.bottom += scrollTop * modifier;
274
  rect.left += scrollLeft * modifier;
275
  rect.right += scrollLeft * modifier;
276
  return rect;
277
}
278

279
/*
280
 * Helper to detect borders of a given element
281
 * @method
282
 * @memberof Popper.Utils
283
 * @param {CSSStyleDeclaration} styles
284
 * Result of `getStyleComputedProperty` on the given element
285
 * @param {String} axis - `x` or `y`
286
 * @return {number} borders - The borders size of the given axis
287
 */
288

289
function getBordersSize(styles, axis) {
290
  var sideA = axis === 'x' ? 'Left' : 'Top';
291
  var sideB = sideA === 'Left' ? 'Right' : 'Bottom';
292

293
  return parseFloat(styles['border' + sideA + 'Width']) + parseFloat(styles['border' + sideB + 'Width']);
294
}
295

296
function getSize(axis, body, html, computedStyle) {
297
  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);
298
}
299

300
function getWindowSizes(document) {
301
  var body = document.body;
302
  var html = document.documentElement;
303
  var computedStyle = isIE(10) && getComputedStyle(html);
304

305
  return {
306
    height: getSize('Height', body, html, computedStyle),
307
    width: getSize('Width', body, html, computedStyle)
308
  };
309
}
310

311
var _extends = Object.assign || function (target) {
312
  for (var i = 1; i < arguments.length; i++) {
313
    var source = arguments[i];
314

315
    for (var key in source) {
316
      if (Object.prototype.hasOwnProperty.call(source, key)) {
317
        target[key] = source[key];
318
      }
319
    }
320
  }
321

322
  return target;
323
};
324

325
/**
326
 * Given element offsets, generate an output similar to getBoundingClientRect
327
 * @method
328
 * @memberof Popper.Utils
329
 * @argument {Object} offsets
330
 * @returns {Object} ClientRect like output
331
 */
332
function getClientRect(offsets) {
333
  return _extends({}, offsets, {
334
    right: offsets.left + offsets.width,
335
    bottom: offsets.top + offsets.height
336
  });
337
}
338

339
/**
340
 * Get bounding client rect of given element
341
 * @method
342
 * @memberof Popper.Utils
343
 * @param {HTMLElement} element
344
 * @return {Object} client rect
345
 */
346
function getBoundingClientRect(element) {
347
  var rect = {};
348

349
  // IE10 10 FIX: Please, don't ask, the element isn't
350
  // considered in DOM in some circumstances...
351
  // This isn't reproducible in IE10 compatibility mode of IE11
352
  try {
353
    if (isIE(10)) {
354
      rect = element.getBoundingClientRect();
355
      var scrollTop = getScroll(element, 'top');
356
      var scrollLeft = getScroll(element, 'left');
357
      rect.top += scrollTop;
358
      rect.left += scrollLeft;
359
      rect.bottom += scrollTop;
360
      rect.right += scrollLeft;
361
    } else {
362
      rect = element.getBoundingClientRect();
363
    }
364
  } catch (e) {}
365

366
  var result = {
367
    left: rect.left,
368
    top: rect.top,
369
    width: rect.right - rect.left,
370
    height: rect.bottom - rect.top
371
  };
372

373
  // subtract scrollbar size from sizes
374
  var sizes = element.nodeName === 'HTML' ? getWindowSizes(element.ownerDocument) : {};
375
  var width = sizes.width || element.clientWidth || result.width;
376
  var height = sizes.height || element.clientHeight || result.height;
377

378
  var horizScrollbar = element.offsetWidth - width;
379
  var vertScrollbar = element.offsetHeight - height;
380

381
  // if an hypothetical scrollbar is detected, we must be sure it's not a `border`
382
  // we make this check conditional for performance reasons
383
  if (horizScrollbar || vertScrollbar) {
384
    var styles = getStyleComputedProperty(element);
385
    horizScrollbar -= getBordersSize(styles, 'x');
386
    vertScrollbar -= getBordersSize(styles, 'y');
387

388
    result.width -= horizScrollbar;
389
    result.height -= vertScrollbar;
390
  }
391

392
  return getClientRect(result);
393
}
394

395
function getOffsetRectRelativeToArbitraryNode(children, parent) {
396
  var fixedPosition = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
397

398
  var isIE10 = isIE(10);
399
  var isHTML = parent.nodeName === 'HTML';
400
  var childrenRect = getBoundingClientRect(children);
401
  var parentRect = getBoundingClientRect(parent);
402
  var scrollParent = getScrollParent(children);
403

404
  var styles = getStyleComputedProperty(parent);
405
  var borderTopWidth = parseFloat(styles.borderTopWidth);
406
  var borderLeftWidth = parseFloat(styles.borderLeftWidth);
407

408
  // In cases where the parent is fixed, we must ignore negative scroll in offset calc
409
  if (fixedPosition && isHTML) {
410
    parentRect.top = Math.max(parentRect.top, 0);
411
    parentRect.left = Math.max(parentRect.left, 0);
412
  }
413
  var offsets = getClientRect({
414
    top: childrenRect.top - parentRect.top - borderTopWidth,
415
    left: childrenRect.left - parentRect.left - borderLeftWidth,
416
    width: childrenRect.width,
417
    height: childrenRect.height
418
  });
419
  offsets.marginTop = 0;
420
  offsets.marginLeft = 0;
421

422
  // Subtract margins of documentElement in case it's being used as parent
423
  // we do this only on HTML because it's the only element that behaves
424
  // differently when margins are applied to it. The margins are included in
425
  // the box of the documentElement, in the other cases not.
426
  if (!isIE10 && isHTML) {
427
    var marginTop = parseFloat(styles.marginTop);
428
    var marginLeft = parseFloat(styles.marginLeft);
429

430
    offsets.top -= borderTopWidth - marginTop;
431
    offsets.bottom -= borderTopWidth - marginTop;
432
    offsets.left -= borderLeftWidth - marginLeft;
433
    offsets.right -= borderLeftWidth - marginLeft;
434

435
    // Attach marginTop and marginLeft because in some circumstances we may need them
436
    offsets.marginTop = marginTop;
437
    offsets.marginLeft = marginLeft;
438
  }
439

440
  if (isIE10 && !fixedPosition ? parent.contains(scrollParent) : parent === scrollParent && scrollParent.nodeName !== 'BODY') {
441
    offsets = includeScroll(offsets, parent);
442
  }
443

444
  return offsets;
445
}
446

447
function getViewportOffsetRectRelativeToArtbitraryNode(element) {
448
  var excludeScroll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
449

450
  var html = element.ownerDocument.documentElement;
451
  var relativeOffset = getOffsetRectRelativeToArbitraryNode(element, html);
452
  var width = Math.max(html.clientWidth, window.innerWidth || 0);
453
  var height = Math.max(html.clientHeight, window.innerHeight || 0);
454

455
  var scrollTop = !excludeScroll ? getScroll(html) : 0;
456
  var scrollLeft = !excludeScroll ? getScroll(html, 'left') : 0;
457

458
  var offset = {
459
    top: scrollTop - relativeOffset.top + relativeOffset.marginTop,
460
    left: scrollLeft - relativeOffset.left + relativeOffset.marginLeft,
461
    width: width,
462
    height: height
463
  };
464

465
  return getClientRect(offset);
466
}
467

468
/**
469
 * Check if the given element is fixed or is inside a fixed parent
470
 * @method
471
 * @memberof Popper.Utils
472
 * @argument {Element} element
473
 * @argument {Element} customContainer
474
 * @returns {Boolean} answer to "isFixed?"
475
 */
476
function isFixed(element) {
477
  var nodeName = element.nodeName;
478
  if (nodeName === 'BODY' || nodeName === 'HTML') {
479
    return false;
480
  }
481
  if (getStyleComputedProperty(element, 'position') === 'fixed') {
482
    return true;
483
  }
484
  var parentNode = getParentNode(element);
485
  if (!parentNode) {
486
    return false;
487
  }
488
  return isFixed(parentNode);
489
}
490

491
/**
492
 * Finds the first parent of an element that has a transformed property defined
493
 * @method
494
 * @memberof Popper.Utils
495
 * @argument {Element} element
496
 * @returns {Element} first transformed parent or documentElement
497
 */
498

499
function getFixedPositionOffsetParent(element) {
500
  // This check is needed to avoid errors in case one of the elements isn't defined for any reason
501
  if (!element || !element.parentElement || isIE()) {
502
    return document.documentElement;
503
  }
504
  var el = element.parentElement;
505
  while (el && getStyleComputedProperty(el, 'transform') === 'none') {
506
    el = el.parentElement;
507
  }
508
  return el || document.documentElement;
509
}
510

511
/**
512
 * Computed the boundaries limits and return them
513
 * @method
514
 * @memberof Popper.Utils
515
 * @param {HTMLElement} popper
516
 * @param {HTMLElement} reference
517
 * @param {number} padding
518
 * @param {HTMLElement} boundariesElement - Element used to define the boundaries
519
 * @param {Boolean} fixedPosition - Is in fixed position mode
520
 * @returns {Object} Coordinates of the boundaries
521
 */
522
function getBoundaries(popper, reference, padding, boundariesElement) {
523
  var fixedPosition = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
524

525
  // NOTE: 1 DOM access here
526

527
  var boundaries = { top: 0, left: 0 };
528
  var offsetParent = fixedPosition ? getFixedPositionOffsetParent(popper) : findCommonOffsetParent(popper, getReferenceNode(reference));
529

530
  // Handle viewport case
531
  if (boundariesElement === 'viewport') {
532
    boundaries = getViewportOffsetRectRelativeToArtbitraryNode(offsetParent, fixedPosition);
533
  } else {
534
    // Handle other cases based on DOM element used as boundaries
535
    var boundariesNode = void 0;
536
    if (boundariesElement === 'scrollParent') {
537
      boundariesNode = getScrollParent(getParentNode(reference));
538
      if (boundariesNode.nodeName === 'BODY') {
539
        boundariesNode = popper.ownerDocument.documentElement;
540
      }
541
    } else if (boundariesElement === 'window') {
542
      boundariesNode = popper.ownerDocument.documentElement;
543
    } else {
544
      boundariesNode = boundariesElement;
545
    }
546

547
    var offsets = getOffsetRectRelativeToArbitraryNode(boundariesNode, offsetParent, fixedPosition);
548

549
    // In case of HTML, we need a different computation
550
    if (boundariesNode.nodeName === 'HTML' && !isFixed(offsetParent)) {
551
      var _getWindowSizes = getWindowSizes(popper.ownerDocument),
552
          height = _getWindowSizes.height,
553
          width = _getWindowSizes.width;
554

555
      boundaries.top += offsets.top - offsets.marginTop;
556
      boundaries.bottom = height + offsets.top;
557
      boundaries.left += offsets.left - offsets.marginLeft;
558
      boundaries.right = width + offsets.left;
559
    } else {
560
      // for all the other DOM elements, this one is good
561
      boundaries = offsets;
562
    }
563
  }
564

565
  // Add paddings
566
  padding = padding || 0;
567
  var isPaddingNumber = typeof padding === 'number';
568
  boundaries.left += isPaddingNumber ? padding : padding.left || 0;
569
  boundaries.top += isPaddingNumber ? padding : padding.top || 0;
570
  boundaries.right -= isPaddingNumber ? padding : padding.right || 0;
571
  boundaries.bottom -= isPaddingNumber ? padding : padding.bottom || 0;
572

573
  return boundaries;
574
}
575

576
function getArea(_ref) {
577
  var width = _ref.width,
578
      height = _ref.height;
579

580
  return width * height;
581
}
582

583
/**
584
 * Utility used to transform the `auto` placement to the placement with more
585
 * available space.
586
 * @method
587
 * @memberof Popper.Utils
588
 * @argument {Object} data - The data object generated by update method
589
 * @argument {Object} options - Modifiers configuration and options
590
 * @returns {Object} The data object, properly modified
591
 */
592
function computeAutoPlacement(placement, refRect, popper, reference, boundariesElement) {
593
  var padding = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
594

595
  if (placement.indexOf('auto') === -1) {
596
    return placement;
597
  }
598

599
  var boundaries = getBoundaries(popper, reference, padding, boundariesElement);
600

601
  var rects = {
602
    top: {
603
      width: boundaries.width,
604
      height: refRect.top - boundaries.top
605
    },
606
    right: {
607
      width: boundaries.right - refRect.right,
608
      height: boundaries.height
609
    },
610
    bottom: {
611
      width: boundaries.width,
612
      height: boundaries.bottom - refRect.bottom
613
    },
614
    left: {
615
      width: refRect.left - boundaries.left,
616
      height: boundaries.height
617
    }
618
  };
619

620
  var sortedAreas = Object.keys(rects).map(function (key) {
621
    return _extends({
622
      key: key
623
    }, rects[key], {
624
      area: getArea(rects[key])
625
    });
626
  }).sort(function (a, b) {
627
    return b.area - a.area;
628
  });
629

630
  var filteredAreas = sortedAreas.filter(function (_ref2) {
631
    var width = _ref2.width,
632
        height = _ref2.height;
633
    return width >= popper.clientWidth && height >= popper.clientHeight;
634
  });
635

636
  var computedPlacement = filteredAreas.length > 0 ? filteredAreas[0].key : sortedAreas[0].key;
637

638
  var variation = placement.split('-')[1];
639

640
  return computedPlacement + (variation ? '-' + variation : '');
641
}
642

643
var timeoutDuration = function () {
644
  var longerTimeoutBrowsers = ['Edge', 'Trident', 'Firefox'];
645
  for (var i = 0; i < longerTimeoutBrowsers.length; i += 1) {
646
    if (isBrowser && navigator.userAgent.indexOf(longerTimeoutBrowsers[i]) >= 0) {
647
      return 1;
648
    }
649
  }
650
  return 0;
651
}();
652

653
function microtaskDebounce(fn) {
654
  var called = false;
655
  return function () {
656
    if (called) {
657
      return;
658
    }
659
    called = true;
660
    window.Promise.resolve().then(function () {
661
      called = false;
662
      fn();
663
    });
664
  };
665
}
666

667
function taskDebounce(fn) {
668
  var scheduled = false;
669
  return function () {
670
    if (!scheduled) {
671
      scheduled = true;
672
      setTimeout(function () {
673
        scheduled = false;
674
        fn();
675
      }, timeoutDuration);
676
    }
677
  };
678
}
679

680
var supportsMicroTasks = isBrowser && window.Promise;
681

682
/**
683
* Create a debounced version of a method, that's asynchronously deferred
684
* but called in the minimum time possible.
685
*
686
* @method
687
* @memberof Popper.Utils
688
* @argument {Function} fn
689
* @returns {Function}
690
*/
691
var debounce = supportsMicroTasks ? microtaskDebounce : taskDebounce;
692

693
/**
694
 * Mimics the `find` method of Array
695
 * @method
696
 * @memberof Popper.Utils
697
 * @argument {Array} arr
698
 * @argument prop
699
 * @argument value
700
 * @returns index or -1
701
 */
702
function find(arr, check) {
703
  // use native find if supported
704
  if (Array.prototype.find) {
705
    return arr.find(check);
706
  }
707

708
  // use `filter` to obtain the same behavior of `find`
709
  return arr.filter(check)[0];
710
}
711

712
/**
713
 * Return the index of the matching object
714
 * @method
715
 * @memberof Popper.Utils
716
 * @argument {Array} arr
717
 * @argument prop
718
 * @argument value
719
 * @returns index or -1
720
 */
721
function findIndex(arr, prop, value) {
722
  // use native findIndex if supported
723
  if (Array.prototype.findIndex) {
724
    return arr.findIndex(function (cur) {
725
      return cur[prop] === value;
726
    });
727
  }
728

729
  // use `find` + `indexOf` if `findIndex` isn't supported
730
  var match = find(arr, function (obj) {
731
    return obj[prop] === value;
732
  });
733
  return arr.indexOf(match);
734
}
735

736
/**
737
 * Get the position of the given element, relative to its offset parent
738
 * @method
739
 * @memberof Popper.Utils
740
 * @param {Element} element
741
 * @return {Object} position - Coordinates of the element and its `scrollTop`
742
 */
743
function getOffsetRect(element) {
744
  var elementRect = void 0;
745
  if (element.nodeName === 'HTML') {
746
    var _getWindowSizes = getWindowSizes(element.ownerDocument),
747
        width = _getWindowSizes.width,
748
        height = _getWindowSizes.height;
749

750
    elementRect = {
751
      width: width,
752
      height: height,
753
      left: 0,
754
      top: 0
755
    };
756
  } else {
757
    elementRect = {
758
      width: element.offsetWidth,
759
      height: element.offsetHeight,
760
      left: element.offsetLeft,
761
      top: element.offsetTop
762
    };
763
  }
764

765
  // position
766
  return getClientRect(elementRect);
767
}
768

769
/**
770
 * Get the outer sizes of the given element (offset size + margins)
771
 * @method
772
 * @memberof Popper.Utils
773
 * @argument {Element} element
774
 * @returns {Object} object containing width and height properties
775
 */
776
function getOuterSizes(element) {
777
  var window = element.ownerDocument.defaultView;
778
  var styles = window.getComputedStyle(element);
779
  var x = parseFloat(styles.marginTop || 0) + parseFloat(styles.marginBottom || 0);
780
  var y = parseFloat(styles.marginLeft || 0) + parseFloat(styles.marginRight || 0);
781
  var result = {
782
    width: element.offsetWidth + y,
783
    height: element.offsetHeight + x
784
  };
785
  return result;
786
}
787

788
/**
789
 * Get the opposite placement of the given one
790
 * @method
791
 * @memberof Popper.Utils
792
 * @argument {String} placement
793
 * @returns {String} flipped placement
794
 */
795
function getOppositePlacement(placement) {
796
  var hash = { left: 'right', right: 'left', bottom: 'top', top: 'bottom' };
797
  return placement.replace(/left|right|bottom|top/g, function (matched) {
798
    return hash[matched];
799
  });
800
}
801

802
/**
803
 * Get offsets to the popper
804
 * @method
805
 * @memberof Popper.Utils
806
 * @param {Object} position - CSS position the Popper will get applied
807
 * @param {HTMLElement} popper - the popper element
808
 * @param {Object} referenceOffsets - the reference offsets (the popper will be relative to this)
809
 * @param {String} placement - one of the valid placement options
810
 * @returns {Object} popperOffsets - An object containing the offsets which will be applied to the popper
811
 */
812
function getPopperOffsets(popper, referenceOffsets, placement) {
813
  placement = placement.split('-')[0];
814

815
  // Get popper node sizes
816
  var popperRect = getOuterSizes(popper);
817

818
  // Add position, width and height to our offsets object
819
  var popperOffsets = {
820
    width: popperRect.width,
821
    height: popperRect.height
822
  };
823

824
  // depending by the popper placement we have to compute its offsets slightly differently
825
  var isHoriz = ['right', 'left'].indexOf(placement) !== -1;
826
  var mainSide = isHoriz ? 'top' : 'left';
827
  var secondarySide = isHoriz ? 'left' : 'top';
828
  var measurement = isHoriz ? 'height' : 'width';
829
  var secondaryMeasurement = !isHoriz ? 'height' : 'width';
830

831
  popperOffsets[mainSide] = referenceOffsets[mainSide] + referenceOffsets[measurement] / 2 - popperRect[measurement] / 2;
832
  if (placement === secondarySide) {
833
    popperOffsets[secondarySide] = referenceOffsets[secondarySide] - popperRect[secondaryMeasurement];
834
  } else {
835
    popperOffsets[secondarySide] = referenceOffsets[getOppositePlacement(secondarySide)];
836
  }
837

838
  return popperOffsets;
839
}
840

841
/**
842
 * Get offsets to the reference element
843
 * @method
844
 * @memberof Popper.Utils
845
 * @param {Object} state
846
 * @param {Element} popper - the popper element
847
 * @param {Element} reference - the reference element (the popper will be relative to this)
848
 * @param {Element} fixedPosition - is in fixed position mode
849
 * @returns {Object} An object containing the offsets which will be applied to the popper
850
 */
851
function getReferenceOffsets(state, popper, reference) {
852
  var fixedPosition = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
853

854
  var commonOffsetParent = fixedPosition ? getFixedPositionOffsetParent(popper) : findCommonOffsetParent(popper, getReferenceNode(reference));
855
  return getOffsetRectRelativeToArbitraryNode(reference, commonOffsetParent, fixedPosition);
856
}
857

858
/**
859
 * Get the prefixed supported property name
860
 * @method
861
 * @memberof Popper.Utils
862
 * @argument {String} property (camelCase)
863
 * @returns {String} prefixed property (camelCase or PascalCase, depending on the vendor prefix)
864
 */
865
function getSupportedPropertyName(property) {
866
  var prefixes = [false, 'ms', 'Webkit', 'Moz', 'O'];
867
  var upperProp = property.charAt(0).toUpperCase() + property.slice(1);
868

869
  for (var i = 0; i < prefixes.length; i++) {
870
    var prefix = prefixes[i];
871
    var toCheck = prefix ? '' + prefix + upperProp : property;
872
    if (typeof document.body.style[toCheck] !== 'undefined') {
873
      return toCheck;
874
    }
875
  }
876
  return null;
877
}
878

879
/**
880
 * Check if the given variable is a function
881
 * @method
882
 * @memberof Popper.Utils
883
 * @argument {Any} functionToCheck - variable to check
884
 * @returns {Boolean} answer to: is a function?
885
 */
886
function isFunction(functionToCheck) {
887
  var getType = {};
888
  return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
889
}
890

891
/**
892
 * Helper used to know if the given modifier is enabled.
893
 * @method
894
 * @memberof Popper.Utils
895
 * @returns {Boolean}
896
 */
897
function isModifierEnabled(modifiers, modifierName) {
898
  return modifiers.some(function (_ref) {
899
    var name = _ref.name,
900
        enabled = _ref.enabled;
901
    return enabled && name === modifierName;
902
  });
903
}
904

905
/**
906
 * Helper used to know if the given modifier depends from another one.<br />
907
 * It checks if the needed modifier is listed and enabled.
908
 * @method
909
 * @memberof Popper.Utils
910
 * @param {Array} modifiers - list of modifiers
911
 * @param {String} requestingName - name of requesting modifier
912
 * @param {String} requestedName - name of requested modifier
913
 * @returns {Boolean}
914
 */
915
function isModifierRequired(modifiers, requestingName, requestedName) {
916
  var requesting = find(modifiers, function (_ref) {
917
    var name = _ref.name;
918
    return name === requestingName;
919
  });
920

921
  var isRequired = !!requesting && modifiers.some(function (modifier) {
922
    return modifier.name === requestedName && modifier.enabled && modifier.order < requesting.order;
923
  });
924

925
  if (!isRequired) {
926
    var _requesting = '`' + requestingName + '`';
927
    var requested = '`' + requestedName + '`';
928
    console.warn(requested + ' modifier is required by ' + _requesting + ' modifier in order to work, be sure to include it before ' + _requesting + '!');
929
  }
930
  return isRequired;
931
}
932

933
/**
934
 * Tells if a given input is a number
935
 * @method
936
 * @memberof Popper.Utils
937
 * @param {*} input to check
938
 * @return {Boolean}
939
 */
940
function isNumeric(n) {
941
  return n !== '' && !isNaN(parseFloat(n)) && isFinite(n);
942
}
943

944
/**
945
 * Get the window associated with the element
946
 * @argument {Element} element
947
 * @returns {Window}
948
 */
949
function getWindow(element) {
950
  var ownerDocument = element.ownerDocument;
951
  return ownerDocument ? ownerDocument.defaultView : window;
952
}
953

954
/**
955
 * Remove event listeners used to update the popper position
956
 * @method
957
 * @memberof Popper.Utils
958
 * @private
959
 */
960
function removeEventListeners(reference, state) {
961
  // Remove resize event listener on window
962
  getWindow(reference).removeEventListener('resize', state.updateBound);
963

964
  // Remove scroll event listener on scroll parents
965
  state.scrollParents.forEach(function (target) {
966
    target.removeEventListener('scroll', state.updateBound);
967
  });
968

969
  // Reset state
970
  state.updateBound = null;
971
  state.scrollParents = [];
972
  state.scrollElement = null;
973
  state.eventsEnabled = false;
974
  return state;
975
}
976

977
/**
978
 * Loop trough the list of modifiers and run them in order,
979
 * each of them will then edit the data object.
980
 * @method
981
 * @memberof Popper.Utils
982
 * @param {dataObject} data
983
 * @param {Array} modifiers
984
 * @param {String} ends - Optional modifier name used as stopper
985
 * @returns {dataObject}
986
 */
987
function runModifiers(modifiers, data, ends) {
988
  var modifiersToRun = ends === undefined ? modifiers : modifiers.slice(0, findIndex(modifiers, 'name', ends));
989

990
  modifiersToRun.forEach(function (modifier) {
991
    if (modifier['function']) {
992
      // eslint-disable-line dot-notation
993
      console.warn('`modifier.function` is deprecated, use `modifier.fn`!');
994
    }
995
    var fn = modifier['function'] || modifier.fn; // eslint-disable-line dot-notation
996
    if (modifier.enabled && isFunction(fn)) {
997
      // Add properties to offsets to make them a complete clientRect object
998
      // we do this before each modifier to make sure the previous one doesn't
999
      // mess with these values
1000
      data.offsets.popper = getClientRect(data.offsets.popper);
1001
      data.offsets.reference = getClientRect(data.offsets.reference);
1002

1003
      data = fn(data, modifier);
1004
    }
1005
  });
1006

1007
  return data;
1008
}
1009

1010
/**
1011
 * Set the attributes to the given popper
1012
 * @method
1013
 * @memberof Popper.Utils
1014
 * @argument {Element} element - Element to apply the attributes to
1015
 * @argument {Object} styles
1016
 * Object with a list of properties and values which will be applied to the element
1017
 */
1018
function setAttributes(element, attributes) {
1019
  Object.keys(attributes).forEach(function (prop) {
1020
    var value = attributes[prop];
1021
    if (value !== false) {
1022
      element.setAttribute(prop, attributes[prop]);
1023
    } else {
1024
      element.removeAttribute(prop);
1025
    }
1026
  });
1027
}
1028

1029
/**
1030
 * Set the style to the given popper
1031
 * @method
1032
 * @memberof Popper.Utils
1033
 * @argument {Element} element - Element to apply the style to
1034
 * @argument {Object} styles
1035
 * Object with a list of properties and values which will be applied to the element
1036
 */
1037
function setStyles(element, styles) {
1038
  Object.keys(styles).forEach(function (prop) {
1039
    var unit = '';
1040
    // add unit if the value is numeric and is one of the following
1041
    if (['width', 'height', 'top', 'right', 'bottom', 'left'].indexOf(prop) !== -1 && isNumeric(styles[prop])) {
1042
      unit = 'px';
1043
    }
1044
    element.style[prop] = styles[prop] + unit;
1045
  });
1046
}
1047

1048
function attachToScrollParents(scrollParent, event, callback, scrollParents) {
1049
  var isBody = scrollParent.nodeName === 'BODY';
1050
  var target = isBody ? scrollParent.ownerDocument.defaultView : scrollParent;
1051
  target.addEventListener(event, callback, { passive: true });
1052

1053
  if (!isBody) {
1054
    attachToScrollParents(getScrollParent(target.parentNode), event, callback, scrollParents);
1055
  }
1056
  scrollParents.push(target);
1057
}
1058

1059
/**
1060
 * Setup needed event listeners used to update the popper position
1061
 * @method
1062
 * @memberof Popper.Utils
1063
 * @private
1064
 */
1065
function setupEventListeners(reference, options, state, updateBound) {
1066
  // Resize event listener on window
1067
  state.updateBound = updateBound;
1068
  getWindow(reference).addEventListener('resize', state.updateBound, { passive: true });
1069

1070
  // Scroll event listener on scroll parents
1071
  var scrollElement = getScrollParent(reference);
1072
  attachToScrollParents(scrollElement, 'scroll', state.updateBound, state.scrollParents);
1073
  state.scrollElement = scrollElement;
1074
  state.eventsEnabled = true;
1075

1076
  return state;
1077
}
1078

1079
// This is here just for backward compatibility with versions lower than v1.10.3
1080
// you should import the utilities using named exports, if you want them all use:
1081
// ```
1082
// import * as PopperUtils from 'popper-utils';
1083
// ```
1084
// The default export will be removed in the next major version.
1085
var index = {
1086
  computeAutoPlacement: computeAutoPlacement,
1087
  debounce: debounce,
1088
  findIndex: findIndex,
1089
  getBordersSize: getBordersSize,
1090
  getBoundaries: getBoundaries,
1091
  getBoundingClientRect: getBoundingClientRect,
1092
  getClientRect: getClientRect,
1093
  getOffsetParent: getOffsetParent,
1094
  getOffsetRect: getOffsetRect,
1095
  getOffsetRectRelativeToArbitraryNode: getOffsetRectRelativeToArbitraryNode,
1096
  getOuterSizes: getOuterSizes,
1097
  getParentNode: getParentNode,
1098
  getPopperOffsets: getPopperOffsets,
1099
  getReferenceOffsets: getReferenceOffsets,
1100
  getScroll: getScroll,
1101
  getScrollParent: getScrollParent,
1102
  getStyleComputedProperty: getStyleComputedProperty,
1103
  getSupportedPropertyName: getSupportedPropertyName,
1104
  getWindowSizes: getWindowSizes,
1105
  isFixed: isFixed,
1106
  isFunction: isFunction,
1107
  isModifierEnabled: isModifierEnabled,
1108
  isModifierRequired: isModifierRequired,
1109
  isNumeric: isNumeric,
1110
  removeEventListeners: removeEventListeners,
1111
  runModifiers: runModifiers,
1112
  setAttributes: setAttributes,
1113
  setStyles: setStyles,
1114
  setupEventListeners: setupEventListeners
1115
};
1116

1117
exports.computeAutoPlacement = computeAutoPlacement;
1118
exports.debounce = debounce;
1119
exports.findIndex = findIndex;
1120
exports.getBordersSize = getBordersSize;
1121
exports.getBoundaries = getBoundaries;
1122
exports.getBoundingClientRect = getBoundingClientRect;
1123
exports.getClientRect = getClientRect;
1124
exports.getOffsetParent = getOffsetParent;
1125
exports.getOffsetRect = getOffsetRect;
1126
exports.getOffsetRectRelativeToArbitraryNode = getOffsetRectRelativeToArbitraryNode;
1127
exports.getOuterSizes = getOuterSizes;
1128
exports.getParentNode = getParentNode;
1129
exports.getPopperOffsets = getPopperOffsets;
1130
exports.getReferenceOffsets = getReferenceOffsets;
1131
exports.getScroll = getScroll;
1132
exports.getScrollParent = getScrollParent;
1133
exports.getStyleComputedProperty = getStyleComputedProperty;
1134
exports.getSupportedPropertyName = getSupportedPropertyName;
1135
exports.getWindowSizes = getWindowSizes;
1136
exports.isFixed = isFixed;
1137
exports.isFunction = isFunction;
1138
exports.isModifierEnabled = isModifierEnabled;
1139
exports.isModifierRequired = isModifierRequired;
1140
exports.isNumeric = isNumeric;
1141
exports.removeEventListeners = removeEventListeners;
1142
exports.runModifiers = runModifiers;
1143
exports.setAttributes = setAttributes;
1144
exports.setStyles = setStyles;
1145
exports.setupEventListeners = setupEventListeners;
1146
exports['default'] = index;
1147

1148
Object.defineProperty(exports, '__esModule', { value: true });
1149

1150
})));
1151
//# sourceMappingURL=popper-utils.js.map
1152

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.