ng2-split-pane-patch
Version:
A simple resizable split pane Angular 2 library
120 lines • 4.62 kB
JavaScript
export class PositionService {
static position(element) {
let nativeEl = element.nativeElement;
let elBCR = this.offset(nativeEl);
let offsetParentBCR = { top: 0, left: 0 };
let offsetParentEl = this.parentOffsetEl(nativeEl);
if (offsetParentEl !== this.document) {
offsetParentBCR = this.offset(offsetParentEl);
offsetParentBCR.top += offsetParentEl.clientTop - offsetParentEl.scrollTop;
offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
}
let boundingClientRect = nativeEl.getBoundingClientRect();
return {
width: boundingClientRect.width || nativeEl.offsetWidth,
height: boundingClientRect.height || nativeEl.offsetHeight,
top: elBCR.top - offsetParentBCR.top,
left: elBCR.left - offsetParentBCR.left
};
}
static offset(element) {
let nativeEl = element.nativeElement;
let boundingClientRect = nativeEl.getBoundingClientRect();
return {
width: boundingClientRect.width || nativeEl.offsetWidth,
height: boundingClientRect.height || nativeEl.offsetHeight,
top: boundingClientRect.top + (this.window.pageYOffset || this.document.documentElement.scrollTop),
left: boundingClientRect.left + (this.window.pageXOffset || this.document.documentElement.scrollLeft)
};
}
static positionElements(host, target, positionStr, appendToBody) {
let hostEl = host.nativeElement;
let targetEl = target.nativeElement;
let positionStrParts = positionStr.split('-');
let pos0 = positionStrParts[0];
let pos1 = positionStrParts[1] || 'center';
let hostElPos = appendToBody ?
this.offset(hostEl) :
this.position(hostEl);
let targetElWidth = targetEl.offsetWidth;
let targetElHeight = targetEl.offsetHeight;
let shiftWidth = {
center: function () {
return hostElPos.left + hostElPos.width / 2 - targetElWidth / 2;
},
left: function () {
return hostElPos.left;
},
right: function () {
return hostElPos.left + hostElPos.width;
}
};
let shiftHeight = {
center: function () {
return hostElPos.top + hostElPos.height / 2 - targetElHeight / 2;
},
top: function () {
return hostElPos.top;
},
bottom: function () {
return hostElPos.top + hostElPos.height;
}
};
let targetElPos;
switch (pos0) {
case 'right':
targetElPos = {
top: shiftHeight[pos1](),
left: shiftWidth[pos0]()
};
break;
case 'left':
targetElPos = {
top: shiftHeight[pos1](),
left: hostElPos.left - targetElWidth
};
break;
case 'bottom':
targetElPos = {
top: shiftHeight[pos0](),
left: shiftWidth[pos1]()
};
break;
default:
targetElPos = {
top: hostElPos.top - targetElHeight,
left: shiftWidth[pos1]()
};
break;
}
return targetElPos;
}
static get window() {
return window;
}
static get document() {
return window.document;
}
static getStyle(nativeEl, cssProp) {
if (nativeEl.currentStyle) {
return nativeEl.currentStyle[cssProp];
}
if (this.window.getComputedStyle) {
return this.window.getComputedStyle(nativeEl)[cssProp];
}
return nativeEl.style[cssProp];
}
static isStaticPositioned(nativeEl) {
return (this.getStyle(nativeEl, 'position') || 'static') === 'static';
}
static parentOffsetEl(nativeEl) {
let offsetParent = nativeEl.offsetParent || this.document;
while (offsetParent && offsetParent !== this.document &&
this.isStaticPositioned(offsetParent)) {
offsetParent = offsetParent.offsetParent;
}
return offsetParent || this.document;
}
;
}
//# sourceMappingURL=position.service.js.map