Skip to content

Commit

Permalink
Fix the feature and unlisteners
Browse files Browse the repository at this point in the history
  • Loading branch information
llienher committed Sep 23, 2019
1 parent 25d50f4 commit 72fc345
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
24 changes: 14 additions & 10 deletions contribs/gmf/src/drawing/drawFeatureComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,6 @@ exports.Controller_.prototype.handleActiveChange_ = function(active) {
this.menu_ = null;
}
}

olEvents.unlistenByKey(this.cancelEventKey_);

};


Expand Down Expand Up @@ -546,6 +543,9 @@ exports.Controller_.prototype.handleMapSelectActiveChange_ = function(
olEvents.listen(mapDiv, 'touchend',
this.handleMapTouchEnd_, this);

olEvents.listen(document.body, 'keydown',
this.handleCancelKeyEvent_, this);

} else {
olEvents.unlisten(this.map, 'click',
this.handleMapClick_, this);
Expand All @@ -561,6 +561,9 @@ exports.Controller_.prototype.handleMapSelectActiveChange_ = function(

olEvents.unlisten(mapDiv, 'touchend',
this.handleMapTouchEnd_, this);

olEvents.unlisten(document.body, 'keydown',
this.handleCancelKeyEvent_, this);
}
};

Expand Down Expand Up @@ -594,20 +597,21 @@ exports.Controller_.prototype.handleMapClick_ = function(evt) {
return;
}

this.cancelEventKey_ = olEvents.listen(document.body, 'keydown', (e) => {
this.selectedFeature = feature;

this.scope_.$apply();
};


exports.Controller_.prototype.handleCancelKeyEvent_ = function() {
olEvents.listen(document.body, 'keydown', (e) => {
const escPressed = event.keyCode === 27; // Escape key
if (escPressed && this.selectedFeature) {
this.selectedFeature = null;
olEvents.unlistenByKey(this.cancelEventKey_);
}
});

this.selectedFeature = feature;

this.scope_.$apply();
};


/**
* @param {!Event} evt Event.
* @private
Expand Down
18 changes: 11 additions & 7 deletions contribs/gmf/src/editing/editFeatureComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,13 +990,16 @@ exports.Controller_.prototype.handleMapClick_ = function(evt) {
const interactions = evt.target.getInteractions().getArray();
const activeInteraction = interactions.find(interaction => interaction.getActive() === true);

this.cancelEventKey_ = olEvents.listen(document.body, 'keydown', (e) => {
const escPressed = event.keyCode === 27; // Escape key
if (escPressed && activeInteraction.getActive()) {
this.cancel();
olEvents.unlistenByKey(this.cancelEventKey_);
}
});
if (this.cancelEventKey_ === undefined) {
this.cancelEventKey_ = olEvents.listen(document.body, 'keydown', (e) => {
const escPressed = event.keyCode === 27; // Escape key
if (escPressed && activeInteraction.getActive()) {
this.cancel();
olEvents.unlistenByKey(this.cancelEventKey_);
this.cancelEventKey_ = undefined;
}
});
}

const coordinate = evt.coordinate;
const pixel = evt.pixel;
Expand Down Expand Up @@ -1264,6 +1267,7 @@ exports.Controller_.prototype.handleDestroy_ = function() {
this.handleMapSelectActiveChange_(false);
this.unregisterInteractions_();
olEvents.unlistenByKey(this.cancelEventKey_);
this.cancelEventKey_ = undefined;
};


Expand Down
30 changes: 16 additions & 14 deletions src/draw/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import ngeoFormatFeatureProperties from 'ngeo/format/FeatureProperties.js';
import ngeoGeometryType from 'ngeo/GeometryType.js';
import ngeoMiscBtnComponent from 'ngeo/misc/btnComponent.js';
import ngeoMiscDecorate from 'ngeo/misc/decorate.js';
import ngeoMiscEventHelper from 'ngeo/misc/EventHelper.js';
import ngeoMiscFeatureHelper from 'ngeo/misc/FeatureHelper.js';
import olFeature from 'ol/Feature.js';
import * as olBase from 'ol/index.js';
import * as olEvents from 'ol/events.js';
import ngeoMiscEventHelper from 'ngeo/misc/EventHelper.js';
import {getUid} from 'ol/index.js';
import {listen} from 'ol/events.js';

/**
* @param {!angular.Scope} $scope Scope.
Expand Down Expand Up @@ -187,17 +187,19 @@ exports.prototype.registerInteraction = function(
exports.prototype.handleActiveChange = function(event) {
this.active = this.interactions_.some(interaction => interaction.getActive(), this);
this.interaction_ = this.interactions_.find(interaction => interaction.getActive() === true);
const uid = olBase.getUid(this);

this.ngeoEventHelper_.addListenerKey(
uid,
olEvents.listen(
document.body,
'keydown',
this.handleEscapeKeyDown_,
this
)
);

if (this.interaction_) {
const uid = getUid(this);
this.ngeoEventHelper_.addListenerKey(
uid,
listen(
document.body,
'keydown',
this.handleEscapeKeyDown_,
this
)
);
}
};


Expand Down
7 changes: 4 additions & 3 deletions src/editing/createfeatureComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,11 @@ exports.Controller_.prototype.$onInit = function() {
* @export
*/
exports.Controller_.prototype.handleEscapeKeyDown_ = function(event) {
const interaction = this.interaction_;
const escPressed = event.keyCode === 27; // Escape key
if (escPressed && this.interaction_.getActive()) {
this.interaction_.setActive(false);
this.interaction_.setActive(true);
if (escPressed && interaction.getActive()) {
interaction.setActive(false);
interaction.setActive(true);
}
};

Expand Down

0 comments on commit 72fc345

Please sign in to comment.