Repository navigation
Add custom model editor to support custom routing - #183
Conversation
| // todonow: this belongs to this app and we should not take it from the demo... | ||
| import 'custom-model-editor/demo/style.css' |
There was a problem hiding this comment.
I was not sure how to add (where to put the file) global css rules as we need them for codemirror like .cm-s-default.
| readonly customModelEnabled: boolean | ||
| readonly customModelValid: boolean | ||
| readonly customModel: CustomModel | null |
There was a problem hiding this comment.
I added three new fields to the app's state: customModelEnabled simply tells us whether we are in 'custom model mode' or not (and right now it also determines whether the custom model editor is shown or not). customModelValid tells us whether or not the model is valid as it is determined by the editor component. customModel is the current custom model. We cannot just set the customModel field to null in case it is invalid, because we like to be able to send requests also for invalid models. Note that customModel is not used as backend for the editor's actual content. This is because if we managed the editor's content using react we would need to manage all of the editor's state (like cursor position, annotations etc.) with react (or at least keep it up to date somehow). Right now I don't see a real advantage of this.
| } | ||
| } | ||
| } | ||
| Dispatcher.dispatch(new SetCustomModel(initialCustomModel, true)) |
There was a problem hiding this comment.
We need to update the app state here. This is the price we pay for not using the app's custom model state as the editor's backend. Afterwards we just update the customModel state whenever the editor evaluated the input's validity (see validListener above).
|
I tried the branch and it seems to work. Let's fix the Dispatch of the Error Actions, then this could be merged I think. One thing to think about is optics. Would you like the editor to be where it is at the moment, or should we think of something else? Instead of a check box we could also have an icon for this and a "closing x". Also, 15rem is quite large, if the model is simple. |
Thanks for taking a look! Sorry, but which error action dispatch issue do you mean?
You are more than welcome to improve on this. I just put it anywhere really so I could go on implementing the functionality. |
# Conflicts: # src/stores/QueryStore.ts # test/stores/QueryStore.test.ts
…s when appropriate
…s when appropriate
| const triggerRouting = useCallback( | ||
| (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
| if (event.ctrlKey && event.key === 'Enter') { | ||
| dispatchCustomModel(editor.value, isValid, true) | ||
| } | ||
| }, | ||
| [editor, isValid] | ||
| ) | ||
|
|
There was a problem hiding this comment.
Not sure if this is better or worse, but we can also use the event handling of the text component using this API: https://github.com/graphhopper/custom-model-editor/blob/693a44097271ed84f603f46efbfcfc56ab97b608/src/custom_model_editor.js#L84-L86
This is how we did it in GH maps 1.0 as well: https://github.com/graphhopper/graphhopper/blob/876ffaa9fb990e2ea812c8956091809084025dcd/web-bundle/src/main/resources/com/graphhopper/maps/js/main-template.js#L153
And is it possible to send the request/json without validation when pressing Ctrl+Enter? @karussell should we do this? This is what we did for the old maps client.
There was a problem hiding this comment.
Using Ctrl+Enter we can now force the request regardless of the client side validation. 78c612c
|
Maybe it would be useful to add some kind of hint that advises the user to use the Ctrl+Space shortcut for auto-completion? |
# Conflicts: # src/api/graphhopper.d.ts # src/custom.d.ts # src/stores/QueryStore.ts # test/routing/Api.test.ts # test/stores/QueryStore.test.ts
|
@Janekdererste Are you planning to work on this? Or is there something I should change? Or can we merge this and do further improvements later? You wrote:
But unfortunately I don't know what you meant? Here are a few things that could be still done, but that might not be strictly required before merging:
|

This PR adds support for routing using so-called 'custom models' and fixes #17. A custom model is a set of rules written in JSON language that affects the outcome of the route calculation. Custom models are not used standalone, but rather they are modifications of existing profiles. So for example one can take the car profile and modify the routing using a custom model to reduce the speed on motorways. See the documentation for more details here.
Users shall be able to enter such custom models using a text field (the 'custom model editor'). For this and to guide the user when writing custom models we developed an interactive text field that supports syntax checking, error-highlighting and auto-completion here. This shall be added here.
Routing with custom models comes at a cost: The response will be slower, sometimes much slower. Therefore, we clearly need to distinguish two cases:
For 2) there is a catch: Custom models are not supported for all built-in profiles. Currently we offer
car,foot,bike,hike,racingbike,scooter,small_truck,truck,mtbon http://graphhopper.com/maps, but custom models are only available forcar,small_truck,foot,bike,scooter(not fortruck,hike,mtb,racingbike). This might be different for self-deployed GraphHopper instances etc. Unfortunately, currently we do not have this information available on the client side, so we might just ignore this for now as we do for the current GH maps app.Here I added a simple checkbox to the sidebar that opens/closes the custom model box right in the sidebar, just like we do for current GH maps. This is probably subject to change, because the space in the sidebar is rather limited (horizontally, but also vertically when there are instructions, many via-points etc.). Also we might need even more space for the following features that we might add at a later time:
So maybe at some point we need extra layout changes for these things, add tabs to the sidebar etc.
Opening/closing the custom model editor using the checkbox also defines whether we are in 'standard' or 'custom model' mode. This way we can easily compare routes with and without a custom model by opening and closing the text field.
For an open custom model box we need to distinguish three further cases:
The custom model editor/text field helps us dealing with these three cases. For example when there are syntax errors they will already be highlighted in red and the user can hover the corresponding text to see more details about the error. The same applies when the syntax is correct, but the custom model is invalid. Still, we need a way to alert the user about these errors. For example if the user adds a new query point we usually send a routing request right away, but if the custom model is invalid we should rather tell the user about this. I use the same error notification (
ErrorAction) we use to report error responses (like 'point not found' etc.) for this.