popper.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /**
  2. * @fileOverview Kickass library to create and place poppers near their reference elements.
  3. * @version {{version}}
  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. //
  26. // Cross module loader
  27. // Supported: Node, AMD, Browser globals
  28. //
  29. ;(function (root, factory) {
  30. if (typeof define === 'function' && define.amd) {
  31. // AMD. Register as an anonymous module.
  32. define(factory);
  33. } else if (typeof module === 'object' && module.exports) {
  34. // Node. Does not work with strict CommonJS, but
  35. // only CommonJS-like environments that support module.exports,
  36. // like Node.
  37. module.exports = factory();
  38. } else {
  39. // Browser globals (root is window)
  40. root.Popper = factory();
  41. }
  42. }(this, function () {
  43. 'use strict';
  44. var root = window;
  45. // default options
  46. var DEFAULTS = {
  47. // placement of the popper
  48. placement: 'bottom',
  49. gpuAcceleration: true,
  50. // shift popper from its origin by the given amount of pixels (can be negative)
  51. offset: 0,
  52. // the element which will act as boundary of the popper
  53. boundariesElement: 'viewport',
  54. // amount of pixel used to define a minimum distance between the boundaries and the popper
  55. boundariesPadding: 5,
  56. // popper will try to prevent overflow following this order,
  57. // by default, then, it could overflow on the left and on top of the boundariesElement
  58. preventOverflowOrder: ['left', 'right', 'top', 'bottom'],
  59. // the behavior used by flip to change the placement of the popper
  60. flipBehavior: 'flip',
  61. arrowElement: '[x-arrow]',
  62. // list of functions used to modify the offsets before they are applied to the popper
  63. modifiers: [ 'shift', 'offset', 'preventOverflow', 'keepTogether', 'arrow', 'flip', 'applyStyle'],
  64. modifiersIgnored: [],
  65. forceAbsolute: false
  66. };
  67. /**
  68. * Create a new Popper.js instance
  69. * @constructor Popper
  70. * @param {HTMLElement} reference - The reference element used to position the popper
  71. * @param {HTMLElement|Object} popper
  72. * The HTML element used as popper, or a configuration used to generate the popper.
  73. * @param {String} [popper.tagName='div'] The tag name of the generated popper.
  74. * @param {Array} [popper.classNames=['popper']] Array of classes to apply to the generated popper.
  75. * @param {Array} [popper.attributes] Array of attributes to apply, specify `attr:value` to assign a value to it.
  76. * @param {HTMLElement|String} [popper.parent=window.document.body] The parent element, given as HTMLElement or as query string.
  77. * @param {String} [popper.content=''] The content of the popper, it can be text, html, or node; if it is not text, set `contentType` to `html` or `node`.
  78. * @param {String} [popper.contentType='text'] If `html`, the `content` will be parsed as HTML. If `node`, it will be appended as-is.
  79. * @param {String} [popper.arrowTagName='div'] Same as `popper.tagName` but for the arrow element.
  80. * @param {Array} [popper.arrowClassNames='popper__arrow'] Same as `popper.classNames` but for the arrow element.
  81. * @param {String} [popper.arrowAttributes=['x-arrow']] Same as `popper.attributes` but for the arrow element.
  82. * @param {Object} options
  83. * @param {String} [options.placement=bottom]
  84. * Placement of the popper accepted values: `top(-start, -end), right(-start, -end), bottom(-start, -right),
  85. * left(-start, -end)`
  86. *
  87. * @param {HTMLElement|String} [options.arrowElement='[x-arrow]']
  88. * The DOM Node used as arrow for the popper, or a CSS selector used to get the DOM node. It must be child of
  89. * its parent Popper. Popper.js will apply to the given element the style required to align the arrow with its
  90. * reference element.
  91. * By default, it will look for a child node of the popper with the `x-arrow` attribute.
  92. *
  93. * @param {Boolean} [options.gpuAcceleration=true]
  94. * When this property is set to true, the popper position will be applied using CSS3 translate3d, allowing the
  95. * browser to use the GPU to accelerate the rendering.
  96. * If set to false, the popper will be placed using `top` and `left` properties, not using the GPU.
  97. *
  98. * @param {Number} [options.offset=0]
  99. * Amount of pixels the popper will be shifted (can be negative).
  100. *
  101. * @param {String|Element} [options.boundariesElement='viewport']
  102. * The element which will define the boundaries of the popper position, the popper will never be placed outside
  103. * of the defined boundaries (except if `keepTogether` is enabled)
  104. *
  105. * @param {Number} [options.boundariesPadding=5]
  106. * Additional padding for the boundaries
  107. *
  108. * @param {Array} [options.preventOverflowOrder=['left', 'right', 'top', 'bottom']]
  109. * Order used when Popper.js tries to avoid overflows from the boundaries, they will be checked in order,
  110. * this means that the last ones will never overflow
  111. *
  112. * @param {String|Array} [options.flipBehavior='flip']
  113. * The behavior used by the `flip` modifier to change the placement of the popper when the latter is trying to
  114. * overlap its reference element. Defining `flip` as value, the placement will be flipped on
  115. * its axis (`right - left`, `top - bottom`).
  116. * You can even pass an array of placements (eg: `['right', 'left', 'top']` ) to manually specify
  117. * how alter the placement when a flip is needed. (eg. in the above example, it would first flip from right to left,
  118. * then, if even in its new placement, the popper is overlapping its reference element, it will be moved to top)
  119. *
  120. * @param {Array} [options.modifiers=[ 'shift', 'offset', 'preventOverflow', 'keepTogether', 'arrow', 'flip', 'applyStyle']]
  121. * List of functions used to modify the data before they are applied to the popper, add your custom functions
  122. * to this array to edit the offsets and placement.
  123. * The function should reflect the @params and @returns of preventOverflow
  124. *
  125. * @param {Array} [options.modifiersIgnored=[]]
  126. * Put here any built-in modifier name you want to exclude from the modifiers list
  127. * The function should reflect the @params and @returns of preventOverflow
  128. *
  129. * @param {Boolean} [options.removeOnDestroy=false]
  130. * Set to true if you want to automatically remove the popper when you call the `destroy` method.
  131. */
  132. function Popper(reference, popper, options) {
  133. this._reference = reference.jquery ? reference[0] : reference;
  134. this.state = {};
  135. // if the popper variable is a configuration object, parse it to generate an HTMLElement
  136. // generate a default popper if is not defined
  137. var isNotDefined = typeof popper === 'undefined' || popper === null;
  138. var isConfig = popper && Object.prototype.toString.call(popper) === '[object Object]';
  139. if (isNotDefined || isConfig) {
  140. this._popper = this.parse(isConfig ? popper : {});
  141. }
  142. // otherwise, use the given HTMLElement as popper
  143. else {
  144. this._popper = popper.jquery ? popper[0] : popper;
  145. }
  146. // with {} we create a new object with the options inside it
  147. this._options = Object.assign({}, DEFAULTS, options);
  148. // refactoring modifiers' list
  149. this._options.modifiers = this._options.modifiers.map(function(modifier){
  150. // remove ignored modifiers
  151. if (this._options.modifiersIgnored.indexOf(modifier) !== -1) return;
  152. // set the x-placement attribute before everything else because it could be used to add margins to the popper
  153. // margins needs to be calculated to get the correct popper offsets
  154. if (modifier === 'applyStyle') {
  155. this._popper.setAttribute('x-placement', this._options.placement);
  156. }
  157. // return predefined modifier identified by string or keep the custom one
  158. return this.modifiers[modifier] || modifier;
  159. }.bind(this));
  160. // make sure to apply the popper position before any computation
  161. this.state.position = this._getPosition(this._popper, this._reference);
  162. setStyle(this._popper, { position: this.state.position});
  163. // fire the first update to position the popper in the right place
  164. this.update();
  165. // setup event listeners, they will take care of update the position in specific situations
  166. this._setupEventListeners();
  167. return this;
  168. }
  169. //
  170. // Methods
  171. //
  172. /**
  173. * Destroy the popper
  174. * @method
  175. * @memberof Popper
  176. */
  177. Popper.prototype.destroy = function() {
  178. this._popper.removeAttribute('x-placement');
  179. this._popper.style.left = '';
  180. this._popper.style.position = '';
  181. this._popper.style.top = '';
  182. this._popper.style[getSupportedPropertyName('transform')] = '';
  183. this._removeEventListeners();
  184. // remove the popper if user explicity asked for the deletion on destroy
  185. if (this._options.removeOnDestroy) {
  186. this._popper.remove();
  187. }
  188. return this;
  189. };
  190. /**
  191. * Updates the position of the popper, computing the new offsets and applying the new style
  192. * @method
  193. * @memberof Popper
  194. */
  195. Popper.prototype.update = function() {
  196. var data = { instance: this, styles: {} };
  197. // store placement inside the data object, modifiers will be able to edit `placement` if needed
  198. // and refer to _originalPlacement to know the original value
  199. data.placement = this._options.placement;
  200. data._originalPlacement = this._options.placement;
  201. // compute the popper and reference offsets and put them inside data.offsets
  202. data.offsets = this._getOffsets(this._popper, this._reference, data.placement);
  203. // get boundaries
  204. data.boundaries = this._getBoundaries(data, this._options.boundariesPadding, this._options.boundariesElement);
  205. data = this.runModifiers(data, this._options.modifiers);
  206. if (typeof this.state.updateCallback === 'function') {
  207. this.state.updateCallback(data);
  208. }
  209. };
  210. /**
  211. * If a function is passed, it will be executed after the initialization of popper with as first argument the Popper instance.
  212. * @method
  213. * @memberof Popper
  214. * @param {Function} callback
  215. */
  216. Popper.prototype.onCreate = function(callback) {
  217. // the createCallbacks return as first argument the popper instance
  218. callback(this);
  219. return this;
  220. };
  221. /**
  222. * If a function is passed, it will be executed after each update of popper with as first argument the set of coordinates and informations
  223. * used to style popper and its arrow.
  224. * NOTE: it doesn't get fired on the first call of the `Popper.update()` method inside the `Popper` constructor!
  225. * @method
  226. * @memberof Popper
  227. * @param {Function} callback
  228. */
  229. Popper.prototype.onUpdate = function(callback) {
  230. this.state.updateCallback = callback;
  231. return this;
  232. };
  233. /**
  234. * Helper used to generate poppers from a configuration file
  235. * @method
  236. * @memberof Popper
  237. * @param config {Object} configuration
  238. * @returns {HTMLElement} popper
  239. */
  240. Popper.prototype.parse = function(config) {
  241. var defaultConfig = {
  242. tagName: 'div',
  243. classNames: [ 'popper' ],
  244. attributes: [],
  245. parent: root.document.body,
  246. content: '',
  247. contentType: 'text',
  248. arrowTagName: 'div',
  249. arrowClassNames: [ 'popper__arrow' ],
  250. arrowAttributes: [ 'x-arrow']
  251. };
  252. config = Object.assign({}, defaultConfig, config);
  253. var d = root.document;
  254. var popper = d.createElement(config.tagName);
  255. addClassNames(popper, config.classNames);
  256. addAttributes(popper, config.attributes);
  257. if (config.contentType === 'node') {
  258. popper.appendChild(config.content.jquery ? config.content[0] : config.content);
  259. }else if (config.contentType === 'html') {
  260. popper.innerHTML = config.content;
  261. } else {
  262. popper.textContent = config.content;
  263. }
  264. if (config.arrowTagName) {
  265. var arrow = d.createElement(config.arrowTagName);
  266. addClassNames(arrow, config.arrowClassNames);
  267. addAttributes(arrow, config.arrowAttributes);
  268. popper.appendChild(arrow);
  269. }
  270. var parent = config.parent.jquery ? config.parent[0] : config.parent;
  271. // if the given parent is a string, use it to match an element
  272. // if more than one element is matched, the first one will be used as parent
  273. // if no elements are matched, the script will throw an error
  274. if (typeof parent === 'string') {
  275. parent = d.querySelectorAll(config.parent);
  276. if (parent.length > 1) {
  277. console.warn('WARNING: the given `parent` query(' + config.parent + ') matched more than one element, the first one will be used');
  278. }
  279. if (parent.length === 0) {
  280. throw 'ERROR: the given `parent` doesn\'t exists!';
  281. }
  282. parent = parent[0];
  283. }
  284. // if the given parent is a DOM nodes list or an array of nodes with more than one element,
  285. // the first one will be used as parent
  286. if (parent.length > 1 && parent instanceof Element === false) {
  287. console.warn('WARNING: you have passed as parent a list of elements, the first one will be used');
  288. parent = parent[0];
  289. }
  290. // append the generated popper to its parent
  291. parent.appendChild(popper);
  292. return popper;
  293. /**
  294. * Adds class names to the given element
  295. * @function
  296. * @ignore
  297. * @param {HTMLElement} target
  298. * @param {Array} classes
  299. */
  300. function addClassNames(element, classNames) {
  301. classNames.forEach(function(className) {
  302. element.classList.add(className);
  303. });
  304. }
  305. /**
  306. * Adds attributes to the given element
  307. * @function
  308. * @ignore
  309. * @param {HTMLElement} target
  310. * @param {Array} attributes
  311. * @example
  312. * addAttributes(element, [ 'data-info:foobar' ]);
  313. */
  314. function addAttributes(element, attributes) {
  315. attributes.forEach(function(attribute) {
  316. element.setAttribute(attribute.split(':')[0], attribute.split(':')[1] || '');
  317. });
  318. }
  319. };
  320. /**
  321. * Helper used to get the position which will be applied to the popper
  322. * @method
  323. * @memberof Popper
  324. * @param config {HTMLElement} popper element
  325. * @param reference {HTMLElement} reference element
  326. * @returns {String} position
  327. */
  328. Popper.prototype._getPosition = function(popper, reference) {
  329. var container = getOffsetParent(reference);
  330. if (this._options.forceAbsolute) {
  331. return 'absolute';
  332. }
  333. // Decide if the popper will be fixed
  334. // If the reference element is inside a fixed context, the popper will be fixed as well to allow them to scroll together
  335. var isParentFixed = isFixed(reference, container);
  336. return isParentFixed ? 'fixed' : 'absolute';
  337. };
  338. /**
  339. * Get offsets to the popper
  340. * @method
  341. * @memberof Popper
  342. * @access private
  343. * @param {Element} popper - the popper element
  344. * @param {Element} reference - the reference element (the popper will be relative to this)
  345. * @returns {Object} An object containing the offsets which will be applied to the popper
  346. */
  347. Popper.prototype._getOffsets = function(popper, reference, placement) {
  348. placement = placement.split('-')[0];
  349. var popperOffsets = {};
  350. popperOffsets.position = this.state.position;
  351. var isParentFixed = popperOffsets.position === 'fixed';
  352. //
  353. // Get reference element position
  354. //
  355. var referenceOffsets = getOffsetRectRelativeToCustomParent(reference, getOffsetParent(popper), isParentFixed);
  356. //
  357. // Get popper sizes
  358. //
  359. var popperRect = getOuterSizes(popper);
  360. //
  361. // Compute offsets of popper
  362. //
  363. // depending by the popper placement we have to compute its offsets slightly differently
  364. if (['right', 'left'].indexOf(placement) !== -1) {
  365. popperOffsets.top = referenceOffsets.top + referenceOffsets.height / 2 - popperRect.height / 2;
  366. if (placement === 'left') {
  367. popperOffsets.left = referenceOffsets.left - popperRect.width;
  368. } else {
  369. popperOffsets.left = referenceOffsets.right;
  370. }
  371. } else {
  372. popperOffsets.left = referenceOffsets.left + referenceOffsets.width / 2 - popperRect.width / 2;
  373. if (placement === 'top') {
  374. popperOffsets.top = referenceOffsets.top - popperRect.height;
  375. } else {
  376. popperOffsets.top = referenceOffsets.bottom;
  377. }
  378. }
  379. // Add width and height to our offsets object
  380. popperOffsets.width = popperRect.width;
  381. popperOffsets.height = popperRect.height;
  382. return {
  383. popper: popperOffsets,
  384. reference: referenceOffsets
  385. };
  386. };
  387. /**
  388. * Setup needed event listeners used to update the popper position
  389. * @method
  390. * @memberof Popper
  391. * @access private
  392. */
  393. Popper.prototype._setupEventListeners = function() {
  394. // NOTE: 1 DOM access here
  395. this.state.updateBound = this.update.bind(this);
  396. root.addEventListener('resize', this.state.updateBound);
  397. // if the boundariesElement is window we don't need to listen for the scroll event
  398. if (this._options.boundariesElement !== 'window') {
  399. var target = getScrollParent(this._reference);
  400. // here it could be both `body` or `documentElement` thanks to Firefox, we then check both
  401. if (target === root.document.body || target === root.document.documentElement) {
  402. target = root;
  403. }
  404. target.addEventListener('scroll', this.state.updateBound);
  405. }
  406. };
  407. /**
  408. * Remove event listeners used to update the popper position
  409. * @method
  410. * @memberof Popper
  411. * @access private
  412. */
  413. Popper.prototype._removeEventListeners = function() {
  414. // NOTE: 1 DOM access here
  415. root.removeEventListener('resize', this.state.updateBound);
  416. if (this._options.boundariesElement !== 'window') {
  417. var target = getScrollParent(this._reference);
  418. // here it could be both `body` or `documentElement` thanks to Firefox, we then check both
  419. if (target === root.document.body || target === root.document.documentElement) {
  420. target = root;
  421. }
  422. target.removeEventListener('scroll', this.state.updateBound);
  423. }
  424. this.state.updateBound = null;
  425. };
  426. /**
  427. * Computed the boundaries limits and return them
  428. * @method
  429. * @memberof Popper
  430. * @access private
  431. * @param {Object} data - Object containing the property "offsets" generated by `_getOffsets`
  432. * @param {Number} padding - Boundaries padding
  433. * @param {Element} boundariesElement - Element used to define the boundaries
  434. * @returns {Object} Coordinates of the boundaries
  435. */
  436. Popper.prototype._getBoundaries = function(data, padding, boundariesElement) {
  437. // NOTE: 1 DOM access here
  438. var boundaries = {};
  439. var width, height;
  440. if (boundariesElement === 'window') {
  441. var body = root.document.body,
  442. html = root.document.documentElement;
  443. height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
  444. width = Math.max( body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth );
  445. boundaries = {
  446. top: 0,
  447. right: width,
  448. bottom: height,
  449. left: 0
  450. };
  451. } else if (boundariesElement === 'viewport') {
  452. var offsetParent = getOffsetParent(this._popper);
  453. var scrollParent = getScrollParent(this._popper);
  454. var offsetParentRect = getOffsetRect(offsetParent);
  455. // if the popper is fixed we don't have to substract scrolling from the boundaries
  456. var scrollTop = data.offsets.popper.position === 'fixed' ? 0 : scrollParent.scrollTop;
  457. var scrollLeft = data.offsets.popper.position === 'fixed' ? 0 : scrollParent.scrollLeft;
  458. boundaries = {
  459. top: 0 - (offsetParentRect.top - scrollTop),
  460. right: root.document.documentElement.clientWidth - (offsetParentRect.left - scrollLeft),
  461. bottom: root.document.documentElement.clientHeight - (offsetParentRect.top - scrollTop),
  462. left: 0 - (offsetParentRect.left - scrollLeft)
  463. };
  464. } else {
  465. if (getOffsetParent(this._popper) === boundariesElement) {
  466. boundaries = {
  467. top: 0,
  468. left: 0,
  469. right: boundariesElement.clientWidth,
  470. bottom: boundariesElement.clientHeight
  471. };
  472. } else {
  473. boundaries = getOffsetRect(boundariesElement);
  474. }
  475. }
  476. boundaries.left += padding;
  477. boundaries.right -= padding;
  478. boundaries.top = boundaries.top + padding;
  479. boundaries.bottom = boundaries.bottom - padding;
  480. return boundaries;
  481. };
  482. /**
  483. * Loop trough the list of modifiers and run them in order, each of them will then edit the data object
  484. * @method
  485. * @memberof Popper
  486. * @access public
  487. * @param {Object} data
  488. * @param {Array} modifiers
  489. * @param {Function} ends
  490. */
  491. Popper.prototype.runModifiers = function(data, modifiers, ends) {
  492. var modifiersToRun = modifiers.slice();
  493. if (ends !== undefined) {
  494. modifiersToRun = this._options.modifiers.slice(0, getArrayKeyIndex(this._options.modifiers, ends));
  495. }
  496. modifiersToRun.forEach(function(modifier) {
  497. if (isFunction(modifier)) {
  498. data = modifier.call(this, data);
  499. }
  500. }.bind(this));
  501. return data;
  502. };
  503. /**
  504. * Helper used to know if the given modifier depends from another one.
  505. * @method
  506. * @memberof Popper
  507. * @param {String} requesting - name of requesting modifier
  508. * @param {String} requested - name of requested modifier
  509. * @returns {Boolean}
  510. */
  511. Popper.prototype.isModifierRequired = function(requesting, requested) {
  512. var index = getArrayKeyIndex(this._options.modifiers, requesting);
  513. return !!this._options.modifiers.slice(0, index).filter(function(modifier) {
  514. return modifier === requested;
  515. }).length;
  516. };
  517. //
  518. // Modifiers
  519. //
  520. /**
  521. * Modifiers list
  522. * @namespace Popper.modifiers
  523. * @memberof Popper
  524. * @type {Object}
  525. */
  526. Popper.prototype.modifiers = {};
  527. /**
  528. * Apply the computed styles to the popper element
  529. * @method
  530. * @memberof Popper.modifiers
  531. * @argument {Object} data - The data object generated by `update` method
  532. * @returns {Object} The same data object
  533. */
  534. Popper.prototype.modifiers.applyStyle = function(data) {
  535. // apply the final offsets to the popper
  536. // NOTE: 1 DOM access here
  537. var styles = {
  538. position: data.offsets.popper.position
  539. };
  540. // round top and left to avoid blurry text
  541. var left = Math.round(data.offsets.popper.left);
  542. var top = Math.round(data.offsets.popper.top);
  543. // if gpuAcceleration is set to true and transform is supported, we use `translate3d` to apply the position to the popper
  544. // we automatically use the supported prefixed version if needed
  545. var prefixedProperty;
  546. if (this._options.gpuAcceleration && (prefixedProperty = getSupportedPropertyName('transform'))) {
  547. styles[prefixedProperty] = 'translate3d(' + left + 'px, ' + top + 'px, 0)';
  548. styles.top = 0;
  549. styles.left = 0;
  550. }
  551. // othwerise, we use the standard `left` and `top` properties
  552. else {
  553. styles.left =left;
  554. styles.top = top;
  555. }
  556. // any property present in `data.styles` will be applied to the popper,
  557. // in this way we can make the 3rd party modifiers add custom styles to it
  558. // Be aware, modifiers could override the properties defined in the previous
  559. // lines of this modifier!
  560. Object.assign(styles, data.styles);
  561. setStyle(this._popper, styles);
  562. // set an attribute which will be useful to style the tooltip (use it to properly position its arrow)
  563. // NOTE: 1 DOM access here
  564. this._popper.setAttribute('x-placement', data.placement);
  565. // if the arrow modifier is required and the arrow style has been computed, apply the arrow style
  566. if (this.isModifierRequired(this.modifiers.applyStyle, this.modifiers.arrow) && data.offsets.arrow) {
  567. setStyle(data.arrowElement, data.offsets.arrow);
  568. }
  569. return data;
  570. };
  571. /**
  572. * Modifier used to shift the popper on the start or end of its reference element side
  573. * @method
  574. * @memberof Popper.modifiers
  575. * @argument {Object} data - The data object generated by `update` method
  576. * @returns {Object} The data object, properly modified
  577. */
  578. Popper.prototype.modifiers.shift = function(data) {
  579. var placement = data.placement;
  580. var basePlacement = placement.split('-')[0];
  581. var shiftVariation = placement.split('-')[1];
  582. // if shift shiftVariation is specified, run the modifier
  583. if (shiftVariation) {
  584. var reference = data.offsets.reference;
  585. var popper = getPopperClientRect(data.offsets.popper);
  586. var shiftOffsets = {
  587. y: {
  588. start: { top: reference.top },
  589. end: { top: reference.top + reference.height - popper.height }
  590. },
  591. x: {
  592. start: { left: reference.left },
  593. end: { left: reference.left + reference.width - popper.width }
  594. }
  595. };
  596. var axis = ['bottom', 'top'].indexOf(basePlacement) !== -1 ? 'x' : 'y';
  597. data.offsets.popper = Object.assign(popper, shiftOffsets[axis][shiftVariation]);
  598. }
  599. return data;
  600. };
  601. /**
  602. * Modifier used to make sure the popper does not overflows from it's boundaries
  603. * @method
  604. * @memberof Popper.modifiers
  605. * @argument {Object} data - The data object generated by `update` method
  606. * @returns {Object} The data object, properly modified
  607. */
  608. Popper.prototype.modifiers.preventOverflow = function(data) {
  609. var order = this._options.preventOverflowOrder;
  610. var popper = getPopperClientRect(data.offsets.popper);
  611. var check = {
  612. left: function() {
  613. var left = popper.left;
  614. if (popper.left < data.boundaries.left) {
  615. left = Math.max(popper.left, data.boundaries.left);
  616. }
  617. return { left: left };
  618. },
  619. right: function() {
  620. var left = popper.left;
  621. if (popper.right > data.boundaries.right) {
  622. left = Math.min(popper.left, data.boundaries.right - popper.width);
  623. }
  624. return { left: left };
  625. },
  626. top: function() {
  627. var top = popper.top;
  628. if (popper.top < data.boundaries.top) {
  629. top = Math.max(popper.top, data.boundaries.top);
  630. }
  631. return { top: top };
  632. },
  633. bottom: function() {
  634. var top = popper.top;
  635. if (popper.bottom > data.boundaries.bottom) {
  636. top = Math.min(popper.top, data.boundaries.bottom - popper.height);
  637. }
  638. return { top: top };
  639. }
  640. };
  641. order.forEach(function(direction) {
  642. data.offsets.popper = Object.assign(popper, check[direction]());
  643. });
  644. return data;
  645. };
  646. /**
  647. * Modifier used to make sure the popper is always near its reference
  648. * @method
  649. * @memberof Popper.modifiers
  650. * @argument {Object} data - The data object generated by _update method
  651. * @returns {Object} The data object, properly modified
  652. */
  653. Popper.prototype.modifiers.keepTogether = function(data) {
  654. var popper = getPopperClientRect(data.offsets.popper);
  655. var reference = data.offsets.reference;
  656. var f = Math.floor;
  657. if (popper.right < f(reference.left)) {
  658. data.offsets.popper.left = f(reference.left) - popper.width;
  659. }
  660. if (popper.left > f(reference.right)) {
  661. data.offsets.popper.left = f(reference.right);
  662. }
  663. if (popper.bottom < f(reference.top)) {
  664. data.offsets.popper.top = f(reference.top) - popper.height;
  665. }
  666. if (popper.top > f(reference.bottom)) {
  667. data.offsets.popper.top = f(reference.bottom);
  668. }
  669. return data;
  670. };
  671. /**
  672. * Modifier used to flip the placement of the popper when the latter is starting overlapping its reference element.
  673. * Requires the `preventOverflow` modifier before it in order to work.
  674. * **NOTE:** This modifier will run all its previous modifiers everytime it tries to flip the popper!
  675. * @method
  676. * @memberof Popper.modifiers
  677. * @argument {Object} data - The data object generated by _update method
  678. * @returns {Object} The data object, properly modified
  679. */
  680. Popper.prototype.modifiers.flip = function(data) {
  681. // check if preventOverflow is in the list of modifiers before the flip modifier.
  682. // otherwise flip would not work as expected.
  683. if (!this.isModifierRequired(this.modifiers.flip, this.modifiers.preventOverflow)) {
  684. console.warn('WARNING: preventOverflow modifier is required by flip modifier in order to work, be sure to include it before flip!');
  685. return data;
  686. }
  687. if (data.flipped && data.placement === data._originalPlacement) {
  688. // seems like flip is trying to loop, probably there's not enough space on any of the flippable sides
  689. return data;
  690. }
  691. var placement = data.placement.split('-')[0];
  692. var placementOpposite = getOppositePlacement(placement);
  693. var variation = data.placement.split('-')[1] || '';
  694. var flipOrder = [];
  695. if(this._options.flipBehavior === 'flip') {
  696. flipOrder = [
  697. placement,
  698. placementOpposite
  699. ];
  700. } else {
  701. flipOrder = this._options.flipBehavior;
  702. }
  703. flipOrder.forEach(function(step, index) {
  704. if (placement !== step || flipOrder.length === index + 1) {
  705. return;
  706. }
  707. placement = data.placement.split('-')[0];
  708. placementOpposite = getOppositePlacement(placement);
  709. var popperOffsets = getPopperClientRect(data.offsets.popper);
  710. // this boolean is used to distinguish right and bottom from top and left
  711. // they need different computations to get flipped
  712. var a = ['right', 'bottom'].indexOf(placement) !== -1;
  713. // using Math.floor because the reference offsets may contain decimals we are not going to consider here
  714. if (
  715. a && Math.floor(data.offsets.reference[placement]) > Math.floor(popperOffsets[placementOpposite]) ||
  716. !a && Math.floor(data.offsets.reference[placement]) < Math.floor(popperOffsets[placementOpposite])
  717. ) {
  718. // we'll use this boolean to detect any flip loop
  719. data.flipped = true;
  720. data.placement = flipOrder[index + 1];
  721. if (variation) {
  722. data.placement += '-' + variation;
  723. }
  724. data.offsets.popper = this._getOffsets(this._popper, this._reference, data.placement).popper;
  725. data = this.runModifiers(data, this._options.modifiers, this._flip);
  726. }
  727. }.bind(this));
  728. return data;
  729. };
  730. /**
  731. * Modifier used to add an offset to the popper, useful if you more granularity positioning your popper.
  732. * The offsets will shift the popper on the side of its reference element.
  733. * @method
  734. * @memberof Popper.modifiers
  735. * @argument {Object} data - The data object generated by _update method
  736. * @returns {Object} The data object, properly modified
  737. */
  738. Popper.prototype.modifiers.offset = function(data) {
  739. var offset = this._options.offset;
  740. var popper = data.offsets.popper;
  741. if (data.placement.indexOf('left') !== -1) {
  742. popper.top -= offset;
  743. }
  744. else if (data.placement.indexOf('right') !== -1) {
  745. popper.top += offset;
  746. }
  747. else if (data.placement.indexOf('top') !== -1) {
  748. popper.left -= offset;
  749. }
  750. else if (data.placement.indexOf('bottom') !== -1) {
  751. popper.left += offset;
  752. }
  753. return data;
  754. };
  755. /**
  756. * Modifier used to move the arrows on the edge of the popper to make sure them are always between the popper and the reference element
  757. * It will use the CSS outer size of the arrow element to know how many pixels of conjuction are needed
  758. * @method
  759. * @memberof Popper.modifiers
  760. * @argument {Object} data - The data object generated by _update method
  761. * @returns {Object} The data object, properly modified
  762. */
  763. Popper.prototype.modifiers.arrow = function(data) {
  764. var arrow = this._options.arrowElement;
  765. // if the arrowElement is a string, suppose it's a CSS selector
  766. if (typeof arrow === 'string') {
  767. arrow = this._popper.querySelector(arrow);
  768. }
  769. // if arrow element is not found, don't run the modifier
  770. if (!arrow) {
  771. return data;
  772. }
  773. // the arrow element must be child of its popper
  774. if (!this._popper.contains(arrow)) {
  775. console.warn('WARNING: `arrowElement` must be child of its popper element!');
  776. return data;
  777. }
  778. // arrow depends on keepTogether in order to work
  779. if (!this.isModifierRequired(this.modifiers.arrow, this.modifiers.keepTogether)) {
  780. console.warn('WARNING: keepTogether modifier is required by arrow modifier in order to work, be sure to include it before arrow!');
  781. return data;
  782. }
  783. var arrowStyle = {};
  784. var placement = data.placement.split('-')[0];
  785. var popper = getPopperClientRect(data.offsets.popper);
  786. var reference = data.offsets.reference;
  787. var isVertical = ['left', 'right'].indexOf(placement) !== -1;
  788. var len = isVertical ? 'height' : 'width';
  789. var side = isVertical ? 'top' : 'left';
  790. var altSide = isVertical ? 'left' : 'top';
  791. var opSide = isVertical ? 'bottom' : 'right';
  792. var arrowSize = getOuterSizes(arrow)[len];
  793. //
  794. // extends keepTogether behavior making sure the popper and its reference have enough pixels in conjuction
  795. //
  796. // top/left side
  797. if (reference[opSide] - arrowSize < popper[side]) {
  798. data.offsets.popper[side] -= popper[side] - (reference[opSide] - arrowSize);
  799. }
  800. // bottom/right side
  801. if (reference[side] + arrowSize > popper[opSide]) {
  802. data.offsets.popper[side] += (reference[side] + arrowSize) - popper[opSide];
  803. }
  804. // compute center of the popper
  805. var center = reference[side] + (reference[len] / 2) - (arrowSize / 2);
  806. var sideValue = center - popper[side];
  807. // prevent arrow from being placed not contiguously to its popper
  808. sideValue = Math.max(Math.min(popper[len] - arrowSize, sideValue), 0);
  809. arrowStyle[side] = sideValue;
  810. arrowStyle[altSide] = ''; // make sure to remove any old style from the arrow
  811. data.offsets.arrow = arrowStyle;
  812. data.arrowElement = arrow;
  813. return data;
  814. };
  815. //
  816. // Helpers
  817. //
  818. /**
  819. * Get the outer sizes of the given element (offset size + margins)
  820. * @function
  821. * @ignore
  822. * @argument {Element} element
  823. * @returns {Object} object containing width and height properties
  824. */
  825. function getOuterSizes(element) {
  826. // NOTE: 1 DOM access here
  827. var _display = element.style.display, _visibility = element.style.visibility;
  828. element.style.display = 'block'; element.style.visibility = 'hidden';
  829. var calcWidthToForceRepaint = element.offsetWidth;
  830. // original method
  831. var styles = root.getComputedStyle(element);
  832. var x = parseFloat(styles.marginTop) + parseFloat(styles.marginBottom);
  833. var y = parseFloat(styles.marginLeft) + parseFloat(styles.marginRight);
  834. var result = { width: element.offsetWidth + y, height: element.offsetHeight + x };
  835. // reset element styles
  836. element.style.display = _display; element.style.visibility = _visibility;
  837. return result;
  838. }
  839. /**
  840. * Get the opposite placement of the given one/
  841. * @function
  842. * @ignore
  843. * @argument {String} placement
  844. * @returns {String} flipped placement
  845. */
  846. function getOppositePlacement(placement) {
  847. var hash = {left: 'right', right: 'left', bottom: 'top', top: 'bottom' };
  848. return placement.replace(/left|right|bottom|top/g, function(matched){
  849. return hash[matched];
  850. });
  851. }
  852. /**
  853. * Given the popper offsets, generate an output similar to getBoundingClientRect
  854. * @function
  855. * @ignore
  856. * @argument {Object} popperOffsets
  857. * @returns {Object} ClientRect like output
  858. */
  859. function getPopperClientRect(popperOffsets) {
  860. var offsets = Object.assign({}, popperOffsets);
  861. offsets.right = offsets.left + offsets.width;
  862. offsets.bottom = offsets.top + offsets.height;
  863. return offsets;
  864. }
  865. /**
  866. * Given an array and the key to find, returns its index
  867. * @function
  868. * @ignore
  869. * @argument {Array} arr
  870. * @argument keyToFind
  871. * @returns index or null
  872. */
  873. function getArrayKeyIndex(arr, keyToFind) {
  874. var i = 0, key;
  875. for (key in arr) {
  876. if (arr[key] === keyToFind) {
  877. return i;
  878. }
  879. i++;
  880. }
  881. return null;
  882. }
  883. /**
  884. * Get CSS computed property of the given element
  885. * @function
  886. * @ignore
  887. * @argument {Eement} element
  888. * @argument {String} property
  889. */
  890. function getStyleComputedProperty(element, property) {
  891. // NOTE: 1 DOM access here
  892. var css = root.getComputedStyle(element, null);
  893. return css[property];
  894. }
  895. /**
  896. * Returns the offset parent of the given element
  897. * @function
  898. * @ignore
  899. * @argument {Element} element
  900. * @returns {Element} offset parent
  901. */
  902. function getOffsetParent(element) {
  903. // NOTE: 1 DOM access here
  904. var offsetParent = element.offsetParent;
  905. return offsetParent === root.document.body || !offsetParent ? root.document.documentElement : offsetParent;
  906. }
  907. /**
  908. * Returns the scrolling parent of the given element
  909. * @function
  910. * @ignore
  911. * @argument {Element} element
  912. * @returns {Element} offset parent
  913. */
  914. function getScrollParent(element) {
  915. var parent = element.parentNode;
  916. if (!parent) {
  917. return element;
  918. }
  919. if (parent === root.document) {
  920. // Firefox puts the scrollTOp value on `documentElement` instead of `body`, we then check which of them is
  921. // greater than 0 and return the proper element
  922. if (root.document.body.scrollTop) {
  923. return root.document.body;
  924. } else {
  925. return root.document.documentElement;
  926. }
  927. }
  928. // Firefox want us to check `-x` and `-y` variations as well
  929. if (
  930. ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow')) !== -1 ||
  931. ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow-x')) !== -1 ||
  932. ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow-y')) !== -1
  933. ) {
  934. // If the detected scrollParent is body, we perform an additional check on its parentNode
  935. // in this way we'll get body if the browser is Chrome-ish, or documentElement otherwise
  936. // fixes issue #65
  937. return parent;
  938. }
  939. return getScrollParent(element.parentNode);
  940. }
  941. /**
  942. * Check if the given element is fixed or is inside a fixed parent
  943. * @function
  944. * @ignore
  945. * @argument {Element} element
  946. * @argument {Element} customContainer
  947. * @returns {Boolean} answer to "isFixed?"
  948. */
  949. function isFixed(element) {
  950. if (element === root.document.body) {
  951. return false;
  952. }
  953. if (getStyleComputedProperty(element, 'position') === 'fixed') {
  954. return true;
  955. }
  956. return element.parentNode ? isFixed(element.parentNode) : element;
  957. }
  958. /**
  959. * Set the style to the given popper
  960. * @function
  961. * @ignore
  962. * @argument {Element} element - Element to apply the style to
  963. * @argument {Object} styles - Object with a list of properties and values which will be applied to the element
  964. */
  965. function setStyle(element, styles) {
  966. function is_numeric(n) {
  967. return (n !== '' && !isNaN(parseFloat(n)) && isFinite(n));
  968. }
  969. Object.keys(styles).forEach(function(prop) {
  970. var unit = '';
  971. // add unit if the value is numeric and is one of the following
  972. if (['width', 'height', 'top', 'right', 'bottom', 'left'].indexOf(prop) !== -1 && is_numeric(styles[prop])) {
  973. unit = 'px';
  974. }
  975. element.style[prop] = styles[prop] + unit;
  976. });
  977. }
  978. /**
  979. * Check if the given variable is a function
  980. * @function
  981. * @ignore
  982. * @argument {*} functionToCheck - variable to check
  983. * @returns {Boolean} answer to: is a function?
  984. */
  985. function isFunction(functionToCheck) {
  986. var getType = {};
  987. return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  988. }
  989. /**
  990. * Get the position of the given element, relative to its offset parent
  991. * @function
  992. * @ignore
  993. * @param {Element} element
  994. * @return {Object} position - Coordinates of the element and its `scrollTop`
  995. */
  996. function getOffsetRect(element) {
  997. var elementRect = {
  998. width: element.offsetWidth,
  999. height: element.offsetHeight,
  1000. left: element.offsetLeft,
  1001. top: element.offsetTop
  1002. };
  1003. elementRect.right = elementRect.left + elementRect.width;
  1004. elementRect.bottom = elementRect.top + elementRect.height;
  1005. // position
  1006. return elementRect;
  1007. }
  1008. /**
  1009. * Get bounding client rect of given element
  1010. * @function
  1011. * @ignore
  1012. * @param {HTMLElement} element
  1013. * @return {Object} client rect
  1014. */
  1015. function getBoundingClientRect(element) {
  1016. var rect = element.getBoundingClientRect();
  1017. // whether the IE version is lower than 11
  1018. var isIE = navigator.userAgent.indexOf("MSIE") != -1;
  1019. // fix ie document bounding top always 0 bug
  1020. var rectTop = isIE && element.tagName === 'HTML'
  1021. ? -element.scrollTop
  1022. : rect.top;
  1023. return {
  1024. left: rect.left,
  1025. top: rectTop,
  1026. right: rect.right,
  1027. bottom: rect.bottom,
  1028. width: rect.right - rect.left,
  1029. height: rect.bottom - rectTop
  1030. };
  1031. }
  1032. /**
  1033. * Given an element and one of its parents, return the offset
  1034. * @function
  1035. * @ignore
  1036. * @param {HTMLElement} element
  1037. * @param {HTMLElement} parent
  1038. * @return {Object} rect
  1039. */
  1040. function getOffsetRectRelativeToCustomParent(element, parent, fixed) {
  1041. var elementRect = getBoundingClientRect(element);
  1042. var parentRect = getBoundingClientRect(parent);
  1043. if (fixed) {
  1044. var scrollParent = getScrollParent(parent);
  1045. parentRect.top += scrollParent.scrollTop;
  1046. parentRect.bottom += scrollParent.scrollTop;
  1047. parentRect.left += scrollParent.scrollLeft;
  1048. parentRect.right += scrollParent.scrollLeft;
  1049. }
  1050. var rect = {
  1051. top: elementRect.top - parentRect.top ,
  1052. left: elementRect.left - parentRect.left ,
  1053. bottom: (elementRect.top - parentRect.top) + elementRect.height,
  1054. right: (elementRect.left - parentRect.left) + elementRect.width,
  1055. width: elementRect.width,
  1056. height: elementRect.height
  1057. };
  1058. return rect;
  1059. }
  1060. /**
  1061. * Get the prefixed supported property name
  1062. * @function
  1063. * @ignore
  1064. * @argument {String} property (camelCase)
  1065. * @returns {String} prefixed property (camelCase)
  1066. */
  1067. function getSupportedPropertyName(property) {
  1068. var prefixes = ['', 'ms', 'webkit', 'moz', 'o'];
  1069. for (var i = 0; i < prefixes.length; i++) {
  1070. var toCheck = prefixes[i] ? prefixes[i] + property.charAt(0).toUpperCase() + property.slice(1) : property;
  1071. if (typeof root.document.body.style[toCheck] !== 'undefined') {
  1072. return toCheck;
  1073. }
  1074. }
  1075. return null;
  1076. }
  1077. /**
  1078. * The Object.assign() method is used to copy the values of all enumerable own properties from one or more source
  1079. * objects to a target object. It will return the target object.
  1080. * This polyfill doesn't support symbol properties, since ES5 doesn't have symbols anyway
  1081. * Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  1082. * @function
  1083. * @ignore
  1084. */
  1085. if (!Object.assign) {
  1086. Object.defineProperty(Object, 'assign', {
  1087. enumerable: false,
  1088. configurable: true,
  1089. writable: true,
  1090. value: function(target) {
  1091. if (target === undefined || target === null) {
  1092. throw new TypeError('Cannot convert first argument to object');
  1093. }
  1094. var to = Object(target);
  1095. for (var i = 1; i < arguments.length; i++) {
  1096. var nextSource = arguments[i];
  1097. if (nextSource === undefined || nextSource === null) {
  1098. continue;
  1099. }
  1100. nextSource = Object(nextSource);
  1101. var keysArray = Object.keys(nextSource);
  1102. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  1103. var nextKey = keysArray[nextIndex];
  1104. var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
  1105. if (desc !== undefined && desc.enumerable) {
  1106. to[nextKey] = nextSource[nextKey];
  1107. }
  1108. }
  1109. }
  1110. return to;
  1111. }
  1112. });
  1113. }
  1114. return Popper;
  1115. }));