-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EventBindingMixin): extract event binding part out
- Loading branch information
Showing
2 changed files
with
72 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** @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: [] | ||
}; | ||
}, | ||
|
||
componentWillMount () { | ||
this.setState({ | ||
eventNames: this._collect_event_names(this.props) | ||
}); | ||
}, | ||
|
||
componentWillReceiveProps (nextProps) { | ||
this.setState({ | ||
eventNames: this._collect_event_names(nextProps) | ||
}); | ||
}, | ||
|
||
upsert_listeners (googleMapsApi, map) { | ||
this.clear_listeners(googleMapsApi, map); | ||
this.state.eventNames.forEach((eventName) => { | ||
var name = EVENT_MAP[eventName]; | ||
googleMapsApi.event.addListener(map, name, this.props[eventName]); | ||
}); | ||
}, | ||
|
||
clear_listeners (googleMapsApi, map) { | ||
googleMapsApi.event.clearInstanceListeners(map); | ||
}, | ||
|
||
_collect_event_names (props) { | ||
return EVENT_LIST.filter((eventName) => { | ||
return eventName in props; | ||
}); | ||
} | ||
}; |