Skip to content

Commit

Permalink
Handle reset of strictly nested dependencies (search inputs) (#644)
Browse files Browse the repository at this point in the history
* 🐛 Change all dependencies value

* Clean code - spaces

* Clean code - remove console.log

* 🐛 Use fformatter instead of unique when search with autocomplete input

* 🐛 return different object based on select autocomplete

* 🐛 revert features in editing
  • Loading branch information
volterra79 authored Jul 5, 2024
1 parent 7f605b9 commit d89c970
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/app/core/editing/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ proto._getFeatures = function(options={}) {
*/
proto.revert = function() {
const d = $.Deferred();
this._featuresstore.setFeatures(this._cloneFeatures(this._layer.readFeatures()));
this.getEditingSource().setFeatures(this._cloneFeatures(this.readFeatures()));
d.resolve();
return d.promise();
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/layers/features/featuresstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ proto._updateFeature = function(feature) {
});
};

proto.setFeatures = function(features) {
proto.setFeatures = function(features = []) {
this._features = features;
};

Expand Down
16 changes: 10 additions & 6 deletions src/app/core/layers/features/olfeaturesstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ proto.getLength = function() {
};

//overwrite
proto.setFeatures = function(features=[]) {
features.forEach(feature => this._features.push(feature));
proto.setFeatures = function(features = []) {
//remove features
this._features.clear();
//add new features
this.addFeatures(features);
this._features.dispatchEvent('change');
};
// overwrite
proto.readFeatures = function() {
Expand All @@ -29,20 +33,20 @@ proto.getFeaturesCollection = function() {
};

proto.getFeatureById = function(featureId) {
return this._features.getArray().find(feature => feature.getId() == featureId);
return this._features.getArray().find(feature => featureId == feature.getId());
};

proto.getFeatureByUid = function(uid) {
return this._features.getArray().find(feature => feature.getUid() === uid);
return this._features.getArray().find(feature => uid === feature.getUid());
};

proto._addFeature = function(feature) {
this._features.push(feature);
// useful for ol.source.Vector
this._features.dispatchEvent('change')
this._features.dispatchEvent('change');
};

//sobtitute the feature after modify
//substitute the feature after modify
proto._updateFeature = function(feature) {
// set index at -1
let index = -1;
Expand Down
62 changes: 34 additions & 28 deletions src/app/gui/search/vue/panel/searchservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ proto.getLayersFilterData = async function(layers, options = {}) {
const data = Array.from(
promisesData
.filter(({status}) => 'fulfilled' === status)
.reduce((accumulator, { value = [] }) => new Set([...accumulator, ...value]), [])
.reduce((accumulator, { value = [] }) => {
return new Set([...accumulator, ...(value.data ? value.data : value)])
}, [])
)
//check if is not empty array
switch (data.length && typeof data[0]) {
Expand All @@ -446,15 +448,14 @@ proto.getUniqueValuesFromField = async function({field, value, output}) {
try {
data = await this.getLayersFilterData(
(1 === this.searchLayers.length ? [this.searchLayer] : this.searchLayers), {
field: this.getAutoFieldDependeciesParamField(field),
suggest: value !== undefined ? `${field}|${value}` : undefined,
unique: field,
ordering: field
field: this.getAutoFieldDependeciesParamField(field),
suggest: value !== undefined ? `${field}|${value}` : undefined,
fformatter: field,
ordering: field,
});

if ('autocomplete' === output) {
data = data.map(value => ({ id:value, text:value }));
}
data = (data || []).map(([value, key]) => 'autocomplete' === output ? { id: value, text: key } : {key, value});

} catch(e) { console.warn(e); }

return data;
Expand Down Expand Up @@ -643,55 +644,60 @@ proto.getDependanceCurrentValue = function(field) {
*
* Fill all dependencies inputs based on value
*/
proto.fillDependencyInputs = function({field, subscribers=[], value=ALLVALUE}={}) {
proto.fillDependencyInputs = function({ field, subscribers = [], value = ALLVALUE } = {}) {
const isRoot = this.inputdependance[field] === undefined;
//check id inpute father is valid to search on subscribers
const invalidValue = value===ALLVALUE || value === null || value === undefined || value.toString().trim() === '';
const invalidValue = ALLVALUE === value || [null, undefined].includes(value) || '' === value.toString().trim() ;
return new Promise((resolve, reject) => {
//loop over dependencies fields inputs
subscribers.forEach(subscribe => {
// in case of autocomplete reset values to empty array
if (subscribe.type === 'autocompletefield') {
subscribe.options.values.splice(0);
} else {
if (subscribe.type === 'autocompletefield') { subscribe.options.values.splice(0) }
else {
//set starting all values
if (subscribe.options._allvalues === undefined) {
subscribe.options._allvalues = [...subscribe.options.values];
}
if (undefined === subscribe.options._allvalues) { subscribe.options._allvalues = [...subscribe.options.values] }
//case of father is set an empty invalid value (all value example)
if (invalidValue) {
//subscribe has to set all valaues
subscribe.options.values.splice(0);
setTimeout(()=>subscribe.options.values = [...subscribe.options._allvalues]);
} else {
subscribe.options.values.splice(1);
} //otherwise has to get first __ALL_VALUE
setTimeout(() => subscribe.options.values = [...subscribe.options._allvalues]);
} else { subscribe.options.values.splice(1) } //otherwise has to get first __ALL_VALUE
}
subscribe.value = subscribe.type !== 'selectfield' ? ALLVALUE : null;
subscribe.value = 'selectfield' === subscribe.type ? ALLVALUE : null;

//need to
if (['selectfield', 'autocompletefield'].includes(subscribe.type)) {
this.fillDependencyInputs({
field: subscribe.attribute,
subscribers: this.getDependencies(subscribe.attribute),
value: subscribe.value,
})
}

});
// check if cache field values are set
this.cachedependencies[field] = this.cachedependencies[field] || {};
this.cachedependencies[field]._currentValue = value;
const notAutocompleteSubscribers = subscribers.filter(subscribe => subscribe.type !== 'autocompletefield');
if (value && value !== ALLVALUE) {
const notAutocompleteSubscribers = subscribers.filter(s => s.type !== 'autocompletefield');
if (value && ALLVALUE !== value) {
let isCached;
let rootValues;
if (isRoot) {
const cachedValue = this.cachedependencies[field] && this.cachedependencies[field][value];
isCached = cachedValue !== undefined;
rootValues = isCached && cachedValue;
isCached = undefined !== cachedValue;
rootValues = isCached && cachedValue;
} else {
const dependenceCurrentValue = this.getDependanceCurrentValue(field);
const cachedValue = this.cachedependencies[field]
const cachedValue = this.cachedependencies[field]
&& this.cachedependencies[field][dependenceCurrentValue]
&& this.cachedependencies[field][dependenceCurrentValue][value];
isCached = cachedValue !== undefined;
isCached = cachedValue !== undefined;
rootValues = isCached && cachedValue;
}
if (isCached) {
for (let i = 0; i < subscribers.length; i++) {
const subscribe = subscribers[i];
const values = rootValues[subscribe.attribute];
const values = rootValues[subscribe.attribute];
if (values && values.length) {
for (let i = 0; i < values.length; i++) {
subscribe.options.values.push(values[i]);
Expand Down
6 changes: 3 additions & 3 deletions src/components/SearchPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<search-panel-label :forminput="forminput"/>
<input @focus="onFocus" type="text" v-model="forminput.value" @change="changeInput(forminput)" class="form-control" :id="forminput.id" >
</div>
<div v-else-if="forminput.type === 'selectfield' || forminput.type === 'autocompletefield'" class="form-group text" v-disabled="isSelectDisabled(forminput)">
<div v-else-if="['selectfield', 'autocompletefield'].includes(forminput.type)" class="form-group text" v-disabled="isSelectDisabled(forminput)">
<search-panel-label :forminput="forminput"/>
<bar-loader v-if ="forminput.options.dependance" :loading="state.loading[forminput.options.dependance] || forminput.loading"/>
<select2 :forminput="forminput" :autocompleteRequest="autocompleteRequest" @select-change="changeInput"/>
Expand All @@ -42,8 +42,8 @@
</template>

<script>
import Select2 from 'components/SearchSelect2.vue';
import SearchDatetime from 'components/SearchDatetime.vue';
import Select2 from 'components/SearchSelect2.vue';
import SearchDatetime from 'components/SearchDatetime.vue';
import SearchPanelLabel from 'components/SearchPanelLabel.vue';

export default {
Expand Down
12 changes: 6 additions & 6 deletions src/components/SearchSelect2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
props: ['forminput','autocompleteRequest'],
mixins: [select2Mixin],
methods: {
emitChangeEvent(evt){
const id = $(evt.target).attr('id');
emitChangeEvent(evt) {
const id = $(evt.target).attr('id');
const attribute = $(evt.target).attr('name');
const data = evt.params.data;
const value = data ? data.id : ALLVALUE;
const data = evt.params.data;
const value = data ? data.id : ALLVALUE;
this.$emit('select-change', {
id,
attribute,
Expand Down Expand Up @@ -75,13 +75,13 @@
watch : {
async 'forminput.value'(value) {
await this.$nextTick();
if (value === ALLVALUE) {
if (ALLVALUE === value) {
this.select2.val(value);
this.select2.trigger('change');
}
}
},
created(){
created() {
this.allvalue = ALLVALUE;
},
async mounted() {
Expand Down

0 comments on commit d89c970

Please sign in to comment.