ngx-bootstrap-positioning.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. import { Injectable, NgZone, RendererFactory2, Inject, PLATFORM_ID, ElementRef } from '@angular/core';
  2. import { isPlatformBrowser } from '@angular/common';
  3. import { Subject, merge, fromEvent, of, animationFrameScheduler } from 'rxjs';
  4. /**
  5. * @fileoverview added by tsickle
  6. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  7. */
  8. /**
  9. * Get CSS computed property of the given element
  10. * @param {?} element
  11. * @param {?=} property
  12. * @return {?}
  13. */
  14. function getStyleComputedProperty(element, property) {
  15. if (element.nodeType !== 1) {
  16. return [];
  17. }
  18. // NOTE: 1 DOM access here
  19. /** @type {?} */
  20. const window = element.ownerDocument.defaultView;
  21. /** @type {?} */
  22. const css = window.getComputedStyle(element, null);
  23. return property ? css[(/** @type {?} */ (property))] : css;
  24. }
  25. /**
  26. * @fileoverview added by tsickle
  27. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  28. */
  29. /**
  30. * Returns the parentNode or the host of the element
  31. * @param {?} element
  32. * @return {?}
  33. */
  34. function getParentNode(element) {
  35. if (element.nodeName === 'HTML') {
  36. return element;
  37. }
  38. return element.parentNode || element.host;
  39. }
  40. /**
  41. * @fileoverview added by tsickle
  42. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  43. */
  44. /**
  45. * @param {?} element
  46. * @return {?}
  47. */
  48. function getScrollParent(element) {
  49. // Return body, `getScroll` will take care to get the correct `scrollTop` from it
  50. if (!element) {
  51. return document.body;
  52. }
  53. switch (element.nodeName) {
  54. case 'HTML':
  55. case 'BODY':
  56. return element.ownerDocument.body;
  57. case '#document':
  58. return element.body;
  59. default:
  60. }
  61. // Firefox want us to check `-x` and `-y` variations as well
  62. const { overflow, overflowX, overflowY } = getStyleComputedProperty(element);
  63. if (/(auto|scroll|overlay)/.test(String(overflow) + String(overflowY) + String(overflowX))) {
  64. return element;
  65. }
  66. return getScrollParent(getParentNode(element));
  67. }
  68. /**
  69. * @fileoverview added by tsickle
  70. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  71. */
  72. /** @type {?} */
  73. const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
  74. /**
  75. * @fileoverview added by tsickle
  76. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  77. */
  78. /** @type {?} */
  79. const isIE11 = isBrowser && !!(((/** @type {?} */ (window))).MSInputMethodContext && ((/** @type {?} */ (document))).documentMode);
  80. /** @type {?} */
  81. const isIE10 = isBrowser && !!(((/** @type {?} */ (window))).MSInputMethodContext && /MSIE 10/.test(((/** @type {?} */ (navigator))).userAgent));
  82. /**
  83. * @param {?=} version
  84. * @return {?}
  85. */
  86. function isIE(version) {
  87. if (version === 11) {
  88. return isIE11;
  89. }
  90. if (version === 10) {
  91. return isIE10;
  92. }
  93. return isIE11 || isIE10;
  94. }
  95. /**
  96. * @fileoverview added by tsickle
  97. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  98. */
  99. /**
  100. * @param {?} element
  101. * @return {?}
  102. */
  103. function getOffsetParent(element) {
  104. if (!element) {
  105. return document.documentElement;
  106. }
  107. /** @type {?} */
  108. const noOffsetParent = isIE(10) ? document.body : null;
  109. // NOTE: 1 DOM access here
  110. /** @type {?} */
  111. let offsetParent = element.offsetParent || null;
  112. // Skip hidden elements which don't have an offsetParent
  113. /** @type {?} */
  114. let sibling;
  115. while (offsetParent === noOffsetParent
  116. && element.nextElementSibling
  117. && sibling !== element.nextElementSibling) {
  118. sibling = element.nextElementSibling;
  119. offsetParent = sibling.offsetParent;
  120. }
  121. /** @type {?} */
  122. const nodeName = offsetParent && offsetParent.nodeName;
  123. if (!nodeName || nodeName === 'BODY' || nodeName === 'HTML') {
  124. return sibling ? sibling.ownerDocument.documentElement : document.documentElement;
  125. }
  126. // .offsetParent will return the closest TH, TD or TABLE in case
  127. if (['TH', 'TD', 'TABLE'].indexOf(offsetParent.nodeName) !== -1 &&
  128. getStyleComputedProperty(offsetParent, 'position') === 'static') {
  129. return getOffsetParent(offsetParent);
  130. }
  131. return offsetParent;
  132. }
  133. /**
  134. * @fileoverview added by tsickle
  135. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  136. */
  137. /**
  138. * @param {?} element
  139. * @return {?}
  140. */
  141. function isOffsetContainer(element) {
  142. const { nodeName } = element;
  143. if (nodeName === 'BODY') {
  144. return false;
  145. }
  146. return (nodeName === 'HTML' || getOffsetParent(element.firstElementChild) === element);
  147. }
  148. /**
  149. * @fileoverview added by tsickle
  150. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  151. */
  152. /**
  153. * Finds the root node (document, shadowDOM root) of the given element
  154. * @param {?} node
  155. * @return {?}
  156. */
  157. function getRoot(node) {
  158. if (node.parentNode !== null) {
  159. return getRoot(node.parentNode);
  160. }
  161. return node;
  162. }
  163. /**
  164. * @fileoverview added by tsickle
  165. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  166. */
  167. /**
  168. * @param {?} element1
  169. * @param {?} element2
  170. * @return {?}
  171. */
  172. function findCommonOffsetParent(element1, element2) {
  173. // This check is needed to avoid errors in case one of the elements isn't defined for any reason
  174. if (!element1 || !element1.nodeType || !element2 || !element2.nodeType) {
  175. return document.documentElement;
  176. }
  177. // Here we make sure to give as "start" the element that comes first in the DOM
  178. /* tslint:disable-next-line: no-bitwise */
  179. /** @type {?} */
  180. const order = element1.compareDocumentPosition(element2) & Node.DOCUMENT_POSITION_FOLLOWING;
  181. /** @type {?} */
  182. const start = order ? element1 : element2;
  183. /** @type {?} */
  184. const end = order ? element2 : element1;
  185. // Get common ancestor container
  186. /** @type {?} */
  187. const range = document.createRange();
  188. range.setStart(start, 0);
  189. range.setEnd(end, 0);
  190. const { commonAncestorContainer } = range;
  191. // Both nodes are inside #document
  192. if ((element1 !== commonAncestorContainer &&
  193. element2 !== commonAncestorContainer) ||
  194. start.contains(end)) {
  195. if (isOffsetContainer(commonAncestorContainer)) {
  196. return commonAncestorContainer;
  197. }
  198. return getOffsetParent(commonAncestorContainer);
  199. }
  200. // one of the nodes is inside shadowDOM, find which one
  201. /** @type {?} */
  202. const element1root = getRoot(element1);
  203. if (element1root.host) {
  204. return findCommonOffsetParent(element1root.host, element2);
  205. }
  206. else {
  207. return findCommonOffsetParent(element1, getRoot(element2).host);
  208. }
  209. }
  210. /**
  211. * @fileoverview added by tsickle
  212. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  213. */
  214. /**
  215. * Helper to detect borders of a given element
  216. */
  217. /**
  218. * @param {?} styles
  219. * @param {?} axis
  220. * @return {?}
  221. */
  222. function getBordersSize(styles, axis) {
  223. /** @type {?} */
  224. const sideA = axis === 'x' ? 'Left' : 'Top';
  225. /** @type {?} */
  226. const sideB = sideA === 'Left' ? 'Right' : 'Bottom';
  227. return (parseFloat(styles[(/** @type {?} */ (`border${sideA}Width`))]) +
  228. parseFloat(styles[(/** @type {?} */ (`border${sideB}Width`))]));
  229. }
  230. /**
  231. * @fileoverview added by tsickle
  232. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  233. */
  234. /**
  235. * @param {?} axis
  236. * @param {?} body
  237. * @param {?} html
  238. * @param {?} computedStyle
  239. * @return {?}
  240. */
  241. function getSize(axis, body, html, computedStyle) {
  242. return Math.max(((/** @type {?} */ (body)))[`offset${axis}`], ((/** @type {?} */ (body)))[`scroll${axis}`], ((/** @type {?} */ (html)))[`client${axis}`], ((/** @type {?} */ (html)))[`offset${axis}`], ((/** @type {?} */ (html)))[`scroll${axis}`], isIE(10)
  243. ? (parseInt(((/** @type {?} */ (html)))[`offset${axis}`], 10) +
  244. parseInt(computedStyle[(/** @type {?} */ (`margin${axis === 'Height' ? 'Top' : 'Left'}`))], 10) +
  245. parseInt(computedStyle[(/** @type {?} */ (`margin${axis === 'Height' ? 'Bottom' : 'Right'}`))], 10))
  246. : 0);
  247. }
  248. /**
  249. * @param {?} document
  250. * @return {?}
  251. */
  252. function getWindowSizes(document) {
  253. /** @type {?} */
  254. const body = document.body;
  255. /** @type {?} */
  256. const html = document.documentElement;
  257. /** @type {?} */
  258. const computedStyle = isIE(10) && getComputedStyle(html);
  259. return {
  260. height: getSize('Height', body, html, computedStyle),
  261. width: getSize('Width', body, html, computedStyle)
  262. };
  263. }
  264. /**
  265. * @fileoverview added by tsickle
  266. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  267. */
  268. /**
  269. * Gets the scroll value of the given element in the given side (top and left)
  270. * @param {?} element
  271. * @param {?=} side
  272. * @return {?}
  273. */
  274. function getScroll(element, side = 'top') {
  275. /** @type {?} */
  276. const upperSide = side === 'top' ? 'scrollTop' : 'scrollLeft';
  277. /** @type {?} */
  278. const nodeName = element.nodeName;
  279. if (nodeName === 'BODY' || nodeName === 'HTML') {
  280. /** @type {?} */
  281. const html = element.ownerDocument.documentElement;
  282. /** @type {?} */
  283. const scrollingElement = element.ownerDocument.scrollingElement || html;
  284. return scrollingElement[upperSide];
  285. }
  286. return element[upperSide];
  287. }
  288. /**
  289. * @fileoverview added by tsickle
  290. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  291. */
  292. /**
  293. * @param {?} offsets
  294. * @return {?}
  295. */
  296. function getClientRect(offsets) {
  297. return Object.assign({}, offsets, { right: offsets.left + offsets.width, bottom: offsets.top + offsets.height });
  298. }
  299. /**
  300. * @fileoverview added by tsickle
  301. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  302. */
  303. /**
  304. * @param {?} element
  305. * @return {?}
  306. */
  307. function getBoundingClientRect(element) {
  308. /** @type {?} */
  309. let rect = {};
  310. // IE10 10 FIX: Please, don't ask, the element isn't
  311. // considered in DOM in some circumstances...
  312. // This isn't reproducible in IE10 compatibility mode of IE11
  313. try {
  314. if (isIE(10)) {
  315. rect = element.getBoundingClientRect();
  316. /** @type {?} */
  317. const scrollTop = getScroll(element, 'top');
  318. /** @type {?} */
  319. const scrollLeft = getScroll(element, 'left');
  320. rect.top += scrollTop;
  321. rect.left += scrollLeft;
  322. rect.bottom += scrollTop;
  323. rect.right += scrollLeft;
  324. }
  325. else {
  326. rect = element.getBoundingClientRect();
  327. }
  328. }
  329. catch (e) {
  330. return undefined;
  331. }
  332. /** @type {?} */
  333. const result = {
  334. left: rect.left,
  335. top: rect.top,
  336. width: rect.right - rect.left,
  337. height: rect.bottom - rect.top
  338. };
  339. // subtract scrollbar size from sizes
  340. /** @type {?} */
  341. const sizes = element.nodeName === 'HTML' ? getWindowSizes(element.ownerDocument) : {};
  342. /** @type {?} */
  343. const width = sizes.width || element.clientWidth || result.right - result.left;
  344. /** @type {?} */
  345. const height = sizes.height || element.clientHeight || result.bottom - result.top;
  346. /** @type {?} */
  347. let horizScrollbar = element.offsetWidth - width;
  348. /** @type {?} */
  349. let vertScrollbar = element.offsetHeight - height;
  350. // if an hypothetical scrollbar is detected, we must be sure it's not a `border`
  351. // we make this check conditional for performance reasons
  352. if (horizScrollbar || vertScrollbar) {
  353. /** @type {?} */
  354. const styles = getStyleComputedProperty(element);
  355. horizScrollbar -= getBordersSize(styles, 'x');
  356. vertScrollbar -= getBordersSize(styles, 'y');
  357. result.width -= horizScrollbar;
  358. result.height -= vertScrollbar;
  359. }
  360. return getClientRect(result);
  361. }
  362. /**
  363. * @fileoverview added by tsickle
  364. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  365. */
  366. /**
  367. * @param {?} rect
  368. * @param {?} element
  369. * @param {?=} subtract
  370. * @return {?}
  371. */
  372. function includeScroll(rect, element, subtract = false) {
  373. /** @type {?} */
  374. const scrollTop = getScroll(element, 'top');
  375. /** @type {?} */
  376. const scrollLeft = getScroll(element, 'left');
  377. /** @type {?} */
  378. const modifier = subtract ? -1 : 1;
  379. rect.top += scrollTop * modifier;
  380. rect.bottom += scrollTop * modifier;
  381. rect.left += scrollLeft * modifier;
  382. rect.right += scrollLeft * modifier;
  383. return rect;
  384. }
  385. /**
  386. * @fileoverview added by tsickle
  387. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  388. */
  389. /**
  390. * @param {?} children
  391. * @param {?} parent
  392. * @param {?=} fixedPosition
  393. * @return {?}
  394. */
  395. function getOffsetRectRelativeToArbitraryNode(children, parent, fixedPosition = false) {
  396. /** @type {?} */
  397. const isIE10 = isIE(10);
  398. /** @type {?} */
  399. const isHTML = parent.nodeName === 'HTML';
  400. /** @type {?} */
  401. const childrenRect = getBoundingClientRect(children);
  402. /** @type {?} */
  403. const parentRect = getBoundingClientRect(parent);
  404. /** @type {?} */
  405. const scrollParent = getScrollParent(children);
  406. /** @type {?} */
  407. const styles = getStyleComputedProperty(parent);
  408. /** @type {?} */
  409. const borderTopWidth = parseFloat(styles.borderTopWidth);
  410. /** @type {?} */
  411. const borderLeftWidth = parseFloat(styles.borderLeftWidth);
  412. // In cases where the parent is fixed, we must ignore negative scroll in offset calc
  413. if (fixedPosition && isHTML) {
  414. parentRect.top = Math.max(parentRect.top, 0);
  415. parentRect.left = Math.max(parentRect.left, 0);
  416. }
  417. /** @type {?} */
  418. let offsets = getClientRect({
  419. top: childrenRect.top - parentRect.top - borderTopWidth,
  420. left: childrenRect.left - parentRect.left - borderLeftWidth,
  421. width: childrenRect.width,
  422. height: childrenRect.height
  423. });
  424. offsets.marginTop = 0;
  425. offsets.marginLeft = 0;
  426. // Subtract margins of documentElement in case it's being used as parent
  427. // we do this only on HTML because it's the only element that behaves
  428. // differently when margins are applied to it. The margins are included in
  429. // the box of the documentElement, in the other cases not.
  430. if (!isIE10 && isHTML) {
  431. /** @type {?} */
  432. const marginTop = parseFloat(styles.marginTop);
  433. /** @type {?} */
  434. const marginLeft = parseFloat(styles.marginLeft);
  435. offsets.top -= borderTopWidth - marginTop;
  436. offsets.bottom -= borderTopWidth - marginTop;
  437. offsets.left -= borderLeftWidth - marginLeft;
  438. offsets.right -= borderLeftWidth - marginLeft;
  439. // Attach marginTop and marginLeft because in some circumstances we may need them
  440. offsets.marginTop = marginTop;
  441. offsets.marginLeft = marginLeft;
  442. }
  443. if (isIE10 && !fixedPosition
  444. ? parent.contains(scrollParent)
  445. : parent === scrollParent && scrollParent.nodeName !== 'BODY') {
  446. offsets = includeScroll(offsets, parent);
  447. }
  448. return offsets;
  449. }
  450. /**
  451. * @fileoverview added by tsickle
  452. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  453. */
  454. /**
  455. * @param {?} element
  456. * @param {?=} excludeScroll
  457. * @return {?}
  458. */
  459. function getViewportOffsetRectRelativeToArtbitraryNode(element, excludeScroll = false) {
  460. /** @type {?} */
  461. const html = element.ownerDocument.documentElement;
  462. /** @type {?} */
  463. const relativeOffset = getOffsetRectRelativeToArbitraryNode(element, html);
  464. /** @type {?} */
  465. const width = Math.max(html.clientWidth, window.innerWidth || 0);
  466. /** @type {?} */
  467. const height = Math.max(html.clientHeight, window.innerHeight || 0);
  468. /** @type {?} */
  469. const scrollTop = !excludeScroll ? getScroll(html) : 0;
  470. /** @type {?} */
  471. const scrollLeft = !excludeScroll ? getScroll(html, 'left') : 0;
  472. /** @type {?} */
  473. const offset = {
  474. top: scrollTop - Number(relativeOffset.top) + Number(relativeOffset.marginTop),
  475. left: scrollLeft - Number(relativeOffset.left) + Number(relativeOffset.marginLeft),
  476. width,
  477. height
  478. };
  479. return getClientRect(offset);
  480. }
  481. /**
  482. * @fileoverview added by tsickle
  483. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  484. */
  485. /**
  486. * @param {?} element
  487. * @return {?}
  488. */
  489. function isFixed(element) {
  490. /** @type {?} */
  491. const nodeName = element.nodeName;
  492. if (nodeName === 'BODY' || nodeName === 'HTML') {
  493. return false;
  494. }
  495. if (getStyleComputedProperty(element, 'position') === 'fixed') {
  496. return true;
  497. }
  498. return isFixed(getParentNode(element));
  499. }
  500. /**
  501. * @fileoverview added by tsickle
  502. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  503. */
  504. /**
  505. * @param {?} element
  506. * @return {?}
  507. */
  508. function getFixedPositionOffsetParent(element) {
  509. // This check is needed to avoid errors in case one of the elements isn't defined for any reason
  510. if (!element || !element.parentElement || isIE()) {
  511. return document.documentElement;
  512. }
  513. /** @type {?} */
  514. let el = element.parentElement;
  515. while (el && getStyleComputedProperty(el, 'transform') === 'none') {
  516. el = el.parentElement;
  517. }
  518. return el || document.documentElement;
  519. }
  520. /**
  521. * @fileoverview added by tsickle
  522. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  523. */
  524. /**
  525. * @param {?} target
  526. * @param {?} host
  527. * @param {?=} padding
  528. * @param {?=} boundariesElement
  529. * @param {?=} fixedPosition
  530. * @return {?}
  531. */
  532. function getBoundaries(target, host, padding = 0, boundariesElement, fixedPosition = false) {
  533. // NOTE: 1 DOM access here
  534. // NOTE: 1 DOM access here
  535. /** @type {?} */
  536. let boundaries = { top: 0, left: 0 };
  537. /** @type {?} */
  538. const offsetParent = fixedPosition ? getFixedPositionOffsetParent(target) : findCommonOffsetParent(target, host);
  539. // Handle viewport case
  540. if (boundariesElement === 'viewport') {
  541. boundaries = getViewportOffsetRectRelativeToArtbitraryNode(offsetParent, fixedPosition);
  542. }
  543. else {
  544. // Handle other cases based on DOM element used as boundaries
  545. /** @type {?} */
  546. let boundariesNode;
  547. if (boundariesElement === 'scrollParent') {
  548. boundariesNode = getScrollParent(getParentNode(host));
  549. if (boundariesNode.nodeName === 'BODY') {
  550. boundariesNode = target.ownerDocument.documentElement;
  551. }
  552. }
  553. else if (boundariesElement === 'window') {
  554. boundariesNode = target.ownerDocument.documentElement;
  555. }
  556. else {
  557. boundariesNode = boundariesElement;
  558. }
  559. /** @type {?} */
  560. const offsets = getOffsetRectRelativeToArbitraryNode(boundariesNode, offsetParent, fixedPosition);
  561. // In case of HTML, we need a different computation
  562. if (boundariesNode.nodeName === 'HTML' && !isFixed(offsetParent)) {
  563. const { height, width } = getWindowSizes(target.ownerDocument);
  564. boundaries.top += offsets.top - offsets.marginTop;
  565. boundaries.bottom = Number(height) + Number(offsets.top);
  566. boundaries.left += offsets.left - offsets.marginLeft;
  567. boundaries.right = Number(width) + Number(offsets.left);
  568. }
  569. else {
  570. // for all the other DOM elements, this one is good
  571. boundaries = offsets;
  572. }
  573. }
  574. // Add paddings
  575. boundaries.left += padding;
  576. boundaries.top += padding;
  577. boundaries.right -= padding;
  578. boundaries.bottom -= padding;
  579. return boundaries;
  580. }
  581. /**
  582. * @fileoverview added by tsickle
  583. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  584. */
  585. /**
  586. * @param {?} __0
  587. * @return {?}
  588. */
  589. function getArea({ width, height }) {
  590. return width * height;
  591. }
  592. /**
  593. * @param {?} placement
  594. * @param {?} refRect
  595. * @param {?} target
  596. * @param {?} host
  597. * @param {?=} allowedPositions
  598. * @param {?=} boundariesElement
  599. * @param {?=} padding
  600. * @return {?}
  601. */
  602. function computeAutoPlacement(placement, refRect, target, host, allowedPositions = ['top', 'bottom', 'right', 'left'], boundariesElement = 'viewport', padding = 0) {
  603. if (placement.indexOf('auto') === -1) {
  604. return placement;
  605. }
  606. /** @type {?} */
  607. const boundaries = getBoundaries(target, host, padding, boundariesElement);
  608. /** @type {?} */
  609. const rects = {
  610. top: {
  611. width: boundaries.width,
  612. height: refRect.top - boundaries.top
  613. },
  614. right: {
  615. width: boundaries.right - refRect.right,
  616. height: boundaries.height
  617. },
  618. bottom: {
  619. width: boundaries.width,
  620. height: boundaries.bottom - refRect.bottom
  621. },
  622. left: {
  623. width: refRect.left - boundaries.left,
  624. height: boundaries.height
  625. }
  626. };
  627. /** @type {?} */
  628. const sortedAreas = Object.keys(rects)
  629. .map((/**
  630. * @param {?} key
  631. * @return {?}
  632. */
  633. key => (Object.assign({ key }, rects[key], { area: getArea(rects[key]) }))))
  634. .sort((/**
  635. * @param {?} a
  636. * @param {?} b
  637. * @return {?}
  638. */
  639. (a, b) => b.area - a.area));
  640. /** @type {?} */
  641. let filteredAreas = sortedAreas.filter((/**
  642. * @param {?} __0
  643. * @return {?}
  644. */
  645. ({ width, height }) => {
  646. return width >= target.clientWidth
  647. && height >= target.clientHeight;
  648. }));
  649. filteredAreas = filteredAreas.filter((/**
  650. * @param {?} position
  651. * @return {?}
  652. */
  653. (position) => {
  654. return allowedPositions
  655. .some((/**
  656. * @param {?} allowedPosition
  657. * @return {?}
  658. */
  659. (allowedPosition) => {
  660. return allowedPosition === position.key;
  661. }));
  662. }));
  663. /** @type {?} */
  664. const computedPlacement = filteredAreas.length > 0
  665. ? filteredAreas[0].key
  666. : sortedAreas[0].key;
  667. /** @type {?} */
  668. const variation = placement.split(' ')[1];
  669. // for tooltip on auto position
  670. target.className = target.className.replace(/bs-tooltip-auto/g, `bs-tooltip-${computedPlacement}`);
  671. return computedPlacement + (variation ? `-${variation}` : '');
  672. }
  673. /**
  674. * @fileoverview added by tsickle
  675. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  676. */
  677. /**
  678. * @param {?} data
  679. * @return {?}
  680. */
  681. function getOffsets(data) {
  682. return {
  683. width: data.offsets.target.width,
  684. height: data.offsets.target.height,
  685. left: Math.floor(data.offsets.target.left),
  686. top: Math.round(data.offsets.target.top),
  687. bottom: Math.round(data.offsets.target.bottom),
  688. right: Math.floor(data.offsets.target.right)
  689. };
  690. }
  691. /**
  692. * @fileoverview added by tsickle
  693. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  694. */
  695. /**
  696. * Get the opposite placement of the given one
  697. * @param {?} placement
  698. * @return {?}
  699. */
  700. function getOppositePlacement(placement) {
  701. /** @type {?} */
  702. const hash = { left: 'right', right: 'left', bottom: 'top', top: 'bottom' };
  703. return placement.replace(/left|right|bottom|top/g, (/**
  704. * @param {?} matched
  705. * @return {?}
  706. */
  707. matched => ((/** @type {?} */ (hash)))[matched]));
  708. }
  709. /**
  710. * @fileoverview added by tsickle
  711. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  712. */
  713. /**
  714. * Get the opposite placement variation of the given one
  715. * @param {?} variation
  716. * @return {?}
  717. */
  718. function getOppositeVariation(variation) {
  719. if (variation === 'right') {
  720. return 'left';
  721. }
  722. else if (variation === 'left') {
  723. return 'right';
  724. }
  725. return variation;
  726. }
  727. /**
  728. * @fileoverview added by tsickle
  729. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  730. */
  731. /**
  732. * Get the outer sizes of the given element (offset size + margins)
  733. * @param {?} element
  734. * @return {?}
  735. */
  736. function getOuterSizes(element) {
  737. /** @type {?} */
  738. const window = element.ownerDocument.defaultView;
  739. /** @type {?} */
  740. const styles = window.getComputedStyle(element);
  741. /** @type {?} */
  742. const x = parseFloat(styles.marginTop || 0) + parseFloat(styles.marginBottom || 0);
  743. /** @type {?} */
  744. const y = parseFloat(styles.marginLeft || 0) + parseFloat(styles.marginRight || 0);
  745. return {
  746. width: Number(element.offsetWidth) + y,
  747. height: Number(element.offsetHeight) + x
  748. };
  749. }
  750. /**
  751. * @fileoverview added by tsickle
  752. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  753. */
  754. /**
  755. * @param {?} target
  756. * @param {?} host
  757. * @param {?=} fixedPosition
  758. * @return {?}
  759. */
  760. function getReferenceOffsets(target, host, fixedPosition = null) {
  761. /** @type {?} */
  762. const commonOffsetParent = fixedPosition
  763. ? getFixedPositionOffsetParent(target)
  764. : findCommonOffsetParent(target, host);
  765. return getOffsetRectRelativeToArbitraryNode(host, commonOffsetParent, fixedPosition);
  766. }
  767. /**
  768. * @fileoverview added by tsickle
  769. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  770. */
  771. /**
  772. * @param {?} target
  773. * @param {?} hostOffsets
  774. * @param {?} position
  775. * @return {?}
  776. */
  777. function getTargetOffsets(target, hostOffsets, position) {
  778. /** @type {?} */
  779. const placement = position.split(' ')[0];
  780. // Get target node sizes
  781. /** @type {?} */
  782. const targetRect = getOuterSizes(target);
  783. // Add position, width and height to our offsets object
  784. /** @type {?} */
  785. const targetOffsets = {
  786. width: targetRect.width,
  787. height: targetRect.height
  788. };
  789. // depending by the target placement we have to compute its offsets slightly differently
  790. /** @type {?} */
  791. const isHoriz = ['right', 'left'].indexOf(placement) !== -1;
  792. /** @type {?} */
  793. const mainSide = isHoriz ? 'top' : 'left';
  794. /** @type {?} */
  795. const secondarySide = isHoriz ? 'left' : 'top';
  796. /** @type {?} */
  797. const measurement = isHoriz ? 'height' : 'width';
  798. /** @type {?} */
  799. const secondaryMeasurement = !isHoriz ? 'height' : 'width';
  800. ((/** @type {?} */ (targetOffsets)))[mainSide] =
  801. hostOffsets[mainSide] +
  802. hostOffsets[measurement] / 2 -
  803. targetRect[measurement] / 2;
  804. ((/** @type {?} */ (targetOffsets)))[secondarySide] = placement === secondarySide
  805. ? hostOffsets[secondarySide] - targetRect[secondaryMeasurement]
  806. : ((/** @type {?} */ (hostOffsets)))[getOppositePlacement(secondarySide)];
  807. return targetOffsets;
  808. }
  809. /**
  810. * @fileoverview added by tsickle
  811. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  812. */
  813. /**
  814. * Helper used to know if the given modifier is enabled.
  815. * @param {?} options
  816. * @param {?} modifierName
  817. * @return {?}
  818. */
  819. function isModifierEnabled(options, modifierName) {
  820. return options
  821. && options.modifiers
  822. && options.modifiers[modifierName]
  823. && options.modifiers[modifierName].enabled;
  824. }
  825. /**
  826. * @fileoverview added by tsickle
  827. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  828. */
  829. /**
  830. * Tells if a given input is a number
  831. * @param {?} n
  832. * @return {?}
  833. */
  834. function isNumeric(n) {
  835. return n !== '' && !isNaN(parseFloat(n)) && isFinite(n);
  836. }
  837. /**
  838. * @fileoverview added by tsickle
  839. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  840. */
  841. /**
  842. * @param {?} data
  843. * @param {?=} renderer
  844. * @return {?}
  845. */
  846. function updateContainerClass(data, renderer) {
  847. /** @type {?} */
  848. const target = data.instance.target;
  849. /** @type {?} */
  850. let containerClass = target.className;
  851. if (data.placementAuto) {
  852. containerClass = containerClass.replace(/bs-popover-auto/g, `bs-popover-${data.placement}`);
  853. containerClass = containerClass.replace(/bs-tooltip-auto/g, `bs-tooltip-${data.placement}`);
  854. containerClass = containerClass.replace(/\sauto/g, ` ${data.placement}`);
  855. if (containerClass.indexOf('popover') !== -1 && containerClass.indexOf('popover-auto') === -1) {
  856. containerClass += ' popover-auto';
  857. }
  858. if (containerClass.indexOf('tooltip') !== -1 && containerClass.indexOf('tooltip-auto') === -1) {
  859. containerClass += ' tooltip-auto';
  860. }
  861. }
  862. containerClass = containerClass.replace(/left|right|top|bottom/g, `${data.placement.split(' ')[0]}`);
  863. if (renderer) {
  864. renderer.setAttribute(target, 'class', containerClass);
  865. return;
  866. }
  867. target.className = containerClass;
  868. }
  869. /**
  870. * @fileoverview added by tsickle
  871. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  872. */
  873. /**
  874. * @param {?} element
  875. * @param {?} styles
  876. * @param {?=} renderer
  877. * @return {?}
  878. */
  879. function setStyles(element, styles, renderer) {
  880. Object.keys(styles).forEach((/**
  881. * @param {?} prop
  882. * @return {?}
  883. */
  884. (prop) => {
  885. /** @type {?} */
  886. let unit = '';
  887. // add unit if the value is numeric and is one of the following
  888. if (['width', 'height', 'top', 'right', 'bottom', 'left'].indexOf(prop) !== -1 &&
  889. isNumeric(styles[prop])) {
  890. unit = 'px';
  891. }
  892. if (renderer) {
  893. renderer.setStyle(element, prop, `${String(styles[prop])}${unit}`);
  894. return;
  895. }
  896. element.style[prop] = String(styles[prop]) + unit;
  897. }));
  898. }
  899. /**
  900. * @fileoverview added by tsickle
  901. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  902. */
  903. /**
  904. * @param {?} data
  905. * @return {?}
  906. */
  907. function arrow(data) {
  908. /** @type {?} */
  909. let targetOffsets = data.offsets.target;
  910. // if arrowElement is a string, suppose it's a CSS selector
  911. /** @type {?} */
  912. const arrowElement = data.instance.target.querySelector('.arrow');
  913. // if arrowElement is not found, don't run the modifier
  914. if (!arrowElement) {
  915. return data;
  916. }
  917. /** @type {?} */
  918. const isVertical = ['left', 'right'].indexOf(data.placement) !== -1;
  919. /** @type {?} */
  920. const len = isVertical ? 'height' : 'width';
  921. /** @type {?} */
  922. const sideCapitalized = isVertical ? 'Top' : 'Left';
  923. /** @type {?} */
  924. const side = sideCapitalized.toLowerCase();
  925. /** @type {?} */
  926. const altSide = isVertical ? 'left' : 'top';
  927. /** @type {?} */
  928. const opSide = isVertical ? 'bottom' : 'right';
  929. /** @type {?} */
  930. const arrowElementSize = getOuterSizes(arrowElement)[len];
  931. // top/left side
  932. if (data.offsets.host[opSide] - arrowElementSize < ((/** @type {?} */ (targetOffsets)))[side]) {
  933. ((/** @type {?} */ (targetOffsets)))[side] -=
  934. ((/** @type {?} */ (targetOffsets)))[side] - (data.offsets.host[opSide] - arrowElementSize);
  935. }
  936. // bottom/right side
  937. if (Number(((/** @type {?} */ (data))).offsets.host[side]) + Number(arrowElementSize) > ((/** @type {?} */ (targetOffsets)))[opSide]) {
  938. ((/** @type {?} */ (targetOffsets)))[side] +=
  939. Number(((/** @type {?} */ (data))).offsets.host[side]) + Number(arrowElementSize) - Number(((/** @type {?} */ (targetOffsets)))[opSide]);
  940. }
  941. targetOffsets = getClientRect(targetOffsets);
  942. // compute center of the target
  943. /** @type {?} */
  944. const center = Number(((/** @type {?} */ (data))).offsets.host[side]) + Number(data.offsets.host[len] / 2 - arrowElementSize / 2);
  945. // Compute the sideValue using the updated target offsets
  946. // take target margin in account because we don't have this info available
  947. /** @type {?} */
  948. const css = getStyleComputedProperty(data.instance.target);
  949. /** @type {?} */
  950. const targetMarginSide = parseFloat(css[`margin${sideCapitalized}`]);
  951. /** @type {?} */
  952. const targetBorderSide = parseFloat(css[`border${sideCapitalized}Width`]);
  953. /** @type {?} */
  954. let sideValue = center - ((/** @type {?} */ (targetOffsets)))[side] - targetMarginSide - targetBorderSide;
  955. // prevent arrowElement from being placed not contiguously to its target
  956. sideValue = Math.max(Math.min(targetOffsets[len] - arrowElementSize, sideValue), 0);
  957. data.offsets.arrow = {
  958. [side]: Math.round(sideValue),
  959. [altSide]: '' // make sure to unset any eventual altSide value from the DOM node
  960. };
  961. data.instance.arrow = arrowElement;
  962. return data;
  963. }
  964. /**
  965. * @fileoverview added by tsickle
  966. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  967. */
  968. /**
  969. * @param {?} data
  970. * @return {?}
  971. */
  972. function flip(data) {
  973. data.offsets.target = getClientRect(data.offsets.target);
  974. if (!isModifierEnabled(data.options, 'flip')) {
  975. data.offsets.target = Object.assign({}, data.offsets.target, getTargetOffsets(data.instance.target, data.offsets.host, data.placement));
  976. return data;
  977. }
  978. /** @type {?} */
  979. const boundaries = getBoundaries(data.instance.target, data.instance.host, 0, // padding
  980. 'viewport', false // positionFixed
  981. );
  982. /** @type {?} */
  983. let placement = data.placement.split(' ')[0];
  984. /** @type {?} */
  985. let variation = data.placement.split(' ')[1] || '';
  986. /** @type {?} */
  987. const offsetsHost = data.offsets.host;
  988. /** @type {?} */
  989. const target = data.instance.target;
  990. /** @type {?} */
  991. const host = data.instance.host;
  992. /** @type {?} */
  993. const adaptivePosition = computeAutoPlacement('auto', offsetsHost, target, host, data.options.allowedPositions);
  994. /** @type {?} */
  995. const flipOrder = [placement, adaptivePosition];
  996. /* tslint:disable-next-line: cyclomatic-complexity */
  997. flipOrder.forEach((/**
  998. * @param {?} step
  999. * @param {?} index
  1000. * @return {?}
  1001. */
  1002. (step, index) => {
  1003. if (placement !== step || flipOrder.length === index + 1) {
  1004. return data;
  1005. }
  1006. placement = data.placement.split(' ')[0];
  1007. // using floor because the host offsets may contain decimals we are not going to consider here
  1008. /** @type {?} */
  1009. const overlapsRef = (placement === 'left' &&
  1010. Math.floor(data.offsets.target.right) > Math.floor(data.offsets.host.left)) ||
  1011. (placement === 'right' &&
  1012. Math.floor(data.offsets.target.left) < Math.floor(data.offsets.host.right)) ||
  1013. (placement === 'top' &&
  1014. Math.floor(data.offsets.target.bottom) > Math.floor(data.offsets.host.top)) ||
  1015. (placement === 'bottom' &&
  1016. Math.floor(data.offsets.target.top) < Math.floor(data.offsets.host.bottom));
  1017. /** @type {?} */
  1018. const overflowsLeft = Math.floor(data.offsets.target.left) < Math.floor(boundaries.left);
  1019. /** @type {?} */
  1020. const overflowsRight = Math.floor(data.offsets.target.right) > Math.floor(boundaries.right);
  1021. /** @type {?} */
  1022. const overflowsTop = Math.floor(data.offsets.target.top) < Math.floor(boundaries.top);
  1023. /** @type {?} */
  1024. const overflowsBottom = Math.floor(data.offsets.target.bottom) > Math.floor(boundaries.bottom);
  1025. /** @type {?} */
  1026. const overflowsBoundaries = (placement === 'left' && overflowsLeft) ||
  1027. (placement === 'right' && overflowsRight) ||
  1028. (placement === 'top' && overflowsTop) ||
  1029. (placement === 'bottom' && overflowsBottom);
  1030. // flip the variation if required
  1031. /** @type {?} */
  1032. const isVertical = ['top', 'bottom'].indexOf(placement) !== -1;
  1033. /** @type {?} */
  1034. const flippedVariation = ((isVertical && variation === 'left' && overflowsLeft) ||
  1035. (isVertical && variation === 'right' && overflowsRight) ||
  1036. (!isVertical && variation === 'left' && overflowsTop) ||
  1037. (!isVertical && variation === 'right' && overflowsBottom));
  1038. if (overlapsRef || overflowsBoundaries || flippedVariation) {
  1039. if (overlapsRef || overflowsBoundaries) {
  1040. placement = flipOrder[index + 1];
  1041. }
  1042. if (flippedVariation) {
  1043. variation = getOppositeVariation(variation);
  1044. }
  1045. data.placement = placement + (variation ? ` ${variation}` : '');
  1046. data.offsets.target = Object.assign({}, data.offsets.target, getTargetOffsets(data.instance.target, data.offsets.host, data.placement));
  1047. }
  1048. }));
  1049. return data;
  1050. }
  1051. /**
  1052. * @fileoverview added by tsickle
  1053. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1054. */
  1055. /**
  1056. * @param {?} targetElement
  1057. * @param {?} hostElement
  1058. * @param {?} position
  1059. * @param {?} options
  1060. * @return {?}
  1061. */
  1062. function initData(targetElement, hostElement, position, options) {
  1063. /** @type {?} */
  1064. const hostElPosition = getReferenceOffsets(targetElement, hostElement);
  1065. if (!position.match(/^(auto)*\s*(left|right|top|bottom)*$/)
  1066. && !position.match(/^(left|right|top|bottom)*\s*(start|end)*$/)) {
  1067. /* tslint:disable-next-line: no-parameter-reassignment */
  1068. position = 'auto';
  1069. }
  1070. /** @type {?} */
  1071. const placementAuto = !!position.match(/auto/g);
  1072. // support old placements 'auto left|right|top|bottom'
  1073. /** @type {?} */
  1074. let placement = position.match(/auto\s(left|right|top|bottom)/)
  1075. ? position.split(' ')[1] || 'auto'
  1076. : position;
  1077. /** @type {?} */
  1078. const targetOffset = getTargetOffsets(targetElement, hostElPosition, placement);
  1079. placement = computeAutoPlacement(placement, hostElPosition, targetElement, hostElement, options ? options.allowedPositions : undefined);
  1080. return {
  1081. options,
  1082. instance: {
  1083. target: targetElement,
  1084. host: hostElement,
  1085. arrow: null
  1086. },
  1087. offsets: {
  1088. target: targetOffset,
  1089. host: hostElPosition,
  1090. arrow: null
  1091. },
  1092. positionFixed: false,
  1093. placement,
  1094. placementAuto
  1095. };
  1096. }
  1097. /**
  1098. * @fileoverview added by tsickle
  1099. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1100. */
  1101. /**
  1102. * @param {?} data
  1103. * @return {?}
  1104. */
  1105. function preventOverflow(data) {
  1106. if (!isModifierEnabled(data.options, 'preventOverflow')) {
  1107. return data;
  1108. }
  1109. // NOTE: DOM access here
  1110. // resets the targetOffsets's position so that the document size can be calculated excluding
  1111. // the size of the targetOffsets element itself
  1112. /** @type {?} */
  1113. const transformProp = 'transform';
  1114. /** @type {?} */
  1115. const targetStyles = data.instance.target.style;
  1116. // assignment to help minification
  1117. const { top, left, [transformProp]: transform } = targetStyles;
  1118. targetStyles.top = '';
  1119. targetStyles.left = '';
  1120. targetStyles[transformProp] = '';
  1121. /** @type {?} */
  1122. const boundaries = getBoundaries(data.instance.target, data.instance.host, 0, // padding
  1123. 'scrollParent', false // positionFixed
  1124. );
  1125. // NOTE: DOM access here
  1126. // restores the original style properties after the offsets have been computed
  1127. targetStyles.top = top;
  1128. targetStyles.left = left;
  1129. targetStyles[transformProp] = transform;
  1130. /** @type {?} */
  1131. const order = ['left', 'right', 'top', 'bottom'];
  1132. /** @type {?} */
  1133. const check = {
  1134. /**
  1135. * @param {?} placement
  1136. * @return {?}
  1137. */
  1138. primary(placement) {
  1139. /** @type {?} */
  1140. let value = ((/** @type {?} */ (data))).offsets.target[placement];
  1141. if (((/** @type {?} */ (data))).offsets.target[placement] < boundaries[placement] &&
  1142. !false // options.escapeWithReference
  1143. ) {
  1144. value = Math.max(((/** @type {?} */ (data))).offsets.target[placement], boundaries[placement]);
  1145. }
  1146. return { [placement]: value };
  1147. },
  1148. /**
  1149. * @param {?} placement
  1150. * @return {?}
  1151. */
  1152. secondary(placement) {
  1153. /** @type {?} */
  1154. const mainSide = placement === 'right' ? 'left' : 'top';
  1155. /** @type {?} */
  1156. let value = data.offsets.target[mainSide];
  1157. if (((/** @type {?} */ (data))).offsets.target[placement] > boundaries[placement] &&
  1158. !false // escapeWithReference
  1159. ) {
  1160. value = Math.min(data.offsets.target[mainSide], boundaries[placement] -
  1161. (placement === 'right' ? data.offsets.target.width : data.offsets.target.height));
  1162. }
  1163. return { [mainSide]: value };
  1164. }
  1165. };
  1166. /** @type {?} */
  1167. let side;
  1168. order.forEach((/**
  1169. * @param {?} placement
  1170. * @return {?}
  1171. */
  1172. placement => {
  1173. side = ['left', 'top']
  1174. .indexOf(placement) !== -1
  1175. ? 'primary'
  1176. : 'secondary';
  1177. data.offsets.target = Object.assign({}, data.offsets.target, ((/** @type {?} */ (check)))[side](placement));
  1178. }));
  1179. return data;
  1180. }
  1181. /**
  1182. * @fileoverview added by tsickle
  1183. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1184. */
  1185. /**
  1186. * @param {?} data
  1187. * @return {?}
  1188. */
  1189. function shift(data) {
  1190. /** @type {?} */
  1191. const placement = data.placement;
  1192. /** @type {?} */
  1193. const basePlacement = placement.split(' ')[0];
  1194. /** @type {?} */
  1195. const shiftvariation = placement.split(' ')[1];
  1196. if (shiftvariation) {
  1197. const { host, target } = data.offsets;
  1198. /** @type {?} */
  1199. const isVertical = ['bottom', 'top'].indexOf(basePlacement) !== -1;
  1200. /** @type {?} */
  1201. const side = isVertical ? 'left' : 'top';
  1202. /** @type {?} */
  1203. const measurement = isVertical ? 'width' : 'height';
  1204. /** @type {?} */
  1205. const shiftOffsets = {
  1206. start: { [side]: host[side] },
  1207. end: {
  1208. [side]: host[side] + host[measurement] - target[measurement]
  1209. }
  1210. };
  1211. data.offsets.target = Object.assign({}, target, ((/** @type {?} */ (shiftOffsets)))[shiftvariation]);
  1212. }
  1213. return data;
  1214. }
  1215. /**
  1216. * @fileoverview added by tsickle
  1217. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1218. */
  1219. class Positioning {
  1220. /**
  1221. * @param {?} hostElement
  1222. * @param {?} targetElement
  1223. * @param {?=} round
  1224. * @return {?}
  1225. */
  1226. position(hostElement, targetElement, round = true) {
  1227. return this.offset(hostElement, targetElement, false);
  1228. }
  1229. /**
  1230. * @param {?} hostElement
  1231. * @param {?} targetElement
  1232. * @param {?=} round
  1233. * @return {?}
  1234. */
  1235. offset(hostElement, targetElement, round = true) {
  1236. return getReferenceOffsets(targetElement, hostElement);
  1237. }
  1238. /**
  1239. * @param {?} hostElement
  1240. * @param {?} targetElement
  1241. * @param {?} position
  1242. * @param {?=} appendToBody
  1243. * @param {?=} options
  1244. * @return {?}
  1245. */
  1246. positionElements(hostElement, targetElement, position, appendToBody, options) {
  1247. /** @type {?} */
  1248. const chainOfModifiers = [flip, shift, preventOverflow, arrow];
  1249. return chainOfModifiers.reduce((/**
  1250. * @param {?} modifiedData
  1251. * @param {?} modifier
  1252. * @return {?}
  1253. */
  1254. (modifiedData, modifier) => modifier(modifiedData)), initData(targetElement, hostElement, position, options));
  1255. }
  1256. }
  1257. /** @type {?} */
  1258. const positionService = new Positioning();
  1259. /**
  1260. * @param {?} hostElement
  1261. * @param {?} targetElement
  1262. * @param {?} placement
  1263. * @param {?=} appendToBody
  1264. * @param {?=} options
  1265. * @param {?=} renderer
  1266. * @return {?}
  1267. */
  1268. function positionElements(hostElement, targetElement, placement, appendToBody, options, renderer) {
  1269. /** @type {?} */
  1270. const data = positionService.positionElements(hostElement, targetElement, placement, appendToBody, options);
  1271. /** @type {?} */
  1272. const offsets = getOffsets(data);
  1273. setStyles(targetElement, {
  1274. 'will-change': 'transform',
  1275. top: '0px',
  1276. left: '0px',
  1277. transform: `translate3d(${offsets.left}px, ${offsets.top}px, 0px)`
  1278. }, renderer);
  1279. if (data.instance.arrow) {
  1280. setStyles(data.instance.arrow, data.offsets.arrow, renderer);
  1281. }
  1282. updateContainerClass(data, renderer);
  1283. }
  1284. /**
  1285. * @fileoverview added by tsickle
  1286. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1287. */
  1288. class PositioningService {
  1289. /**
  1290. * @param {?} ngZone
  1291. * @param {?} rendererFactory
  1292. * @param {?} platformId
  1293. */
  1294. constructor(ngZone, rendererFactory, platformId) {
  1295. this.update$$ = new Subject();
  1296. this.positionElements = new Map();
  1297. this.isDisabled = false;
  1298. if (isPlatformBrowser(platformId)) {
  1299. ngZone.runOutsideAngular((/**
  1300. * @return {?}
  1301. */
  1302. () => {
  1303. this.triggerEvent$ = merge(fromEvent(window, 'scroll', { passive: true }), fromEvent(window, 'resize', { passive: true }),
  1304. /* tslint:disable-next-line: deprecation */
  1305. of(0, animationFrameScheduler), this.update$$);
  1306. this.triggerEvent$.subscribe((/**
  1307. * @return {?}
  1308. */
  1309. () => {
  1310. if (this.isDisabled) {
  1311. return;
  1312. }
  1313. this.positionElements
  1314. /* tslint:disable-next-line: no-any */
  1315. .forEach((/**
  1316. * @param {?} positionElement
  1317. * @return {?}
  1318. */
  1319. (positionElement) => {
  1320. positionElements(_getHtmlElement(positionElement.target), _getHtmlElement(positionElement.element), positionElement.attachment, positionElement.appendToBody, this.options, rendererFactory.createRenderer(null, null));
  1321. }));
  1322. }));
  1323. }));
  1324. }
  1325. }
  1326. /**
  1327. * @param {?} options
  1328. * @return {?}
  1329. */
  1330. position(options) {
  1331. this.addPositionElement(options);
  1332. }
  1333. /**
  1334. * @return {?}
  1335. */
  1336. get event$() {
  1337. return this.triggerEvent$;
  1338. }
  1339. /**
  1340. * @return {?}
  1341. */
  1342. disable() {
  1343. this.isDisabled = true;
  1344. }
  1345. /**
  1346. * @return {?}
  1347. */
  1348. enable() {
  1349. this.isDisabled = false;
  1350. }
  1351. /**
  1352. * @param {?} options
  1353. * @return {?}
  1354. */
  1355. addPositionElement(options) {
  1356. this.positionElements.set(_getHtmlElement(options.element), options);
  1357. }
  1358. /**
  1359. * @return {?}
  1360. */
  1361. calcPosition() {
  1362. this.update$$.next();
  1363. }
  1364. /**
  1365. * @param {?} elRef
  1366. * @return {?}
  1367. */
  1368. deletePositionElement(elRef) {
  1369. this.positionElements.delete(_getHtmlElement(elRef));
  1370. }
  1371. /**
  1372. * @param {?} options
  1373. * @return {?}
  1374. */
  1375. setOptions(options) {
  1376. this.options = options;
  1377. }
  1378. }
  1379. PositioningService.decorators = [
  1380. { type: Injectable }
  1381. ];
  1382. /** @nocollapse */
  1383. PositioningService.ctorParameters = () => [
  1384. { type: NgZone },
  1385. { type: RendererFactory2 },
  1386. { type: Number, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] }
  1387. ];
  1388. /**
  1389. * @param {?} element
  1390. * @return {?}
  1391. */
  1392. function _getHtmlElement(element) {
  1393. // it means that we got a selector
  1394. if (typeof element === 'string') {
  1395. return document.querySelector(element);
  1396. }
  1397. if (element instanceof ElementRef) {
  1398. return element.nativeElement;
  1399. }
  1400. return element;
  1401. }
  1402. export { Positioning, PositioningService, positionElements };
  1403. //# sourceMappingURL=ngx-bootstrap-positioning.js.map