Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi fields relations #159

Merged
merged 46 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6e8a7d1
added management of relation with multifields
volterra79 Sep 10, 2021
d62534b
Merge branch 'dev' into relations_multi_fields
volterra79 Aug 8, 2022
7078224
Merge branch 'dev' into relations_multi_fields
volterra79 Aug 29, 2022
8285acd
add file description
Raruto Jan 12, 2023
657b51e
improve documentation of `relations.js`
Raruto Jan 12, 2023
ad5b14f
improve documentation of `relation.js`
Raruto Jan 12, 2023
9d95cd6
mispelling
Raruto Jan 13, 2023
425b33c
Merge branch 'dev' into relations_multi_fields
volterra79 Aug 30, 2023
972071f
Merge branch 'dev' into relations_multi_fields
volterra79 Sep 11, 2023
23b1366
:bulb: Add comments and clean code
volterra79 Sep 11, 2023
80d1ec2
:recycle: Take in account fatherFields and childFields as Array
volterra79 Sep 13, 2023
0338af7
:recycle: Add comments and clean code
volterra79 Sep 13, 2023
a17917c
:sparkles: when call applyCommitResponse after commit, need to check …
volterra79 Sep 13, 2023
9d82768
:recycle: Check and transform referencedField and referencingField re…
volterra79 Sep 14, 2023
c416de4
Merge branch 'dev' into relations_multi_fields
volterra79 Sep 18, 2023
9a6da5d
refactor function: `setFieldValueToRelationField`
Raruto Sep 20, 2023
ce119fb
refactor function: `applyCommitResponse`
Raruto Sep 20, 2023
3d4ff6f
refactor function: `commit`
Raruto Sep 20, 2023
d29fb00
refactor function: `commit`
Raruto Sep 20, 2023
46f8c43
:bug: get response attribute from response
volterra79 Sep 20, 2023
f31fb74
Merge branch 'relations_multi_fields' of https://github.com/g3w-suite…
volterra79 Sep 20, 2023
5dd7ba1
:bug: get response attribute from response
volterra79 Sep 20, 2023
5e70e87
refactor class: `Relation`
Raruto Sep 21, 2023
2e8cd84
refactor class: `Relations`
Raruto Sep 21, 2023
6fabf66
refactor class constructor: `TableService`
Raruto Sep 21, 2023
b939c47
:bug: Change response condition to reject
volterra79 Sep 21, 2023
da7d380
refactor function: `addRemoveSelectedFeature`
Raruto Sep 21, 2023
06d1a4d
refactor function: `switchSelection`
Raruto Sep 21, 2023
e6ba13b
:bulb: comment
volterra79 Sep 21, 2023
6b0a44d
refactor function: `selectAllFeatures`
Raruto Sep 21, 2023
40329c1
refactor function: `getData`
Raruto Sep 21, 2023
0958e94
global replace: `void 0` --> `undefined`
Raruto Sep 21, 2023
109d90b
refactor function: `zoomAndHighLightGeometryRelationFeatures`
Raruto Sep 21, 2023
e73af82
Update editor.js
Raruto Sep 21, 2023
02d34ac
:bulb: Comment anc readable code
volterra79 Sep 21, 2023
1840ecc
undefined variable
Raruto Sep 21, 2023
1cdbc6a
:bug: _canDoGetFeaturesRequest fix wrong returned value.
volterra79 Sep 21, 2023
3736a72
code format
Raruto Sep 22, 2023
e8325f5
Disable also show/hide sidebar button
volterra79 Sep 22, 2023
861f4cb
spacing
Raruto Sep 22, 2023
d1d1b45
comments
Raruto Sep 22, 2023
1fb4b7e
comments
Raruto Sep 22, 2023
0c46fdb
remove log
Raruto Sep 22, 2023
c747148
comments
Raruto Sep 22, 2023
9d8fe18
comments
Raruto Sep 22, 2023
fad4bfe
comments
Raruto Sep 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
469 changes: 308 additions & 161 deletions src/app/core/editing/editor.js

Large diffs are not rendered by default.

90 changes: 61 additions & 29 deletions src/app/core/editing/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,38 +379,70 @@ proto.set3DGeometryType = function({layerId=this.getId(), commitItems}={}){
}));
};

