Skip to content

Commit 0d02861

Browse files
committed
Initial check-in
0 parents  commit 0d02861

21 files changed

+841
-0
lines changed

.babelrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": ["es2015", "react", "stage-2"],
3+
"env": {
4+
"development": {
5+
"presets": ["react-hmre"]
6+
},
7+
"test": {
8+
"presets": []
9+
}
10+
}
11+
}

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GAPI_KEY=FILL_IN_YOUR_KEY_HERE

.eslintrc

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"parser": "babel-eslint",
3+
"env": {
4+
"browser": true,
5+
"node": true,
6+
"es6": true
7+
},
8+
"ecmaFeatures": {
9+
"modules": true
10+
},
11+
"rules": {
12+
"no-bitwise": 2,
13+
"no-else-return": 2,
14+
"no-eq-null": 2,
15+
"no-extra-parens": 0,
16+
"no-floating-decimal": 2,
17+
"no-inner-declarations": [2, "both"],
18+
"no-lonely-if": 2,
19+
"no-multiple-empty-lines": [2, {"max": 3}],
20+
"no-self-compare": 2,
21+
"no-underscore-dangle": 0,
22+
"no-use-before-define": 0,
23+
"no-unused-expressions": 0,
24+
"no-void": 2,
25+
"brace-style": [2, "1tbs"],
26+
"camelcase": [1, {"properties": "never"}],
27+
"consistent-return": 0,
28+
"comma-style": [2, "last"],
29+
"complexity": [1, 12],
30+
"func-names": 0,
31+
"guard-for-in": 2,
32+
"indent": [2, 4],
33+
"max-len": [0, 120, 4],
34+
"new-cap": [2, {"newIsCap": true, "capIsNew": false}],
35+
"quotes": [2, "single"],
36+
"keyword-spacing": [2, {"before": true, "after": true}],
37+
"space-before-blocks": [2, "always"],
38+
"array-bracket-spacing": [2, "never"],
39+
"space-in-parens": [2, "never"],
40+
"strict": [0],
41+
"valid-jsdoc": 2,
42+
"wrap-iife": [2, "any"],
43+
"yoda": [1, "never"]
44+
},
45+
"plugins": [
46+
"react"
47+
],
48+
"globals": {
49+
50+
}
51+
}

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.swp
2+
*~
3+
*.iml
4+
.*.haste_cache.*
5+
.DS_Store
6+
.idea
7+
npm-debug.log
8+
node_modules
9+
.env

.npmignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.swp
2+
*~
3+
*.iml
4+
.*.haste_cache.*
5+
.DS_Store
6+
.idea
7+
.babelrc
8+
.eslintrc
9+
npm-debug.log
10+
lib

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Julian Ćwirko <julian.io>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Google Map

examples/basic.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
4+
import GoogleApiComponent from '../src/GoogleApiComponent'
5+
import Map from '../src/index'
6+
import Marker from '../src/MarkerComponent'
7+
8+
9+
const Container = React.createClass({
10+
onMapMoved: function(map) {
11+
const center = map.center;
12+
},
13+
14+
render: function() {
15+
if (!this.props.loaded) {
16+
return <div>Loading...</div>
17+
}
18+
return (
19+
<div ref='map' style={{width: '100vw', height: '100vh'}}>
20+
<Map google={this.props.google}
21+
onMove={this.onMapMoved}>
22+
<Marker />
23+
</Map>
24+
</div>
25+
)
26+
}
27+
});
28+
29+
const Wrapped = GoogleApiComponent({
30+
apiKey: __GAPI_KEY__
31+
})(Container)
32+
33+
const mountNode = document.querySelector('#root')
34+
ReactDOM.render(<Wrapped />, mountNode)

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/index');

package.json

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "GoogleMaps",
3+
"version": "1.0.0",
4+
"description": "Google maps container",
5+
"author": "Ari Lerner",
6+
"license": "MIT",
7+
"options": {
8+
"mocha": "--require scripts/mocha_runner -t rewireify src/**/__tests__/**/*.js"
9+
},
10+
"scripts": {
11+
"prepublish": "babel --plugins 'transform-es2015-modules-umd' src --ignore __tests__ --out-dir ./dist",
12+
"dev": "NODE_ENV=development ./node_modules/hjs-webpack/bin/hjs-dev-server.js",
13+
"lint": "eslint ./src",
14+
"lintfix": "eslint ./src --fix",
15+
"testonly": "NODE_ENV=test mocha $npm_package_options_mocha",
16+
"test": "npm run lint && npm run testonly",
17+
"test-watch": "npm run testonly -- --watch --watch-extensions js"
18+
},
19+
"devDependencies": {
20+
"babel-cli": "^6.6.4",
21+
"babel-core": "^6.7.4",
22+
"babel-eslint": "^6.0.2",
23+
"babel-loader": "^6.2.4",
24+
"babel-plugin-transform-es2015-modules-umd": "^6.6.5",
25+
"babel-polyfill": "^6.7.4",
26+
"babel-preset-es2015": "^6.6.0",
27+
"babel-preset-react": "^6.5.0",
28+
"babel-preset-react-hmre": "^1.1.1",
29+
"babel-preset-stage-2": "^6.5.0",
30+
"chai": "^3.5.0",
31+
"css-loader": "^0.23.1",
32+
"dotenv": "^2.0.0",
33+
"enzyme": "^2.2.0",
34+
"eslint": "^2.7.0",
35+
"eslint-plugin-babel": "^3.1.0",
36+
"eslint-plugin-react": "^4.2.3",
37+
"hjs-webpack": "^8.1.0",
38+
"jsdom": "^8.1.0",
39+
"mocha": "^2.4.5",
40+
"nodemon": "^1.9.1",
41+
"postcss-loader": "^0.8.2",
42+
"react": "^15.0.0",
43+
"react-addons-test-utils": "^15.0.0",
44+
"react-dom": "^15.0.0",
45+
"sinon": "^1.17.3",
46+
"style-loader": "^0.13.1",
47+
"webpack": "^1.13.0"
48+
},
49+
"peerDependencies": {
50+
"react": "~0.14.8 || ^15.0.0",
51+
"react-dom": "~0.14.8 || ^15.0.0"
52+
},
53+
"dependencies": {
54+
"babel-runtime": "^6.6.1"
55+
}
56+
}

