Add SelectEvent to Select interaction

Fixes
This commit is contained in:
Björn Harrtell
2015-02-25 19:21:36 +01:00
parent 927a0a4627
commit e4b1d6e1de
3 changed files with 63 additions and 0 deletions

View File

@@ -35,6 +35,7 @@
<h4 id="title">Select features example</h4> <h4 id="title">Select features example</h4>
<p id="shortdesc">Example of using the Select interaction. Choose between <code>Single-click</code>, <code>Click</code> and <code>Hover</code> as the event type for selection in the combobox below. When using <code>Single-click</code> or <code>Click</code> you can hold do <code>Shift</code> key to toggle the feature in the selection.</p> <p id="shortdesc">Example of using the Select interaction. Choose between <code>Single-click</code>, <code>Click</code> and <code>Hover</code> as the event type for selection in the combobox below. When using <code>Single-click</code> or <code>Click</code> you can hold do <code>Shift</code> key to toggle the feature in the selection.</p>
<p>Note: when <code>Single-click</code> is used double-clicks won't select features. This in contrast to <code>Click</code>, where a double-click will both select the feature and zoom the map (because of the <code>DoubleClickZoom</code> interaction). Note that <code>Single-click</code> is less responsive than <code>Click</code> because of the delay it uses to detect double-clicks.</p> <p>Note: when <code>Single-click</code> is used double-clicks won't select features. This in contrast to <code>Click</code>, where a double-click will both select the feature and zoom the map (because of the <code>DoubleClickZoom</code> interaction). Note that <code>Single-click</code> is less responsive than <code>Click</code> because of the delay it uses to detect double-clicks.</p>
<p>In this example, a listener is registered for the Select interaction's <code>select</code> event in order to update the selection status below.
<form class="form-inline"> <form class="form-inline">
<label>Action type &nbsp;</label> <label>Action type &nbsp;</label>
<select id="type"> <select id="type">
@@ -43,6 +44,7 @@
<option value="click">Click</option> <option value="click">Click</option>
<option value="pointermove">Hover</option> <option value="pointermove">Hover</option>
</select> </select>
<span id="status">&nbsp;0 selected features</span>
</form> </form>
<div id="docs"> <div id="docs">
<p>See the <a href="select-features.js" target="_blank">select-features.js source</a> to see how this is done.</p> <p>See the <a href="select-features.js" target="_blank">select-features.js source</a> to see how this is done.</p>

View File

@@ -61,6 +61,11 @@ var changeInteraction = function() {
} }
if (select !== null) { if (select !== null) {
map.addInteraction(select); map.addInteraction(select);
select.on('select', function(e) {
$('#status').html('&nbsp;' + e.target.getFeatures().getLength() +
' selected features (last operation selected ' + e.selected.length +
' and deselected ' + e.deselected.length + ' features)');
});
} }
}; };

View File

@@ -3,6 +3,7 @@ goog.provide('ol.interaction.Select');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.functions'); goog.require('goog.functions');
goog.require('ol.CollectionEventType'); goog.require('ol.CollectionEventType');
goog.require('ol.Feature'); goog.require('ol.Feature');
@@ -13,6 +14,51 @@ goog.require('ol.interaction.Interaction');
goog.require('ol.style.Style'); goog.require('ol.style.Style');
/**
* @enum {string}
*/
ol.SelectEventType = {
/**
* Triggered when feature(s) has been (de)selected.
* @event ol.SelectEvent#select
* @api
*/
SELECT: 'select'
};
/**
* @classdesc
* Events emitted by {@link ol.interaction.Select} instances are instances of
* this type.
*
* @param {string} type The event type.
* @param {Array.<ol.Feature>} selected Selected features.
* @param {Array.<ol.Feature>} deselected Deselected features.
* @extends {goog.events.Event}
* @constructor
*/
ol.SelectEvent = function(type, selected, deselected) {
goog.base(this, type);
/**
* Selected features array.
* @type {Array.<ol.Feature>}
* @api
*/
this.selected = selected;
/**
* Deselected features array.
* @type {Array.<ol.Feature>}
* @api
*/
this.deselected = deselected;
};
goog.inherits(ol.SelectEvent, goog.events.Event);
/** /**
* @classdesc * @classdesc
@@ -140,6 +186,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
var features = this.featureOverlay_.getFeatures(); var features = this.featureOverlay_.getFeatures();
var /** @type {Array.<ol.Feature>} */ deselected = []; var /** @type {Array.<ol.Feature>} */ deselected = [];
var /** @type {Array.<ol.Feature>} */ selected = []; var /** @type {Array.<ol.Feature>} */ selected = [];
var change = false;
if (set) { if (set) {
// Replace the currently selected feature(s) with the feature(s) at the // Replace the currently selected feature(s) with the feature(s) at the
// pixel, or clear the selected feature(s) if there is no feature at // pixel, or clear the selected feature(s) if there is no feature at
@@ -156,7 +203,9 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
features.item(0) == selected[0]) { features.item(0) == selected[0]) {
// No change // No change
} else { } else {
change = true;
if (features.getLength() !== 0) { if (features.getLength() !== 0) {
deselected = Array.prototype.concat(features.getArray());
features.clear(); features.clear();
} }
if (this.multi_) { if (this.multi_) {
@@ -189,6 +238,13 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
features.remove(deselected[i]); features.remove(deselected[i]);
} }
features.extend(selected); features.extend(selected);
if (selected.length > 0 || deselected.length > 0) {
change = true;
}
}
if (change) {
this.dispatchEvent(
new ol.SelectEvent(ol.SelectEventType.SELECT, selected, deselected));
} }
return ol.events.condition.pointerMove(mapBrowserEvent); return ol.events.condition.pointerMove(mapBrowserEvent);
}; };