Make code prettier

This updates ESLint and our shared eslint-config-openlayers to use Prettier.  Most formatting changes were automatically applied with this:

    npm run lint -- --fix

A few manual changes were required:

 * In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
 * In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason.  While editing this, I reworked `ExampleBuilder` to be a class.
 * In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
This commit is contained in:
Tim Schaub
2020-04-06 12:25:12 -06:00
parent 53b48baf62
commit 054af09032
790 changed files with 46833 additions and 33765 deletions

View File

@@ -1,51 +1,50 @@
import View from '../../../../src/ol/View.js';
import DragAndDrop from '../../../../src/ol/interaction/DragAndDrop.js';
import Event from '../../../../src/ol/events/Event.js';
import EventTarget from '../../../../src/ol/events/Target.js';
import GeoJSON from '../../../../src/ol/format/GeoJSON.js';
import DragAndDrop from '../../../../src/ol/interaction/DragAndDrop.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import View from '../../../../src/ol/View.js';
where('FileReader').describe('ol.interaction.DragAndDrop', function() {
where('FileReader').describe('ol.interaction.DragAndDrop', function () {
let viewport, map, interaction;
beforeEach(function() {
beforeEach(function () {
viewport = new EventTarget();
map = {
getViewport: function() {
getViewport: function () {
return viewport;
},
getView: function() {
getView: function () {
return new View();
}
},
};
interaction = new DragAndDrop({
formatConstructors: [GeoJSON]
formatConstructors: [GeoJSON],
});
});
describe('constructor', function() {
it('can be constructed without arguments', function() {
describe('constructor', function () {
it('can be constructed without arguments', function () {
const interaction = new DragAndDrop();
expect(interaction).to.be.an(DragAndDrop);
});
it('sets formatConstructors on the instance', function() {
it('sets formatConstructors on the instance', function () {
expect(interaction.formatConstructors_).to.have.length(1);
});
it('accepts a source option', function() {
it('accepts a source option', function () {
const source = new VectorSource();
const drop = new DragAndDrop({
formatConstructors: [GeoJSON],
source: source
source: source,
});
expect(drop.source_).to.equal(source);
});
});
describe('#setActive()', function() {
it('registers and unregisters listeners', function() {
describe('#setActive()', function () {
it('registers and unregisters listeners', function () {
interaction.setMap(map);
interaction.setActive(true);
expect(viewport.hasListener('dragenter')).to.be(true);
@@ -58,8 +57,8 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
});
});
describe('#setMap()', function() {
it('registers and unregisters listeners', function() {
describe('#setMap()', function () {
it('registers and unregisters listeners', function () {
interaction.setMap(map);
expect(viewport.hasListener('dragenter')).to.be(true);
expect(viewport.hasListener('dragover')).to.be(true);
@@ -70,11 +69,11 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
expect(viewport.hasListener('drop')).to.be(false);
});
it('registers and unregisters listeners on a custom target', function() {
it('registers and unregisters listeners on a custom target', function () {
const customTarget = new EventTarget();
interaction = new DragAndDrop({
formatConstructors: [GeoJSON],
target: customTarget
target: customTarget,
});
interaction.setMap(map);
expect(customTarget.hasListener('dragenter')).to.be(true);
@@ -87,10 +86,10 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
});
});
describe('#handleDrop_', function() {
describe('#handleDrop_', function () {
let OrigFileReader;
beforeEach(function() {
beforeEach(function () {
OrigFileReader = FileReader;
class MockFileReader extends EventTarget {
@@ -105,12 +104,12 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
FileReader = MockFileReader;
});
afterEach(function() {
afterEach(function () {
FileReader = OrigFileReader;
});
it('reads dropped files', function(done) {
interaction.on('addfeatures', function(evt) {
it('reads dropped files', function (done) {
interaction.on('addfeatures', function (evt) {
expect(evt.features.length).to.be(1);
done();
});
@@ -124,27 +123,27 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
event.type = 'drop';
event.dataTransfer.files = {
length: 1,
item: function() {
item: function () {
return JSON.stringify({
type: 'FeatureCollection',
features: [{type: 'Feature', id: '1'}]
features: [{type: 'Feature', id: '1'}],
});
}
},
};
viewport.dispatchEvent(event);
expect(event.dataTransfer.dropEffect).to.be('copy');
expect(event.propagationStopped).to.be(true);
});
it('adds dropped features to a source', function(done) {
it('adds dropped features to a source', function (done) {
const source = new VectorSource();
const drop = new DragAndDrop({
formatConstructors: [GeoJSON],
source: source
source: source,
});
drop.setMap(map);
drop.on('addfeatures', function(evt) {
drop.on('addfeatures', function (evt) {
const features = source.getFeatures();
expect(features.length).to.be(1);
done();
@@ -159,17 +158,16 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
event.type = 'drop';
event.dataTransfer.files = {
length: 1,
item: function() {
item: function () {
return JSON.stringify({
type: 'FeatureCollection',
features: [{type: 'Feature', id: '1'}]
features: [{type: 'Feature', id: '1'}],
});
}
},
};
viewport.dispatchEvent(event);
expect(event.dataTransfer.dropEffect).to.be('copy');
expect(event.propagationStopped).to.be(true);
});
});
});

View File

@@ -1,30 +1,26 @@
import DragRotateAndZoom from '../../../../src/ol/interaction/DragRotateAndZoom.js';
import Event from '../../../../src/ol/events/Event.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserPointerEvent from '../../../../src/ol/MapBrowserPointerEvent.js';
import View from '../../../../src/ol/View.js';
import DragRotateAndZoom from '../../../../src/ol/interaction/DragRotateAndZoom.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import Event from '../../../../src/ol/events/Event.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import View from '../../../../src/ol/View.js';
describe('ol.interaction.DragRotateAndZoom', function() {
describe('constructor', function() {
it('can be constructed without arguments', function() {
describe('ol.interaction.DragRotateAndZoom', function () {
describe('constructor', function () {
it('can be constructed without arguments', function () {
const instance = new DragRotateAndZoom();
expect(instance).to.be.an(DragRotateAndZoom);
});
});
describe('#handleDragEvent()', function() {
describe('#handleDragEvent()', function () {
let target, map, interaction;
const width = 360;
const height = 180;
beforeEach(function(done) {
beforeEach(function (done) {
target = document.createElement('div');
const style = target.style;
style.position = 'absolute';
@@ -43,32 +39,37 @@ describe('ol.interaction.DragRotateAndZoom', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.once('postrender', function() {
map.once('postrender', function () {
done();
});
});
afterEach(function() {
afterEach(function () {
map.dispose();
document.body.removeChild(target);
});
it('does not rotate when rotation is disabled on the view', function() {
it('does not rotate when rotation is disabled on the view', function () {
const pointerEvent = new Event();
pointerEvent.type = 'pointermove';
pointerEvent.clientX = 20;
pointerEvent.clientY = 10;
pointerEvent.pointerType = 'mouse';
let event = new MapBrowserPointerEvent('pointermove', map, pointerEvent, true);
let event = new MapBrowserPointerEvent(
'pointermove',
map,
pointerEvent,
true
);
interaction.lastAngle_ = Math.PI;
let callCount = 0;
let view = map.getView();
view.on('change:rotation', function() {
view.on('change:rotation', function () {
callCount++;
});
@@ -81,10 +82,10 @@ describe('ol.interaction.DragRotateAndZoom', function() {
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1,
enableRotation: false
enableRotation: false,
});
map.setView(view);
view.on('change:rotation', function() {
view.on('change:rotation', function () {
callCount++;
});
@@ -92,11 +93,15 @@ describe('ol.interaction.DragRotateAndZoom', function() {
pointerEvent.clientX = 24;
pointerEvent.clientY = 16;
pointerEvent.pointerType = 'mouse';
event = new MapBrowserPointerEvent('pointermove', map, pointerEvent, true);
event = new MapBrowserPointerEvent(
'pointermove',
map,
pointerEvent,
true
);
interaction.handleDragEvent(event);
expect(callCount).to.be(0);
});
});
});

View File

@@ -1,21 +1,19 @@
import DragZoom from '../../../../src/ol/interaction/DragZoom.js';
import Map from '../../../../src/ol/Map.js';
import RenderBox from '../../../../src/ol/render/Box.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import View from '../../../../src/ol/View.js';
import {getCenter} from '../../../../src/ol/extent.js';
import {fromExtent as polygonFromExtent} from '../../../../src/ol/geom/Polygon.js';
import DragZoom from '../../../../src/ol/interaction/DragZoom.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import RenderBox from '../../../../src/ol/render/Box.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
describe('ol.interaction.DragZoom', function() {
describe('ol.interaction.DragZoom', function () {
let target, map, source;
const width = 360;
const height = 180;
beforeEach(function(done) {
beforeEach(function (done) {
target = document.createElement('div');
const style = target.style;
style.position = 'absolute';
@@ -32,41 +30,38 @@ describe('ol.interaction.DragZoom', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.once('postrender', function() {
map.once('postrender', function () {
done();
});
});
afterEach(function() {
afterEach(function () {
map.dispose();
document.body.removeChild(target);
});
describe('constructor', function() {
it('can be constructed without arguments', function() {
describe('constructor', function () {
it('can be constructed without arguments', function () {
const instance = new DragZoom();
expect(instance).to.be.an(DragZoom);
});
it('sets "ol-dragzoom" as box className', function() {
it('sets "ol-dragzoom" as box className', function () {
const instance = new DragZoom();
expect(instance.box_.element_.className).to.be('ol-box ol-dragzoom');
});
it('sets a custom box className', function() {
it('sets a custom box className', function () {
const instance = new DragZoom({className: 'test-dragzoom'});
expect(instance.box_.element_.className).to.be('ol-box test-dragzoom');
});
});
describe('#onBoxEnd()', function() {
it('centers the view on the box geometry', function(done) {
describe('#onBoxEnd()', function () {
it('centers the view on the box geometry', function (done) {
const interaction = new DragZoom({
duration: 10
duration: 10,
});
map.addInteraction(interaction);
@@ -76,19 +71,18 @@ describe('ol.interaction.DragZoom', function() {
interaction.box_ = box;
interaction.onBoxEnd_();
setTimeout(function() {
setTimeout(function () {
const view = map.getView();
const center = view.getCenterInternal();
expect(center).to.eql(getCenter(extent));
done();
}, 50);
});
it('sets new resolution while zooming out', function(done) {
it('sets new resolution while zooming out', function (done) {
const interaction = new DragZoom({
duration: 10,
out: true
out: true,
});
map.addInteraction(interaction);
@@ -98,19 +92,15 @@ describe('ol.interaction.DragZoom', function() {
interaction.box_ = box;
map.getView().setResolution(0.25);
setTimeout(function() {
setTimeout(function () {
interaction.onBoxEnd_();
setTimeout(function() {
setTimeout(function () {
const view = map.getView();
const resolution = view.getResolution();
expect(resolution).to.eql(view.getConstrainedResolution(0.5));
done();
}, 50);
}, 50);
});
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -1,15 +1,15 @@
import ExtentInteraction from '../../../../src/ol/interaction/Extent.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserPointerEvent from '../../../../src/ol/MapBrowserPointerEvent.js';
import View from '../../../../src/ol/View.js';
import ExtentInteraction from '../../../../src/ol/interaction/Extent.js';
describe('ol.interaction.Extent', function() {
describe('ol.interaction.Extent', function () {
let map, interaction;
const width = 360;
const height = 180;
beforeEach(function() {
beforeEach(function () {
const target = createMapDiv(width, height);
map = new Map({
@@ -18,8 +18,8 @@ describe('ol.interaction.Extent', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.renderSync();
@@ -27,7 +27,7 @@ describe('ol.interaction.Extent', function() {
map.addInteraction(interaction);
});
afterEach(function() {
afterEach(function () {
if (map) {
disposeMap(map);
}
@@ -57,33 +57,31 @@ describe('ol.interaction.Extent', function() {
pointerEvent.clientY = position.top - y + height / 2;
pointerEvent.shiftKey = shiftKey;
pointerEvent.pointerId = 0;
pointerEvent.preventDefault = function() {};
pointerEvent.preventDefault = function () {};
const event = new MapBrowserPointerEvent(type, map, pointerEvent);
event.pointerEvent.pointerId = 1;
map.handleMapBrowserEvent(event);
}
describe('Constructor', function() {
it('can be configured with an extent', function() {
expect(function() {
describe('Constructor', function () {
it('can be configured with an extent', function () {
expect(function () {
new ExtentInteraction({
extent: [-10, -10, 10, 10]
extent: [-10, -10, 10, 10],
});
}).to.not.throwException();
});
});
describe('snap to vertex', function() {
it('snap to vertex works', function() {
describe('snap to vertex', function () {
it('snap to vertex works', function () {
interaction.setExtent([-50, -50, 50, 50]);
expect(interaction.snapToVertex_([230, 40], map)).to.eql([50, 50]);
expect(interaction.snapToVertex_([231, 41], map)).to.eql([50, 50]);
});
it('snap to edge works', function() {
it('snap to edge works', function () {
interaction.setExtent([-50, -50, 50, 50]);
expect(interaction.snapToVertex_([230, 90], map)).to.eql([50, 0]);
@@ -92,9 +90,8 @@ describe('ol.interaction.Extent', function() {
});
});
describe('draw extent', function() {
it('drawing extent works', function() {
describe('draw extent', function () {
it('drawing extent works', function () {
simulateEvent('pointerdown', -50, -50, false, 0);
simulateEvent('pointerdrag', 50, 50, false, 0);
simulateEvent('pointerup', 50, 50, false, 0);
@@ -102,7 +99,7 @@ describe('ol.interaction.Extent', function() {
expect(interaction.getExtent()).to.eql([-50, -50, 50, 50]);
});
it('clicking off extent nulls extent', function() {
it('clicking off extent nulls extent', function () {
interaction.setExtent([-50, -50, 50, 50]);
simulateEvent('pointerdown', -10, -10, false, 0);
@@ -111,7 +108,7 @@ describe('ol.interaction.Extent', function() {
expect(interaction.getExtent()).to.equal(null);
});
it('clicking on extent does not null extent', function() {
it('clicking on extent does not null extent', function () {
interaction.setExtent([-50, -50, 50, 50]);
simulateEvent('pointerdown', 50, 50, false, 0);
@@ -120,7 +117,7 @@ describe('ol.interaction.Extent', function() {
expect(interaction.getExtent()).to.eql([-50, -50, 50, 50]);
});
it('snap and drag vertex works', function() {
it('snap and drag vertex works', function () {
interaction.setExtent([-50, -50, 50, 50]);
simulateEvent('pointerdown', 51, 49, false, 0);
@@ -130,7 +127,7 @@ describe('ol.interaction.Extent', function() {
expect(interaction.getExtent()).to.eql([-70, -50, -50, -40]);
});
it('snap and drag edge works', function() {
it('snap and drag edge works', function () {
interaction.setExtent([-50, -50, 50, 50]);
simulateEvent('pointerdown', 51, 5, false, 0);

View File

@@ -1,64 +1,59 @@
import {Map, View} from '../../../../src/ol/index.js';
import EventTarget from '../../../../src/ol/events/Target.js';
import Interaction, {zoomByDelta} from '../../../../src/ol/interaction/Interaction.js';
import Interaction, {
zoomByDelta,
} from '../../../../src/ol/interaction/Interaction.js';
import {FALSE} from '../../../../src/ol/functions.js';
import {useGeographic, clearUserProjection} from '../../../../src/ol/proj.js';
import {Map, View} from '../../../../src/ol/index.js';
import {clearUserProjection, useGeographic} from '../../../../src/ol/proj.js';
describe('ol.interaction.Interaction', function() {
describe('constructor', function() {
describe('ol.interaction.Interaction', function () {
describe('constructor', function () {
let interaction;
beforeEach(function() {
beforeEach(function () {
interaction = new Interaction({});
});
it('creates a new interaction', function() {
it('creates a new interaction', function () {
expect(interaction).to.be.a(Interaction);
expect(interaction).to.be.a(EventTarget);
});
it('creates an active interaction', function() {
it('creates an active interaction', function () {
expect(interaction.getActive()).to.be(true);
});
});
describe('#getMap()', function() {
it('retrieves the associated map', function() {
describe('#getMap()', function () {
it('retrieves the associated map', function () {
const map = new Map({});
const interaction = new Interaction({});
interaction.setMap(map);
expect(interaction.getMap()).to.be(map);
});
it('returns null if no map', function() {
it('returns null if no map', function () {
const interaction = new Interaction({});
expect(interaction.getMap()).to.be(null);
});
});
describe('#setMap()', function() {
it('allows a map to be set', function() {
describe('#setMap()', function () {
it('allows a map to be set', function () {
const map = new Map({});
const interaction = new Interaction({});
interaction.setMap(map);
expect(interaction.getMap()).to.be(map);
});
it('accepts null', function() {
it('accepts null', function () {
const interaction = new Interaction({});
interaction.setMap(null);
expect(interaction.getMap()).to.be(null);
});
});
describe('#handleEvent()', function() {
describe('#handleEvent()', function () {
class MockInteraction extends Interaction {
constructor() {
super(...arguments);
@@ -68,25 +63,23 @@ describe('ol.interaction.Interaction', function() {
}
}
it('has a default event handler', function() {
it('has a default event handler', function () {
const interaction = new Interaction({});
expect(interaction.handleEvent()).to.be(true);
});
it('allows event handler overrides via options', function() {
it('allows event handler overrides via options', function () {
const interaction = new Interaction({
handleEvent: FALSE
handleEvent: FALSE,
});
expect(interaction.handleEvent()).to.be(false);
});
it('allows event handler overrides via class extension', function() {
it('allows event handler overrides via class extension', function () {
const interaction = new MockInteraction({});
expect(interaction.handleEvent()).to.be(false);
});
});
});
describe('zoomByDelta - useGeographic', () => {
@@ -96,7 +89,7 @@ describe('zoomByDelta - useGeographic', () => {
it('works with a user projection set', () => {
const view = new View({
center: [0, 0],
zoom: 0
zoom: 0,
});
const spy = sinon.spy(view, 'animate');

View File

@@ -1,34 +1,34 @@
import Event from '../../../../src/ol/events/Event.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserEvent from '../../../../src/ol/MapBrowserEvent.js';
import View from '../../../../src/ol/View.js';
import Event from '../../../../src/ol/events/Event.js';
describe('ol.interaction.KeyboardPan', function() {
describe('ol.interaction.KeyboardPan', function () {
let map;
beforeEach(function() {
beforeEach(function () {
map = new Map({
target: createMapDiv(100, 100),
view: new View({
center: [0, 0],
resolutions: [1],
zoom: 0
})
zoom: 0,
}),
});
map.renderSync();
});
afterEach(function() {
afterEach(function () {
disposeMap(map);
});
describe('handleEvent()', function() {
it('pans on arrow keys', function() {
describe('handleEvent()', function () {
it('pans on arrow keys', function () {
const view = map.getView();
const spy = sinon.spy(view, 'animateInternal');
const event = new MapBrowserEvent('keydown', map, {
type: 'keydown',
target: map.getTargetElement(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.originalEvent.keyCode = 40; // DOWN
@@ -54,5 +54,4 @@ describe('ol.interaction.KeyboardPan', function() {
view.animateInternal.restore();
});
});
});

View File

@@ -1,34 +1,34 @@
import Event from '../../../../src/ol/events/Event.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserEvent from '../../../../src/ol/MapBrowserEvent.js';
import View from '../../../../src/ol/View.js';
import Event from '../../../../src/ol/events/Event.js';
describe('ol.interaction.KeyboardZoom', function() {
describe('ol.interaction.KeyboardZoom', function () {
let map;
beforeEach(function() {
beforeEach(function () {
map = new Map({
target: createMapDiv(100, 100),
view: new View({
center: [0, 0],
resolutions: [4, 2, 1],
zoom: 1
})
zoom: 1,
}),
});
map.renderSync();
});
afterEach(function() {
afterEach(function () {
disposeMap(map);
});
describe('handleEvent()', function() {
it('zooms on + and - keys', function() {
describe('handleEvent()', function () {
it('zooms on + and - keys', function () {
const view = map.getView();
const spy = sinon.spy(view, 'animateInternal');
const event = new MapBrowserEvent('keydown', map, {
type: 'keydown',
target: map.getTargetElement(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.originalEvent.charCode = '+'.charCodeAt(0);
@@ -44,20 +44,18 @@ describe('ol.interaction.KeyboardZoom', function() {
view.animateInternal.restore();
});
it('does nothing if the target is editable', function() {
it('does nothing if the target is editable', function () {
const view = map.getView();
const spy = sinon.spy(view, 'animateInternal');
const event = new MapBrowserEvent('keydown', map, {
type: 'keydown',
target: document.createElement('input'),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.originalEvent.charCode = '+'.charCodeAt(0);
map.handleMapBrowserEvent(event);
expect(spy.called).to.be(false);
});
});
});

View File

@@ -1,30 +1,31 @@
import Circle from '../../../../src/ol/geom/Circle.js';
import Collection from '../../../../src/ol/Collection.js';
import Event from '../../../../src/ol/events/Event.js';
import Feature from '../../../../src/ol/Feature.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserPointerEvent from '../../../../src/ol/MapBrowserPointerEvent.js';
import View from '../../../../src/ol/View.js';
import {doubleClick} from '../../../../src/ol/events/condition.js';
import Circle from '../../../../src/ol/geom/Circle.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Modify, {ModifyEvent} from '../../../../src/ol/interaction/Modify.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import Modify, {ModifyEvent} from '../../../../src/ol/interaction/Modify.js';
import Snap from '../../../../src/ol/interaction/Snap.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import Event from '../../../../src/ol/events/Event.js';
import View from '../../../../src/ol/View.js';
import {
clearUserProjection,
setUserProjection,
} from '../../../../src/ol/proj.js';
import {doubleClick} from '../../../../src/ol/events/condition.js';
import {getValues} from '../../../../src/ol/obj.js';
import {clearUserProjection, setUserProjection} from '../../../../src/ol/proj.js';
describe('ol.interaction.Modify', function() {
describe('ol.interaction.Modify', function () {
let target, map, source, features;
const width = 360;
const height = 180;
beforeEach(function(done) {
beforeEach(function (done) {
target = document.createElement('div');
const style = target.style;
@@ -38,13 +39,19 @@ describe('ol.interaction.Modify', function() {
features = [
new Feature({
geometry: new Polygon([
[[0, 0], [10, 20], [0, 40], [40, 40], [40, 0]]
])
})
[
[0, 0],
[10, 20],
[0, 40],
[40, 40],
[40, 0],
],
]),
}),
];
source = new VectorSource({
features: features
features: features,
});
const layer = new VectorLayer({source: source});
@@ -55,16 +62,16 @@ describe('ol.interaction.Modify', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.once('postrender', function() {
map.once('postrender', function () {
done();
});
});
afterEach(function() {
afterEach(function () {
map.dispose();
document.body.removeChild(target);
clearUserProjection();
@@ -92,7 +99,7 @@ describe('ol.interaction.Modify', function() {
pointerEvent.shiftKey = modifiers.shift || false;
pointerEvent.altKey = modifiers.alt || false;
pointerEvent.pointerId = 1;
pointerEvent.preventDefault = function() {};
pointerEvent.preventDefault = function () {};
pointerEvent.button = button;
pointerEvent.isPrimary = true;
const event = new MapBrowserPointerEvent(type, map, pointerEvent);
@@ -108,27 +115,26 @@ describe('ol.interaction.Modify', function() {
*/
function trackEvents(feature, interaction) {
const events = [];
feature.on('change', function(event) {
feature.on('change', function (event) {
events.push('change');
});
interaction.on('modifystart', function(event) {
interaction.on('modifystart', function (event) {
events.push(event);
});
interaction.on('modifyend', function(event) {
interaction.on('modifyend', function (event) {
events.push(event);
});
return events;
}
/**
* Validates the event array to verify proper event sequence. Checks
* that first and last event are correct ModifyEvents and that feature
* modifications event are in between.
* @param {Array<ModifyEvent|string>} events The events.
* @param {Array<ol.Feature>} features The features.
*/
* Validates the event array to verify proper event sequence. Checks
* that first and last event are correct ModifyEvents and that feature
* modifications event are in between.
* @param {Array<ModifyEvent|string>} events The events.
* @param {Array<ol.Feature>} features The features.
*/
function validateEvents(events, features) {
const startevent = events[0];
const endevent = events[events.length - 1];
@@ -152,24 +158,23 @@ describe('ol.interaction.Modify', function() {
expect(endevent.features.getArray()).to.eql(features);
}
describe('constructor', function() {
it('adds features to the RTree', function() {
const feature = new Feature(
new Point([0, 0]));
describe('constructor', function () {
it('adds features to the RTree', function () {
const feature = new Feature(new Point([0, 0]));
const features = new Collection([feature]);
const modify = new Modify({
features: features
features: features,
});
const rbushEntries = modify.rBush_.getAll();
expect(rbushEntries.length).to.be(1);
expect(rbushEntries[0].feature).to.be(feature);
});
it('accepts feature without geometry', function() {
it('accepts feature without geometry', function () {
const feature = new Feature();
const features = new Collection([feature]);
const modify = new Modify({
features: features
features: features,
});
let rbushEntries = modify.rBush_.getAll();
expect(rbushEntries.length).to.be(0);
@@ -180,21 +185,18 @@ describe('ol.interaction.Modify', function() {
expect(rbushEntries[0].feature).to.be(feature);
});
it('accepts a source', function() {
const feature = new Feature(
new Point([0, 0]));
it('accepts a source', function () {
const feature = new Feature(new Point([0, 0]));
const source = new VectorSource({features: [feature]});
const modify = new Modify({source: source});
const rbushEntries = modify.rBush_.getAll();
expect(rbushEntries.length).to.be(1);
expect(rbushEntries[0].feature).to.be(feature);
});
});
describe('vertex deletion', function() {
it('works when clicking on a shared vertex', function() {
describe('vertex deletion', function () {
it('works when clicking on a shared vertex', function () {
features.push(features[0].clone());
const first = features[0];
@@ -203,7 +205,7 @@ describe('ol.interaction.Modify', function() {
const secondRevision = second.getGeometry().getRevision();
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -227,11 +229,15 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, features);
});
it('deletes first vertex of a LineString', function() {
it('deletes first vertex of a LineString', function () {
const lineFeature = new Feature({
geometry: new LineString(
[[0, 0], [10, 20], [0, 40], [40, 40], [40, 0]]
)
geometry: new LineString([
[0, 0],
[10, 20],
[0, 40],
[40, 40],
[40, 0],
]),
});
features.length = 0;
features.push(lineFeature);
@@ -241,7 +247,7 @@ describe('ol.interaction.Modify', function() {
const firstRevision = first.getGeometry().getRevision();
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -263,11 +269,15 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, features);
});
it('deletes last vertex of a LineString', function() {
it('deletes last vertex of a LineString', function () {
const lineFeature = new Feature({
geometry: new LineString(
[[0, 0], [10, 20], [0, 40], [40, 40], [40, 0]]
)
geometry: new LineString([
[0, 0],
[10, 20],
[0, 40],
[40, 40],
[40, 0],
]),
});
features.length = 0;
features.push(lineFeature);
@@ -277,7 +287,7 @@ describe('ol.interaction.Modify', function() {
const firstRevision = first.getGeometry().getRevision();
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -299,11 +309,15 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, features);
});
it('deletes vertex of a LineString programmatically', function() {
it('deletes vertex of a LineString programmatically', function () {
const lineFeature = new Feature({
geometry: new LineString(
[[0, 0], [10, 20], [0, 40], [40, 40], [40, 0]]
)
geometry: new LineString([
[0, 0],
[10, 20],
[0, 40],
[40, 40],
[40, 0],
]),
});
features.length = 0;
features.push(lineFeature);
@@ -313,7 +327,7 @@ describe('ol.interaction.Modify', function() {
const firstRevision = first.getGeometry().getRevision();
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -335,23 +349,24 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, features);
});
});
describe('vertex modification', function() {
it('keeps the third dimension', function() {
describe('vertex modification', function () {
it('keeps the third dimension', function () {
const lineFeature = new Feature({
geometry: new LineString(
[[0, 0, 10], [10, 20, 20], [0, 40, 30], [40, 40, 40], [40, 0, 50]]
)
geometry: new LineString([
[0, 0, 10],
[10, 20, 20],
[0, 40, 30],
[40, 40, 40],
[40, 0, 50],
]),
});
features.length = 0;
features.push(lineFeature);
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -380,17 +395,16 @@ describe('ol.interaction.Modify', function() {
expect(lineFeature.getGeometry().getCoordinates()[2][2]).to.equal(30);
expect(lineFeature.getGeometry().getCoordinates()[4][2]).to.equal(50);
});
});
describe('circle modification', function() {
it('changes the circle radius and center', function() {
describe('circle modification', function () {
it('changes the circle radius and center', function () {
const circleFeature = new Feature(new Circle([10, 10], 20));
features.length = 0;
features.push(circleFeature);
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -425,17 +439,19 @@ describe('ol.interaction.Modify', function() {
expect(circleFeature.getGeometry().getCenter()).to.eql([5, 5]);
});
it('changes the circle radius and center in a user projection', function() {
it('changes the circle radius and center in a user projection', function () {
const userProjection = 'EPSG:3857';
setUserProjection(userProjection);
const viewProjection = map.getView().getProjection();
const circleFeature = new Feature(new Circle([10, 10], 20).transform(viewProjection, userProjection));
const circleFeature = new Feature(
new Circle([10, 10], 20).transform(viewProjection, userProjection)
);
features.length = 0;
features.push(circleFeature);
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -446,7 +462,10 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointerdrag', 5, -5, null, 0);
simulateEvent('pointerup', 5, -5, null, 0);
const geometry1 = circleFeature.getGeometry().clone().transform(userProjection, viewProjection);
const geometry1 = circleFeature
.getGeometry()
.clone()
.transform(userProjection, viewProjection);
expect(geometry1.getRadius()).to.roughlyEqual(20, 1e-9);
expect(geometry1.getCenter()).to.eql([5, 5]);
@@ -457,7 +476,10 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointerdrag', 30, -5, null, 0);
simulateEvent('pointerup', 30, -5, null, 0);
const geometry2 = circleFeature.getGeometry().clone().transform(userProjection, viewProjection);
const geometry2 = circleFeature
.getGeometry()
.clone()
.transform(userProjection, viewProjection);
expect(geometry2.getRadius()).to.roughlyEqual(25, 1e-9);
expect(geometry2.getCenter()).to.eql([5, 5]);
@@ -468,18 +490,21 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointerdrag', 5, -35, null, 0);
simulateEvent('pointerup', 5, -35, null, 0);
const geometry3 = circleFeature.getGeometry().clone().transform(userProjection, viewProjection);
const geometry3 = circleFeature
.getGeometry()
.clone()
.transform(userProjection, viewProjection);
expect(geometry3.getRadius()).to.roughlyEqual(30, 1e-9);
expect(geometry3.getCenter()).to.eql([5, 5]);
});
});
describe('boundary modification', function() {
describe('boundary modification', function () {
let modify, feature, events;
beforeEach(function() {
beforeEach(function () {
modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -488,7 +513,7 @@ describe('ol.interaction.Modify', function() {
events = trackEvents(feature, modify);
});
it('clicking vertex should delete it and +r1', function() {
it('clicking vertex should delete it and +r1', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -503,7 +528,7 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, [feature]);
});
it('single clicking boundary should add vertex and +r1', function() {
it('single clicking boundary should add vertex and +r1', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -518,7 +543,7 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, [feature]);
});
it('single clicking on created vertex should delete it again', function() {
it('single clicking on created vertex should delete it again', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -544,7 +569,7 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, [feature]);
});
it('clicking with drag should add vertex and +r3', function() {
it('clicking with drag should add vertex and +r3', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -560,7 +585,7 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, [feature]);
});
it('clicking with right button should not add a vertex', function() {
it('clicking with right button should not add a vertex', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -575,17 +600,15 @@ describe('ol.interaction.Modify', function() {
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
expect(events).to.have.length(0);
});
});
describe('double click deleteCondition', function() {
describe('double click deleteCondition', function () {
let modify, feature, events;
beforeEach(function() {
beforeEach(function () {
modify = new Modify({
features: new Collection(features),
deleteCondition: doubleClick
deleteCondition: doubleClick,
});
map.addInteraction(modify);
@@ -594,8 +617,7 @@ describe('ol.interaction.Modify', function() {
events = trackEvents(feature, modify);
});
it('should delete vertex on double click', function() {
it('should delete vertex on double click', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -613,8 +635,7 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, features);
});
it('should do nothing on single click', function() {
it('should do nothing on single click', function () {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
@@ -630,15 +651,15 @@ describe('ol.interaction.Modify', function() {
});
});
describe('insertVertexCondition', function() {
it('calls the callback function', function() {
const listenerSpy = sinon.spy(function(event) {
describe('insertVertexCondition', function () {
it('calls the callback function', function () {
const listenerSpy = sinon.spy(function (event) {
return false;
});
const modify = new Modify({
features: new Collection(features),
insertVertexCondition: listenerSpy
insertVertexCondition: listenerSpy,
});
map.addInteraction(modify);
const feature = features[0];
@@ -664,26 +685,26 @@ describe('ol.interaction.Modify', function() {
});
});
describe('handle feature change', function() {
describe('handle feature change', function () {
let getModifyListeners;
beforeEach(function() {
getModifyListeners = function(feature, modify) {
beforeEach(function () {
getModifyListeners = function (feature, modify) {
const listeners = feature.listeners_['change'];
const candidates = getValues(modify);
return listeners.filter(function(listener) {
return listeners.filter(function (listener) {
return candidates.indexOf(listener) !== -1;
});
};
});
it('updates circle segment data', function() {
it('updates circle segment data', function () {
const feature = new Feature(new Circle([10, 10], 20));
features.length = 0;
features.push(feature);
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -694,10 +715,11 @@ describe('ol.interaction.Modify', function() {
let firstSegmentData;
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5],
function(node) {
return node;
});
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5], function (
node
) {
return node;
});
expect(firstSegmentData.segment[0]).to.eql([10, 10]);
expect(firstSegmentData.segment[1]).to.eql([10, 10]);
@@ -706,10 +728,11 @@ describe('ol.interaction.Modify', function() {
center[1] = 1;
feature.getGeometry().setCenter(center);
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5],
function(node) {
return node;
});
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5], function (
node
) {
return node;
});
expect(firstSegmentData.segment[0]).to.eql([1, 1]);
expect(firstSegmentData.segment[1]).to.eql([1, 1]);
@@ -717,9 +740,9 @@ describe('ol.interaction.Modify', function() {
expect(listeners).to.have.length(1);
});
it('updates polygon segment data', function() {
it('updates polygon segment data', function () {
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
@@ -731,10 +754,11 @@ describe('ol.interaction.Modify', function() {
let firstSegmentData;
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5],
function(node) {
return node;
});
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5], function (
node
) {
return node;
});
expect(firstSegmentData.segment[0]).to.eql([0, 0]);
expect(firstSegmentData.segment[1]).to.eql([10, 20]);
@@ -744,10 +768,11 @@ describe('ol.interaction.Modify', function() {
firstVertex[1] = 1;
feature.getGeometry().setCoordinates(coordinates);
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5],
function(node) {
return node;
});
firstSegmentData = modify.rBush_.forEachInExtent([0, 0, 5, 5], function (
node
) {
return node;
});
expect(firstSegmentData.segment[0]).to.eql([1, 1]);
expect(firstSegmentData.segment[1]).to.eql([10, 20]);
@@ -756,11 +781,11 @@ describe('ol.interaction.Modify', function() {
});
});
describe('handle feature removal during down-up sequence', function() {
it('removes segment data of removed features from dragSegments_', function() {
describe('handle feature removal during down-up sequence', function () {
it('removes segment data of removed features from dragSegments_', function () {
const collection = new Collection(features);
const modify = new Modify({
features: collection
features: collection,
});
map.addInteraction(modify);
simulateEvent('pointermove', 0, 0, null, 0);
@@ -768,16 +793,16 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointermove', -10, -10, null, 0);
simulateEvent('pointerdrag', -10, -10, null, 0);
collection.remove(features[0]);
expect(function() {
expect(function () {
simulateEvent('pointerup', -10, -10, null, 0);
}).to.not.throwException();
});
});
describe('#setActive', function() {
it('removes the vertexFeature of deactivation', function() {
describe('#setActive', function () {
it('removes the vertexFeature of deactivation', function () {
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
expect(modify.vertexFeature_).to.be(null);
@@ -790,29 +815,29 @@ describe('ol.interaction.Modify', function() {
});
});
describe('#getOverlay', function() {
it('returns the feature overlay layer', function() {
describe('#getOverlay', function () {
it('returns the feature overlay layer', function () {
const modify = new Modify({
features: new Collection()
features: new Collection(),
});
expect (modify.getOverlay()).to.eql(modify.overlay_);
expect(modify.getOverlay()).to.eql(modify.overlay_);
});
});
describe('circle modification with snap', function() {
it('changes the circle radius and center', function() {
describe('circle modification with snap', function () {
it('changes the circle radius and center', function () {
const circleFeature = new Feature(new Circle([10, 10], 20));
features.length = 0;
features.push(circleFeature);
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
const snap = new Snap({
features: new Collection(features),
pixelTolerance: 1
pixelTolerance: 1,
});
map.addInteraction(snap);
@@ -847,23 +872,25 @@ describe('ol.interaction.Modify', function() {
expect(circleFeature.getGeometry().getCenter()).to.eql([5, 5]);
});
it('changes the circle radius and center in a user projection', function() {
it('changes the circle radius and center in a user projection', function () {
const userProjection = 'EPSG:3857';
setUserProjection(userProjection);
const viewProjection = map.getView().getProjection();
const circleFeature = new Feature(new Circle([10, 10], 20).transform(viewProjection, userProjection));
const circleFeature = new Feature(
new Circle([10, 10], 20).transform(viewProjection, userProjection)
);
features.length = 0;
features.push(circleFeature);
const modify = new Modify({
features: new Collection(features)
features: new Collection(features),
});
map.addInteraction(modify);
const snap = new Snap({
features: new Collection(features),
pixelTolerance: 1
pixelTolerance: 1,
});
map.addInteraction(snap);
@@ -874,7 +901,10 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointerdrag', 5, -5, null, 0);
simulateEvent('pointerup', 5, -5, null, 0);
const geometry1 = circleFeature.getGeometry().clone().transform(userProjection, viewProjection);
const geometry1 = circleFeature
.getGeometry()
.clone()
.transform(userProjection, viewProjection);
expect(geometry1.getRadius()).to.roughlyEqual(20, 1e-9);
expect(geometry1.getCenter()).to.eql([5, 5]);
@@ -885,7 +915,10 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointerdrag', 30, -5, null, 0);
simulateEvent('pointerup', 30, -5, null, 0);
const geometry2 = circleFeature.getGeometry().clone().transform(userProjection, viewProjection);
const geometry2 = circleFeature
.getGeometry()
.clone()
.transform(userProjection, viewProjection);
expect(geometry2.getRadius()).to.roughlyEqual(25, 1e-9);
expect(geometry2.getCenter()).to.eql([5, 5]);
@@ -896,10 +929,12 @@ describe('ol.interaction.Modify', function() {
simulateEvent('pointerdrag', 5, -35, null, 0);
simulateEvent('pointerup', 5, -35, null, 0);
const geometry3 = circleFeature.getGeometry().clone().transform(userProjection, viewProjection);
const geometry3 = circleFeature
.getGeometry()
.clone()
.transform(userProjection, viewProjection);
expect(geometry3.getRadius()).to.roughlyEqual(30, 1e-9);
expect(geometry3.getCenter()).to.eql([5, 5]);
});
});
});

View File

@@ -1,15 +1,16 @@
import Event from '../../../../src/ol/events/Event.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserEvent from '../../../../src/ol/MapBrowserEvent.js';
import MouseWheelZoom, {
Mode,
} from '../../../../src/ol/interaction/MouseWheelZoom.js';
import View from '../../../../src/ol/View.js';
import Event from '../../../../src/ol/events/Event.js';
import {DEVICE_PIXEL_RATIO, FIREFOX} from '../../../../src/ol/has.js';
import MouseWheelZoom, {Mode} from '../../../../src/ol/interaction/MouseWheelZoom.js';
describe('ol.interaction.MouseWheelZoom', function() {
describe('ol.interaction.MouseWheelZoom', function () {
let map, interaction;
beforeEach(function() {
beforeEach(function () {
interaction = new MouseWheelZoom();
map = new Map({
target: createMapDiv(100, 100),
@@ -17,35 +18,35 @@ describe('ol.interaction.MouseWheelZoom', function() {
view: new View({
center: [0, 0],
resolutions: [2, 1, 0.5],
zoom: 1
})
zoom: 1,
}),
});
map.renderSync();
});
afterEach(function() {
afterEach(function () {
disposeMap(map);
map = null;
interaction = null;
});
describe('timeout duration', function() {
describe('timeout duration', function () {
let clock;
beforeEach(function() {
beforeEach(function () {
sinon.spy(interaction, 'handleWheelZoom_');
clock = sinon.useFakeTimers();
});
afterEach(function() {
afterEach(function () {
clock.restore();
interaction.handleWheelZoom_.restore();
});
it('works with the default value', function(done) {
it('works with the default value', function (done) {
const event = new MapBrowserEvent('wheel', map, {
type: 'wheel',
target: map.getViewport(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
map.handleMapBrowserEvent(event);
@@ -58,14 +59,12 @@ describe('ol.interaction.MouseWheelZoom', function() {
done();
});
});
describe('handleEvent()', function() {
describe('handleEvent()', function () {
if (FIREFOX) {
it('works on Firefox in DOM_DELTA_PIXEL mode (trackpad)', function(done) {
map.once('postrender', function() {
it('works on Firefox in DOM_DELTA_PIXEL mode (trackpad)', function (done) {
map.once('postrender', function () {
expect(interaction.mode_).to.be(Mode.TRACKPAD);
done();
});
@@ -74,7 +73,7 @@ describe('ol.interaction.MouseWheelZoom', function() {
deltaMode: WheelEvent.DOM_DELTA_PIXEL,
deltaY: DEVICE_PIXEL_RATIO,
target: map.getViewport(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
@@ -82,8 +81,8 @@ describe('ol.interaction.MouseWheelZoom', function() {
}
if (!FIREFOX) {
it('works in DOM_DELTA_PIXEL mode (trackpad)', function(done) {
map.once('postrender', function() {
it('works in DOM_DELTA_PIXEL mode (trackpad)', function (done) {
map.once('postrender', function () {
expect(interaction.mode_).to.be(Mode.TRACKPAD);
done();
});
@@ -92,26 +91,26 @@ describe('ol.interaction.MouseWheelZoom', function() {
deltaMode: WheelEvent.DOM_DELTA_PIXEL,
deltaY: 1,
target: map.getViewport(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
});
}
describe('spying on view.animateInternal()', function() {
describe('spying on view.animateInternal()', function () {
let view;
beforeEach(function() {
beforeEach(function () {
view = map.getView();
sinon.spy(view, 'animateInternal');
});
afterEach(function() {
afterEach(function () {
view.animateInternal.restore();
});
it('works in DOM_DELTA_LINE mode (wheel)', function(done) {
map.once('postrender', function() {
it('works in DOM_DELTA_LINE mode (wheel)', function (done) {
map.once('postrender', function () {
const call = view.animateInternal.getCall(0);
expect(call.args[0].resolution).to.be(2);
expect(call.args[0].anchor).to.eql([0, 0]);
@@ -123,15 +122,15 @@ describe('ol.interaction.MouseWheelZoom', function() {
deltaMode: WheelEvent.DOM_DELTA_LINE,
deltaY: 20,
target: map.getViewport(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
});
it('works on all browsers (wheel)', function(done) {
map.once('postrender', function() {
it('works on all browsers (wheel)', function (done) {
map.once('postrender', function () {
const call = view.animateInternal.getCall(0);
expect(call.args[0].resolution).to.be(2);
expect(call.args[0].anchor).to.eql([0, 0]);
@@ -142,15 +141,12 @@ describe('ol.interaction.MouseWheelZoom', function() {
type: 'wheel',
deltaY: 300,
target: map.getViewport(),
preventDefault: Event.prototype.preventDefault
preventDefault: Event.prototype.preventDefault,
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
});
});
});
});

View File

@@ -1,65 +1,62 @@
import Event from '../../../../src/ol/events/Event.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserPointerEvent from '../../../../src/ol/MapBrowserPointerEvent.js';
import Event from '../../../../src/ol/events/Event.js';
import PointerInteraction from '../../../../src/ol/interaction/Pointer.js';
describe('ol.interaction.Pointer', function() {
describe('#handleEvent', function() {
describe('ol.interaction.Pointer', function () {
describe('#handleEvent', function () {
let event;
let defaultPrevented;
beforeEach(function() {
beforeEach(function () {
const type = 'pointerdown';
const pointerEvent = new Event();
pointerEvent.type = type;
pointerEvent.pointerId = 0;
pointerEvent.preventDefault = function() {
pointerEvent.preventDefault = function () {
defaultPrevented = true;
};
event = new MapBrowserPointerEvent(type, new Map(), pointerEvent);
defaultPrevented = false;
});
it('does not prevent default on handled down event', function() {
it('does not prevent default on handled down event', function () {
const interaction = new PointerInteraction({
handleDownEvent: function() {
handleDownEvent: function () {
return true;
}
},
});
interaction.handleEvent(event);
expect(defaultPrevented).to.be(false);
});
it('does not prevent default on unhandled down event', function() {
it('does not prevent default on unhandled down event', function () {
const interaction = new PointerInteraction({
handleDownEvent: function() {
handleDownEvent: function () {
return false;
}
},
});
interaction.handleEvent(event);
expect(defaultPrevented).to.be(false);
});
});
describe('event handlers', function() {
describe('event handlers', function () {
let handleDownCalled, handleDragCalled, handleMoveCalled, handleUpCalled;
const flagHandleDown = function() {
const flagHandleDown = function () {
handleDownCalled = true;
};
const flagHandleDrag = function() {
const flagHandleDrag = function () {
handleDragCalled = true;
};
const flagHandleMove = function() {
const flagHandleMove = function () {
handleMoveCalled = true;
};
const flagHandleUp = function() {
const flagHandleUp = function () {
handleUpCalled = true;
};
@@ -83,25 +80,25 @@ describe('ol.interaction.Pointer', function() {
}
}
beforeEach(function() {
beforeEach(function () {
handleDownCalled = false;
handleDragCalled = false;
handleMoveCalled = false;
handleUpCalled = false;
});
it('has default event handlers', function() {
it('has default event handlers', function () {
const interaction = new PointerInteraction({});
expect(interaction.handleDownEvent()).to.be(false);
expect(interaction.handleUpEvent()).to.be(false);
});
it('allows event handler overrides via options', function() {
it('allows event handler overrides via options', function () {
const interaction = new PointerInteraction({
handleDownEvent: flagHandleDown,
handleDragEvent: flagHandleDrag,
handleMoveEvent: flagHandleMove,
handleUpEvent: flagHandleUp
handleUpEvent: flagHandleUp,
});
interaction.handleDownEvent();
@@ -117,7 +114,7 @@ describe('ol.interaction.Pointer', function() {
expect(handleUpCalled).to.be(true);
});
it('allows event handler overrides via class extension', function() {
it('allows event handler overrides via class extension', function () {
const interaction = new MockPointerInteraction({});
interaction.handleDownEvent();
@@ -132,7 +129,5 @@ describe('ol.interaction.Pointer', function() {
interaction.handleUpEvent();
expect(handleUpCalled).to.be(true);
});
});
});

View File

@@ -1,24 +1,23 @@
import Collection from '../../../../src/ol/Collection.js';
import Feature from '../../../../src/ol/Feature.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserEventType from '../../../../src/ol/MapBrowserEventType.js';
import MapBrowserPointerEvent from '../../../../src/ol/MapBrowserPointerEvent.js';
import View from '../../../../src/ol/View.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Select from '../../../../src/ol/interaction/Select.js';
import Style from '../../../../src/ol/style/Style.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import Style from '../../../../src/ol/style/Style.js';
import View from '../../../../src/ol/View.js';
describe('ol.interaction.Select', function() {
describe('ol.interaction.Select', function () {
let target, map, layer, source;
const width = 360;
const height = 180;
beforeEach(function(done) {
beforeEach(function (done) {
target = document.createElement('div');
const style = target.style;
@@ -29,7 +28,14 @@ describe('ol.interaction.Select', function() {
style.height = height + 'px';
document.body.appendChild(target);
const geometry = new Polygon([[[0, 0], [0, 40], [40, 40], [40, 0]]]);
const geometry = new Polygon([
[
[0, 0],
[0, 40],
[40, 40],
[40, 0],
],
]);
// Four overlapping features, two features of type "foo" and two features
// of type "bar". The rendering order is, from top to bottom, foo -> bar
@@ -38,23 +44,24 @@ describe('ol.interaction.Select', function() {
features.push(
new Feature({
geometry: geometry,
type: 'bar'
type: 'bar',
}),
new Feature({
geometry: geometry,
type: 'foo'
type: 'foo',
}),
new Feature({
geometry: geometry,
type: 'bar'
type: 'bar',
}),
new Feature({
geometry: geometry,
type: 'foo'
}));
type: 'foo',
})
);
source = new VectorSource({
features: features
features: features,
});
layer = new VectorLayer({source: source});
@@ -65,16 +72,16 @@ describe('ol.interaction.Select', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.once('postrender', function() {
map.once('postrender', function () {
done();
});
});
afterEach(function() {
afterEach(function () {
map.dispose();
document.body.removeChild(target);
});
@@ -97,41 +104,37 @@ describe('ol.interaction.Select', function() {
target: viewport.firstChild,
clientX: position.left + x + width / 2,
clientY: position.top + y + height / 2,
shiftKey: shiftKey
shiftKey: shiftKey,
};
map.handleMapBrowserEvent(new MapBrowserPointerEvent(type, map, event));
}
describe('constructor', function() {
it('creates a new interaction', function() {
describe('constructor', function () {
it('creates a new interaction', function () {
const select = new Select();
expect(select).to.be.a(Select);
expect(select).to.be.a(Interaction);
});
describe('user-provided collection', function() {
it('uses the user-provided collection', function() {
describe('user-provided collection', function () {
it('uses the user-provided collection', function () {
const features = new Collection();
const select = new Select({features: features});
expect(select.getFeatures()).to.be(features);
});
});
});
describe('selecting a polygon', function() {
describe('selecting a polygon', function () {
let select;
beforeEach(function() {
beforeEach(function () {
select = new Select();
map.addInteraction(select);
});
it('select with single-click', function() {
const listenerSpy = sinon.spy(function(e) {
it('select with single-click', function () {
const listenerSpy = sinon.spy(function (e) {
expect(e.selected).to.have.length(1);
});
select.on('select', listenerSpy);
@@ -144,8 +147,8 @@ describe('ol.interaction.Select', function() {
expect(features.getLength()).to.equal(1);
});
it('single-click outside the geometry', function() {
const listenerSpy = sinon.spy(function(e) {
it('single-click outside the geometry', function () {
const listenerSpy = sinon.spy(function (e) {
expect(e.selected).to.have.length(1);
});
select.on('select', listenerSpy);
@@ -158,8 +161,8 @@ describe('ol.interaction.Select', function() {
expect(features.getLength()).to.equal(0);
});
it('select twice with single-click', function() {
const listenerSpy = sinon.spy(function(e) {
it('select twice with single-click', function () {
const listenerSpy = sinon.spy(function (e) {
expect(e.selected).to.have.length(1);
});
select.on('select', listenerSpy);
@@ -173,8 +176,8 @@ describe('ol.interaction.Select', function() {
expect(features.getLength()).to.equal(1);
});
it('select with shift single-click', function() {
const listenerSpy = sinon.spy(function(e) {
it('select with shift single-click', function () {
const listenerSpy = sinon.spy(function (e) {
expect(e.selected).to.have.length(1);
});
select.on('select', listenerSpy);
@@ -188,18 +191,18 @@ describe('ol.interaction.Select', function() {
});
});
describe('multiselecting polygons', function() {
describe('multiselecting polygons', function () {
let select;
beforeEach(function() {
beforeEach(function () {
select = new Select({
multi: true
multi: true,
});
map.addInteraction(select);
});
it('select with single-click', function() {
const listenerSpy = sinon.spy(function(e) {
it('select with single-click', function () {
const listenerSpy = sinon.spy(function (e) {
expect(e.selected).to.have.length(4);
});
select.on('select', listenerSpy);
@@ -212,8 +215,8 @@ describe('ol.interaction.Select', function() {
expect(features.getLength()).to.equal(4);
});
it('select with shift single-click', function() {
const listenerSpy = sinon.spy(function(e) {
it('select with shift single-click', function () {
const listenerSpy = sinon.spy(function (e) {
expect(e.selected).to.have.length(4);
});
select.on('select', listenerSpy);
@@ -237,17 +240,17 @@ describe('ol.interaction.Select', function() {
});
});
describe('toggle selecting polygons', function() {
describe('toggle selecting polygons', function () {
let select;
beforeEach(function() {
beforeEach(function () {
select = new Select({
multi: true
multi: true,
});
map.addInteraction(select);
});
it('with SHIFT + single-click', function() {
it('with SHIFT + single-click', function () {
const listenerSpy = sinon.spy();
select.on('select', listenerSpy);
@@ -269,16 +272,14 @@ describe('ol.interaction.Select', function() {
});
});
describe('filter features using the filter option', function() {
describe('with multi set to true', function() {
it('only selects features that pass the filter', function() {
describe('filter features using the filter option', function () {
describe('with multi set to true', function () {
it('only selects features that pass the filter', function () {
const select = new Select({
multi: true,
filter: function(feature, layer) {
filter: function (feature, layer) {
return feature.get('type') === 'bar';
}
},
});
map.addInteraction(select);
@@ -289,33 +290,34 @@ describe('ol.interaction.Select', function() {
expect(features.item(1).get('type')).to.be('bar');
});
it('only selects features that pass the filter ' +
'using shift single-click', function() {
const select = new Select({
multi: true,
filter: function(feature, layer) {
return feature.get('type') === 'bar';
}
});
map.addInteraction(select);
it(
'only selects features that pass the filter ' +
'using shift single-click',
function () {
const select = new Select({
multi: true,
filter: function (feature, layer) {
return feature.get('type') === 'bar';
},
});
map.addInteraction(select);
simulateEvent('singleclick', 10, -20,
true);
const features = select.getFeatures();
expect(features.getLength()).to.equal(2);
expect(features.item(0).get('type')).to.be('bar');
expect(features.item(1).get('type')).to.be('bar');
});
simulateEvent('singleclick', 10, -20, true);
const features = select.getFeatures();
expect(features.getLength()).to.equal(2);
expect(features.item(0).get('type')).to.be('bar');
expect(features.item(1).get('type')).to.be('bar');
}
);
});
describe('with multi set to false', function() {
it('only selects the first feature that passes the filter', function() {
describe('with multi set to false', function () {
it('only selects the first feature that passes the filter', function () {
const select = new Select({
multi: false,
filter: function(feature, layer) {
filter: function (feature, layer) {
return feature.get('type') === 'bar';
}
},
});
map.addInteraction(select);
simulateEvent('singleclick', 10, -20);
@@ -324,37 +326,39 @@ describe('ol.interaction.Select', function() {
expect(features.item(0).get('type')).to.be('bar');
});
it('only selects the first feature that passes the filter ' +
'using shift single-click', function() {
const select = new Select({
multi: false,
filter: function(feature, layer) {
return feature.get('type') === 'bar';
}
});
map.addInteraction(select);
simulateEvent('singleclick', 10, -20,
true);
const features = select.getFeatures();
expect(features.getLength()).to.equal(1);
expect(features.item(0).get('type')).to.be('bar');
});
it(
'only selects the first feature that passes the filter ' +
'using shift single-click',
function () {
const select = new Select({
multi: false,
filter: function (feature, layer) {
return feature.get('type') === 'bar';
},
});
map.addInteraction(select);
simulateEvent('singleclick', 10, -20, true);
const features = select.getFeatures();
expect(features.getLength()).to.equal(1);
expect(features.item(0).get('type')).to.be('bar');
}
);
});
});
describe('#getLayer(feature)', function() {
describe('#getLayer(feature)', function () {
let interaction;
beforeEach(function() {
beforeEach(function () {
interaction = new Select();
map.addInteraction(interaction);
});
afterEach(function() {
afterEach(function () {
map.removeInteraction(interaction);
});
it('returns a layer from a selected feature', function() {
const listenerSpy = sinon.spy(function(e) {
it('returns a layer from a selected feature', function () {
const listenerSpy = sinon.spy(function (e) {
const feature = e.selected[0];
const layer_ = interaction.getLayer(feature);
expect(e.selected).to.have.length(1);
@@ -370,10 +374,10 @@ describe('ol.interaction.Select', function() {
});
});
describe('#setActive()', function() {
describe('#setActive()', function () {
let interaction;
beforeEach(function() {
beforeEach(function () {
interaction = new Select();
expect(interaction.getActive()).to.be(true);
@@ -383,35 +387,34 @@ describe('ol.interaction.Select', function() {
simulateEvent('singleclick', 10, -20);
});
afterEach(function() {
afterEach(function () {
map.removeInteraction(interaction);
});
describe('#setActive(false)', function() {
it('keeps the the selection', function() {
describe('#setActive(false)', function () {
it('keeps the the selection', function () {
interaction.setActive(false);
expect(interaction.getFeatures().getLength()).to.equal(1);
});
});
describe('#setActive(true)', function() {
beforeEach(function() {
describe('#setActive(true)', function () {
beforeEach(function () {
interaction.setActive(false);
});
it('fires change:active', function() {
it('fires change:active', function () {
const listenerSpy = sinon.spy();
interaction.on('change:active', listenerSpy);
interaction.setActive(true);
expect(listenerSpy.callCount).to.be(1);
});
});
});
describe('clear event listeners on interaction removal', function() {
describe('clear event listeners on interaction removal', function () {
let firstInteraction, secondInteraction, feature;
beforeEach(function() {
beforeEach(function () {
feature = source.getFeatures()[3]; // top feature is selected
const style = new Style({});
@@ -421,14 +424,14 @@ describe('ol.interaction.Select', function() {
secondInteraction = new Select({style, features});
});
afterEach(function() {
afterEach(function () {
map.removeInteraction(secondInteraction);
map.removeInteraction(firstInteraction);
});
// The base case
describe('with a single interaction added', function() {
it('changes the selected feature once', function() {
describe('with a single interaction added', function () {
it('changes the selected feature once', function () {
map.addInteraction(firstInteraction);
const listenerSpy = sinon.spy();
@@ -441,8 +444,8 @@ describe('ol.interaction.Select', function() {
});
// The "difficult" case. To prevent regression
describe('with a replaced interaction', function() {
it('changes the selected feature once', function() {
describe('with a replaced interaction', function () {
it('changes the selected feature once', function () {
map.addInteraction(firstInteraction);
map.removeInteraction(firstInteraction);
map.addInteraction(secondInteraction);

View File

@@ -1,33 +1,34 @@
import Circle from '../../../../src/ol/geom/Circle.js';
import Collection from '../../../../src/ol/Collection.js';
import Feature from '../../../../src/ol/Feature.js';
import Map from '../../../../src/ol/Map.js';
import View from '../../../../src/ol/View.js';
import Circle from '../../../../src/ol/geom/Circle.js';
import Point from '../../../../src/ol/geom/Point.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Map from '../../../../src/ol/Map.js';
import Point from '../../../../src/ol/geom/Point.js';
import Snap from '../../../../src/ol/interaction/Snap.js';
import {useGeographic, clearUserProjection, setUserProjection, transform} from '../../../../src/ol/proj.js';
import View from '../../../../src/ol/View.js';
import {
clearUserProjection,
setUserProjection,
transform,
useGeographic,
} from '../../../../src/ol/proj.js';
import {overrideRAF} from '../../util.js';
describe('ol.interaction.Snap', function() {
describe('constructor', function() {
it('can be constructed without arguments', function() {
describe('ol.interaction.Snap', function () {
describe('constructor', function () {
it('can be constructed without arguments', function () {
const instance = new Snap();
expect(instance).to.be.an(Snap);
});
});
describe('handleEvent', function() {
describe('handleEvent', function () {
let target, map;
const width = 360;
const height = 180;
beforeEach(function(done) {
beforeEach(function (done) {
target = document.createElement('div');
const style = target.style;
@@ -43,166 +44,207 @@ describe('ol.interaction.Snap', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.once('postrender', function() {
map.once('postrender', function () {
done();
});
});
afterEach(function() {
afterEach(function () {
map.dispose();
document.body.removeChild(target);
clearUserProjection();
});
it('can handle XYZ coordinates', function() {
it('can handle XYZ coordinates', function () {
const point = new Feature(new Point([0, 0, 123]));
const snapInteraction = new Snap({
features: new Collection([point])
features: new Collection([point]),
});
snapInteraction.setMap(map);
const event = {
pixel: [width / 2, height / 2],
coordinate: [0, 0],
map: map
map: map,
};
snapInteraction.handleEvent(event);
// check that the coordinate is in XY and not XYZ
expect(event.coordinate).to.eql([0, 0]);
});
it('snaps to edges only', function() {
const point = new Feature(new LineString([[-10, 0], [10, 0]]));
it('snaps to edges only', function () {
const point = new Feature(
new LineString([
[-10, 0],
[10, 0],
])
);
const snapInteraction = new Snap({
features: new Collection([point]),
pixelTolerance: 5,
vertex: false
vertex: false,
});
snapInteraction.setMap(map);
const event = {
pixel: [7 + width / 2, height / 2 - 4],
coordinate: [7, 4],
map: map
map: map,
};
snapInteraction.handleEvent(event);
expect(event.coordinate).to.eql([7, 0]);
});
it('snaps to vertices only', function() {
const point = new Feature(new LineString([[-10, 0], [10, 0]]));
it('snaps to vertices only', function () {
const point = new Feature(
new LineString([
[-10, 0],
[10, 0],
])
);
const snapInteraction = new Snap({
features: new Collection([point]),
pixelTolerance: 5,
edge: false
edge: false,
});
snapInteraction.setMap(map);
const event = {
pixel: [7 + width / 2, height / 2 - 4],
coordinate: [7, 4],
map: map
map: map,
};
snapInteraction.handleEvent(event);
expect(event.coordinate).to.eql([10, 0]);
});
it('snaps to circle', function() {
it('snaps to circle', function () {
const circle = new Feature(new Circle([0, 0], 10));
const snapInteraction = new Snap({
features: new Collection([circle]),
pixelTolerance: 5
pixelTolerance: 5,
});
snapInteraction.setMap(map);
const event = {
pixel: [5 + width / 2, height / 2 - 5],
coordinate: [5, 5],
map: map
map: map,
};
snapInteraction.handleEvent(event);
expect(event.coordinate[0]).to.roughlyEqual(Math.sin(Math.PI / 4) * 10, 1e-10);
expect(event.coordinate[1]).to.roughlyEqual(Math.sin(Math.PI / 4) * 10, 1e-10);
expect(event.coordinate[0]).to.roughlyEqual(
Math.sin(Math.PI / 4) * 10,
1e-10
);
expect(event.coordinate[1]).to.roughlyEqual(
Math.sin(Math.PI / 4) * 10,
1e-10
);
});
it('snaps to circle in a user projection', function() {
it('snaps to circle in a user projection', function () {
const userProjection = 'EPSG:3857';
setUserProjection(userProjection);
const viewProjection = map.getView().getProjection();
const circle = new Feature(new Circle([0, 0], 10).transform(viewProjection, userProjection));
const circle = new Feature(
new Circle([0, 0], 10).transform(viewProjection, userProjection)
);
const snapInteraction = new Snap({
features: new Collection([circle]),
pixelTolerance: 5
pixelTolerance: 5,
});
snapInteraction.setMap(map);
const event = {
pixel: [5 + width / 2, height / 2 - 5],
coordinate: transform([5, 5], viewProjection, userProjection),
map: map
map: map,
};
snapInteraction.handleEvent(event);
const coordinate = transform([Math.sin(Math.PI / 4) * 10, Math.sin(Math.PI / 4) * 10], viewProjection, userProjection);
const coordinate = transform(
[Math.sin(Math.PI / 4) * 10, Math.sin(Math.PI / 4) * 10],
viewProjection,
userProjection
);
expect(event.coordinate[0]).to.roughlyEqual(coordinate[0], 1e-10);
expect(event.coordinate[1]).to.roughlyEqual(coordinate[1], 1e-10);
});
it('handle feature without geometry', function() {
it('handle feature without geometry', function () {
const feature = new Feature();
const snapInteraction = new Snap({
features: new Collection([feature]),
pixelTolerance: 5,
edge: false
edge: false,
});
snapInteraction.setMap(map);
feature.setGeometry(new LineString([[-10, 0], [10, 0]]));
feature.setGeometry(
new LineString([
[-10, 0],
[10, 0],
])
);
const event = {
pixel: [7 + width / 2, height / 2 - 4],
coordinate: [7, 4],
map: map
map: map,
};
snapInteraction.handleEvent(event);
expect(event.coordinate).to.eql([10, 0]);
});
it('handle geometry changes', function() {
const line = new Feature(new LineString([[-10, 0], [0, 0]]));
it('handle geometry changes', function () {
const line = new Feature(
new LineString([
[-10, 0],
[0, 0],
])
);
const snapInteraction = new Snap({
features: new Collection([line]),
pixelTolerance: 5,
edge: false
edge: false,
});
snapInteraction.setMap(map);
line.getGeometry().setCoordinates([[-10, 0], [10, 0]]);
line.getGeometry().setCoordinates([
[-10, 0],
[10, 0],
]);
const event = {
pixel: [7 + width / 2, height / 2 - 4],
coordinate: [7, 4],
map: map
map: map,
};
snapInteraction.handleEvent(event);
expect(event.coordinate).to.eql([10, 0]);
});
it('handle geometry name changes', function() {
it('handle geometry name changes', function () {
const line = new Feature({
geometry: new LineString([[-10, 0], [0, 0]]),
alt_geometry: new LineString([[-10, 0], [10, 0]])
geometry: new LineString([
[-10, 0],
[0, 0],
]),
alt_geometry: new LineString([
[-10, 0],
[10, 0],
]),
});
const snapInteraction = new Snap({
features: new Collection([line]),
pixelTolerance: 5,
edge: false
edge: false,
});
snapInteraction.setMap(map);
@@ -211,12 +253,11 @@ describe('ol.interaction.Snap', function() {
const event = {
pixel: [7 + width / 2, height / 2 - 4],
coordinate: [7, 4],
map: map
map: map,
};
snapInteraction.handleEvent(event);
expect(event.coordinate).to.eql([10, 0]);
});
});
describe('handleEvent - useGeographic', () => {
@@ -225,7 +266,7 @@ describe('ol.interaction.Snap', function() {
let restoreRAF;
beforeEach(done => {
beforeEach((done) => {
restoreRAF = overrideRAF();
useGeographic();
@@ -236,7 +277,7 @@ describe('ol.interaction.Snap', function() {
top: 0,
left: 0,
width: `${size}px`,
height: `${size}px`
height: `${size}px`,
});
document.body.appendChild(target);
@@ -244,8 +285,8 @@ describe('ol.interaction.Snap', function() {
target: target,
view: new View({
center: [0, 0],
zoom: 0
})
zoom: 0,
}),
});
map.once('postrender', () => {
@@ -266,11 +307,13 @@ describe('ol.interaction.Snap', function() {
const point = new Feature(new Point([lon, lat]));
const snap = new Snap({
features: new Collection([point])
features: new Collection([point]),
});
snap.setMap(map);
const expectedPixel = map.getPixelFromCoordinate([lon, lat]).map(value => Math.round(value));
const expectedPixel = map
.getPixelFromCoordinate([lon, lat])
.map((value) => Math.round(value));
const delta = 5;
const pixel = expectedPixel.slice();
@@ -282,14 +325,12 @@ describe('ol.interaction.Snap', function() {
const event = {
pixel: pixel,
coordinate: coordinate,
map: map
map: map,
};
snap.handleEvent(event);
expect(event.coordinate).to.eql([lon, lat]);
expect(event.pixel).to.eql(expectedPixel);
});
});
});

View File

@@ -1,22 +1,23 @@
import Collection from '../../../../src/ol/Collection.js';
import Feature from '../../../../src/ol/Feature.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Map from '../../../../src/ol/Map.js';
import MapBrowserPointerEvent from '../../../../src/ol/MapBrowserPointerEvent.js';
import View from '../../../../src/ol/View.js';
import Point from '../../../../src/ol/geom/Point.js';
import Translate, {TranslateEvent} from '../../../../src/ol/interaction/Translate.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Translate, {
TranslateEvent,
} from '../../../../src/ol/interaction/Translate.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import View from '../../../../src/ol/View.js';
describe('ol.interaction.Translate', function() {
describe('ol.interaction.Translate', function () {
let target, map, source, features;
const width = 360;
const height = 180;
beforeEach(function(done) {
beforeEach(function (done) {
target = document.createElement('div');
const style = target.style;
style.position = 'absolute';
@@ -26,11 +27,14 @@ describe('ol.interaction.Translate', function() {
style.height = height + 'px';
document.body.appendChild(target);
source = new VectorSource();
features = [new Feature({
geometry: new Point([10, -20])
}), new Feature({
geometry: new Point([20, -30])
})];
features = [
new Feature({
geometry: new Point([10, -20]),
}),
new Feature({
geometry: new Point([20, -30]),
}),
];
source.addFeatures(features);
const layer = new VectorLayer({source: source});
map = new Map({
@@ -39,15 +43,15 @@ describe('ol.interaction.Translate', function() {
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1
})
resolution: 1,
}),
});
map.once('postrender', function() {
map.once('postrender', function () {
done();
});
});
afterEach(function() {
afterEach(function () {
map.dispose();
document.body.removeChild(target);
});
@@ -72,7 +76,7 @@ describe('ol.interaction.Translate', function() {
clientX: position.left + x + width / 2,
clientY: position.top + y + height / 2,
shiftKey: shiftKey,
preventDefault: function() {}
preventDefault: function () {},
});
map.handleMapBrowserEvent(event);
}
@@ -86,13 +90,13 @@ describe('ol.interaction.Translate', function() {
*/
function trackEvents(feature, interaction) {
const events = [];
feature.on('change', function(event) {
feature.on('change', function (event) {
events.push('change');
});
interaction.on('translatestart', function(event) {
interaction.on('translatestart', function (event) {
events.push(event);
});
interaction.on('translateend', function(event) {
interaction.on('translateend', function (event) {
events.push(event);
});
return events;
@@ -106,7 +110,6 @@ describe('ol.interaction.Translate', function() {
* @param {Array<ol.Feature>} features The features.
*/
function validateEvents(events, features) {
const startevent = events[0];
const endevent = events[events.length - 1];
@@ -130,43 +133,38 @@ describe('ol.interaction.Translate', function() {
expect(endevent.features.getArray()).to.eql(features);
}
describe('constructor', function() {
it('creates a new interaction', function() {
describe('constructor', function () {
it('creates a new interaction', function () {
const translate = new Translate({
features: features
features: features,
});
expect(translate).to.be.a(Translate);
expect(translate).to.be.a(Interaction);
});
});
describe('setActive', function() {
it('works when the map is not set', function() {
describe('setActive', function () {
it('works when the map is not set', function () {
const translate = new Translate({
features: features
features: features,
});
expect(translate.getActive()).to.be(true);
translate.setActive(false);
expect(translate.getActive()).to.be(false);
});
});
describe('moving features, with features option', function() {
describe('moving features, with features option', function () {
let translate;
beforeEach(function() {
beforeEach(function () {
translate = new Translate({
features: new Collection([features[0]])
features: new Collection([features[0]]),
});
map.addInteraction(translate);
});
it('moves a selected feature', function() {
it('moves a selected feature', function () {
const events = trackEvents(features[0], translate);
simulateEvent('pointermove', 10, 20);
@@ -180,7 +178,7 @@ describe('ol.interaction.Translate', function() {
validateEvents(events, [features[0]]);
});
it('does not move an unselected feature', function() {
it('does not move an unselected feature', function () {
const events = trackEvents(features[0], translate);
simulateEvent('pointermove', 20, 30);
@@ -195,15 +193,15 @@ describe('ol.interaction.Translate', function() {
});
});
describe('moving features, without features option', function() {
describe('moving features, without features option', function () {
let translate;
beforeEach(function() {
beforeEach(function () {
translate = new Translate();
map.addInteraction(translate);
});
it('moves only targeted feature', function() {
it('moves only targeted feature', function () {
const events = trackEvents(features[0], translate);
simulateEvent('pointermove', 10, 20);
@@ -217,19 +215,19 @@ describe('ol.interaction.Translate', function() {
});
});
describe('moving features, with filter option', function() {
describe('moving features, with filter option', function () {
let translate;
beforeEach(function() {
beforeEach(function () {
translate = new Translate({
filter: function(feature, layer) {
filter: function (feature, layer) {
return feature == features[0];
}
},
});
map.addInteraction(translate);
});
it('moves a filter-passing feature', function() {
it('moves a filter-passing feature', function () {
const events = trackEvents(features[0], translate);
simulateEvent('pointermove', 10, 20);
@@ -243,7 +241,7 @@ describe('ol.interaction.Translate', function() {
validateEvents(events, [features[0]]);
});
it('does not move a filter-discarded feature', function() {
it('does not move a filter-discarded feature', function () {
const events = trackEvents(features[0], translate);
simulateEvent('pointermove', 20, 30);
@@ -258,16 +256,16 @@ describe('ol.interaction.Translate', function() {
});
});
describe('changes css cursor', function() {
describe('changes css cursor', function () {
let element, translate;
beforeEach(function() {
beforeEach(function () {
translate = new Translate();
map.addInteraction(translate);
element = map.getViewport();
});
it('changes css cursor', function() {
it('changes css cursor', function () {
expect(element.classList.contains('ol-grabbing')).to.be(false);
expect(element.classList.contains('ol-grab')).to.be(false);
@@ -288,7 +286,7 @@ describe('ol.interaction.Translate', function() {
expect(element.classList.contains('ol-grab')).to.be(false);
});
it('resets css cursor when interaction is deactivated while pointer is on feature', function() {
it('resets css cursor when interaction is deactivated while pointer is on feature', function () {
simulateEvent('pointermove', 10, 20);
expect(element.classList.contains('ol-grabbing')).to.be(false);
expect(element.classList.contains('ol-grab')).to.be(true);
@@ -300,7 +298,7 @@ describe('ol.interaction.Translate', function() {
expect(element.classList.contains('ol-grab')).to.be(false);
});
it('resets css cursor interaction is removed while pointer is on feature', function() {
it('resets css cursor interaction is removed while pointer is on feature', function () {
simulateEvent('pointermove', 10, 20);
expect(element.classList.contains('ol-grabbing')).to.be(false);
expect(element.classList.contains('ol-grab')).to.be(true);
@@ -312,7 +310,7 @@ describe('ol.interaction.Translate', function() {
expect(element.classList.contains('ol-grab')).to.be(false);
});
it('resets css cursor to existing cursor interaction is removed while pointer is on feature', function() {
it('resets css cursor to existing cursor interaction is removed while pointer is on feature', function () {
element.style.cursor = 'pointer';
simulateEvent('pointermove', 10, 20);
@@ -325,7 +323,5 @@ describe('ol.interaction.Translate', function() {
expect(element.classList.contains('ol-grabbing')).to.be(false);
expect(element.classList.contains('ol-grab')).to.be(false);
});
});
});