Remove remaining static members from Interaction

This commit is contained in:
Tim Schaub
2018-02-25 12:42:37 -07:00
parent 244fbbbb8d
commit 1404e9d61d
13 changed files with 102 additions and 77 deletions

View File

@@ -3,7 +3,7 @@
*/
import {inherits} from '../index.js';
import MapBrowserEventType from '../MapBrowserEventType.js';
import Interaction from '../interaction/Interaction.js';
import Interaction, {zoomByDelta} from '../interaction/Interaction.js';
/**
* @classdesc
@@ -54,7 +54,7 @@ function handleEvent(mapBrowserEvent) {
const anchor = mapBrowserEvent.coordinate;
const delta = browserEvent.shiftKey ? -this.delta_ : this.delta_;
const view = map.getView();
Interaction.zoomByDelta(view, delta, anchor, this.duration_);
zoomByDelta(view, delta, anchor, this.duration_);
mapBrowserEvent.preventDefault();
stopEvent = true;
}

View File

@@ -6,7 +6,7 @@ import {disable} from '../rotationconstraint.js';
import ViewHint from '../ViewHint.js';
import {altShiftKeysOnly, mouseOnly, mouseActionButton} from '../events/condition.js';
import {FALSE} from '../functions.js';
import Interaction, {rotate} from '../interaction/Interaction.js';
import {rotate, rotateWithoutConstraints} from '../interaction/Interaction.js';
import PointerInteraction from '../interaction/Pointer.js';
/**
@@ -75,8 +75,7 @@ function handleDragEvent(mapBrowserEvent) {
if (this.lastAngle_ !== undefined) {
const delta = theta - this.lastAngle_;
const rotation = view.getRotation();
Interaction.rotateWithoutConstraints(
view, rotation - delta);
rotateWithoutConstraints(view, rotation - delta);
}
this.lastAngle_ = theta;
}

View File

@@ -5,7 +5,7 @@ import {inherits} from '../index.js';
import {disable} from '../rotationconstraint.js';
import ViewHint from '../ViewHint.js';
import {shiftKeyOnly, mouseOnly} from '../events/condition.js';
import Interaction, {rotate, zoom, zoomWithoutConstraints} from '../interaction/Interaction.js';
import {rotate, rotateWithoutConstraints, zoom, zoomWithoutConstraints} from '../interaction/Interaction.js';
import PointerInteraction from '../interaction/Pointer.js';
/**
@@ -87,8 +87,7 @@ function handleDragEvent(mapBrowserEvent) {
const view = map.getView();
if (view.getConstraints().rotation !== disable && this.lastAngle_ !== undefined) {
const angleDelta = theta - this.lastAngle_;
Interaction.rotateWithoutConstraints(
view, view.getRotation() - angleDelta);
rotateWithoutConstraints(view, view.getRotation() - angleDelta);
}
this.lastAngle_ = theta;
if (this.lastMagnitude_ !== undefined) {

View File

@@ -108,7 +108,7 @@ Interaction.prototype.setMap = function(map) {
* @param {ol.Coordinate} delta Delta.
* @param {number=} opt_duration Duration.
*/
Interaction.pan = function(view, delta, opt_duration) {
export function pan(view, delta, opt_duration) {
const currentCenter = view.getCenter();
if (currentCenter) {
const center = view.constrainCenter(
@@ -123,7 +123,7 @@ Interaction.pan = function(view, delta, opt_duration) {
view.setCenter(center);
}
}
};
}
/**
@@ -134,8 +134,7 @@ Interaction.pan = function(view, delta, opt_duration) {
*/
export function rotate(view, rotation, opt_anchor, opt_duration) {
rotation = view.constrainRotation(rotation, 0);
Interaction.rotateWithoutConstraints(
view, rotation, opt_anchor, opt_duration);
rotateWithoutConstraints(view, rotation, opt_anchor, opt_duration);
}
@@ -145,7 +144,7 @@ export function rotate(view, rotation, opt_anchor, opt_duration) {
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
Interaction.rotateWithoutConstraints = function(view, rotation, opt_anchor, opt_duration) {
export function rotateWithoutConstraints(view, rotation, opt_anchor, opt_duration) {
if (rotation !== undefined) {
const currentRotation = view.getRotation();
const currentCenter = view.getCenter();
@@ -160,7 +159,7 @@ Interaction.rotateWithoutConstraints = function(view, rotation, opt_anchor, opt_
view.rotate(rotation, opt_anchor);
}
}
};
}
/**
@@ -189,7 +188,7 @@ export function zoom(view, resolution, opt_anchor, opt_duration, opt_direction)
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
Interaction.zoomByDelta = function(view, delta, opt_anchor, opt_duration) {
export function zoomByDelta(view, delta, opt_anchor, opt_duration) {
const currentResolution = view.getResolution();
let resolution = view.constrainResolution(currentResolution, delta, 0);
@@ -218,7 +217,7 @@ Interaction.zoomByDelta = function(view, delta, opt_anchor, opt_duration) {
}
zoomWithoutConstraints(view, resolution, opt_anchor, opt_duration);
};
}
/**

View File

@@ -6,7 +6,7 @@ import {rotate as rotateCoordinate} from '../coordinate.js';
import EventType from '../events/EventType.js';
import KeyCode from '../events/KeyCode.js';
import {noModifierKeys, targetNotEditable} from '../events/condition.js';
import Interaction from '../interaction/Interaction.js';
import Interaction, {pan} from '../interaction/Interaction.js';
/**
* @classdesc
@@ -100,7 +100,7 @@ function handleEvent(mapBrowserEvent) {
}
const delta = [deltaX, deltaY];
rotateCoordinate(delta, view.getRotation());
Interaction.pan(view, delta, this.duration_);
pan(view, delta, this.duration_);
mapBrowserEvent.preventDefault();
stopEvent = true;
}

View File

@@ -4,7 +4,7 @@
import {inherits} from '../index.js';
import EventType from '../events/EventType.js';
import {targetNotEditable} from '../events/condition.js';
import Interaction from '../interaction/Interaction.js';
import Interaction, {zoomByDelta} from '../interaction/Interaction.js';
/**
* @classdesc
@@ -73,8 +73,7 @@ function handleEvent(mapBrowserEvent) {
const map = mapBrowserEvent.map;
const delta = (charCode == '+'.charCodeAt(0)) ? this.delta_ : -this.delta_;
const view = map.getView();
Interaction.zoomByDelta(
view, delta, undefined, this.duration_);
zoomByDelta(view, delta, undefined, this.duration_);
mapBrowserEvent.preventDefault();
stopEvent = true;
}

View File

@@ -7,7 +7,7 @@ import {always} from '../events/condition.js';
import {easeOut} from '../easing.js';
import EventType from '../events/EventType.js';
import {DEVICE_PIXEL_RATIO, FIREFOX, SAFARI} from '../has.js';
import Interaction from '../interaction/Interaction.js';
import Interaction, {zoomByDelta} from '../interaction/Interaction.js';
import {clamp} from '../math.js';
@@ -280,8 +280,7 @@ MouseWheelZoom.prototype.handleWheelZoom_ = function(map) {
}
const maxDelta = MAX_DELTA;
const delta = clamp(this.delta_, -maxDelta, maxDelta);
Interaction.zoomByDelta(view, -delta, this.lastAnchor_,
this.duration_);
zoomByDelta(view, -delta, this.lastAnchor_, this.duration_);
this.mode_ = undefined;
this.delta_ = 0;
this.lastAnchor_ = null;

View File

@@ -4,7 +4,7 @@
import {inherits} from '../index.js';
import ViewHint from '../ViewHint.js';
import {FALSE} from '../functions.js';
import Interaction, {rotate} from '../interaction/Interaction.js';
import {rotate, rotateWithoutConstraints} from '../interaction/Interaction.js';
import PointerInteraction, {centroid as centroidFromPointers} from '../interaction/Pointer.js';
import {disable} from '../rotationconstraint.js';
@@ -114,8 +114,7 @@ function handleDragEvent(mapBrowserEvent) {
if (this.rotating_) {
const rotation = view.getRotation();
map.render();
Interaction.rotateWithoutConstraints(view,
rotation + rotationDelta, this.anchor_);
rotateWithoutConstraints(view, rotation + rotationDelta, this.anchor_);
}
}

View File

@@ -2,7 +2,6 @@ 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 Interaction from '../../../../src/ol/interaction/Interaction.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import PointerEvent from '../../../../src/ol/pointer/PointerEvent.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
@@ -62,22 +61,30 @@ describe('ol.interaction.DragRotateAndZoom', function() {
new PointerEvent('pointermove', {clientX: 20, clientY: 10}, {pointerType: 'mouse'}),
true);
interaction.lastAngle_ = Math.PI;
const spy = sinon.spy(Interaction, 'rotateWithoutConstraints');
let view = map.getView();
let spy = sinon.spy(view, 'rotate');
interaction.handleDragEvent_(event);
expect(spy.callCount).to.be(1);
expect(interaction.lastAngle_).to.be(-0.8308214428190254);
map.setView(new View({
view.rotate.restore();
view = new View({
projection: 'EPSG:4326',
center: [0, 0],
resolution: 1,
enableRotation: false
}));
});
map.setView(view);
event = new MapBrowserPointerEvent('pointermove', map,
new PointerEvent('pointermove', {clientX: 24, clientY: 16}, {pointerType: 'mouse'}),
true);
spy = sinon.spy(view, 'rotate');
interaction.handleDragEvent_(event);
expect(spy.callCount).to.be(1);
Interaction.rotateWithoutConstraints.restore();
expect(spy.callCount).to.be(0);
view.rotate.restore();
});
});

View File

@@ -1,7 +1,7 @@
import Map from '../../../../src/ol/Map.js';
import View from '../../../../src/ol/View.js';
import EventTarget from '../../../../src/ol/events/EventTarget.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Interaction, {zoomByDelta} from '../../../../src/ol/interaction/Interaction.js';
describe('ol.interaction.Interaction', function() {
@@ -64,16 +64,16 @@ describe('ol.interaction.Interaction', function() {
resolutions: [4, 2, 1, 0.5, 0.25]
});
Interaction.zoomByDelta(view, 1);
zoomByDelta(view, 1);
expect(view.getResolution()).to.be(0.5);
Interaction.zoomByDelta(view, -1);
zoomByDelta(view, -1);
expect(view.getResolution()).to.be(1);
Interaction.zoomByDelta(view, 2);
zoomByDelta(view, 2);
expect(view.getResolution()).to.be(0.25);
Interaction.zoomByDelta(view, -2);
zoomByDelta(view, -2);
expect(view.getResolution()).to.be(1);
});
@@ -84,16 +84,16 @@ describe('ol.interaction.Interaction', function() {
resolutions: [4, 2, 1, 0.5, 0.25]
});
Interaction.zoomByDelta(view, 1, [10, 10]);
zoomByDelta(view, 1, [10, 10]);
expect(view.getCenter()).to.eql([5, 5]);
Interaction.zoomByDelta(view, -1, [0, 0]);
zoomByDelta(view, -1, [0, 0]);
expect(view.getCenter()).to.eql([10, 10]);
Interaction.zoomByDelta(view, 2, [0, 0]);
zoomByDelta(view, 2, [0, 0]);
expect(view.getCenter()).to.eql([2.5, 2.5]);
Interaction.zoomByDelta(view, -2, [0, 0]);
zoomByDelta(view, -2, [0, 0]);
expect(view.getCenter()).to.eql([10, 10]);
});
@@ -105,16 +105,16 @@ describe('ol.interaction.Interaction', function() {
resolutions: [4, 2, 1, 0.5, 0.25]
});
Interaction.zoomByDelta(view, 1, [10, 10]);
zoomByDelta(view, 1, [10, 10]);
expect(view.getCenter()).to.eql([2.5, 2.5]);
Interaction.zoomByDelta(view, -1, [0, 0]);
zoomByDelta(view, -1, [0, 0]);
expect(view.getCenter()).to.eql([2.5, 2.5]);
Interaction.zoomByDelta(view, 2, [10, 10]);
zoomByDelta(view, 2, [10, 10]);
expect(view.getCenter()).to.eql([2.5, 2.5]);
Interaction.zoomByDelta(view, -2, [0, 0]);
zoomByDelta(view, -2, [0, 0]);
expect(view.getCenter()).to.eql([2.5, 2.5]);
});
});

View File

@@ -2,7 +2,6 @@ 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';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
describe('ol.interaction.KeyboardPan', function() {
let map;
@@ -24,25 +23,35 @@ describe('ol.interaction.KeyboardPan', function() {
describe('handleEvent()', function() {
it('pans on arrow keys', function() {
const spy = sinon.spy(Interaction, 'pan');
const view = map.getView();
const spy = sinon.spy(view, 'animate');
const event = new MapBrowserEvent('keydown', map, {
type: 'keydown',
target: map.getTargetElement(),
preventDefault: Event.prototype.preventDefault
});
event.originalEvent.keyCode = 40; // DOWN
map.handleMapBrowserEvent(event);
expect(spy.getCall(0).args[0].center).to.eql([0, -128]);
view.setCenter([0, 0]);
event.originalEvent.keyCode = 38; // UP
map.handleMapBrowserEvent(event);
expect(spy.getCall(1).args[0].center).to.eql([0, 128]);
view.setCenter([0, 0]);
event.originalEvent.keyCode = 37; // LEFT
map.handleMapBrowserEvent(event);
expect(spy.getCall(2).args[0].center).to.eql([-128, 0]);
view.setCenter([0, 0]);
event.originalEvent.keyCode = 39; // RIGHT
map.handleMapBrowserEvent(event);
expect(spy.getCall(0).args[1]).to.eql([0, -128]);
expect(spy.getCall(1).args[1]).to.eql([0, 128]);
expect(spy.getCall(2).args[1]).to.eql([-128, 0]);
expect(spy.getCall(3).args[1]).to.eql([128, 0]);
Interaction.pan.restore();
expect(spy.getCall(3).args[0].center).to.eql([128, 0]);
view.setCenter([0, 0]);
view.animate.restore();
});
});

View File

@@ -2,7 +2,6 @@ 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';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
describe('ol.interaction.KeyboardZoom', function() {
let map;
@@ -12,8 +11,8 @@ describe('ol.interaction.KeyboardZoom', function() {
target: createMapDiv(100, 100),
view: new View({
center: [0, 0],
resolutions: [1],
zoom: 0
resolutions: [4, 2, 1],
zoom: 1
})
});
map.renderSync();
@@ -24,19 +23,25 @@ describe('ol.interaction.KeyboardZoom', function() {
describe('handleEvent()', function() {
it('zooms on + and - keys', function() {
const spy = sinon.spy(Interaction, 'zoomByDelta');
const view = map.getView();
const spy = sinon.spy(view, 'animate');
const event = new MapBrowserEvent('keydown', map, {
type: 'keydown',
target: map.getTargetElement(),
preventDefault: Event.prototype.preventDefault
});
event.originalEvent.charCode = '+'.charCodeAt(0);
map.handleMapBrowserEvent(event);
expect(spy.getCall(0).args[0].resolution).to.eql(1);
view.setResolution(2);
event.originalEvent.charCode = '-'.charCodeAt(0);
map.handleMapBrowserEvent(event);
expect(spy.getCall(0).args[1]).to.eql(1);
expect(spy.getCall(1).args[1]).to.eql(-1);
Interaction.zoomByDelta.restore();
expect(spy.getCall(1).args[0].resolution).to.eql(4);
view.setResolution(2);
view.animate.restore();
});
});

View File

@@ -3,7 +3,6 @@ import MapBrowserEvent from '../../../../src/ol/MapBrowserEvent.js';
import View from '../../../../src/ol/View.js';
import Event from '../../../../src/ol/events/Event.js';
import {DEVICE_PIXEL_RATIO, FIREFOX, SAFARI} from '../../../../src/ol/has.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import MouseWheelZoom, {Mode} from '../../../../src/ol/interaction/MouseWheelZoom.js';
@@ -33,13 +32,13 @@ describe('ol.interaction.MouseWheelZoom', function() {
describe('timeout duration', function() {
let clock;
beforeEach(function() {
sinon.spy(Interaction, 'zoomByDelta');
sinon.spy(interaction, 'handleWheelZoom_');
clock = sinon.useFakeTimers();
});
afterEach(function() {
clock.restore();
Interaction.zoomByDelta.restore();
interaction.handleWheelZoom_.restore();
});
it('works with the defaut value', function(done) {
@@ -48,12 +47,14 @@ describe('ol.interaction.MouseWheelZoom', function() {
target: map.getViewport(),
preventDefault: Event.prototype.preventDefault
});
map.handleMapBrowserEvent(event);
clock.tick(50);
// default timeout is 80 ms, not called yet
expect(Interaction.zoomByDelta.called).to.be(false);
expect(interaction.handleWheelZoom_.called).to.be(false);
clock.tick(30);
expect(Interaction.zoomByDelta.called).to.be(true);
expect(interaction.handleWheelZoom_.called).to.be(true);
done();
});
@@ -98,21 +99,25 @@ describe('ol.interaction.MouseWheelZoom', function() {
});
}
describe('spying on ol.interaction.Interaction.zoomByDelta', function() {
describe('spying on view.animate()', function() {
let view;
beforeEach(function() {
sinon.spy(Interaction, 'zoomByDelta');
view = map.getView();
sinon.spy(view, 'animate');
});
afterEach(function() {
Interaction.zoomByDelta.restore();
view.animate.restore();
});
it('works in DOM_DELTA_LINE mode (wheel)', function(done) {
map.once('postrender', function() {
const call = Interaction.zoomByDelta.getCall(0);
expect(call.args[1]).to.be(-1);
expect(call.args[2]).to.eql([0, 0]);
const call = view.animate.getCall(0);
expect(call.args[0].resolution).to.be(2);
expect(call.args[0].anchor).to.eql([0, 0]);
done();
});
const event = new MapBrowserEvent('wheel', map, {
type: 'wheel',
deltaMode: WheelEvent.DOM_DELTA_LINE,
@@ -121,17 +126,19 @@ describe('ol.interaction.MouseWheelZoom', function() {
preventDefault: Event.prototype.preventDefault
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
});
if (SAFARI) {
it('works on Safari (wheel)', function(done) {
map.once('postrender', function() {
const call = Interaction.zoomByDelta.getCall(0);
expect(call.args[1]).to.be(-1);
expect(call.args[2]).to.eql([0, 0]);
const call = view.animate.getCall(0);
expect(call.args[0].resolution).to.be(2);
expect(call.args[0].anchor).to.eql([0, 0]);
done();
});
const event = new MapBrowserEvent('mousewheel', map, {
type: 'mousewheel',
wheelDeltaY: -50,
@@ -139,6 +146,7 @@ describe('ol.interaction.MouseWheelZoom', function() {
preventDefault: Event.prototype.preventDefault
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
});
}
@@ -146,11 +154,12 @@ describe('ol.interaction.MouseWheelZoom', function() {
if (!SAFARI) {
it('works on other browsers (wheel)', function(done) {
map.once('postrender', function() {
const call = Interaction.zoomByDelta.getCall(0);
expect(call.args[1]).to.be(-1);
expect(call.args[2]).to.eql([0, 0]);
const call = view.animate.getCall(0);
expect(call.args[0].resolution).to.be(2);
expect(call.args[0].anchor).to.eql([0, 0]);
done();
});
const event = new MapBrowserEvent('mousewheel', map, {
type: 'mousewheel',
wheelDeltaY: -120,
@@ -158,6 +167,7 @@ describe('ol.interaction.MouseWheelZoom', function() {
preventDefault: Event.prototype.preventDefault
});
event.coordinate = [0, 0];
map.handleMapBrowserEvent(event);
});
}