-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c946e99
commit 0a66d56
Showing
5 changed files
with
57 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { Workflow } from '../g3wsdk/workflow/workflow'; | ||
|
||
const {sortAlphabeticallyArray, sortNumericArray} = g3wsdk.core.utils; | ||
/** | ||
* ORIGINAL SOURCE: g3w-client-plugin-editing/workflows/tasks/[email protected] | ||
* | ||
|
@@ -18,14 +17,11 @@ export function getFormFields({ | |
inputs, | ||
context, | ||
feature, //current feature | ||
isChild = false, //true -> relation form | ||
multi, // true -> multi features (e.g edit multi features attributes form) | ||
} = {}) { | ||
|
||
//editing service | ||
const service = g3wsdk.core.plugin.PluginsRegistry.getPlugin('editing'); | ||
// root layerId (in case of child edit relation) | ||
const relationLayerId = Workflow.Stack.getFirst().getInputs().layer.getId(); | ||
// current form layerId// unique values by feature field | ||
const layerId = inputs.layer.getId(); | ||
|
||
|
@@ -39,55 +35,27 @@ export function getFormFields({ | |
|
||
//Loop through fields | ||
const unique_values = fields | ||
//check if field is a unique widget type | ||
.filter(f => f.validate.unique && f.editable) | ||
//check if field is a unique field. Exclude pk not edittable | ||
.filter(f => !(f.pk && false === f.editable) && ('unique' === f.input.type || f.validate.unique)) | ||
.map(field => ({ | ||
field, // feature field | ||
_value: feature.get(field.name), // feature current field value | ||
})) | ||
|
||
//Loop through uniq value field | ||
//Loop through unique fields | ||
unique_values.forEach(({ _value, field }) => { | ||
// current editing feature adds to | ||
let current_values; | ||
//get relations | ||
const relations = isChild && ( | ||
service.state.uniqueFieldsValues[relationLayerId] | ||
&& service.state.uniqueFieldsValues[relationLayerId].__uniqueFieldsValuesRelations | ||
&& service.state.uniqueFieldsValues[relationLayerId].__uniqueFieldsValuesRelations[layerId] | ||
); | ||
|
||
//check if relation field has values | ||
const has_values = relations && (undefined !== (relations[layerId] || {})[field.name]); | ||
|
||
// child form --> belongs to relation (get child layer unique field values) | ||
if (isChild && has_values) { | ||
current_values = relations[layerId][field.name]; | ||
} | ||
//get current stored unique values for field | ||
const current_values = service.state.uniqueFieldsValues[layerId][field.name] || new Set([]); | ||
|
||
// root layer --> get current values of unique field | ||
if (!isChild || !has_values) { | ||
current_values = (service.state.uniqueFieldsValues[layerId] || {})[field.name] || []; | ||
//check if temporary value is not added to values | ||
// @since 3.9.0 | ||
if (!field.input.options.values.includes(field.value)) { | ||
field.input.options.values.push(field.value); | ||
} | ||
} | ||
//NEED TO ADD ALWAYS CURRENT VALUE | ||
field.input.options.values = (['integer', 'float', 'bigint'].includes(field.type) ? sortNumericArray: sortAlphabeticallyArray)(Array.from(current_values)); | ||
|
||
// convert "current" values to string (when not null or undefined) | ||
current_values.forEach(v => field.validate.exclude_values.add(![null, undefined].includes(v)? `${v}` : v ) ); | ||
|
||
// convert "inputs" values to string (when not null or undefined) | ||
inputs.features.forEach(f => { | ||
const value = f.get(field.name); | ||
if (![null, undefined].includes(value)) { | ||
field.validate.exclude_values.add(`${value}`); | ||
} | ||
}); | ||
|
||
// remove current value from exclude_values | ||
field.validate.exclude_values.delete(_value); | ||
field.validate.exclude_values.delete(`${_value}`); | ||
}); | ||
|
||
// skip when no fields are unique in multi features change form attribute | ||
|
@@ -100,53 +68,17 @@ export function getFormFields({ | |
unique_values.forEach(({ _value, field }) => { | ||
// initial value is the same that current field vale (no changed) | ||
if (_value === field.value) { return } | ||
//get | ||
const layer = isChild && service.state.uniqueFieldsValues[relationLayerId]; | ||
|
||
// relation form | ||
if (layer) { | ||
// change relation layer unique field values | ||
layer.__uniqueFieldsValuesRelations = layer.__uniqueFieldsValuesRelations || {}; | ||
layer.__uniqueFieldsValuesRelations[layerId] = layer.__uniqueFieldsValuesRelations[layerId] || {}; | ||
const values = new Set(layer.__uniqueFieldsValuesRelations[layerId][field.name]); | ||
values.delete(_value); | ||
values.add(field.value); | ||
layer.__uniqueFieldsValuesRelations[layerId][field.name] = values; | ||
} | ||
|
||
// root layer form (no relation) | ||
if (!layer && service.state.uniqueFieldsValues[layerId] && service.state.uniqueFieldsValues[layerId][field.name]) { | ||
// layer form | ||
if (service.state.uniqueFieldsValues[layerId][field.name]) { | ||
// change layer unique field values | ||
const values = service.state.uniqueFieldsValues[layerId][field.name]; | ||
//If changed, delete it from _value | ||
values.delete(_value); | ||
//aff new one to value list unique field | ||
values.add(field.value); | ||
} | ||
|
||
}); | ||
|
||
// Save temporary relation feature changes on father (root) layer feature | ||
const relations = false === isChild && ( | ||
service.state.uniqueFieldsValues[layerId] | ||
&& service.state.uniqueFieldsValues[layerId].__uniqueFieldsValuesRelations | ||
); | ||
|
||
// skip when no relation unique fields values is stored | ||
if (relations) { | ||
Object | ||
.keys(relations) | ||
.forEach(id => { | ||
Object | ||
.entries(relations[id]) | ||
.forEach(([name, values]) => { | ||
service.state.uniqueFieldsValues[id][name] = values; | ||
}) | ||
}); | ||
// clear temporary relations unique fields values | ||
if (service.state.uniqueFieldsValues[layerId]) { | ||
delete service.state.uniqueFieldsValues[layerId].__uniqueFieldsValuesRelations; | ||
} | ||
} | ||
|
||
return { once: true }; | ||
}; | ||
|
||
|
@@ -156,10 +88,6 @@ export function getFormFields({ | |
service.subscribe(`closeform_${layerId}`, () => { | ||
//unsubscribe event | ||
service.unsubscribe(`savedfeature_${layerId}`, savedfeatureFnc); | ||
// clear temporary relations unique fields values | ||
if (service.state.uniqueFieldsValues[layerId] && service.state.uniqueFieldsValues[layerId].__uniqueFieldsValuesRelations) { | ||
delete service.state.uniqueFieldsValues[layerId].__uniqueFieldsValuesRelations; | ||
} | ||
return { once: true }; | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ const { CatalogLayersStoresRegistry } = g3wsdk.core.catalog; | |
* ORIGINAL SOURCE: g3w-client-plugin-editing/services/[email protected] | ||
* Method to get unique values of unique input values from server | ||
* It's called | ||
* - When toolbox start | ||
* - Open a relation from OpenFormStep | ||
* - When toolbox start (parent layer and relation) | ||
* - After commit to server (to get fresh new data) | ||
* | ||
* @param { string } layerId | ||
* | ||
|
@@ -15,29 +15,26 @@ const { CatalogLayersStoresRegistry } = g3wsdk.core.catalog; | |
*/ | ||
export async function setLayerUniqueFieldValues(layerId) { | ||
const service = g3wsdk.core.plugin.PluginsRegistry.getPlugin('editing'); //get editing service | ||
|
||
const promises = []; | ||
const layer = CatalogLayersStoresRegistry.getLayerById(layerId); | ||
layer | ||
.getEditingFields() | ||
//filter field that is unique and not yet set unique values | ||
.filter(field => field.validate.unique && undefined === (service.state.uniqueFieldsValues[layerId] && service.state.uniqueFieldsValues[layerId][field.name])) | ||
.forEach((field => { | ||
promises.push(new Promise((resolve, reject) => { | ||
layer | ||
.getFilterData({ unique: field.name }) | ||
.then((values = []) => { | ||
//check if not yet create | ||
if (undefined === service.state.uniqueFieldsValues[layerId]) { | ||
service.state.uniqueFieldsValues[layerId] = {}; | ||
} | ||
//set unique values for the field | ||
service.state.uniqueFieldsValues[layerId][field.name] = new Set(values); | ||
resolve(); | ||
await new Promise((resolve, reject) => { | ||
const layer = g3wsdk.core.plugin.PluginsRegistry.getPlugin('editing').getLayerById(layerId); | ||
//get all values for unique field | ||
layer.getWidgetData({ | ||
type: 'unique', | ||
fields: Object.values(layer | ||
.getEditingFields() | ||
//filter field that is unique and not yet set unique values | ||
.filter(f => !(f.pk && false === f.editable) && ('unique' === f.input.type || f.validate.unique))) | ||
.map(f => f.name).join() | ||
}).then((response) => { | ||
Object | ||
.entries(response.data || []) | ||
.forEach(([name, values]) => { | ||
service.state.uniqueFieldsValues[layerId][name] = new Set(values) | ||
}) | ||
.fail(e => { console.warn(e); reject(e); }) | ||
})) | ||
})) | ||
await Promise.allSettled(promises); | ||
|
||
resolve(service.state.uniqueFieldsValues[layerId][name]) | ||
}) | ||
.fail(e => { console.warn(e); reject(e); }) | ||
}) | ||
return service.state.uniqueFieldsValues[layerId]; | ||
} |