Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Leaflet style #9

Open
tomchadwin opened this issue May 15, 2018 · 7 comments
Open

Import Leaflet style #9

tomchadwin opened this issue May 15, 2018 · 7 comments
Labels
enhancement New feature or request

Comments

@tomchadwin
Copy link
Owner

Not easy - perhaps start with simple styles only (ie when the style is not a function)?

@tomchadwin tomchadwin added the enhancement New feature or request label May 15, 2018
tomchadwin added a commit that referenced this issue May 17, 2018
tomchadwin added a commit that referenced this issue May 17, 2018
@tomchadwin
Copy link
Owner Author

Implemented for object and function styles with simple logic.

@tomchadwin
Copy link
Owner Author

Currently doesn't work for point layers, as need to parse their pointToLayer property.

@tomchadwin
Copy link
Owner Author

I believe this currently only works with style functions if they are anonymous functions directly set as the value of the style property. In order to handle functions declared independently, will have to retrieve the relevant function. No idea how to do that yet.

@tomchadwin
Copy link
Owner Author

tomchadwin commented May 21, 2018

OK, so my current starting point is to get loop through all elements in window and return only those which are an instanceof L.Map. I then parse that with Esprima to get everything I need (layers, etc). This works when properties' values are simple values, objects, or anonymous functions, because in all those cases all the relevant code is within the `L.Map'.

However, this fails when something (in this case a style property) has a value which is a function call. The corresponding function definition is obviously not part of the L.Map, so I haven't parsed it.

Do I have to lop through window beforehand, and build up an array of every object instanceof Function? My main L.Map parse with Esprima gives me the function name which is called, so I guess this could work.

But it seems like a lot of work...

@tomchadwin
Copy link
Owner Author

No, I think I've got that fundamentally wrong...

My existing loop matches the DOM element which is instanceof L.Map. Function definitions are not part of a DOM element, I guess, so this approach won't work.

So, obviously without resorting to the completely impossible task of retrieving relevant script via regex, how can I retrieve the source of a function's definition when all I have is a call to it? I have parsed the call, so I know the function name.

@tomchadwin
Copy link
Owner Author

No, I wasn't wrong. Apparently function definitions within a <script> tag are part of the DOM, so I can get them. Now, how to get a named function definition without looping through all of them...

@tomchadwin
Copy link
Owner Author

tomchadwin commented May 21, 2018

Good grief. I've gone all the way round this in my head, just to arrive at: window.funcName. That means I can do:

esprima.parse(window.funcName.toString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant