Skip to content

Commit

Permalink
feat(EventBindingMixin): move event names definition to caller
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Oct 25, 2014
1 parent dfec95f commit 4c5ca5d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module.exports = React.createClass({
return this._render(this.props, this.state);
},

get_event_names () {
return "bounds_changed center_changed click dblclick drag dragend dragstart heading_changed idle maptypeid_changed mousemove mouseout mouseover projection_changed resize rightclick tilesloaded tilt_changed zoom_changed";
},

_init_map () {
var {context} = this;
var {Map} = context.getApi();
Expand Down
4 changes: 4 additions & 0 deletions src/Marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module.exports = React.createClass({
return this._render(this.props, this.state);
},

get_event_names () {
return "animation_changed click clickable_changed cursor_changed dblclick drag dragend draggable_changed dragstart flat_changed icon_changed mousedown mouseout mouseover mouseup position_changed rightclick shape_changed title_changed visible_changed zindex_changed";
},

_init_marker () {
var {context} = this;
if (this.state.marker || !context.hasMap() || !context.getApi()) {
Expand Down
68 changes: 41 additions & 27 deletions src/mixins/EventBindingMixin.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,31 @@
/** @jsx React.DOM */
"use strict";

var EVENT_MAP = {};
var EVENT_LIST = "bounds_changed center_changed click dblclick drag dragend dragstart heading_changed idle maptypeid_changed mousemove mouseout mouseover projection_changed resize rightclick tilesloaded tilt_changed zoom_changed"
.split(" ")
.map(toEventList, EVENT_MAP);

function toEventList (name) {
var eventName = toEventName(name);
this[eventName] = name;
return eventName;
}

function toEventName(name) {
return `on${ name
.replace(/^(.)/, groupToUpperCase)
.replace(/_(.)/g, groupToUpperCase) }`;
}

function groupToUpperCase (match, group) {
return group.toUpperCase();
}

module.exports = {

getInitialState () {
return {
eventNames: []
_active_event_names: []
};
},

componentWillMount () {
this.setState({
eventNames: this._collect_event_names(this.props)
_active_event_names: this._collect_event_names(this.props)
});
},

componentWillReceiveProps (nextProps) {
this.setState({
eventNames: this._collect_event_names(nextProps)
_active_event_names: this._collect_event_names(nextProps)
});
},

add_listeners (instance) {
var {context, props} = this;
this.state.eventNames.forEach((eventName) => {
var name = EVENT_MAP[eventName];
var {_active_event_names, _event_map} = this.state;
_active_event_names.forEach((eventName) => {
var name = _event_map[eventName];
context.getApi().event.addListener(instance, name, props[eventName]);
});
},
Expand All @@ -55,9 +35,43 @@ module.exports = {
context.getApi().event.clearInstanceListeners(instance);
},

_get_event_list () {
var _event_list = this.state && this.state._event_list;
if (!_event_list) {
if (!this.get_event_names) {
throw new TypeError("Unimplemented get_event_names");
}
var _event_map = {};
_event_list = this.get_event_names()
.split(" ")
.map(toEventList, _event_map);
this.setState({
_event_list,
_event_map
});
}
return _event_list;
},

_collect_event_names (props) {
return EVENT_LIST.filter((eventName) => {
return this._get_event_list().filter((eventName) => {
return eventName in props;
});
}
};

function toEventList (name) {
var eventName = toEventName(name);
this[eventName] = name;
return eventName;
}

function toEventName(name) {
return `on${ name
.replace(/^(.)/, groupToUpperCase)
.replace(/_(.)/g, groupToUpperCase) }`;
}

function groupToUpperCase (match, group) {
return group.toUpperCase();
}

0 comments on commit 4c5ca5d

Please sign in to comment.