Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale update units #6138

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/ui/control/scale_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const util = require('../../util/util');

import type Map from '../map';

const defaultOptions = {
maxWidth: 100,
unit: 'metric'
};

/**
* A `ScaleControl` control displays the ratio of a distance on the map to the corresponding distance on the ground.
*
Expand All @@ -13,21 +18,25 @@ import type Map from '../map';
* @param {number} [options.maxWidth='100'] The maximum length of the scale control in pixels.
* @param {string} [options.unit='metric'] Unit of the distance (`'imperial'`, `'metric'` or `'nautical'`).
* @example
* map.addControl(new mapboxgl.ScaleControl({
* var scale = new mapboxgl.ScaleControl({
* maxWidth: 80,
* unit: 'imperial'
* }));
* });
* map.addControl(scale);
*
* scale.setUnit('metric');
*/
class ScaleControl {
_map: Map;
_container: HTMLElement;
options: any;

constructor(options: any) {
this.options = options;
this.options = util.extend({}, defaultOptions, options);

util.bindAll([
'_onMove'
'_onMove',
'setUnit'
], this);
}

Expand All @@ -54,6 +63,16 @@ class ScaleControl {
this._map.off('move', this._onMove);
this._map = (undefined: any);
}

/**
* Set the scale's unit of the distance
*
* @param {String} unit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String is causing the CI to fail, I believe it should be string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it would be helpful to include the documentation for unit, listing out what the supported values are.

*/
setUnit(unit: string) {
this.options.unit = unit;
updateScale(this._map, this._container, this.options);
}
}

module.exports = ScaleControl;
Expand Down
47 changes: 47 additions & 0 deletions test/unit/ui/control/scale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const test = require('mapbox-gl-js-test').test;
const window = require('../../../../src/util/window');
const Map = require('../../../../src/ui/map');
const ScaleControl = require('../../../../src/ui/control/scale_control');

function createMap() {
const container = window.document.createElement('div');
return new Map({
container,
style: {
version: 8,
sources: {},
layers: []
},
hash: true
});

}

test('ScaleControl appears in bottom-left by default', (t) => {
const map = createMap();
map.addControl(new ScaleControl());

t.equal(map.getContainer().querySelectorAll('.mapboxgl-ctrl-bottom-left .mapboxgl-ctrl-scale').length, 1);
t.end();
});

test('ScaleControl appears in the position specified by the position option', (t) => {
const map = createMap();
map.addControl(new ScaleControl(), 'top-left');

t.equal(map.getContainer().querySelectorAll('.mapboxgl-ctrl-top-left .mapboxgl-ctrl-scale').length, 1);
t.end();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇 thank you for adding these tests! Could you also add one that tests the new updateUnit() method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @anandthakker I will work on this in the next day or so. Thanks for the feedback.


test('ScaleControl should change unit of distance after calling setUnit', (t) => {
const map = createMap();
const scale = new ScaleControl();
map.addControl(scale);

t.equal(scale.options.unit, 'metric');
scale.setUnit('imperial');
t.equal(scale.options.unit, 'imperial');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the unit is stored in scale.options.unit is an implementation detail that could be changed in a future refactor. Let's update this test so that it wouldn't be affected by such a change by having these assertions actually check the content of the rendered scale control, e.g. maybe by searching for an appropriate string within map.getContainer().querySelectorAll('.mapboxgl-ctrl-top-left .mapboxgl-ctrl-scale').innerHTML

t.end();
});