Don't exit drawing mode on hashchange unless the user has moved a lot

(Switching background while drawing a line was causing the line to get
cancelled because of returning to browse mode)
(closes #4099)
This commit is contained in:
Bryan Housel
2017-06-12 02:04:22 -04:00
parent a727c31221
commit e9568e8845
+12 -3
View File
@@ -1,5 +1,6 @@
import * as d3 from 'd3';
import _ from 'lodash';
import { geoSphericalDistance } from '../geo';
import { modeBrowse } from '../modes';
import { utilQsString, utilStringQs } from '../util';
@@ -12,13 +13,21 @@ export function behaviorHash(context) {
var parser = function(map, s) {
var q = utilStringQs(s);
var args = (q.map || '').split('/').map(Number);
if (args.length < 3 || args.some(isNaN)) {
return true; // replace bogus hash
} else if (s !== formatter(map).slice(1)) {
var mode = context.mode();
if (mode && mode.id.match(/^draw/) !== null) {
} else if (s !== formatter(map).slice(1)) { // hash has changed
var mode = context.mode(),
dist = geoSphericalDistance(map.center(), [args[2], args[1]]),
maxdist = 500;
// Don't allow the hash location to change too much while drawing
// This can happen if the user accidently hit the back button. #3996
if (mode && mode.id.match(/^draw/) !== null && dist > maxdist) {
context.enter(modeBrowse(context));
}
map.centerZoom([args[2], Math.min(lat, Math.max(-lat, args[1]))], args[0]);
}
};