amos-tool
Version:
amos ui tool
59 lines (55 loc) • 2.72 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
function getDocumentScroll() {
var t = document, e = t.documentElement, i = t.body;
return [ e && e.scrollTop || i.scrollTop, e && e.scrollLeft || i.scrollLeft ];
}
function PositionFactory(t, e, i, r, n) {
var o = getDocumentScroll(), c = (0, _slicedToArray2.default)(o, 2), l = c[0], g = c[1], u = PositionFactory;
if (u.triggerRect = t.getBoundingClientRect(), u.popoverRect = e.getBoundingClientRect(),
u.direction = u.getComputedDirection(i), u.align = u.getComputedAlign(r), u.center = [ u.triggerRect.left + u.triggerRect.width / 2 + g, u.triggerRect.top + u.triggerRect.height / 2 + l ],
!n) {
var s = u.coordinateMap[u.direction].call(u), h = (0, _slicedToArray2.default)(s, 2), d = h[0], p = h[1];
e.style.left = "".concat(d, "px"), e.style.top = "".concat(p, "px");
}
return [ u.direction, u.align ];
}
(0, _extends2.default)(PositionFactory, {
triggerRect: null,
popoverRect: null,
align: null,
center: null,
getComputedDirection: function(t) {
var e = this.triggerRect, i = this.popoverRect;
return "up" !== t && "down" !== t || (e.top < i.height ? t = "down" : i.height + e.top + e.height > window.innerHeight && (t = "up")),
t;
},
getComputedAlign: function(t) {
if ("middle" === t) return t;
if ("up" === this.direction || "down" === this.direction) {
if ("top" === t || "bottom" === t) return "middle";
} else if ("left" === t || "right" === t) return "middle";
return t;
},
getAlignPosition: function(t) {
var e = t ? [ "left", "right" ] : [ "top", "bottom" ], i = t ? 0 : 1, r = t ? "width" : "height";
return this.align === e[0] ? this.center[i] - this.triggerRect[r] / 2 : this.align === e[1] ? this.center[i] - this.popoverRect[r] + this.triggerRect[r] / 2 : this.center[i] - this.popoverRect[r] / 2;
},
coordinateMap: {
up: function() {
var t = this.center[1] - this.triggerRect.height / 2 - this.popoverRect.height;
return [ this.getAlignPosition(!0), t ];
},
down: function() {
var t = this.center[1] + this.triggerRect.height / 2;
return [ this.getAlignPosition(!0), t ];
},
left: function() {
var t = this.center[0] - this.triggerRect.width / 2 - this.popoverRect.width;
return [ t, this.getAlignPosition() ];
},
right: function() {
return [ this.center[0] + this.triggerRect.width / 2, this.getAlignPosition() ];
}
}
}), module.exports = PositionFactory;