Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

popper.js 91KB

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