v3-popper.js 87 KB

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