Skip to content

Commit

Permalink
feat(ScriptjsGoogleMap): add "scriptjs" support
Browse files Browse the repository at this point in the history
* Ref #92
  • Loading branch information
tomchentw committed Nov 16, 2015
1 parent 7e6d709 commit b80b731
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@
"react": "^0.14.0",
"react-dom": "^0.14.0",
"rimraf": "^2.4.3",
"scriptjs": "^2.5.8",
"tomchentw-npm-dev": "^3.1.0"
},
"dependencies": {
"can-use-dom": "^0.1.0",
"google-maps-infobox": "^1.1.13",
"invariant": "^2.1.1"
"invariant": "^2.1.1",
"warning": "^2.1.0"
},
"peerDependencies": {
"react": "^0.14.0",
Expand Down
68 changes: 68 additions & 0 deletions src/async/ScriptjsGoogleMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
default as React,
Component,
PropTypes,
} from "react";

import {
default as canUseDOM,
} from "can-use-dom";

import {
default as warning,
} from "warning";

import {
GoogleMap,
} from "../index";

import {
default as makeUrl,
} from "../utils/makeUrl";

export default class ScriptjsGoogleMap extends Component {
static propTypes = {
// PropTypes for URL generation
// https://nodejs.org/api/url.html#url_url_format_urlobj
protocol: PropTypes.string,
hostname: PropTypes.string.isRequired,
port: PropTypes.number,
pathname: PropTypes.string.isRequired,
query: PropTypes.object.isRequired,
// PropTypes for ScriptjsGoogleMap
loadingElement: PropTypes.node,
}

state = {
isLoaded: false,
}

componentWillMount () {
if (!canUseDOM) {
return;
}
const scriptjs = require("scriptjs");
const {protocol, hostname, port, pathname, query, ...restProps} = this.props;
const urlObj = {protocol, hostname, port, pathname, query};
const url = makeUrl(urlObj);
scriptjs(url, () => this.setState({ isLoaded: true }));
}

componentWillReceiveProps (nextProps) {
const changedKeys = Object.keys(ScriptjsGoogleMap.propTypes)
.filter(key => this.props[key] !== nextProps[key]);

warning(0 === changedKeys.length, `ScriptjsGoogleMap doesn't support mutating props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`);
}

render () {
if (this.state.isLoaded) {
const {protocol, hostname, port, pathname, query, ...restProps} = this.props;
return (
<GoogleMap {...restProps} />
);
} else {
return this.props.loadingElement;
}
}
}
13 changes: 13 additions & 0 deletions src/utils/makeUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
format as formatUrlObj,
} from "url";

export default function makeUrl (urlObj) {
return formatUrlObj({
protocol: urlObj.protocol,
hostname: urlObj.hostname,
port: urlObj.port,
pathname: urlObj.pathname,
query: urlObj.query,
});
}

0 comments on commit b80b731

Please sign in to comment.