popper.js 80 KB

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