Skip to content

Commit

Permalink
🐛 Fix show relations of new father feature (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
volterra79 committed Apr 2, 2024
1 parent af91586 commit 079bd38
Showing 1 changed file with 71 additions and 11 deletions.
82 changes: 71 additions & 11 deletions services/editingservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,14 +1309,11 @@ proto.getRelationsAttributesByFeature = function({
relation,
feature,
} = {}) {

const layer = this.getToolBoxById(layerId).getLayer();
const relations = this.getRelationsByFeature({layerId, relation, feature});
const relations = this.getRelationsByFeature({ layerId, relation, feature });
return relations
.map(relation => ({
fields: layer.getFieldsWithValues(relation, {
relation: true
}),
fields: layer.getFieldsWithValues(relation, { relation: true }),
id: relation.getId()
}))

Expand Down Expand Up @@ -1358,10 +1355,18 @@ proto.getRelationsByFeature = function({
relation,
feature,
} = {}) {
//get father layer
const fatherLayer = this.getLayerById(relation.getFather ? relation.getFather() : relation.father);
const { ownField, relationField } = this._getRelationFieldsFromRelation({ layerId, relation });
// get features of relation child layers
// Loop relation fields
const values = relationField.map(field => feature.get(field));
const values = relationField.map(field => {
//In case of new feature, need to check if field is pk field
if (feature.isNew() && fatherLayer.isPkField(field)) {
return feature.getId();
}
return feature.get(field)
});
return this
._getFeaturesByLayerId(layerId)
.filter(feature => ownField.every((field, i) => feature.get(field) == values[i]));
Expand Down Expand Up @@ -1556,6 +1561,7 @@ proto.getRelationsInEditing = function({
let relationinediting;
relations.forEach(relation => {
const relationLayerId = this._getRelationLayerId({layerId, relation});
//check if the layer is editable
if (this.getLayerById(relationLayerId)) {
relationinediting = {
relation: relation.getState(),
Expand Down Expand Up @@ -2028,10 +2034,64 @@ proto.showCommitModalWindow = function({
closeButton: false
});
resolve(messages);
commitPromise.always(()=>dialog.modal('hide')) // hide saving dialog
commitPromise.always(() => dialog.modal('hide')) // hide saving dialog
})
.fail(error => reject(error))
.always(()=> workflow.stop())
.fail(async error => {
const promises = [];
const rollbackRelations = (relations={}) => {
Object
.entries(relations)
.forEach(([ layerId, {add, delete:del, update, relations = {}}]) => {
const layer = this.getLayerById(layerId);
const sourceLayer = layer.getEditingSource();
// check if the relation layer has some features
if (sourceLayer.readFeatures().length > 0) {

//add a need to remove
add.forEach(({id}) => {
sourceLayer.removeFeature(sourceLayer.getFeatureById(id))
})

// //need to get original values
update.forEach(({ id }) => {
promises.push(
new Promise((resolve, reject) => {
this.getProjectLayerFeatureById({ layerId, fid: id })
.then(f => {
const feature = sourceLayer.getFeatureById(id);
feature.setProperties(f.properties);
feature.setGeometry(f.geometry);
resolve();
}).catch(reject);
})
)
})
}

//need to add again.
del.forEach(id => {
promises.push(
new Promise((resolve, reject) => {
this.getProjectLayerFeatureById({ layerId, fid: id })
.then(f => {
const feature = new ol.Feature({ geometry: f.geometry })
feature.setProperties(f.properties);
feature.setId(id);
// need to add again to source because it is for relation layer is locked
sourceLayer.addFeature(new Feature({ feature }));
resolve();
}).catch(reject);
})
)
})
rollbackRelations(relations);
})
}
rollbackRelations(commitItems.relations);
await Promise.allSettled(promises);
reject(error)
})
.always(() => workflow.stop())
})
};

Expand Down Expand Up @@ -2298,7 +2358,8 @@ proto.commit = function({
})
}
})
.catch(() => {
.catch((e) => {
console.warn(e);
d.reject(toolbox)
});

Expand Down Expand Up @@ -2682,7 +2743,6 @@ proto.getProjectLayerFeatureById = async function({
} catch(e) {
console.warn(e);
}

return feature;
};

Expand Down

0 comments on commit 079bd38

Please sign in to comment.