From 46ece49ae2dcd65e49bd2e1d3f9948594fb1f569 Mon Sep 17 00:00:00 2001 From: Stepan Kuzmin Date: Tue, 30 Jul 2024 18:38:11 +0300 Subject: [PATCH] Draw#setFeatureProperty fires draw.update event --- src/api.js | 2 +- src/constants.js | 1 + src/store.js | 11 ++++++-- test/interaction_events.test.js | 47 +++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/api.js b/src/api.js index aa1bd3fcd..4936b9eb4 100644 --- a/src/api.js +++ b/src/api.js @@ -192,7 +192,7 @@ export default function(ctx, api) { }; api.setFeatureProperty = function(featureId, property, value) { - ctx.store.setFeatureProperty(featureId, property, value); + ctx.store.setFeatureProperty(featureId, property, value, { silent }); return api; }; diff --git a/src/constants.js b/src/constants.js index 655d16184..581e0e57f 100644 --- a/src/constants.js +++ b/src/constants.js @@ -68,6 +68,7 @@ export const events = { export const updateActions = { MOVE: 'move', + CHANGE_PROPERTIES: 'change_properties', CHANGE_COORDINATES: 'change_coordinates' }; diff --git a/src/store.js b/src/store.js index d32a41c3d..e87e8250e 100644 --- a/src/store.js +++ b/src/store.js @@ -128,7 +128,7 @@ Store.prototype.add = function(feature, options = {}) { this._featureIds.add(feature.id); if (options.silent != null && options.silent === false) { - this.ctx.map.fire(Constants.events.CREATE, { + this.ctx.events.fire(Constants.events.CREATE, { features: [this._features[feature.id].toGeoJSON()] }); } @@ -322,9 +322,16 @@ Store.prototype.isSelected = function(featureId) { * @param {string} property property * @param {string} property value */ -Store.prototype.setFeatureProperty = function(featureId, property, value) { +Store.prototype.setFeatureProperty = function(featureId, property, value, options) { this.get(featureId).setProperty(property, value); this.featureChanged(featureId); + + if (options.silent != null && options.silent === false) { + this.ctx.events.fire(Constants.events.UPDATE, { + action: Constants.updateActions.CHANGE_PROPERTIES, + features: [this.get(featureId).toGeoJSON()] + }); + } }; function refreshSelectedCoordinates(store, options) { diff --git a/test/interaction_events.test.js b/test/interaction_events.test.js index 0389dc2df..a14271fd5 100644 --- a/test/interaction_events.test.js +++ b/test/interaction_events.test.js @@ -1017,6 +1017,10 @@ test('ensure API fire right events', { only: true }, async (t) => { } }; + t.afterEach(() => { + fireSpy.resetHistory(); + }); + await t.test('Draw#add fires draw.create event', async () => { Draw.add(point); assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.create'); @@ -1039,6 +1043,49 @@ test('ensure API fire right events', { only: true }, async (t) => { assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [line]}); }); + await t.test('Draw#set fires draw.create event', async () => { + const collection = { + type: 'FeatureCollection', + features: [point, line] + }; + + Draw.set(collection); + + assert.strictEqual(fireSpy.callCount, 2, 'fires draw.create event for each feature'); + + assert.strictEqual(fireSpy.firstCall.firstArg, 'draw.create'); + assert.deepStrictEqual(fireSpy.firstCall.lastArg, {features: [point]}); + + assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.create'); + assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [line]}); + }); + + await t.test('Draw#set fires draw.delete event', async () => { + const collection = { + type: 'FeatureCollection', + features: [line] + }; + + Draw.set(collection); + + assert.strictEqual(fireSpy.callCount, 1, 'fires draw.delete event for deleted feature'); + + assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.delete'); + assert.deepStrictEqual(fireSpy.lastCall.lastArg, {features: [point]}); + }); + + await t.test('Draw#setFeatureProperty fires draw.update event', () => { + Draw.add(point); + + Draw.setFeatureProperty(point.id, 'price', 200); + + assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.update'); + assert.deepStrictEqual(fireSpy.lastCall.lastArg, { + action: 'change_properties', + features: [{...point, properties: {price: 200}}] + }); + }); + await t.test('Draw#changeMode fires draw.modechange event', async () => { Draw.changeMode('draw_point'); assert.strictEqual(fireSpy.lastCall.firstArg, 'draw.modechange', 'Draw.changeMode triggers draw.modechange event');