popper.js 79 KB

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