Skip to content

Commit

Permalink
Merge pull request #2515 from DenverCoder544/featuredata_default_sort…
Browse files Browse the repository at this point in the history
…ing_column_bugfix

refactor determining the sorting column
  • Loading branch information
ZakarFin authored Nov 20, 2023
2 parents c7011e0 + 0e3d417 commit 0f47555
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions bundles/framework/featuredata/plugin/FeatureDataPluginHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ class FeatureDataPluginUIHandler extends StateHandler {
visibleColumnsSettings,
selectByPropertiesFilter: {},
selectByPropertiesFeatureTypes: this.createSelectByPropertiesFeatureTypes(visibleColumnsSettings),
sorting: this.determineSortingColumn(layerId, features)
sorting: this.determineSortingColumn(layerId)
});
}

toggleShowSelectedFirst () {
const { activeLayerId, activeLayerFeatures, sorting } = this.getState();
const { activeLayerId, sorting } = this.getState();
const newState = { showSelectedFirst: !this.getState().showSelectedFirst };
if (newState.showSelectedFirst && !sorting?.order) {
newState.sorting = this.determineSortingColumn(activeLayerId, activeLayerFeatures);
newState.sorting = this.determineSortingColumn(activeLayerId);
}
this.updateState(newState);
}
Expand Down Expand Up @@ -119,12 +119,14 @@ class FeatureDataPluginUIHandler extends StateHandler {
visibleColumnsSettings = this.createVisibleColumnsSettings(newActiveLayerId);
};

const sorting = this.determineSortingColumn(newActiveLayerId);
return {
activeLayerId: newActiveLayerId,
layers: featureDataLayers,
activeLayerFeatures,
selectedFeatureIds,
visibleColumnsSettings
visibleColumnsSettings,
sorting
};
}

Expand All @@ -137,9 +139,9 @@ class FeatureDataPluginUIHandler extends StateHandler {
updateSorting (sorting) {
// if show selected first - is checked but sorting is cancelled we need to set default sorting to keep the selected items first.
const newState = { sorting };
const { showSelectedFirst, activeLayerFeatures, activeLayerId } = this.getState();
const { showSelectedFirst, activeLayerId } = this.getState();
if (showSelectedFirst && !sorting.order) {
newState.sorting = this.determineSortingColumn(activeLayerId, activeLayerFeatures);
newState.sorting = this.determineSortingColumn(activeLayerId);
}
this.updateState(newState);
}
Expand All @@ -155,7 +157,7 @@ class FeatureDataPluginUIHandler extends StateHandler {
}

updateVisibleColumns (value) {
const { visibleColumnsSettings, activeLayerFeatures, activeLayerId, sorting } = this.getState();
const { visibleColumnsSettings, activeLayerId, sorting } = this.getState();

// Always have to have at least one column selected - don't allow setting this empty.
if (value && value.length) {
Expand All @@ -167,7 +169,7 @@ class FeatureDataPluginUIHandler extends StateHandler {
};

if (!value.includes(sorting?.columnKey)) {
newState.sorting = this.determineSortingColumn(activeLayerId, activeLayerFeatures);
newState.sorting = this.determineSortingColumn(activeLayerId);
}

this.updateState(newState);
Expand All @@ -194,15 +196,14 @@ class FeatureDataPluginUIHandler extends StateHandler {
visibleColumnsSettings,
selectByPropertiesFilter,
selectByPropertiesFeatureTypes,
sorting: activeLayerFeatures ? this.determineSortingColumn(activeLayerId, activeLayerFeatures) : null
sorting: this.determineSortingColumn(activeLayerId)
};

if (!activeLayerFeatures) {
// not empty features, but missing completely
// empty should mean there is no features on viewport to list
const newActiveLayerFeatures = this.getFeaturesByLayerId(activeLayerId);
newState.activeLayerFeatures = newActiveLayerFeatures;
newState.sorting = newActiveLayerFeatures && newActiveLayerFeatures.length ? this.determineSortingColumn(activeLayerId, newActiveLayerFeatures) : null;
newState.selectedFeatureIds = newActiveLayerFeatures && newActiveLayerFeatures.length ? this.getSelectedFeatureIdsByLayerId(activeLayerId) : null;
newState.visibleColumnsSettings = this.createVisibleColumnsSettings(activeLayerId);
newState.selectByPropertiesFilter = {};
Expand Down Expand Up @@ -349,26 +350,24 @@ class FeatureDataPluginUIHandler extends StateHandler {
return currentLayer ? currentLayer.getId() : null;
}

determineSortingColumn (activeLayerId, features) {
if (!features || !features.length) {
return null;
}
// this might not be set to state yet, if this is the first time flyout is opened
// so we're creating this in that case
let { visibleColumnsSettings } = this.getState();
if (!visibleColumnsSettings) {
visibleColumnsSettings = this.createVisibleColumnsSettings(activeLayerId);
determineSortingColumn (newActiveLayerId) {
const { activeLayerId, visibleColumnsSettings, sorting } = this.getState();
const activeLayerChanged = newActiveLayerId !== activeLayerId;

// Same layer and the previous sorting field is still visible -> use the old
if (!activeLayerChanged && sorting?.columnKey && visibleColumnsSettings?.visibleColumns?.includes(sorting?.columnKey)) {
return sorting;
}

// get the first property that isn't in the default hidden fields and use that as default.
const defaultSortingColumn = Object.keys(features[0]?.properties).find((key) => this.columnShouldBeVisible(key, visibleColumnsSettings));
const sortedInfo = { order: 'ascend', columnKey: defaultSortingColumn };
return sortedInfo;
// otherwise this needs to be recreated.
return this.resetSortingColumn(newActiveLayerId);
}

columnShouldBeVisible (key, visibleColumnsSettings) {
const { visibleColumns } = visibleColumnsSettings;
return visibleColumns?.includes(key);
resetSortingColumn (newActiveLayerId) {
const newVisibleColumnsSettings = this.createVisibleColumnsSettings(newActiveLayerId);
const defaultSortingColumn = newVisibleColumnsSettings?.visibleColumns[0] || null;
const sortedInfo = { order: 'ascend', columnKey: defaultSortingColumn };
return sortedInfo;
}

sendExportDataForm (data) {
Expand Down

0 comments on commit 0f47555

Please sign in to comment.