UNPKG

@duoduo-oba/ng-devui

Version:

DevUI components based on Angular

293 lines (287 loc) 12.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('ng-devui/window-ref')) : typeof define === 'function' && define.amd ? define('ng-devui/position', ['exports', '@angular/core', 'ng-devui/window-ref'], factory) : (global = global || self, factory((global['ng-devui'] = global['ng-devui'] || {}, global['ng-devui'].position = {}), global.ng.core, global['ng-devui']['window-ref'])); }(this, (function (exports, core, windowRef) { 'use strict'; /** * @fileoverview added by tsickle * Generated from: positioning.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var PositionService = /** @class */ (function () { function PositionService(documentRef, windowRef) { this.documentRef = documentRef; this.windowRef = windowRef; } /** * @param {?} element * @param {?=} round * @return {?} */ PositionService.prototype.position = /** * @param {?} element * @param {?=} round * @return {?} */ function (element, round) { if (round === void 0) { round = true; } /** @type {?} */ var elPosition; /** @type {?} */ var parentOffset = { width: 0, height: 0, top: 0, bottom: 0, left: 0, right: 0 }; if (this.getStyle(element, 'position') === 'fixed') { elPosition = element.getBoundingClientRect(); } else { /** @type {?} */ var 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 {?} */ PositionService.prototype.offset = /** * @param {?} element * @param {?=} round * @return {?} */ function (element, round) { if (round === void 0) { round = true; } /** @type {?} */ var elBcr = element.getBoundingClientRect(); /** @type {?} */ var viewportOffset = { top: this.windowRef.pageYOffset - this.documentRef.documentElement.clientTop, left: this.windowRef.pageXOffset - this.documentRef.documentElement.clientLeft }; /** @type {?} */ var 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 {?} */ PositionService.prototype.getScrollParent = /** * @param {?} element * @return {?} */ function (element) { /** @type {?} */ var style = getComputedStyle(element); /** @type {?} */ var excludeStaticParent = style.position === 'absolute'; /** @type {?} */ var overflowRegex = /(auto|scroll|hidden)/; if (style.position === 'fixed') { return this.documentRef.body; } for (var parent_1 = element; (parent_1 = parent_1.parentElement); parent_1.parentElement !== this.documentRef.body) { style = getComputedStyle(parent_1); if (excludeStaticParent && style.position === 'static') { continue; } if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) { return parent_1; } } return this.documentRef.body; }; /** * @param {?} hostElement * @param {?} targetElement * @param {?} placement * @param {?=} appendToBody * @return {?} */ PositionService.prototype.positionElements = /** * @param {?} hostElement * @param {?} targetElement * @param {?} placement * @param {?=} appendToBody * @return {?} */ function (hostElement, targetElement, placement, appendToBody) { /** @type {?} */ var hostElPosition = appendToBody ? this.offset(hostElement, false) : this.position(hostElement, false); /** @type {?} */ var shiftWidth = { left: hostElPosition.left, center: hostElPosition.left + hostElPosition.width / 2 - targetElement.offsetWidth / 2, right: hostElPosition.left + hostElPosition.width }; /** @type {?} */ var shiftHeight = { top: hostElPosition.top, center: hostElPosition.top + hostElPosition.height / 2 - targetElement.offsetHeight / 2, bottom: hostElPosition.top + hostElPosition.height }; /** @type {?} */ var targetElBCR = targetElement.getBoundingClientRect(); /** @type {?} */ var placementPrimary = placement.split('-')[0] || 'top'; /** @type {?} */ var placementSecondary = placement.split('-')[1] || 'center'; /** @type {?} */ var 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 {?} */ PositionService.prototype.getStyle = /** * @private * @param {?} element * @param {?} prop * @return {?} */ function (element, prop) { return this.windowRef.getComputedStyle(element)[prop]; }; /** * @private * @param {?} element * @return {?} */ PositionService.prototype.isStaticPositioned = /** * @private * @param {?} element * @return {?} */ function (element) { return (this.getStyle(element, 'position') || 'static') === 'static'; }; /** * @private * @param {?} element * @return {?} */ PositionService.prototype.offsetParent = /** * @private * @param {?} element * @return {?} */ function (element) { /** @type {?} */ var 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: core.Injectable } ]; /** @nocollapse */ PositionService.ctorParameters = function () { return [ { type: windowRef.DocumentRef }, { type: windowRef.WindowRef } ]; }; return PositionService; }()); 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 */ var PositioningModule = /** @class */ (function () { function PositioningModule() { } PositioningModule.decorators = [ { type: core.NgModule, args: [{ providers: [PositionService, windowRef.DocumentRef, windowRef.WindowRef] },] } ]; return PositioningModule; }()); exports.PositionService = PositionService; exports.PositioningModule = PositioningModule; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ng-devui-position.umd.js.map