-
Notifications
You must be signed in to change notification settings - Fork 81
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
Separate js from html #785 #1538
base: master
Are you sure you want to change the base?
Conversation
Hi, I'm very in favour of this PR :) I would suggest however making workWithQ() into it's own file, and have: {% if q %}
<script src="{{ url_for('static', filename='workWithQ.js') }}"></script>
{% endif %} This would help reduce page size (#1502) as not all pages need the full |
@carlinmack something like this? : {% if q %}
<script src="{{ url_for('static', filename='workWithQ.js') }}"></script>
workWithQ("{{ q }}")
{% if q2 %}
// The same here
workWithQ2("{{ q2 }}")
{% endif %}
.
.
.
{% endif %} |
Hmm sorry just looking at |
e9fb46f
to
1a1ed42
Compare
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.
Will review the other changes once scholia.js is fixed :)
scholia/app/static/scholia.js
Outdated
try { | ||
var orcid = item.claims.P496[0].mainsnak.datavalue.value; | ||
detailsList.push( '<a href="https://orcid.org/"><img alt="' + | ||
'ORCID logo" src="{{ url_for(\'static\', filename=\'images/orcid_16x16.gif\') }}"' + |
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.
This might set this PR back awhile, but you can't use any {{ }}
code in the extracted JS. This is because only the HTML is rendered as a Jinja template, replacing these values before being executed in the browser.
I would suggest either dropping the template (as you can do here - replace it with "/static/images/orcid_16x16.gif") or in the case of dynamic stuff like {{ q }}
, make it a parameter to the function.
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.
PS, the above results in a GET http://127.0.0.1:8100/author/%7B%7B%20url_for('static',%20filename='images/orcid_16x16.gif')%20%7D%7D 404 (NOT FOUND)
error in the console
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.
This might set this PR back awhile, but you can't use any
{{ }}
code in the extracted JS. This is because only the HTML is rendered as a Jinja template, replacing these values before being executed in the browser.I would suggest either dropping the template (as you can do here - replace it with "/static/images/orcid_16x16.gif") or in the case of dynamic stuff like
{{ q }}
, make it a parameter to the function.
Yeah, you right. I think you first recommendation is better because the GIF file is hard-coded
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.
@carlinmack I used a hybrid between the two solutions in case you want to change the static path and also in the case you want to add more files from the static path
Please, let me know what do you think about that.
c95402d
to
e6ec48e
Compare
scholia/app/templates/base.html
Outdated
// Add anchor links to all headings | ||
var headers = document.querySelectorAll('h2[id], h3[id]') | ||
if (headers) { | ||
headers.forEach(element => { | ||
var title = element.innerText; |
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.
@carlinmack What do you want me to do with this code, leave it here or move it to the sholia.js file like the others?
Lines 125 to 134
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.
sorry for not getting back to you on this, I reckon we should try externalise all of the JS (including these lines)
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.
No problem. Ok, working on it!
Could you have $.ajax(endpointUrl, settings).then(function (data) {
.... stuff with q ...
}).then(function (data) {
if (q2) {
$.ajax(endpointUrl, settings).then(function (data) {
.... stuff with q2 ...
})
}
}) and therefore need to have access to q2 |
@carlinmack sure, no problem. I can do it. |
the js code is now implemented as a macro
Remove jinja expressions and pass some as arguments
e6ec48e
to
31bceea
Compare
Q2 was added as a argument of functions workWithQ, wikiItemAsDifferentAspect,wikiItemSubpage in case of its value could be required for later
@carlinmack I added the Q2 parameter to several functions in case of some of them could require it. This is because, according to the code above, it could be the functions |
Unfortunately there are now some issue that are had for me to resolve. I hope it would be possible to resolve this and add a new PR. I would like to see that we can get a good seperation in the code. |
I will reopen and add a "needs rebasing" instead of closing it. It is unclear how much mess I have made by delaying this PR. |
@curibe, I tried rebasing your 5 patches on top of the latest master, but there are more merge conflicts than I can resolve :( I can try harder, but it will take me a few hours, because it diverged quite a bit :( |
} | ||
|
||
function wikiItemSubpage(query, q, q2, urlfor) { | ||
var endpointUrl = 'https://query.wikidata.org/sparql'; |
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.
Codacy has a fix for the issue: Replace ·····var·endpointUrl·=·'https://query.wikidata.org/sparql'
with ··var·endpointUrl·=·"https://query.wikidata.org/sparql"
var endpointUrl = 'https://query.wikidata.org/sparql'; | |
var endpointUrl = "https://query.wikidata.org/sparql"; |
strictlanguage: true | ||
} | ||
return query | ||
}, |
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.
Codacy has a fix for the issue: Replace ························
with ············
}, | |
}, |
|
||
var endpointUrl = 'https://query.wikidata.org/sparql'; | ||
settings = { | ||
headers: { Accept: 'application/sparql-results+json' }, |
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.
Codacy has a fix for the issue: Replace ·······headers:·{·Accept:·'application/sparql-results+json'
with ····headers:·{·Accept:·"application/sparql-results+json"
headers: { Accept: 'application/sparql-results+json' }, | |
headers: { Accept: "application/sparql-results+json" }, |
fixed #785
Separate JS codes from HTML
This PR basically separate a part of JS codes inside two files:
base.html
and404_doi.html
file to get a more readable HTML file. The JS code extracted was written inside the filescholia.js
Notes
In
base.html
Not all JS code was extracted because it requires some jinja statements and variables
The jQuery statement
$(document).ready
is left inside HTML because It is required by the jinja's macros used to show the result of sparql queriesThe others JS code are related with the externalization of SPARQL queries, and it has been resolved in PR 1487 and PR 1517
In
404_doi.html