Skip to content

Commit cf5afe8

Browse files
Alex Leithmcwhittemore
Alex Leith
authored andcommitted
Store initial map interaction settings (#696)
* working store and restore for map settings * first start at storing initial state of map interaction * Fix function names for store and restore map config, remove argument * Ensure that the store only checks if the map context is available. Included comments. * Revert "Ensure that the store only checks if the map context is available. Included comments." This reverts commit 816aef3. * Check for undefined variables, rather than false. * Ensure that the map and context are valid before checking values. * Remove call count checks. These aren't valid. * This call doesn't exist, so this is a bug fix. * Handle setting up mocks to better replicate the MapBoxGL object for testing. * Include store functions in list for test. * add another check for store being in context * Handle constants differently. * Fix bugs in store functions around setting and getting interactions * Fix an issue with the store's map not being passed in properly. * Mock the interactions better. * Add a set of tests for the three store/restore/check interaction functions. * update yarn.lock * try a different object iterator to fix travis build issue
1 parent 271774e commit cf5afe8

11 files changed

+131
-31
lines changed

debug/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<div id='start-polygon'>POLYGON</div>
4242
</div>
4343
<div class='toggle'>
44+
<button id='doubleClickZoom'>disable dblclick zoom</button>
4445
<button id='addBtn'>add draw</button>
4546
<button id='removeBtn'>remove draw</button>
4647
<button id='flipStyleBtn'>change style</button>
@@ -86,10 +87,12 @@
8687
map.on('load', function() {
8788

8889
// toggle
90+
var doubleClickZoom = document.getElementById('doubleClickZoom');
8991
var addButton = document.getElementById('addBtn');
9092
var removeButton = document.getElementById('removeBtn');
9193
var flipStyleButton = document.getElementById('flipStyleBtn');
9294
var currentStyle = 'streets-v9';
95+
var doubleClickZoomOn = true;
9396
addButton.onclick = function() {
9497
if (drawIsActive) return;
9598
drawIsActive = true;
@@ -105,6 +108,17 @@
105108
map.setStyle('mapbox://styles/mapbox/' + style);
106109
currentStyle = style;
107110
}
111+
doubleClickZoom.onclick = function() {
112+
if (doubleClickZoomOn) {
113+
doubleClickZoomOn = false;
114+
map.doubleClickZoom.disable();
115+
doubleClickZoom.innerText = 'enable dblclick zoom'
116+
} else {
117+
map.doubleClickZoom.enable();
118+
doubleClickZoomOn = true;
119+
doubleClickZoom.innerText = 'disable dblclick zoom'
120+
}
121+
}
108122

109123
var startPoint = document.getElementById('start-point');
110124
var startLine = document.getElementById('start-line');

src/constants.js

+9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ module.exports = {
7373
ACTIVE: 'true',
7474
INACTIVE: 'false'
7575
},
76+
interactions: [
77+
'scrollZoom',
78+
'boxZoom',
79+
'dragRotate',
80+
'dragPan',
81+
'keyboard',
82+
'doubleClickZoom',
83+
'touchZoomRotate'
84+
],
7685
LAT_MIN: -90,
7786
LAT_RENDERED_MIN: -85,
7887
LAT_MAX: 90,

src/lib/double_click_zoom.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
module.exports = {
22
enable(ctx) {
33
setTimeout(() => {
4-
if (!ctx.map || !ctx.map.doubleClickZoom) return;
4+
// First check we've got a map and some context.
5+
if (!ctx.map || !ctx.map.doubleClickZoom || !ctx._ctx || !ctx._ctx.store || !ctx._ctx.store.getInitialConfigValue) return;
6+
// Now check initial state wasn't false (we leave it disabled if so)
7+
if (!ctx._ctx.store.getInitialConfigValue('doubleClickZoom')) return;
58
ctx.map.doubleClickZoom.enable();
69
}, 0);
710
},
811
disable(ctx) {
912
setTimeout(() => {
1013
if (!ctx.map || !ctx.map.doubleClickZoom) return;
14+
// Always disable here, as it's necessary in some cases.
1115
ctx.map.doubleClickZoom.disable();
1216
}, 0);
1317
}

src/setup.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = function(ctx) {
1010
const setup = {
1111
onRemove: function() {
1212
setup.removeLayers();
13+
ctx.store.restoreMapConfig();
1314
ctx.ui.removeButtons();
1415
ctx.events.removeEventListeners();
1516
ctx.map = null;
@@ -28,6 +29,7 @@ module.exports = function(ctx) {
2829
ctx.container = map.getContainer();
2930
ctx.store = new Store(ctx);
3031

32+
3133
controlContainer = ctx.ui.addButtons();
3234

3335
if (ctx.options.boxSelect) {
@@ -44,6 +46,7 @@ module.exports = function(ctx) {
4446
map.off('load', connect);
4547
clearInterval(intervalId);
4648
setup.addLayers();
49+
ctx.store.storeMapConfig();
4750
ctx.events.addEventListeners();
4851
};
4952

src/store.js

+44
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const throttle = require('./lib/throttle');
22
const toDenseArray = require('./lib/to_dense_array');
33
const StringSet = require('./lib/string_set');
44
const render = require('./render');
5+
const interactions = require('./constants').interactions;
56

67
const Store = module.exports = function(ctx) {
78
this._features = {};
@@ -11,6 +12,7 @@ const Store = module.exports = function(ctx) {
1112
this._changedFeatureIds = new StringSet();
1213
this._deletedFeaturesToEmit = [];
1314
this._emitSelectionChange = false;
15+
this._mapInitialConfig = {};
1416
this.ctx = ctx;
1517
this.sources = {
1618
hot: [],
@@ -293,3 +295,45 @@ function refreshSelectedCoordinates(options) {
293295
}
294296
this._selectedCoordinates = newSelectedCoordinates;
295297
}
298+
299+
/**
300+
* Stores the initial config for a map, so that we can set it again after we're done.
301+
*/
302+
Store.prototype.storeMapConfig = function() {
303+
interactions.forEach((interaction) => {
304+
const interactionSet = this.ctx.map[interaction];
305+
if (interactionSet) {
306+
this._mapInitialConfig[interaction] = this.ctx.map[interaction].isEnabled();
307+
}
308+
});
309+
};
310+
311+
/**
312+
* Restores the initial config for a map, ensuring all is well.
313+
*/
314+
Store.prototype.restoreMapConfig = function() {
315+
Object.keys(this._mapInitialConfig).forEach(key => {
316+
const value = this._mapInitialConfig[key];
317+
if (value) {
318+
this.ctx.map[key].enable();
319+
} else {
320+
this.ctx.map[key].disable();
321+
}
322+
});
323+
};
324+
325+
/**
326+
* Returns the initial state of an interaction setting.
327+
* @param {string} interaction
328+
* @return {boolean} `true` if the interaction is enabled, `false` if not.
329+
* Defaults to `true`. (Todo: include defaults.)
330+
*/
331+
Store.prototype.getInitialConfigValue = function(interaction) {
332+
if (this._mapInitialConfig[interaction] !== undefined) {
333+
return this._mapInitialConfig[interaction];
334+
} else {
335+
// This needs to be set to whatever the default is for that interaction
336+
// It seems to be true for all cases currently, so let's send back `true`.
337+
return true;
338+
}
339+
};

test/draw_line_string.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ test('draw_line_string stop with invalid line', t => {
106106
], 'store.delete received correct arguments');
107107
}
108108

109-
setTimeout(() => {
110-
t.equal(context.map.doubleClickZoom.enable.callCount, 1);
111-
t.end();
112-
}, 10);
109+
t.end();
113110
});
114111

115112
test('draw_line_string render active line with 0 coordinates', t => {

test/draw_polygon.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ test('draw_polygon stop with invalid polygon', t => {
104104
{ silent: true }
105105
], 'store.delete received correct arguments');
106106

107-
setTimeout(() => {
108-
t.equal(context.map.doubleClickZoom.enable.callCount, 1);
109-
t.end();
110-
}, 10);
107+
t.end();
111108
});
112109

113110
test('draw_polygon render active polygon with no coordinates', t => {

test/static.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test('static', t => {
1111

1212
const map = createMap();
1313
spy(map, 'fire');
14-
map.dragPan.disable.restore();
14+
map.dragPan.disable();
1515
spy(map.dragPan, 'disable');
1616

1717
const opts = {

test/store.test.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import createMap from './utils/create_map';
66

77
function createStore() {
88
const map = createMap();
9-
const ctx = { map };
9+
const ctx = { map: map };
1010
return new Store(ctx);
1111
}
1212

@@ -27,6 +27,7 @@ test('Store constructor and public API', t => {
2727
cold: []
2828
}, 'exposes store.sources');
2929
t.equal(store.ctx, ctx, 'exposes store.ctx');
30+
t.equal(store.ctx.map, map, 'exposes store.ctx.map');
3031
t.equal(store.isDirty, false, 'exposes store.isDirty');
3132
t.equal(typeof store.render, 'function', 'exposes store.render');
3233

@@ -54,8 +55,11 @@ test('Store constructor and public API', t => {
5455
t.equal(typeof Store.prototype.getSelectedCoordinates, 'function', 'exposes store.getSelectedCoordinates');
5556
t.equal(typeof Store.prototype.clearSelectedCoordinates, 'function', 'exposes store.clearSelectedCoordinates');
5657
t.equal(typeof Store.prototype.setFeatureProperty, 'function', 'exposes store.setFeatureProperty');
58+
t.equal(typeof Store.prototype.storeMapConfig, 'function', 'exposes store.storeMapConfig');
59+
t.equal(typeof Store.prototype.restoreMapConfig, 'function', 'exposes store.restoreMapConfig');
60+
t.equal(typeof Store.prototype.getInitialConfigValue, 'function', 'exposes store.getInitialConfigValue');
5761

58-
t.equal(getPublicMemberKeys(Store.prototype).length, 21, 'no untested prototype members');
62+
t.equal(getPublicMemberKeys(Store.prototype).length, 24, 'no untested prototype members');
5963

6064
t.end();
6165
});
@@ -254,3 +258,21 @@ test('Store#setFeatureProperty', t => {
254258
t.end();
255259
});
256260

261+
test('Store#storeAndRestoreMapConfig', t => {
262+
const map = createMap();
263+
// Disable doubleClickZoom
264+
map.doubleClickZoom.disable();
265+
// Check it's disabled
266+
t.equal(map.doubleClickZoom.isEnabled(), false, 'Disables doubleClickZoom on the map');
267+
const ctx = { map: map };
268+
const store = new Store(ctx);
269+
store.storeMapConfig();
270+
// Check we can get the initial state of it
271+
console.log(store);
272+
t.equal(store.getInitialConfigValue('doubleClickZoom'), false, 'Retrieves the initial value for the doubleClickZoom');
273+
// Enable it again, byt then use restore to reset the initial state
274+
map.doubleClickZoom.enable();
275+
store.restoreMapConfig();
276+
t.equal(map.doubleClickZoom.isEnabled(), false, 'Restores doubleClickZoom on the map');
277+
t.end();
278+
});

test/utils/create_map.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mapboxgl from 'mapbox-gl-js-mock';
2+
import { interactions } from '../../src/constants';
23

34
export default function createMap(mapOptions = {}) {
45

@@ -13,6 +14,16 @@ export default function createMap(mapOptions = {}) {
1314
map.getContainer = () => mapOptions.container;
1415
}
1516

17+
// Mock up the interaction functions
18+
interactions.forEach((interaction) => {
19+
map[interaction] = {
20+
enabled: true,
21+
disable: function () { this.enabled = false; },
22+
enable: function () { this.enabled = true; },
23+
isEnabled: function () { return this.enabled; },
24+
};
25+
});
26+
1627
map.getCanvas = function() {
1728
return map.getContainer();
1829
};

yarn.lock

+18-19
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@
9191
"@turf/helpers" "^3.13.0"
9292
jsts "1.3.0"
9393

94-
"@turf/centroid@^4.0.0":
95-
version "4.1.0"
96-
resolved "https://registry.yarnpkg.com/@turf/centroid/-/centroid-4.1.0.tgz#e5c0a539748e35456d837f87b10515cf785bcfe6"
94+
"@turf/centroid@^5.0.4":
95+
version "5.0.4"
96+
resolved "https://registry.yarnpkg.com/@turf/centroid/-/centroid-5.0.4.tgz#31fcef5c2ca94b234217c7bb779091473358c7ec"
9797
dependencies:
98-
"@turf/helpers" "^4.1.0"
99-
"@turf/meta" "^4.1.0"
98+
"@turf/helpers" "^5.0.4"
99+
"@turf/meta" "^5.0.4"
100100

101101
"@turf/combine@^3.14.0":
102102
version "3.14.0"
@@ -108,17 +108,19 @@
108108
version "3.13.0"
109109
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-3.13.0.tgz#d06078a1464cf56cdb7ea624ea1e13a71b88b806"
110110

111-
"@turf/helpers@^4.1.0":
112-
version "4.1.0"
113-
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-4.1.0.tgz#013a9c53db6a9386e47c7ab445d93ca2035c4b81"
111+
"@turf/helpers@^5.0.4":
112+
version "5.0.4"
113+
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-5.0.4.tgz#e47a4e4f38dee3b47a3177a69de162d7a7a5837f"
114114

115115
"@turf/meta@^3.14.0":
116116
version "3.14.0"
117117
resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-3.14.0.tgz#8d3050c1a0f44bf406a633b6bd28c510f7bcee27"
118118

119-
"@turf/meta@^4.1.0":
120-
version "4.1.0"
121-
resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-4.1.0.tgz#7b0715832ff483d28d2669051f1e00c6cefcbf75"
119+
"@turf/meta@^5.0.4":
120+
version "5.0.4"
121+
resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-5.0.4.tgz#b41d08f19d2ecc934805b6d713a663abd9f83213"
122+
dependencies:
123+
"@turf/helpers" "^5.0.4"
122124

123125
"@turf/union@^3.0.10":
124126
version "3.14.0"
@@ -390,7 +392,7 @@ babel-code-frame@^6.22.0:
390392
esutils "^2.0.2"
391393
js-tokens "^3.0.0"
392394

393-
babel-core@^6.0.14, babel-core@^6.24.1, babel-core@^6.9.1:
395+
babel-core@^6.24.1, babel-core@^6.9.1:
394396
version "6.24.1"
395397
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.1.tgz#8c428564dce1e1f41fb337ec34f4c3b022b5ad83"
396398
dependencies:
@@ -793,12 +795,9 @@ babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.23.0, babel-types@^6.24
793795
lodash "^4.2.0"
794796
to-fast-properties "^1.0.1"
795797

796-
babelify@^7.2.0:
797-
version "7.3.0"
798-
resolved "https://registry.yarnpkg.com/babelify/-/babelify-7.3.0.tgz#aa56aede7067fd7bd549666ee16dc285087e88e5"
799-
dependencies:
800-
babel-core "^6.0.14"
801-
object-assign "^4.0.0"
798+
babelify@^8.0.0:
799+
version "8.0.0"
800+
resolved "https://registry.yarnpkg.com/babelify/-/babelify-8.0.0.tgz#6f60f5f062bfe7695754ef2403b842014a580ed3"
802801

803802
babylon@^6.11.0, babylon@^6.15.0, babylon@^6.17.0:
804803
version "6.17.0"
@@ -3334,7 +3333,7 @@ oauth-sign@~0.8.1:
33343333
version "0.8.2"
33353334
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43"
33363335

3337-
object-assign@^4.0.0, object-assign@^4.0.1, object-assign@^4.1.0:
3336+
object-assign@^4.0.1, object-assign@^4.1.0:
33383337
version "4.1.1"
33393338
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
33403339

0 commit comments

Comments
 (0)