Skip to content

Interactive custom model text box - #2239

Merged
easbar merged 52 commits into
masterfrom
custom_model_textbox
Feb 26, 2021
Merged

easbar merged 52 commits into
masterfrom
custom_model_textbox

Conversation

@easbar

@easbar easbar commented Feb 16, 2021 •

Copy link
Copy Markdown
Member

This is work in progress to realize a text input box that allows entering a custom model with error highlighting and auto-completion. The goal here is to provide a very flat learning curve for the custom model language. The custom model is written in YAML that can contain boolean expression strings that refer to encoded values like road_class == PRIMARY etc.

Since the encoded values are unknown to most users they are provided as auto-completion suggestions. The auto-completion can be triggered by pressing Ctrl+Space. Note that the auto-completion suggestions depend on the cursor position. If the cursor is positioned within some word the conditions are filtered by the characters between the beginning of the word and the cursor.

|road_class + Ctrl+Space yields surface, road_class, road_class_link, toll, ..., but ro|ad_class + Ctrl+Space only yields road_class, road_class_link, ... (all values starting with ro)! Is this what most people would expect?

Usage: see the README.md file in the textbox folder

Currently the boolean expressions are colored in blue, errors in boolean expressions are red and errors in yaml format are indicated by red underlines.

todo:

  • calculate error ranges also for YAML errors related to null values
  • allow multiple 'else if' (there is a minor bug that prevents this currently)
  • move YAML error messages inside text editor (tooltip or maybe error indicator at line number). for a tooltip maybe take a look here: https://github.com/codemirror/CodeMirror/blob/master/addon/tern/tern.js#L595
  • support boolean and numeric encoded values
  • support boolean literals true, false
  • integrate with GH maps
  • adjust /info endpoint to return encoded value types (numeric/boolean/enum) not needed -> show ev from /info
  • fix CI build: Error: Can't walk dependency graph: Cannot find module 'custom-model-editor'
  • replace string.startsWith (done) and also check IE/browser compatibility in general IE won't be supported
  • set initial content to make it easier to get started? could be comments as well...
  • read-only view for json (to be able to copy&paste the model to request)
  • colored frame of the editor on valid/invalid input? or at least disable search button as long as the input is invalid.
  • support area encoded values (?)
  • minify codemirror css files (not worth it), add version information as well
  • update 'Help' link to point somewhere the new syntax is explained
  • remove js-yaml (we only need one yaml parser)
  • update documentation for separate editor module

image

@easbar

easbar commented Feb 19, 2021

Copy link
Copy Markdown
Member Author

@karussell I found this code in main-template.js:

if(urlParams.flex)
$("#flex-input-link").click();
var customURL = urlParams.load_custom;
if(customURL && ghenv.environment === 'development')
$.ajax(customURL).
done(function(data) { $("#flex-input-text").val(data); $("#flex-input-link").click(); }).
fail(function(err) { console.log("Cannot load custom URL " + customURL); });

It looks like it takes a custom model from the url parameters? Is this an actual working feature or can it be removed? So far I thought it would be nice to be able to store custom models inside the url, but it was not possible...

@karussell

karussell commented Feb 19, 2021 •

Copy link
Copy Markdown
Member

Thanks for this incredible work. It will help us a lot to promote the customizable routing I think :)

Is this an actual working feature or can it be removed?

For development it should work but we can also remove it.

@easbar

easbar commented Feb 20, 2021 •

Copy link
Copy Markdown
Member Author

Instead of the package.json why can't we do a relative require statement like

Yes, good idea, this works 👍 Its not ideal, because this way we have to run npm install && npm run build from the custom-model-editor directory separately, but this can be done via the maven frontend plugin at least. See my last commit.

Apparently npm used to have a different behavior where instead of using the symlink it copied the local child module to node_modules, but this comes with other problems. Another solution could be yarn workspaces... But for now using require(../..) is good enough?

Or publishing it as a real separate dependency to npm? (For now I would find this a bit of an overkill)

Yes an also this only really works for CI (or maybe not even). For local development uploading every change to npm (or even some private registry) seems way to cumbersome.

And probably move the custom-model-editor to the same root src/main/resources/com/graphhopper/maps/

I do not think this is related to the problem I was having, but if anything I would copy the maps sources to src/main/js not the other way around.

@easbar

easbar commented Feb 20, 2021

Copy link
Copy Markdown
Member Author

In the last commit I added a button that switches the editor to a read-only json view

image

I'd like to disable it as long as the yaml input is invalid. WDYT is this useful? Can/should we change these three 'buttons' from anchors to buttons? Or how can we make it appear disabled? Maybe use strikethrough, or even remove it entirely?

Btw I also disable the search button as long as the input is invalid now, does this make sense?

@karussell

karussell commented Feb 22, 2021 •

Copy link
Copy Markdown
Member

