simple-react-ui
Version:
a simple react component library written in TypeScript+ React.js
74 lines • 2.95 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
/**
* BaiduMap 的 React 封装
*/
var BaiduMap = /** @class */ (function (_super) {
__extends(BaiduMap, _super);
function BaiduMap() {
return _super !== null && _super.apply(this, arguments) || this;
}
BaiduMap.prototype.componentWillMount = function () {
// 注意callback=init参数不能去掉,因为这是百度地图异步加载的接口,
// 否则,会因为React异步创建了script,百度返回的script中又调用document.write(),从而触发错误
var bmapSrc = "http://api.map.baidu.com/api?v=2.0&ak=" + this.props.ak + "&callback=init";
if (typeof BMap != 'undefined') {
return;
}
else {
var script = document.querySelector("script[src='" + bmapSrc + "']");
if (!script) {
script = document.createElement("script");
script.src = bmapSrc;
document.body.appendChild(script);
}
}
};
BaiduMap.prototype.componentDidMount = function () {
var _this = this;
function timeoutPromise(timeout) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, timeout);
});
}
function waitUntil(props) {
return new Promise(function (resolve, reject) {
var map = new BMap.Map(props.id);
resolve(map);
}).catch(function (err) {
console.log("there's no BMap yet. Waitting ...", err);
return timeoutPromise(200).then(function () {
return waitUntil(props);
});
});
}
waitUntil(this.props).then(function (map) {
console.log("[+] bmap loaded", map);
_this.props.callback(map);
});
};
BaiduMap.prototype.render = function () {
return React.createElement("div", { id: this.props.id });
};
BaiduMap.defaultProps = {
ak: '',
callback: function (map) { },
id: "itminus_bmap" + parseInt('' + Math.random() * 10000000),
};
return BaiduMap;
}(React.Component));
exports.default = BaiduMap;
//# sourceMappingURL=index.js.map