@vtx/cs-map
Version:
React components for Vortex
127 lines (124 loc) • 6.93 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _cesium = require("cesium");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var getTop = function getTop(top, height, type) {
if (type === 'top') {
return top;
} else if (type === 'bottom') {
return top - height;
} else if (type === 'center') {
return top - height / 2;
}
return top;
};
var getLeft = function getLeft(left, width, type) {
if (type === 'left') {
return left;
} else if (type === 'right') {
return left - width;
} else if (type === 'center') {
return left - width / 2;
}
return left;
};
var getTopLeft = function getTopLeft(ele, canvasPosition, positioning) {
var width = ele.offsetWidth;
var height = ele.offsetHeight;
var top = canvasPosition.y;
var left = canvasPosition.x;
var _positioning$split = positioning.split('-'),
_positioning$split2 = _slicedToArray(_positioning$split, 2),
v = _positioning$split2[0],
h = _positioning$split2[1];
return [getTop(top, height, v), getLeft(left, width, h)];
};
var Dom = exports["default"] = /*#__PURE__*/function () {
function Dom(options) {
_classCallCheck(this, Dom);
var element = options.element,
position = options.position,
positioning = options.positioning,
map = options.map;
this.map = map;
this.element = element;
this.position = position;
this.positioning = positioning || 'bottom-center';
this.loadData();
}
_createClass(Dom, [{
key: "setPosition",
value: function setPosition(position) {
this.position = position;
this.loadData();
}
}, {
key: "loadData",
value: function loadData() {
var _this = this;
var map = this.map;
var position = this.position;
map.scene.preRender.removeEventListener(_this.render.bind(_this));
if (position !== null && position !== void 0 && position.length) {
this._style = {};
map.scene.preRender.addEventListener(_this.render.bind(_this));
}
}
}, {
key: "render",
value: function render() {
var map = this.map;
var position = this.position;
if (position !== null && position !== void 0 && position.length) {
var scratch = new _cesium.Cartesian2();
var point = _cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] || 0);
var canvasPosition = map.scene.cartesianToCanvasCoordinates(point, scratch);
if ((0, _cesium.defined)(canvasPosition)) {
var _getTopLeft = getTopLeft(this.element, canvasPosition, this.positioning),
_getTopLeft2 = _slicedToArray(_getTopLeft, 2),
top = _getTopLeft2[0],
left = _getTopLeft2[1];
if (this._style.left) {
this._style.left = this._style.left - left > 1 || this._style.left - left < -1 ? left : this._style.left;
} else {
this._style.left = left;
}
this._style.top = top;
this.element.style.top = top + "px";
this.element.style.left = left + "px";
// const cameraPosition = map.camera.position;
// let height = map.scene.globe.ellipsoid.cartesianToCartographic(cameraPosition)
// .height;
// height += map.scene.globe.ellipsoid.maximumRadius;
// if (!(Cartesian3.distance(cameraPosition, position) > height)) {
// this.element.style.display = 'block';
// } else {
// this.element.style.display = 'none';
// }
}
}
}
}, {
key: "destroy",
value: function destroy() {
var _this = this;
this.map.scene.preRender.removeEventListener(_this.render.bind(_this));
}
}]);
return Dom;
}();
//# sourceMappingURL=Dom.js.map