Skip to content

Commit

Permalink
Rename utils/ExpressionParser to utils/EditingUtils, move getFeatureT…
Browse files Browse the repository at this point in the history
…emplate to EditingUtils, expose in API
  • Loading branch information
manisandro committed Apr 16, 2024
1 parent 13656d7 commit 48d28e7
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 38 deletions.
31 changes: 1 addition & 30 deletions actions/editing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import editingReducer from '../reducers/editing';
import ReducerIndex from '../reducers/index';
import {ExpressionFeatureCache, parseExpressionsAsync} from '../utils/ExpressionParser';
import {ExpressionFeatureCache, parseExpressionsAsync} from '../utils/EditingUtils';
ReducerIndex.register("editing", editingReducer);

export const SET_EDIT_CONTEXT = 'SET_EDIT_CONTEXT';
Expand All @@ -29,32 +29,3 @@ export function clearEditContext(contextId, newActiveContextId = null) {
newActiveContextId: newActiveContextId
};
}

const FeatureTemplateFactories = {};

export function setFeatureTemplateFactory(dataset, factory) {
FeatureTemplateFactories[dataset] = factory;
}

export function getFeatureTemplate(editConfig, feature, editIface, mapPrefix, mapCrs, callback) {
if (editConfig.editDataset in FeatureTemplateFactories) {
feature = FeatureTemplateFactories[editConfig.editDataset](feature);
}
// Apply default values
const defaultFieldExpressions = editConfig.fields.reduce((res, field) => {
if (field.defaultValue) {
return {...res, [field.id]: field.defaultValue.replace(/^expr:/, '')};
}
return res;
}, {});
ExpressionFeatureCache.clear();
parseExpressionsAsync(defaultFieldExpressions, feature, editIface, mapPrefix, mapCrs).then(result => {
// Adjust values based on field type
editConfig.fields.forEach(field => {
if (field.id in result && field.type === "date") {
result[field.id] = result[field.id].split("T")[0];
}
});
callback({...feature, properties: {...feature.properties, ...result}});
});
}
3 changes: 2 additions & 1 deletion components/AttributeForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import isEmpty from 'lodash.isempty';
import PropTypes from 'prop-types';
import {v1 as uuidv1} from 'uuid';

import {setEditContext, clearEditContext, getFeatureTemplate} from '../actions/editing';
import {setEditContext, clearEditContext} from '../actions/editing';
import {LayerRole, refreshLayer} from '../actions/layers';
import {setCurrentTaskBlocked} from '../actions/task';
import CoordinatesUtils from '../utils/CoordinatesUtils';
import {getFeatureTemplate} from '../utils/EditingUtils';
import LocaleUtils from '../utils/LocaleUtils';
import AutoEditForm from './AutoEditForm';
import LinkFeatureForm from './LinkFeatureForm';
Expand Down
3 changes: 2 additions & 1 deletion components/LinkFeatureForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {connect} from 'react-redux';

import PropTypes from 'prop-types';

import {setEditContext, getFeatureTemplate} from '../actions/editing';
import {setEditContext} from '../actions/editing';
import {LayerRole, addLayerFeatures, removeLayer} from '../actions/layers';
import {getFeatureTemplate} from '../utils/EditingUtils';
import LocaleUtils from '../utils/LocaleUtils';
import MapUtils from '../utils/MapUtils';
import AttributeForm from './AttributeForm';
Expand Down
3 changes: 1 addition & 2 deletions components/QtDesignerForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {v1 as uuidv1} from 'uuid';
import xml2js from 'xml2js';

import ConfigUtils from '../utils/ConfigUtils';
import {parseExpression, ExpressionFeatureCache} from '../utils/ExpressionParser';
import {parseExpression, ExpressionFeatureCache} from '../utils/EditingUtils';
import LocaleUtils from '../utils/LocaleUtils';
import MiscUtils from '../utils/MiscUtils';
import EditComboField, {KeyValCache} from './EditComboField';
Expand All @@ -25,7 +25,6 @@ import Icon from './Icon';
import Spinner from './Spinner';
import ButtonBar from './widgets/ButtonBar';
import DateTimeInput from './widgets/DateTimeInput';
import NumberInput from './widgets/NumberInput';
import TextInput from './widgets/TextInput';

