Skip to content

Commit b80b731

Browse files
committed
feat(ScriptjsGoogleMap): add "scriptjs" support
* Ref #92
1 parent 7e6d709 commit b80b731

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@
7676
"react": "^0.14.0",
7777
"react-dom": "^0.14.0",
7878
"rimraf": "^2.4.3",
79+
"scriptjs": "^2.5.8",
7980
"tomchentw-npm-dev": "^3.1.0"
8081
},
8182
"dependencies": {
8283
"can-use-dom": "^0.1.0",
8384
"google-maps-infobox": "^1.1.13",
84-
"invariant": "^2.1.1"
85+
"invariant": "^2.1.1",
86+
"warning": "^2.1.0"
8587
},
8688
"peerDependencies": {
8789
"react": "^0.14.0",

src/async/ScriptjsGoogleMap.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
default as React,
3+
Component,
4+
PropTypes,
5+
} from "react";
6+
7+
import {
8+
default as canUseDOM,
9+
} from "can-use-dom";
10+
11+
import {
12+
default as warning,
13+
} from "warning";
14+
15+
import {
16+
GoogleMap,
17+
} from "../index";
18+
19+
import {
20+
default as makeUrl,
21+
} from "../utils/makeUrl";
22+
23+
export default class ScriptjsGoogleMap extends Component {
24+
static propTypes = {
25+
// PropTypes for URL generation
26+
// https://nodejs.org/api/url.html#url_url_format_urlobj
27+
protocol: PropTypes.string,
28+
hostname: PropTypes.string.isRequired,
29+
port: PropTypes.number,
30+
pathname: PropTypes.string.isRequired,
31+
query: PropTypes.object.isRequired,
32+
// PropTypes for ScriptjsGoogleMap
33+
loadingElement: PropTypes.node,
34+
}
35+
36+
state = {
37+
isLoaded: false,
38+
}
39+
40+
componentWillMount () {
41+
if (!canUseDOM) {
42+
return;
43+
}
44+
const scriptjs = require("scriptjs");
45+
const {protocol, hostname, port, pathname, query, ...restProps} = this.props;
46+
const urlObj = {protocol, hostname, port, pathname, query};
47+
const url = makeUrl(urlObj);
48+
scriptjs(url, () => this.setState({ isLoaded: true }));
49+
}
50+
51+
componentWillReceiveProps (nextProps) {
52+
const changedKeys = Object.keys(ScriptjsGoogleMap.propTypes)
53+
.filter(key => this.props[key] !== nextProps[key]);
54+
55+
warning(0 === changedKeys.length, `ScriptjsGoogleMap doesn't support mutating props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`);
56+
}
57+
58+
render () {
59+
if (this.state.isLoaded) {
60+
const {protocol, hostname, port, pathname, query, ...restProps} = this.props;
61+
return (
62+
<GoogleMap {...restProps} />
63+
);
64+
} else {
65+
return this.props.loadingElement;
66+
}
67+
}
68+
}

src/utils/makeUrl.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
format as formatUrlObj,
3+
} from "url";
4+
5+
export default function makeUrl (urlObj) {
6+
return formatUrlObj({
7+
protocol: urlObj.protocol,
8+
hostname: urlObj.hostname,
9+
port: urlObj.port,
10+
pathname: urlObj.pathname,
11+
query: urlObj.query,
12+
});
13+
}

0 commit comments

Comments
 (0)