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:
+102
-73
@@ -1,18 +1,17 @@
|
||||
/**
|
||||
* @module ol/interaction/Select
|
||||
*/
|
||||
import {getUid} from '../util.js';
|
||||
import Collection from '../Collection.js';
|
||||
import CollectionEventType from '../CollectionEventType.js';
|
||||
import {extend, includes} from '../array.js';
|
||||
import Event from '../events/Event.js';
|
||||
import {singleClick, never, shiftKeyOnly} from '../events/condition.js';
|
||||
import {TRUE} from '../functions.js';
|
||||
import GeometryType from '../geom/GeometryType.js';
|
||||
import Interaction from './Interaction.js';
|
||||
import {TRUE} from '../functions.js';
|
||||
import {clear} from '../obj.js';
|
||||
import {createEditingStyle} from '../style/Style.js';
|
||||
import Collection from '../Collection.js';
|
||||
|
||||
import {extend, includes} from '../array.js';
|
||||
import {getUid} from '../util.js';
|
||||
import {never, shiftKeyOnly, singleClick} from '../events/condition.js';
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
@@ -23,10 +22,9 @@ const SelectEventType = {
|
||||
* @event SelectEvent#select
|
||||
* @api
|
||||
*/
|
||||
SELECT: 'select'
|
||||
SELECT: 'select',
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A function that takes an {@link module:ol/Feature} or
|
||||
* {@link module:ol/render/Feature} and an
|
||||
@@ -35,7 +33,6 @@ const SelectEventType = {
|
||||
* @typedef {function(import("../Feature.js").FeatureLike, import("../layer/Layer.js").default):boolean} FilterFunction
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {import("../events/condition.js").Condition} [addCondition] A function
|
||||
@@ -91,7 +88,6 @@ const SelectEventType = {
|
||||
* the radius around the given position will be checked for features.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Events emitted by {@link module:ol/interaction/Select~Select} instances are instances of
|
||||
@@ -128,9 +124,7 @@ class SelectEvent extends Event {
|
||||
* @api
|
||||
*/
|
||||
this.mapBrowserEvent = mapBrowserEvent;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +133,6 @@ class SelectEvent extends Event {
|
||||
*/
|
||||
const originalFeatureStyles = {};
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Interaction for selecting vector features. By default, selected features are
|
||||
@@ -160,9 +153,8 @@ class Select extends Interaction {
|
||||
* @param {Options=} opt_options Options.
|
||||
*/
|
||||
constructor(opt_options) {
|
||||
|
||||
super({
|
||||
handleEvent: handleEvent
|
||||
handleEvent: handleEvent,
|
||||
});
|
||||
|
||||
const options = opt_options ? opt_options : {};
|
||||
@@ -193,13 +185,17 @@ class Select extends Interaction {
|
||||
* @private
|
||||
* @type {import("../events/condition.js").Condition}
|
||||
*/
|
||||
this.removeCondition_ = options.removeCondition ? options.removeCondition : never;
|
||||
this.removeCondition_ = options.removeCondition
|
||||
? options.removeCondition
|
||||
: never;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {import("../events/condition.js").Condition}
|
||||
*/
|
||||
this.toggleCondition_ = options.toggleCondition ? options.toggleCondition : shiftKeyOnly;
|
||||
this.toggleCondition_ = options.toggleCondition
|
||||
? options.toggleCondition
|
||||
: shiftKeyOnly;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -223,7 +219,8 @@ class Select extends Interaction {
|
||||
* @private
|
||||
* @type {import("../style/Style.js").default|Array.<import("../style/Style.js").default>|import("../style/Style.js").StyleFunction|null}
|
||||
*/
|
||||
this.style_ = options.style !== undefined ? options.style : getDefaultStyleFunction();
|
||||
this.style_ =
|
||||
options.style !== undefined ? options.style : getDefaultStyleFunction();
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -238,7 +235,7 @@ class Select extends Interaction {
|
||||
layerFilter = options.layers;
|
||||
} else {
|
||||
const layers = options.layers;
|
||||
layerFilter = function(layer) {
|
||||
layerFilter = function (layer) {
|
||||
return includes(layers, layer);
|
||||
};
|
||||
}
|
||||
@@ -298,9 +295,8 @@ class Select extends Interaction {
|
||||
* @api
|
||||
*/
|
||||
getLayer(feature) {
|
||||
return (
|
||||
/** @type {import('../layer/Vector.js').default} */ (this.featureLayerAssociation_[getUid(feature)])
|
||||
);
|
||||
return /** @type {import('../layer/Vector.js').default} */ (this
|
||||
.featureLayerAssociation_[getUid(feature)]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -326,15 +322,27 @@ class Select extends Interaction {
|
||||
}
|
||||
super.setMap(map);
|
||||
if (map) {
|
||||
this.features_.addEventListener(CollectionEventType.ADD, this.boundAddFeature_);
|
||||
this.features_.addEventListener(CollectionEventType.REMOVE, this.boundRemoveFeature_);
|
||||
this.features_.addEventListener(
|
||||
CollectionEventType.ADD,
|
||||
this.boundAddFeature_
|
||||
);
|
||||
this.features_.addEventListener(
|
||||
CollectionEventType.REMOVE,
|
||||
this.boundRemoveFeature_
|
||||
);
|
||||
|
||||
if (this.style_) {
|
||||
this.features_.forEach(this.applySelectedStyle_.bind(this));
|
||||
}
|
||||
} else {
|
||||
this.features_.removeEventListener(CollectionEventType.ADD, this.boundAddFeature_);
|
||||
this.features_.removeEventListener(CollectionEventType.REMOVE, this.boundRemoveFeature_);
|
||||
this.features_.removeEventListener(
|
||||
CollectionEventType.ADD,
|
||||
this.boundAddFeature_
|
||||
);
|
||||
this.features_.removeEventListener(
|
||||
CollectionEventType.REMOVE,
|
||||
this.boundRemoveFeature_
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,11 +393,20 @@ class Select extends Interaction {
|
||||
*/
|
||||
restorePreviousStyle_(feature) {
|
||||
const key = getUid(feature);
|
||||
const selectInteractions = /** @type {Array<Select>} */ (this.getMap().getInteractions().getArray().filter(function(interaction) {
|
||||
return interaction instanceof Select && interaction.getStyle() && interaction.getFeatures().getArray().indexOf(feature) !== -1;
|
||||
}));
|
||||
const selectInteractions = /** @type {Array<Select>} */ (this.getMap()
|
||||
.getInteractions()
|
||||
.getArray()
|
||||
.filter(function (interaction) {
|
||||
return (
|
||||
interaction instanceof Select &&
|
||||
interaction.getStyle() &&
|
||||
interaction.getFeatures().getArray().indexOf(feature) !== -1
|
||||
);
|
||||
}));
|
||||
if (selectInteractions.length > 0) {
|
||||
feature.setStyle(selectInteractions[selectInteractions.length - 1].getStyle());
|
||||
feature.setStyle(
|
||||
selectInteractions[selectInteractions.length - 1].getStyle()
|
||||
);
|
||||
} else {
|
||||
feature.setStyle(originalFeatureStyles[key]);
|
||||
delete originalFeatureStyles[key];
|
||||
@@ -405,7 +422,6 @@ class Select extends Interaction {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles the {@link module:ol/MapBrowserEvent map browser event} and may change the
|
||||
* selected state of features.
|
||||
@@ -430,23 +446,25 @@ function handleEvent(mapBrowserEvent) {
|
||||
// pixel, or clear the selected feature(s) if there is no feature at
|
||||
// the pixel.
|
||||
clear(this.featureLayerAssociation_);
|
||||
map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
|
||||
(
|
||||
/**
|
||||
* @param {import("../Feature.js").FeatureLike} feature Feature.
|
||||
* @param {import("../layer/Layer.js").default} layer Layer.
|
||||
* @return {boolean|undefined} Continue to iterate over the features.
|
||||
*/
|
||||
function(feature, layer) {
|
||||
if (this.filter_(feature, layer)) {
|
||||
selected.push(feature);
|
||||
this.addFeatureLayerAssociation_(feature, layer);
|
||||
return !this.multi_;
|
||||
}
|
||||
}).bind(this), {
|
||||
map.forEachFeatureAtPixel(
|
||||
mapBrowserEvent.pixel,
|
||||
/**
|
||||
* @param {import("../Feature.js").FeatureLike} feature Feature.
|
||||
* @param {import("../layer/Layer.js").default} layer Layer.
|
||||
* @return {boolean|undefined} Continue to iterate over the features.
|
||||
*/
|
||||
function (feature, layer) {
|
||||
if (this.filter_(feature, layer)) {
|
||||
selected.push(feature);
|
||||
this.addFeatureLayerAssociation_(feature, layer);
|
||||
return !this.multi_;
|
||||
}
|
||||
}.bind(this),
|
||||
{
|
||||
layerFilter: this.layerFilter_,
|
||||
hitTolerance: this.hitTolerance_
|
||||
});
|
||||
hitTolerance: this.hitTolerance_,
|
||||
}
|
||||
);
|
||||
for (let i = features.getLength() - 1; i >= 0; --i) {
|
||||
const feature = features.item(i);
|
||||
const index = selected.indexOf(feature);
|
||||
@@ -463,28 +481,33 @@ function handleEvent(mapBrowserEvent) {
|
||||
}
|
||||
} else {
|
||||
// Modify the currently selected feature(s).
|
||||
map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
|
||||
(
|
||||
/**
|
||||
* @param {import("../Feature.js").FeatureLike} feature Feature.
|
||||
* @param {import("../layer/Layer.js").default} layer Layer.
|
||||
* @return {boolean|undefined} Continue to iterate over the features.
|
||||
*/
|
||||
function(feature, layer) {
|
||||
if (this.filter_(feature, layer)) {
|
||||
if ((add || toggle) && !includes(features.getArray(), feature)) {
|
||||
selected.push(feature);
|
||||
this.addFeatureLayerAssociation_(feature, layer);
|
||||
} else if ((remove || toggle) && includes(features.getArray(), feature)) {
|
||||
deselected.push(feature);
|
||||
this.removeFeatureLayerAssociation_(feature);
|
||||
}
|
||||
return !this.multi_;
|
||||
map.forEachFeatureAtPixel(
|
||||
mapBrowserEvent.pixel,
|
||||
/**
|
||||
* @param {import("../Feature.js").FeatureLike} feature Feature.
|
||||
* @param {import("../layer/Layer.js").default} layer Layer.
|
||||
* @return {boolean|undefined} Continue to iterate over the features.
|
||||
*/
|
||||
function (feature, layer) {
|
||||
if (this.filter_(feature, layer)) {
|
||||
if ((add || toggle) && !includes(features.getArray(), feature)) {
|
||||
selected.push(feature);
|
||||
this.addFeatureLayerAssociation_(feature, layer);
|
||||
} else if (
|
||||
(remove || toggle) &&
|
||||
includes(features.getArray(), feature)
|
||||
) {
|
||||
deselected.push(feature);
|
||||
this.removeFeatureLayerAssociation_(feature);
|
||||
}
|
||||
}).bind(this), {
|
||||
return !this.multi_;
|
||||
}
|
||||
}.bind(this),
|
||||
{
|
||||
layerFilter: this.layerFilter_,
|
||||
hitTolerance: this.hitTolerance_
|
||||
});
|
||||
hitTolerance: this.hitTolerance_,
|
||||
}
|
||||
);
|
||||
for (let j = deselected.length - 1; j >= 0; --j) {
|
||||
features.remove(deselected[j]);
|
||||
}
|
||||
@@ -492,22 +515,29 @@ function handleEvent(mapBrowserEvent) {
|
||||
}
|
||||
if (selected.length > 0 || deselected.length > 0) {
|
||||
this.dispatchEvent(
|
||||
new SelectEvent(SelectEventType.SELECT,
|
||||
selected, deselected, mapBrowserEvent));
|
||||
new SelectEvent(
|
||||
SelectEventType.SELECT,
|
||||
selected,
|
||||
deselected,
|
||||
mapBrowserEvent
|
||||
)
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return {import("../style/Style.js").StyleFunction} Styles.
|
||||
*/
|
||||
function getDefaultStyleFunction() {
|
||||
const styles = createEditingStyle();
|
||||
extend(styles[GeometryType.POLYGON], styles[GeometryType.LINE_STRING]);
|
||||
extend(styles[GeometryType.GEOMETRY_COLLECTION], styles[GeometryType.LINE_STRING]);
|
||||
extend(
|
||||
styles[GeometryType.GEOMETRY_COLLECTION],
|
||||
styles[GeometryType.LINE_STRING]
|
||||
);
|
||||
|
||||
return function(feature) {
|
||||
return function (feature) {
|
||||
if (!feature.getGeometry()) {
|
||||
return null;
|
||||
}
|
||||
@@ -515,5 +545,4 @@ function getDefaultStyleFunction() {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export default Select;
|
||||
|
||||
Reference in New Issue
Block a user