@duoduo-oba/ng-devui
Version:
DevUI components based on Angular
258 lines (252 loc) • 9.93 kB
JavaScript
import { Injectable, NgModule } from '@angular/core';
import { DocumentRef, WindowRef } from '@duoduo-oba/ng-devui/window-ref';
/**
* @fileoverview added by tsickle
* Generated from: positioning.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class PositionService {
/**
* @param {?} documentRef
* @param {?} windowRef
*/
constructor(documentRef, windowRef) {
this.documentRef = documentRef;
this.windowRef = windowRef;
}
/**
* @param {?} element
* @param {?=} round
* @return {?}
*/
position(element, round = true) {
/** @type {?} */
let elPosition;
/** @type {?} */
let parentOffset = { width: 0, height: 0, top: 0, bottom: 0, left: 0, right: 0 };
if (this.getStyle(element, 'position') === 'fixed') {
elPosition = element.getBoundingClientRect();
}
else {
/** @type {?} */
const offsetParentEl = this.offsetParent(element);
elPosition = this.offset(element, false);
if (offsetParentEl !== this.documentRef.documentElement) {
parentOffset = this.offset(offsetParentEl, false);
}
parentOffset.top += offsetParentEl.clientTop - offsetParentEl.scrollTop;
parentOffset.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
}
elPosition.top -= parentOffset.top;
elPosition.bottom -= parentOffset.top;
elPosition.left -= parentOffset.left;
elPosition.right -= parentOffset.left;
if (round) {
elPosition.top = Math.round(elPosition.top);
elPosition.bottom = Math.round(elPosition.bottom);
elPosition.left = Math.round(elPosition.left);
elPosition.right = Math.round(elPosition.right);
}
return elPosition;
}
/**
* @param {?} element
* @param {?=} round
* @return {?}
*/
offset(element, round = true) {
/** @type {?} */
const elBcr = element.getBoundingClientRect();
/** @type {?} */
const viewportOffset = {
top: this.windowRef.pageYOffset - this.documentRef.documentElement.clientTop,
left: this.windowRef.pageXOffset - this.documentRef.documentElement.clientLeft
};
/** @type {?} */
const elOffset = {
height: elBcr.height || element.offsetHeight,
width: elBcr.width || element.offsetWidth,
top: elBcr.top + viewportOffset.top,
bottom: elBcr.bottom + viewportOffset.top,
left: elBcr.left + viewportOffset.left,
right: elBcr.right + viewportOffset.left
};
if (round) {
elOffset.height = Math.round(elOffset.height);
elOffset.width = Math.round(elOffset.width);
elOffset.top = Math.round(elOffset.top);
elOffset.bottom = Math.round(elOffset.bottom);
elOffset.left = Math.round(elOffset.left);
elOffset.right = Math.round(elOffset.right);
}
return elOffset;
}
/**
* @param {?} element
* @return {?}
*/
getScrollParent(element) {
/** @type {?} */
let style = getComputedStyle(element);
/** @type {?} */
const excludeStaticParent = style.position === 'absolute';
/** @type {?} */
const overflowRegex = /(auto|scroll|hidden)/;
if (style.position === 'fixed') {
return this.documentRef.body;
}
for (let parent = element; (parent = parent.parentElement); parent.parentElement !== this.documentRef.body) {
style = getComputedStyle(parent);
if (excludeStaticParent && style.position === 'static') {
continue;
}
if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) {
return parent;
}
}
return this.documentRef.body;
}
/**
* @param {?} hostElement
* @param {?} targetElement
* @param {?} placement
* @param {?=} appendToBody
* @return {?}
*/
positionElements(hostElement, targetElement, placement, appendToBody) {
/** @type {?} */
const hostElPosition = appendToBody ? this.offset(hostElement, false) : this.position(hostElement, false);
/** @type {?} */
const shiftWidth = {
left: hostElPosition.left,
center: hostElPosition.left + hostElPosition.width / 2 - targetElement.offsetWidth / 2,
right: hostElPosition.left + hostElPosition.width
};
/** @type {?} */
const shiftHeight = {
top: hostElPosition.top,
center: hostElPosition.top + hostElPosition.height / 2 - targetElement.offsetHeight / 2,
bottom: hostElPosition.top + hostElPosition.height
};
/** @type {?} */
const targetElBCR = targetElement.getBoundingClientRect();
/** @type {?} */
const placementPrimary = placement.split('-')[0] || 'top';
/** @type {?} */
const placementSecondary = placement.split('-')[1] || 'center';
/** @type {?} */
const targetElPosition = {
height: targetElBCR.height || targetElement.offsetHeight,
width: targetElBCR.width || targetElement.offsetWidth,
top: 0,
bottom: targetElBCR.height || targetElement.offsetHeight,
left: 0,
right: targetElBCR.width || targetElement.offsetWidth
};
switch (placementPrimary) {
case 'top':
targetElPosition.top = hostElPosition.top - targetElement.offsetHeight;
targetElPosition.bottom += hostElPosition.top - targetElement.offsetHeight;
targetElPosition.left = shiftWidth[placementSecondary];
targetElPosition.right += shiftWidth[placementSecondary];
break;
case 'bottom':
targetElPosition.top = shiftHeight[placementPrimary];
targetElPosition.bottom += shiftHeight[placementPrimary];
targetElPosition.left = shiftWidth[placementSecondary];
targetElPosition.right += shiftWidth[placementSecondary];
break;
case 'left':
targetElPosition.top = shiftHeight[placementSecondary];
targetElPosition.bottom += shiftHeight[placementSecondary];
targetElPosition.left = hostElPosition.left - targetElement.offsetWidth;
targetElPosition.right += hostElPosition.left - targetElement.offsetWidth;
break;
case 'right':
targetElPosition.top = shiftHeight[placementSecondary];
targetElPosition.bottom += shiftHeight[placementSecondary];
targetElPosition.left = shiftWidth[placementPrimary];
targetElPosition.right += shiftWidth[placementPrimary];
break;
}
targetElPosition.top = Math.round(targetElPosition.top);
targetElPosition.bottom = Math.round(targetElPosition.bottom);
targetElPosition.left = Math.round(targetElPosition.left);
targetElPosition.right = Math.round(targetElPosition.right);
return targetElPosition;
}
/**
* @private
* @param {?} element
* @param {?} prop
* @return {?}
*/
getStyle(element, prop) {
return this.windowRef.getComputedStyle(element)[prop];
}
/**
* @private
* @param {?} element
* @return {?}
*/
isStaticPositioned(element) {
return (this.getStyle(element, 'position') || 'static') === 'static';
}
/**
* @private
* @param {?} element
* @return {?}
*/
offsetParent(element) {
/** @type {?} */
let offsetParentEl = (/** @type {?} */ (element.offsetParent)) || this.documentRef.documentElement;
while (offsetParentEl && offsetParentEl !== this.documentRef.documentElement && this.isStaticPositioned(offsetParentEl)) {
offsetParentEl = (/** @type {?} */ (offsetParentEl.offsetParent));
}
return offsetParentEl || this.documentRef.documentElement;
}
}
PositionService.decorators = [
{ type: Injectable }
];
/** @nocollapse */
PositionService.ctorParameters = () => [
{ type: DocumentRef },
{ type: WindowRef }
];
if (false) {
/**
* @type {?}
* @private
*/
PositionService.prototype.documentRef;
/**
* @type {?}
* @private
*/
PositionService.prototype.windowRef;
}
/**
* @fileoverview added by tsickle
* Generated from: positioning.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class PositioningModule {
}
PositioningModule.decorators = [
{ type: NgModule, args: [{
providers: [PositionService, DocumentRef, WindowRef]
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: public-api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: ng-devui-position.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { PositionService, PositioningModule };
//# sourceMappingURL=ng-devui-position.js.map