Over one thing I stumbled: before I was able to do the usual npm run watch I had to either build the jar via maven or run cd src/main/js/custom-model-editor/ && npm install && npm run build and restart the GH server.

In the last commit I added a button that switches the editor to a read-only json view

Nice idea!

And should I do a reimport of our customizable routing server in its current state? (and rewrite the examples&screenshots?)

btw: I just picked a random route and thought something is really broken as it did not avoid MOTORWAY at all. No matter what I did. The problem was that although in the map it looked like a motorway it was a TRUNK (I have a strange luck ;) ) ... maybe we should additionally send details:[road_class] ... or better ALL used encoded values :D ?

And previously it worked when I included details: [road_class] in the yaml but we are now too strict :)

(also a bit ugly that the elevation widget currently only works if elevation is enabled :) )

Can/should we change these three 'buttons' from anchors to buttons?

text is fine IMO. Only a minor style adaption so that it matches the sidebar footer? (greenish text without underscore)
And for disabling: gray it out?

@easbar

easbar commented Feb 22, 2021

Copy link
Copy Markdown
Member Author

Over one thing I stumbled: before I was able to do the usual npm run watch I had to either build the jar via maven or run cd src/main/js/custom-model-editor/ && npm install && npm run build and restart the GH server.

Since we now include custom-model-editor via require(...) only instead of package.json we have to build it separately yes. You can also do npm run watch in the custom-model-editor folder to get live updates.

And should I do a reimport of our customizable routing server in its current state? (and rewrite the examples&screenshots?)

Sure! We cannot use areas yet though.

... maybe we should additionally send details:[road_class] ... or better ALL used encoded values :D ?

You mean we should show all encoded values when we hover edges in the mvt view? Yes!

And previously it worked when I included details: [road_class] in the yaml but we are now too strict :)

What do you mean? What are details in yaml?

(also a bit ugly that the elevation widget currently only works if elevation is enabled :) )

Yes, since it is also showing path details this feature should not depend on elevation...

text is fine IMO. Only a minor style adaption so that it matches the sidebar footer? (greenish text without underscore)
And for disabling: gray it out?

Ok will try.

@karussell

karussell commented Feb 22, 2021 •

Copy link
Copy Markdown
Member

What do you mean? What are details in yaml?

Since we (currently) have the properties like speed on the same JSON root level like the other request parameters we would request path details via details:[...]. But as the plan is to change this (in #2232) it is probably not worth a thought :)

Since we now include custom-model-editor via require(...) only instead of package.json we have to build it separately yes. You can also do npm run watch in the custom-model-editor folder to get live updates.

We would need to have this documented or include the editor JS files like we include other JS files?

You mean we should show all encoded values when we hover edges in the mvt view? Yes!

I didn't mean this but it would be nice :)

What I meant is that we collect all encoded values used in the script and include it in the query as path details. This way we can easily see the e.g. the road_class for the route and how it changes if we change e.g. the multiply-by number. (if the elevation widget is shown)

@easbar

easbar commented Feb 22, 2021

Copy link
Copy Markdown
Member Author

Since we (currently) have the properties like speed on the same JSON root level like the other request parameters we would request path details via details:[...]. But as the plan is to change this (in #2232) it is probably not worth a thought :)

Ah ok. Btw so far you can use ctrl+enter to send the request regardless of the validation. Not sure if this should be a 'feature' :) So far the validation check is just not implemented in this case...

We would need to have this documented or include the editor JS files like we include other JS files?

Ok sure it should be documented. We cannot include editor files like other files because the editor uses an additional transpilation step.

What I meant is that we collect all encoded values used in the script and include it in the query as path details. This way we can easily see the e.g. the road_class for the route and how it changes if we change e.g. the multiply-by number. (if the elevation widget is shown)

Ok yes that makes sense. It should not be too hard to return the used encoded values from the expression parser.

@easbar

easbar commented Feb 24, 2021 •

Copy link
Copy Markdown
Member Author

Its still not working with IE11, I get a SCRIPT5021 error that is due to a character range \u{D800}-\u{DFFF} used in the yaml parser. Apparently it is supposed to support IE11 though: eemeli/yaml#73. Maybe I am not correctly making use of it's 'browser build' yet.

Hmm, no it does not. IE11 support was officially dropped 25 days ago: eemeli/yaml@c1639b3

the yaml playground does not work in IE11 either: https://github.com/eemeli/yaml-playground

I'd say let's not support IE11 and move on :) Windows users can use Edge (this branch works for Edge).

@easbar
easbar marked this pull request as ready for review February 24, 2021 20:26
@easbar
easbar merged commit a750fe5 into master Feb 26, 2021
@easbar
easbar deleted the custom_model_textbox branch February 26, 2021 10:19
@easbar easbar added this to the 3.0 milestone Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants