Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-Refactoring for React +16.3 #121

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
"react/jsx-uses-vars": 2,
"react/react-in-jsx-scope": 2,
"no-var": 2,
"max-len": [2, 80],
"max-len": [2, 90],
"semi": 2
},
"plugins": [
"react"
],
"plugins": ["react"],
"ecmaFeatures": {
"jsx": true
}
Expand Down
130 changes: 76 additions & 54 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,88 @@
import React from 'react';
import ReactDOM from 'react-dom';
import createReactClass from 'create-react-class';
import {Gmaps} from '../dist';
import MapEvents from '../dist/events/map';
import MapEvents from '../src/events/map';
import Gmaps from '../src/components/gmaps.js';
import Marker from '../src/components/marker.js';
import Circle from '../src/components/circle.js';
import InfoWindow from '../src/components/info-window.js';
import Polyline from '../src/components/polyline.js';
import Polygon from '../src/components/polygon.js';

const styles = {
item: {
backgroundColor: 'white',
transition: 'background-color 0.2s linear'
},
cols: {
float: 'left'
}
item: {
backgroundColor: 'white',
transition: 'background-color 0.2s linear',
},
cols: {
float: 'left',
},
};

const App = createReactClass({
handler(_event) {
const item = ReactDOM.findDOMNode(this.refs[_event]);
item.style.backgroundColor = '#99ccff';
setTimeout(function() {
item.style.backgroundColor = styles.item.backgroundColor;
}, 500);
},

handler(_event) {
const item = ReactDOM.findDOMNode(this.refs[_event]);
item.style.backgroundColor = '#99ccff';
setTimeout(function() {
item.style.backgroundColor = styles.item.backgroundColor;
}, 500);
},

render() {

const events = [];
const handlers = {};
for (let _event in MapEvents) {
if (MapEvents.hasOwnProperty(_event)) {
events.push(
<li
key={MapEvents[_event]}
ref={MapEvents[_event]}
style={styles.item}
>
{MapEvents[_event]}
</li>
);
handlers[_event] = this.handler.bind(this, MapEvents[_event]);
}
}

return (
<div>
<Gmaps
width={'50%'}
height={'500px'}
style={styles.cols}
lat={51.5258541}
lng={-0.08040660000006028}
zoom={12}
{...handlers} />
<ul style={styles.cols}>
{events}
</ul>
</div>
);

}
render() {
const events = [];
const handlers = {};
for (let _event in MapEvents) {
if (MapEvents.hasOwnProperty(_event)) {
events.push(
<li key={MapEvents[_event]}
ref={MapEvents[_event]}
style={styles.item}>
{MapEvents[_event]}
</li>
);
handlers[_event] = this.handler.bind(this, MapEvents[_event]);
}
}

return (
<div>
<Gmaps
width={'50%'}
height={'500px'}
style={styles.cols}
lat={51.5258541}
lng={-0.08040660000006028}
zoom={12}
{...handlers}>
<Circle
lat={51.52}
lng={-0.08}
radius={5000}
strokeColor={'#4F5FB7'}
fillColor={'#4F5FB7'} />
<InfoWindow
lat={51.52}
lng={-0.08}
content={'Hey there!'} />
<Marker
lat={51.52}
lng={-0.08} />
<Polyline
path={[
{lat: 51.5, lng: -0.11},
{lat: 51.53, lng: -0.04},
]} />
<Polygon paths={[
{lat: 51.52, lng: -0.125},
{lat: 51.5, lng: -0.11},
{lat: 51.51, lng: -0.06},
{lat: 51.53, lng: -0.04},
{lat: 51.53, lng: -0.09}]} />
</Gmaps>
<ul style={styles.cols}>{events}</ul>
</div>
);
},
});

ReactDOM.render(<App />, document.getElementById('gmaps'));
ReactDOM.render(<App />, document.getElementById('gmaps'));
18 changes: 18 additions & 0 deletions dist/components/circle2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _entity2 = require('./entity2');

var _entity22 = _interopRequireDefault(_entity2);

var _eventsCircle = require('../events/circle');

var _eventsCircle2 = _interopRequireDefault(_eventsCircle);

exports['default'] = (0, _entity22['default'])('Circle', 'center', _eventsCircle2['default']);
module.exports = exports['default'];
146 changes: 95 additions & 51 deletions dist/components/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,122 @@ Object.defineProperty(exports, '__esModule', {

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = (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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var _react = require('react');

var _react2 = _interopRequireDefault(_react);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

var _createReactClass = require('create-react-class');
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _createReactClass2 = _interopRequireDefault(_createReactClass);
var _react = require('react');

var _mixinsListener = require('../mixins/listener');
var _react2 = _interopRequireDefault(_react);

var _mixinsListener2 = _interopRequireDefault(_mixinsListener);
// import createReactClass from 'create-react-class';
// import Listener from '../mixins/listener';

var _utilsCompareProps = require('../utils/compare-props');

var _utilsCompareProps2 = _interopRequireDefault(_utilsCompareProps);

// refactored to React class, then to React +16.3

exports['default'] = function (name, latLngProp, events) {
return (0, _createReactClass2['default'])({
return (function (_React$Component) {
_inherits(CreateEntity, _React$Component);

mixins: [_mixinsListener2['default']],
function CreateEntity() {
_classCallCheck(this, CreateEntity);

entity: null,
_get(Object.getPrototypeOf(CreateEntity.prototype), 'constructor', this).apply(this, arguments);

componentDidMount: function componentDidMount() {
var options = this.getOptions(this.props);
this.entity = new google.maps[name](options);
this.addListeners(this.entity, events);
},
this.entity = null;
}

componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
if (!(0, _utilsCompareProps2['default'])(this.props, nextProps)) {
var options = this.getOptions(nextProps);
this.entity.setOptions(options);
_createClass(CreateEntity, [{
key: 'componentDidMount',
value: function componentDidMount() {
var options = this.getOptions(this.props);
this.entity = new google.maps[name](options);
this.addListeners(this.entity, events);
}
},

componentWillUnmount: function componentWillUnmount() {
this.entity.setMap(null);
this.removeListeners();
this.entity = null;
},

getEntity: function getEntity() {
return this.entity;
},

getOptions: function getOptions(props) {
return _extends({}, props, _defineProperty({}, latLngProp, this.switchPaths(name, props)));
},

switchPaths: function switchPaths(name, props) {
switch (name) {
case 'Polyline':
return props.path;
break;
case 'Polygon':
return props.paths;
break;
default:
return new google.maps.LatLng(props.lat, props.lng);
break;
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (!(0, _utilsCompareProps2['default'])(this.props, nextProps)) {
var options = this.getOptions(nextProps);
this.entity.setOptions(options);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.entity.setMap(null);
this.removeListeners();
this.entity = null;
}
}, {
key: 'getEntity',
value: function getEntity() {
return this.entity;
}
}, {
key: 'getOptions',
value: function getOptions(props) {
return _extends({}, props, _defineProperty({}, latLngProp, this.switchPaths(name, props)));
}
},
}, {
key: 'switchPaths',
value: function switchPaths(name, props) {
switch (name) {
case 'Polyline':
return props.path;
break;
case 'Polygon':
return props.paths;
break;
default:
return new google.maps.LatLng(props.lat, props.lng);
break;
}
}
}, {
key: 'addListeners',
value: function addListeners(entity, events) {
for (var prop in this.props) {
if (this.props.hasOwnProperty(prop) && events[prop]) {
var addListener = google.maps.event.addListener;
var listener = addListener(entity, events[prop], this.props[prop]);
if (!this.listeners) {
this.listeners = [];
}
this.listeners.push(listener);
}
}
}
}, {
key: 'removeListeners',
value: function removeListeners() {
if (window.google && this.listeners) {
this.listeners.forEach(function (listener) {
google.maps.event.removeListener(listener);
});
}
}
}, {
key: 'render',
value: function render() {
return null;
}
}]);

render: function render() {
return null;
}
});
return CreateEntity;
})(_react2['default'].Component);
};

module.exports = exports['default'];
Loading