Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created July 5, 2016 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/f1f33bf99f28d81e3054fcc4815081ee to your computer and use it in GitHub Desktop.
Save tmcw/f1f33bf99f28d81e3054fcc4815081ee to your computer and use it in GitHub Desktop.
/* @flow */
var MapStateConstants = require('../constants/mapstate_constants');
var SharedConstants = require('../constants/shared_constants');
var Immutable = require('immutable');
var zoomRange = require('../data/zoom_range');
function storeCreator(source) {
const initialState = Immutable.fromJS({
zoom: 0,
bearing: 0,
center: [0, 0],
pitch: 0,
debug: false,
collisionDebug: false,
backgroundStyleURL: 'mapbox://styles/mapbox/dataset-dark-v1'
});
return (state: Immutable.Map = initialState, action: Object): Immutable.Map => {
if (action.source !== source) return;
switch (action.type) {
case SharedConstants.SHARED_PURGE:
return initialState;
case MapStateConstants.MAPSTATE_UPDATE:
return state.mergeDeep(Immutable.fromJS(action.value));
case MapStateConstants.MAPSTATE_CLEAN:
return state.set('dirty', false);
case MapStateConstants.MAPSTATE_ZOOM_IN:
return state.update('zoom', zoom => {
return Math.min(Math.round(zoom + 1), zoomRange.MAX_ZOOM);
}).set('dirty', true);
case MapStateConstants.MAPSTATE_ZOOM_OUT:
return state.update('zoom', zoom => {
return Math.max(Math.round(zoom - 1), zoomRange.MIN_ZOOM);
}).set('dirty', true);
case MapStateConstants.MAPSTATE_ZOOM_SET:
return state.set('zoom', action.value).set('dirty', true);
case MapStateConstants.MAPSTATE_DEBUG:
return state.set('debug', action.value).set('dirty', true);
case MapStateConstants.MAPSTATE_COLLISION_DEBUG:
return state.set('collisionDebug', action.value).set('dirty', true);
case MapStateConstants.MAPSTATE_RESET_NORTH:
return state.set('bearing', 0).set('dirty', true);
case MapStateConstants.MAPSTATE_SET_BACKGROUND_STYLE_URL:
return state.set('backgroundStyleURL', action.backgroundStyleURL);
case MapStateConstants.MAPSTATE_RESET_PITCH:
return state.set('pitch', 0).set('dirty', true);
case MapStateConstants.MAPSTATE_VIEW:
return state
.set('zoom', action.zoom)
.set('center', Immutable.fromJS(action.center))
.set('bearing', action.bearing)
.set('pitch', action.pitch)
.set('dirty', true);
}
return state;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment