Don't reset the css cursor if it's not needed

This commit is contained in:
Frederic Junod
2016-12-15 09:27:46 +01:00
parent 40afb9d40d
commit c6fe60bcd7
2 changed files with 49 additions and 17 deletions

View File

@@ -171,7 +171,8 @@ ol.interaction.Translate.handleMoveEvent_ = function(event) {
// Change the cursor to grab/grabbing if hovering any of the features managed
// by the interaction
if (this.featuresAtPixel_(event.pixel, event.map)) {
this.previousCursor_ = elem.style.cursor;
this.previousCursor_ = this.previousCursor_ !== undefined ?
this.previousCursor_ : elem.style.cursor;
// WebKit browsers don't support the grab icons without a prefix
elem.style.cursor = this.lastCoordinate_ ?
'-webkit-grabbing' : '-webkit-grab';
@@ -179,9 +180,8 @@ ol.interaction.Translate.handleMoveEvent_ = function(event) {
// Thankfully, attempting to set the standard ones will silently fail,
// keeping the prefixed icons
elem.style.cursor = this.lastCoordinate_ ? 'grabbing' : 'grab';
} else {
elem.style.cursor = this.previousCursor_ !== undefined ?
this.previousCursor_ : '';
} else if (this.previousCursor_ !== undefined) {
elem.style.cursor = this.previousCursor_;
this.previousCursor_ = undefined;
}
};

View File

@@ -56,13 +56,13 @@ describe('ol.interaction.Translate', function() {
});
/**
* Simulates a browser event on the map viewport. The client x/y location
* will be adjusted as if the map were centered at 0,0.
* @param {string} type Event type.
* @param {number} x Horizontal offset from map center.
* @param {number} y Vertical offset from map center.
* @param {boolean=} opt_shiftKey Shift key is pressed.
*/
* Simulates a browser event on the map viewport. The client x/y location
* will be adjusted as if the map were centered at 0,0.
* @param {string} type Event type.
* @param {number} x Horizontal offset from map center.
* @param {number} y Vertical offset from map center.
* @param {boolean=} opt_shiftKey Shift key is pressed.
*/
function simulateEvent(type, x, y, opt_shiftKey) {
var viewport = map.getViewport();
// calculated in case body has top < 0 (test runner with small window)
@@ -99,12 +99,12 @@ describe('ol.interaction.Translate', function() {
}
/**
* Validates the event array to verify proper event sequence. Checks
* that first and last event are correct TranslateEvents and that feature
* modifications event are in between.
* @param {Array<ol.interaction.Translate.Event|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 TranslateEvents and that feature
* modifications event are in between.
* @param {Array<ol.interaction.Translate.Event|string>} events The events.
* @param {Array<ol.Feature>} features The features.
*/
function validateEvents(events, features) {
var startevent = events[0];
@@ -203,4 +203,36 @@ describe('ol.interaction.Translate', function() {
validateEvents(events, [features[0]]);
});
});
describe('changes css cursor', function() {
var element, translate;
beforeEach(function() {
translate = new ol.interaction.Translate();
map.addInteraction(translate);
element = map.getTargetElement();
});
it('changes css cursor', function() {
expect(element.style.cursor).to.eql('');
simulateEvent('pointermove', 10, 20);
expect(element.style.cursor).to.match(/grab$/);
simulateEvent('pointerdown', 10, 20);
expect(element.style.cursor).to.match(/grabbing$/);
simulateEvent('pointerup', 10, 20);
expect(element.style.cursor).to.match(/grab$/);
});
it('respects existing cursor value', function() {
element.style.cursor = 'pointer';
simulateEvent('pointermove', 0, 0);
expect(element.style.cursor).to.eql('pointer');
});
});
});