scripts/mocha_runner.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require('babel-core/register');
2+
require('babel-polyfill');
3+
4+
var jsdom = require('jsdom').jsdom;
5+
6+
var exposedProperties = ['window', 'navigator', 'document'];
7+
8+
global.document = jsdom('');
9+
global.window = document.defaultView;
10+
Object.keys(document.defaultView).forEach((property) => {
11+
if (typeof global[property] === 'undefined') {
12+
exposedProperties.push(property);
13+
global[property] = document.defaultView[property];
14+
}
15+
});
16+
17+
global.navigator = {
18+
userAgent: 'node.js'
19+
};
20+
21+
documentRef = document;

src/GoogleApiComponent.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, {PropTypes as T} from 'react'
2+
import ReactDOM from 'react-dom'
3+
4+
import {ScriptCache} from './lib/ScriptCache'
5+
import GoogleApi from './lib/GoogleApi'
6+
7+
const defaultMapConfig = {}
8+
const defaultCreateCache = (options) => {
9+
options = options || {};
10+
const apiKey = options.apiKey;
11+
const libraries = options.libraries || ['places'];
12+
13+
return ScriptCache({
14+
google: GoogleApi({apiKey: apiKey, libraries: libraries})
15+
});
16+
};
17+
18+
export const wrapper = (options) => (WrappedComponent) => {
19+
const apiKey = options.apiKey;
20+
const libraries = options.libraries || ['places'];
21+
const createCache = options.createCache || defaultCreateCache;
22+
23+
class Wrapper extends React.Component {
24+
constructor(props, context) {
25+
super(props, context);
26+
27+
this.scriptCache = createCache(options);
28+
this.scriptCache.google.onLoad(this.onLoad.bind(this))
29+
30+
this.state = {
31+
loaded: false,
32+
map: null,
33+
google: null
34+
}
35+
}
36+
37+
onLoad(err, tag) {
38+
this._gapi = window.google;
39+
40+
this.setState({loaded: true, google: this._gapi})
41+
}
42+
43+
render() {
44+
const props = Object.assign({}, this.props, {
45+
loaded: this.state.loaded,
46+
google: window.google
47+
});
48+
49+
return (
50+
<div>
51+
<WrappedComponent {...props}/>
52+
<div ref='map'/>
53+
</div>
54+
)
55+
}
56+
}
57+
58+
return Wrapper;
59+
}
60+
61+
export default wrapper;

src/MarkerComponent.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
3+
export class Marker extends React.Component {
4+
5+
componentDidUpdate(prevProps) {
6+
console.log('prevProps in Marker', prevProps);
7+
8+
if ((this.props.mapCenter !== prevProps.mapCenter) ||
9+
(this.props.map !== prevProps.map)) {
10+
const {name, map, google, mapCenter} = this.props;
11+
12+
const pref = Object.assign({}, {
13+
map: map,
14+
position: mapCenter,
15+
name: name
16+
})
17+
this.marker = new google.maps.Marker(pref);
18+
}
19+
}
20+
21+
render() {
22+
return (<div className='marker' />)
23+
}
24+
}
25+
26+
export default Marker

src/__tests__/GoogleApi.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
3+
import {shallow, mount, render} from 'enzyme';
4+
import {expect} from 'chai';
5+
import sinon from 'sinon';
6+
7+
import GoogleApi from '../lib/GoogleApi'
8+
9+
const base = 'https://maps.googleapis.com/maps/api/js'
10+
11+
describe('GoogleApi', () => {
12+
it('loads a url from google api', () => {
13+
expect(GoogleApi({apiKey: '123'}).indexOf(base)).to.be.at.least(0);
14+
});
15+
16+
describe('apiKey', () => {
17+
it('appends the apiKey to the url', () => {
18+
expect(GoogleApi({apiKey: 'abc-123-456'}).indexOf('abc-123-456')).to.be.at.least(0);
19+
});
20+
})
21+
22+
describe('libraries', () => {
23+
let url;
24+
beforeEach(() => {
25+
url = GoogleApi({
26+
apiKey: 'abc-123-456',
27+
libraries: ['places', 'people', 'animals']
28+
})
29+
})
30+
31+
it('adds libraries', () => {
32+
expect(url.indexOf('places,people,animals')).to.be.at.least(0);
33+
});
34+
35+
it('includes places library by default', () => {
36+
url = GoogleApi({apiKey: 'abc-123-456'});
37+
expect(url.indexOf('places')).to.be.at.least(0);
38+
})
39+
})
40+
41+
describe('version', () => {
42+
it('adds the google version', () => {
43+
const url = GoogleApi({apiKey: 'abc-123-456', version: '2016'});
44+
expect(url.indexOf('v=2016')).to.be.above(0);
45+
})
46+
})
47+
48+
})

0 commit comments

Comments
 (0)