let ol = require('openlayers');
let ModifyResult = require('./modifyResult');
let AbstractInteraction = require('./abstract');
/**
* @class Modify
*/
class Modify {
/**
* Конструктор ModifyInteraction
* @param params {object} - объект,
*/
constructor(params) {
let interaction = new ol.interaction.Select(params);
this.interaction = interaction;
this.type = "modify";
this.id = params.layerId;
this.layerId = params.layerId;
this.events = {};
this.stopInteractionKey = params.stopInteractionKey;
this.initEvents(params);
}
initEvents(params) {
this.interaction.setActive(true);
this.interaction.on("select", (data) => {
let selected = data.selected;
if (selected.length > 0) {
let feature = selected[0];
let collection = new ol.Collection();
collection.push(feature);
let helperInteraction = new ol.interaction.Modify({
features: collection
});
let helper = new AbstractInteraction({interaction: helperInteraction, id: Math.random()});
helper.on("modifyend", (data) => {
let features = data.features.getArray();
let feature;
if (features.length > 0) feature = features[0];
let actionResult = new ModifyResult({
features: features,
feature: feature,
target: data.target
});
if (params.func) params.func(actionResult);
});
this.emit("change-interactor", helper);
}
});
}
/**
* Подписка на события
* @param eventName
* @param listener
*/
on(eventName, listener) {
let self = this;
let eventStore = self.events;
eventStore[eventName] = listener;
}
/**
* Выключение подписки на событие
* @param eventName
*/
off(eventName) {
let self = this;
let eventStore = self.events;
if (eventStore[eventName]) delete eventStore[eventName];
}
/**
* Инициация события с контекстом
* @param eventName
* @param context
*/
emit(eventName, context) {
let self = this;
let eventStore = self.events;
let listener = eventStore[eventName];
if (listener && typeof listener === "function") {
listener(context);
}
}
}
module.exports = Modify;