proto.commit = function({ids=null, items, relations=true}={}) {
/**
* Commit changes on server (save)
*
* @param opts.ids
* @param opts.items
* @param opts.relations
*
* @returns {*}
*/
proto.commit = function({
ids = null,
items,
relations = true
} = {}) {

const d = $.Deferred();
let commitItems;

let commit; // committed items

// skip when ..
if (ids) {
commitItems = this._history.commit(ids);
commit = this._history.commit(ids);
this._history.clear(ids);
} else {
if (items) commitItems = items;
else {
commitItems = this._history.commit();
commitItems = this._serializeCommit(commitItems);
}
if (!relations) commitItems.relations = {};
this._editor.commit(commitItems)
.then(response => {
if (response && response.result) {
const {response:data} = response;
const {new_relations={}} = data;
for (const id in new_relations) {
const session = SessionsRegistry.getSession(id);
session.getEditor().applyCommitResponse({
response: new_relations[id],
result: true
})
}
this._history.clear();
this.saveChangesOnServer(commitItems);
d.resolve(commitItems, response)
} else d.reject(response);
})
.fail(err => d.reject(err));
return d.promise();
}

commit = items || this._serializeCommit(this._history.commit());

if (!relations) {
commit.relations = {};
}

this._editor
.commit(commit)
.then(response => {

// skip when response is null or undefined and response.result is false
if (!(response && response.result)) {
d.reject(response);
return;
}

const { new_relations = {} } = response.response; // check if new relations are saved on server

// sync server data with local data
for (const id in new_relations) {
SessionsRegistry
.getSession(id) // get session of relation by id
.getEditor()
.applyCommitResponse({ // apply commit response to current editing relation layer
response: new_relations[id],
result: true
});
}

this._history.clear(); // clear history

this.saveChangesOnServer(commit); // dispatch setter event.

d.resolve(commit, response);

})
.fail(err => d.reject(err));

return d.promise();

};
Expand Down Expand Up @@ -442,7 +474,7 @@ proto.clear = function() {
this._editor.clear();
};

//return l'history
//return history
proto.getHistory = function() {
return this._history;
};
Expand Down
23 changes: 17 additions & 6 deletions src/app/core/layers/tablelayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,26 @@ proto._setOtherConfigParameters = function(config) {
// overwrite by vector layer
};

// return layer fields
proto.getEditingFields = function(editable=false) {
let fields = this.config.editing.fields.length ? this.config.editing.fields: this.config.fields;
if (editable) fields = fields.filter(field => field.editable);
/**
* @returns layer fields
*/
proto.getEditingFields = function(editable = false) {
let fields = this.config.editing.fields.length
? this.config.editing.fields
: this.config.fields;
if (editable) {
fields = fields.filter(field => field.editable);
}
return fields;
};

proto.isPkField = function(field){
const find_field = this.getEditingFields().find(_field => _field.name === field);
/**
* @param field
*
* @returns {boolean} whether field is a Primary Key
*/
proto.isPkField = function(field) {
const find_field = this.getEditingFields().find(f => f.name === field);
return find_field && find_field.pk;
};

Expand Down
115 changes: 94 additions & 21 deletions src/app/core/relations/relation.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,171 @@
const { base, inherit } = require('core/utils/utils');
const G3WObject = require('core/g3wobject');

function Relation(config={}) {
const uniqueSuffix = Date.now();
const id = config.id || `id_${uniqueSuffix}`;
const name = config.name || `name_${uniqueSuffix}`;
const origname = config.origname || `origname_${uniqueSuffix}`;
/**
* Relation Class
*
* @param config
*
* @constructor
*/
function Relation(config = {}) {

const suffix = Date.now();

/** BACKCOMP (g3w-admin < v.3.7.0) */
const multi_fields = [].concat(config.fieldRef.referencedField);

this.state = {
id,
name,
origname,
father: config.referencedLayer,
child: config.referencingLayer,
fatherField: config.fieldRef.referencedField,
childField: config.fieldRef.referencingField,
type: config.type,
loading: false
id: config.id || `id_${suffix}`,
name: config.name || `name_${suffix}`,
origname: config.origname || `origname_${suffix}`,
father: config.referencedLayer,
child: config.referencingLayer,
fatherField: multi_fields,
childField: multi_fields,
type: config.type,
loading: false
};

base(this);
}

inherit(Relation, G3WObject);

const proto = Relation.prototype;

/**
* Get relation id
*
* @returns {string}
*/
proto.getId = function() {
return this.state.id;
};

/**
* Set Relation id
*
* @param id
*/
proto.setId = function(id) {
this.state.id = id;
};

/**
* Get Relation name
*
* @returns {string}
*/
proto.getName = function() {
return this.state.name;
};

/**
* Set Relation name
*
* @param name
*/
proto.setName = function(name) {
this.state.name = name;
};

/**
* @TODO check if deprecated (ie. `this.state.title` is not defined in class constructor)
*
* Get Relation title
*
* @returns { undefined }
*/
proto.getTitle = function() {
return this.state.title;
volterra79 marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* @TODO check if deprecated (ie. `this.state.title` is not defined in class constructor)
*
* Set Relation title
*
* @param title
*
* @returns { undefined }
*/
proto.setTitle = function(title) {
return this.state.title = title;
volterra79 marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* @returns { string[] } layerId of child relation
*/
proto.getChild = function() {
return this.state.child;
};

/**
* @returns { string[] } layerId of father relation
*/
proto.getFather = function() {
return this.state.father;
};

/**
* @returns state Object of relation
*/
proto.getState = function() {
return this.state;
};

/**
* @returns { 'MANY' | ONE' | string } relation type
*/
proto.getType = function() {
return this.state.type;
};

/**
* @returns {{ father, child }} relation fields
*/
proto.getFields = function() {
return {
father: this.state.fatherField,
child: this.state.childField
};
};

/**
* Return father relation field name
*
* @returns {*}
*/
proto.getFatherField = function() {
return this.state.fatherField;
};

/**
* Return relation child layer field name
*
* @returns {*}
*/
proto.getChildField = function() {
return this.state.childField;
};

/**
* For editing purpose
* Set Loading state relation (for editing purpose)
*
* @param bool
*/

proto.setLoading = function(bool=false){
this.state.loading = bool;
};

/**
* Check Loading state Relation (for editing purpose)
*
* @returns { boolean }
*/
proto.isLoading = function(){
return this.state.loading;
};

/**
* End editing loading purpose
*/

module.exports = Relation;
module.exports = Relation;
Loading