-
Notifications
You must be signed in to change notification settings - Fork 77
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
standardized indigenous layer code #207
Conversation
Hey! The approach looks correct 馃憤 |
No problem @sagarpreet-chadha , I'm still trying ways to remove the CORS error. Thanks! |
Hey @ananyaarun , how is it going? |
} | ||
else{ | ||
ILL_url = "https://native-land.ca/api/index.php?maps=treaties&position=" + parseInt(origin.lat) + "," + parseInt(origin.lng); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @sagarpreet-chadha , I tried both passing the ILL_url as a parameter and this if else block like this. But still getting the same cors error. I haven't been able to figure out another way yet. Do you have any other way in mind ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as we are defining URL again , so lets pass name
variable everywhere in arguments,etc. and remove URL
variable in function argument . I hope it makes sense .
example/index.html
Outdated
var IndigenousLandsTerritories = L.layerGroup.indigenousLandsTerritoriesLayer(); | ||
var IndigenousLandsLanguages = L.layerGroup.indigenousLandsLanguagesLayer(); | ||
var IndigenousLandsTreaties = L.layerGroup.indigenousLandsTreatiesLayer(); | ||
var IndigenousLandsTerritories = L.layerGroup.indigenousLayers('https://native-land.ca/api/index.php?maps=territories&position=45,-72',"Territories"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ananyaarun , why are we passing URL here? I see we are not using it , right? Can we remove this as this will make use of this layer easy ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh,so this I passed as the url parameter. It is being used @sagarpreet-chadha
the section of the code where url is defined in options, it is used there.-
url: this.url,
The 3 indigenous layers have different url's.
example/index.html
Outdated
var IndigenousLandsTreaties = L.layerGroup.indigenousLandsTreatiesLayer(); | ||
var IndigenousLandsTerritories = L.layerGroup.indigenousLayers('https://native-land.ca/api/index.php?maps=territories&position=45,-72',"Territories"); | ||
var IndigenousLandsLanguages = L.layerGroup.indigenousLayers('https://native-land.ca/api/index.php?maps=languages&position=45,-72',"Languages"); | ||
var IndigenousLandsTreaties = L.layerGroup.indigenousLayers('https://native-land.ca/api/index.php?maps=treaties&position=44,-80',"Treaties"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here 馃槃 .
src/indigenousLayers.js
Outdated
options: { | ||
url: 'https://native-land.ca/api/index.php?maps=territories&position=45,-72', | ||
url: this.url, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's replace this name: this.name ,
src/indigenousLayers.js
Outdated
@@ -148,6 +138,7 @@ L.LayerGroup.IndigenousLandsTerritoriesLayer = L.LayerGroup.extend( | |||
} | |||
); | |||
|
|||
L.layerGroup.indigenousLandsTerritoriesLayer = function (options) { | |||
return new L.LayerGroup.IndigenousLandsTerritoriesLayer(options); | |||
L.layerGroup.indigenousLayers = function (url,name,options) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing URL from here as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this section of the code i basically pass url and name as a parameter both of which are being used.
L.layerGroup.indigenousLayers = function (url,name,options) {
return new L.LayerGroup.IndigenousLayers(url,name,options);
};
url is the unique url for the 3 layers
name as in territories, languages, treaties
src/indigenousLayers.js
Outdated
|
||
if(this.name == "Territories"){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use this :
if(this.name == "Territories"){ | |
if(this.name === "Territories"){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure :)
I have added some suggestions above . @jywarren ...What could be the reason? |
@sagarpreet-chadha , I have not changed the url part yet as it is being used.
|
I was implying to remove URL field from options , makes sense? |
@sagarpreet-chadha , I made the required changes. I initially misunderstood what u said. Thanks for the review !! @sagarpreet-chadha @rexagod let me know what you think. |
var ILL_url; | ||
|
||
if(this.name === "Territories"){ | ||
ILL_url = "https://native-land.ca/api/index.php?maps=territories&position=" + parseInt(origin.lat) + "," + parseInt(origin.lng); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using eval
here instead of this if ladder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, but in that case won't all three conditions be run unnecessarily ? even if the first one is found to be true ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've suggested some changes on the particular line of code in discussion above. Take a look!
@@ -38,49 +37,35 @@ L.LayerGroup.IndigenousLandsTerritoriesLayer = L.LayerGroup.extend( | |||
(function() { | |||
var zoom = self._map.getZoom(), origin = self._map.getCenter() ; | |||
var $ = window.jQuery; | |||
var ILL_url; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var ILL_url; | |
var ILL_url = this.name === "Territories" || this.name === "Languages" ? "https://native-land.ca/api/index.php?maps="+this.name+"&position=" + parseInt(origin.lat) + "," + parseInt(origin.lng) : ILL_url = "https://native-land.ca/api/index.php?maps=treaties&position=" + parseInt(origin.lat) + "," + parseInt(origin.lng); |
Okay so since jshint
does not take kindly to eval
operations, and since we aren't considering doing a //jshint ignore:line
here (as that should be the last resort), how about replacing the if ladder with a ternary at the time of declaration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this would work!!
Do you feel it makes the code a bit to long for one line and not understandable though ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, linting standards do not recommend any line exceeding more than 80 characters, so I guess you can format this line over a few lines, which should comply with the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll make that change :) Thanks.
But any specific reason why the use of this is better than the use of the if else if block ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'd like to emphasize more on analyzing the similarity between these lines abstracting them into a function for a much improved code quality (less code, also reusable). The ternary and spread is something I'd like to go with in such situations, but leave it completely on your acumen to decide to keep them or not.
@@ -82,10 +82,10 @@ L.LayerGroup.IndigenousLayers = L.LayerGroup.extend( | |||
var ill_poly ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rexagod , how can we possibly use the ternary operator here (in the second if else block)?
All 3 assignments to ill_poly are very different ie not only is the name different (hence this.name based assignment wont work) All three of them are linked to a separate link here.(href part)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function getPoly(coords, clr, ...stringparams) {
return L.polygon(coords, {color: clr}).bindPopup(
`<strong>Name : </strong> ${stringparams.nme} <br>
<strong>Description: </strong> <a href=${stringparams.desc}>Native Lands - ${stringparams.nme}</a><br>
<i>From the <a href='https://github.com/publiclab/leaflet-environmental-layers/pull/${
this.name === "Territories" ? 77 : this.name === "Languages" ? 76 : 78
}'>Indigenous ${this.name} Inventory</a> (<a href='https://publiclab.org/notes/sagarpreet/06-06-2018/leaflet-environmental-layer-library?_=1528283515'>info<a>)</i>`
);
}
var ill_poly = (isNaN(coords[0][0][0]) || isNaN(coords[0][0][1])) ? undefined : getPoly(...options);
@ananyaarun Let me know you have any more doubts or so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rexagod, Thanks for the review!
I tried this code. It initially gave me a reference to option not defined error on my console. Then I removed options passed coords and clr and now it works but the Language layer is loading for both territories and Treties layer as well unlike before with the if else block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// assuming that `this.name` is defined in this scope
function getPoly(coords, clr, ...stringparams) {
return L.polygon(coords, {color: clr}).bindPopup(
`<strong>Name : </strong> ${stringparams[0]} <br>
<strong>Description: </strong> <a href=${stringparams[1]}>Native Lands - ${stringparams[0]}</a><br>
<i>From the <a href='https://github.com/publiclab/leaflet-environmental-layers/pull/${
this.name === "Territories" ? 77 : this.name === "Languages" ? 76 : 78
}'>Indigenous ${this.name} Inventory</a> (<a href='https://publiclab.org/notes/sagarpreet/06-06-2018/leaflet-environmental-layer-library?_=1528283515'>info<a>)</i>`
);
}
// options is an array
var options = [coords, 'blue', 'mario', "mario's desc"];
var ill_poly = (isNaN(coords[0][0][0]) || isNaN(coords[0][0][1])) ? undefined : getPoly(...options);
@ananyaarun You need to pass an array containing the required params needed to initialize the getPoly
method. The code above was only for reference, so you can copy the logic used, not the code itself, but if you need the exact code, you can use the snippet above (with obvious appropriate changes).
PS. I replaced the "L.polygon" and "bindPopup" methods with simple object notations since they weren't defined in the scope, but should work well once you place this inside your js file with all the appropriate scopes defined. Also, one can infer from the image above that the ternary seems to work fine, I hope you get the idea now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function getPoly(coords, clr, ...stringparams) {
return L.polygon(coords, {color: clr}).bindPopup(
`<strong>Name : </strong> ${stringparams[0]} <br>
<strong>Description: </strong> <a href=${stringparams[1]}>Native Lands - ${stringparams[0]} <a><br><i>From the <a href='https://github.com/publiclab/leaflet-environmental-layers/pull/${
this.name === "Territories" ? 77 : this.name === "Languages" ? 76 : 78
}'>Indigenous ${this.name} Inventory</a> (<a href='https://publiclab.org/notes/sagarpreet/06-06-2018/leaflet-environmental-layer-library?_=1528283515'>info<a>)</i>`
);
}
var array = [coords, clr, 'nme', "desc"];
var ill_poly = (isNaN(coords[0][0][0]) || isNaN(coords[0][0][1])) ? undefined : getPoly(...array);
So here is my console, I see that despite choosing the territories layer, the link has 78 (instead of 77). yes the ternary is working but this.name is not accessible here.
I see that it appears as blank on the link too.
I am trying to see what is going wrong
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ananyaarun this
is actually window
in the console, so this.name
translates to window.name
in the console. The snippet above will work, as I stated, once you place this inside your js file with all the appropriate scopes defined, but if you'd like to test it out in your console, you can this.name="Territorries"
(inside the console) or window.name = "Territories"
(inside your js file).
@@ -82,10 +82,10 @@ L.LayerGroup.IndigenousLayers = L.LayerGroup.extend( | |||
var ill_poly ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function getPoly(coords, clr, ...stringparams) {
return L.polygon(coords, {color: clr}).bindPopup(
`<strong>Name : </strong> ${stringparams.nme} <br>
<strong>Description: </strong> <a href=${stringparams.desc}>Native Lands - ${stringparams.nme}</a><br>
<i>From the <a href='https://github.com/publiclab/leaflet-environmental-layers/pull/${
this.name === "Territories" ? 77 : this.name === "Languages" ? 76 : 78
}'>Indigenous ${this.name} Inventory</a> (<a href='https://publiclab.org/notes/sagarpreet/06-06-2018/leaflet-environmental-layer-library?_=1528283515'>info<a>)</i>`
);
}
var ill_poly = (isNaN(coords[0][0][0]) || isNaN(coords[0][0][1])) ? undefined : getPoly(...options);
@ananyaarun Let me know you have any more doubts or so.
@sagarpreet-chadha @rexagod , I changed the order in the index.html file. Now the territories layer is called last and this is what my console looks like. There is no problem with the scope of name I think. I think passing url would be best,but I/m still not sure about the cors error. |
@ananyaarun Can you familiarize me a bit with the context and the specific problem you're facing right now? |
@rexagod , @sagarpreet-chadha I now got a way to remove the cors error.
This is happening because all are accessing the same function. |
Pasting this in from gitter:
|
Hi @ananyaarun -- i think what may be happening is that each time you use:
It is accessing the same object. So any value of "this" is shared among all uses. What we need is to create a /new/ object each time, so really it seems it has to be a constructor. This line should really be doing this:
But i think that it may be that the use of
what I often do is, within the constructor function, I just put almost all of the function definitions in there, defined in /only/ local scope, and then I assemble them into an instance object that I return from the constructor function. You can see this pattern here: https://github.com/publiclab/infragram/blob/main/src/color/colormaps.js What that does, is it controls the scope of ALL functions to a single shared scope that is only the one where they were defined -- inside the constructor function. And, only those functions which are going to be used externally are passed out in the returned object. All functions can refer to variables defined in the same scope (see this example) and we don't end up using Another question i had that maybe @sagarpreet-chadha could offer input on was why this is being defined as a LayerGroup, and not as a Layer, that is then grouped? It seems like perhaps we should be building in functionality to the Layer, not the LayerGroup? https://leafletjs.com/reference-1.5.0.html#layergroup But I could be wrong, just wondering! I hope this helps a bit -- also, what is precisely the value that's being overwritten -- is it |
Thanks a lot @jywarren !! Name is being overwritten not url. So actually url was initially being passed as one of the options but was not actually being used anywhere (this is not the actual API URL that needs to load)
Here is the discussion regarding that. So right now I am just passing the name of the layer (territories,languages, treaties) and there is an if else block to assign the API URL's. In this method the problem I discussed yesterday with you holds. As @rexagod suggested we need to find a way to not call the function from index.html while initializing layers. This might work too but I am still confused how this would render multiple layers accessing the same function. |
Hi @jywarren , I think when we started coding LEL it did not made any difference logically but yes now thinking it makes sense we should extend |
We can extend Layer class via these Minor changes : https://gist.github.com/sagarpreet-chadha/3c864160ed2ea087769e96627f231802 . |
@sagarpreet-chadha , except for making the syntax of defining the layers easy, will this change have any other effect on the scope of variables ?
|
Hey , i think i have found the problem . |
Okay so here is the problem :
Solution :Change
Here is the gist of working code : https://gist.github.com/sagarpreet-chadha/60b4cbfc1cb4fd27d4b1bf600e6d583a Thank you ! cc @ananyaarun , @jywarren , @rexagod |
Great catch @sagarpreet-chadha !! |
Glad we found it 馃槃 馃帀 ! I think we should write tests for this layer as well , i have setup the Jasmine testing environment here and written some tests as well , see this https://github.com/publiclab/leaflet-environmental-layers/pull/206/files . |
Yes definitely :) |
I think let's write tests parallely so that we will be more confident to merge PR by any new contributor . Thanks! |
I'm so glad you figured this out. Will this then be ready for merge now? Awesome teamwork! 馃帀 |
Yes I think this can be merged, and I shall write tests for this layer in another PR. |
Okay great +1 |
Fixes #198
@publiclab/reviewers
for help, in a comment belowThanks!