import './style/QtDesignerForm.css';
Expand Down
4 changes: 3 additions & 1 deletion plugins/API.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ import TextInput from '../components/widgets/TextInput.jsx';
import ToggleSwitch from '../components/widgets/ToggleSwitch.jsx';
import VectorLayerPicker from '../components/widgets/VectorLayerPicker.jsx';
import CoordinatesUtils from '../utils/CoordinatesUtils';
import EditingInterface from '../utils/EditingInterface.js';
import EditingInterface from '../utils/EditingInterface';
import * as EditingUtils from '../utils/EditingUtils';
import LayerUtils from '../utils/LayerUtils';
import * as PermaLinkUtils from '../utils/PermaLinkUtils';
import ServiceLayerUtils from '../utils/ServiceLayerUtils';
Expand Down Expand Up @@ -160,6 +161,7 @@ class API extends React.Component {
window.qwc2.getState = this.getState;
window.qwc2.CoordinatesUtils = CoordinatesUtils;
window.qwc2.EditingInterface = EditingInterface;
window.qwc2.EditingUtils = EditingUtils;
window.qwc2.PermaLinkUtils = PermaLinkUtils;
window.qwc2.VectorLayerUtils = VectorLayerUtils;

Expand Down
3 changes: 1 addition & 2 deletions plugins/AttributeTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import FileSaver from 'file-saver';
import isEmpty from 'lodash.isempty';
import PropTypes from 'prop-types';

import {getFeatureTemplate} from '../actions/editing';
import {LayerRole} from '../actions/layers';
import {zoomToExtent, zoomToPoint} from '../actions/map';
import {setCurrentTask, setCurrentTaskBlocked} from '../actions/task';
Expand All @@ -27,7 +26,7 @@ import TextInput from '../components/widgets/TextInput';
import ConfigUtils from '../utils/ConfigUtils';
import CoordinatesUtils from '../utils/CoordinatesUtils';
import EditingInterface from '../utils/EditingInterface';
import {ExpressionFeatureCache, parseExpression} from '../utils/ExpressionParser';
import {ExpressionFeatureCache, parseExpression, getFeatureTemplate} from '../utils/EditingUtils';
import LayerUtils from '../utils/LayerUtils';
import LocaleUtils from '../utils/LocaleUtils';
import MapUtils from '../utils/MapUtils';
Expand Down
3 changes: 2 additions & 1 deletion plugins/Editing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import isEqual from 'lodash.isequal';
import PropTypes from 'prop-types';
import {v1 as uuidv1} from 'uuid';

import {setEditContext, clearEditContext, getFeatureTemplate} from '../actions/editing';
import {setEditContext, clearEditContext} from '../actions/editing';
import {LayerRole, addLayerFeatures, removeLayer, refreshLayer, changeLayerProperty} from '../actions/layers';
import {setSnappingConfig} from '../actions/map';
import {setCurrentTask, setCurrentTaskBlocked} from '../actions/task';
Expand All @@ -25,6 +25,7 @@ import SideBar from '../components/SideBar';
import ButtonBar from '../components/widgets/ButtonBar';
import ConfigUtils from '../utils/ConfigUtils';
import EditingInterface from '../utils/EditingInterface';
import {getFeatureTemplate} from '../utils/EditingUtils';
import LayerUtils from '../utils/LayerUtils';
import LocaleUtils from '../utils/LocaleUtils';
import MapUtils from '../utils/MapUtils';
Expand Down
30 changes: 30 additions & 0 deletions utils/ExpressionParser.js → utils/EditingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,33 @@ export function parseExpressionsAsync(expressions, feature, editIface, mapPrefix
}
});
}


const FeatureTemplateFactories = {};

export function setFeatureTemplateFactory(dataset, factory) {
FeatureTemplateFactories[dataset] = factory;
}

export function getFeatureTemplate(editConfig, feature, editIface, mapPrefix, mapCrs, callback) {
if (editConfig.editDataset in FeatureTemplateFactories) {
feature = FeatureTemplateFactories[editConfig.editDataset](feature);
}
// Apply default values
const defaultFieldExpressions = editConfig.fields.reduce((res, field) => {
if (field.defaultValue) {
return {...res, [field.id]: field.defaultValue.replace(/^expr:/, '')};
}
return res;
}, {});
ExpressionFeatureCache.clear();
parseExpressionsAsync(defaultFieldExpressions, feature, editIface, mapPrefix, mapCrs).then(result => {
// Adjust values based on field type
editConfig.fields.forEach(field => {
if (field.id in result && field.type === "date") {
result[field.id] = result[field.id].split("T")[0];
}
});
callback({...feature, properties: {...feature.properties, ...result}});
});
}

0 comments on commit 48d28e7

Please sign in to comment.