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

Separate js from html #785 #1538

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

curibe
Copy link
Collaborator

@curibe curibe commented Jul 22, 2021

fixed #785

Separate JS codes from HTML

This PR basically separate a part of JS codes inside two files: base.html and 404_doi.html file to get a more readable HTML file. The JS code extracted was written inside the file scholia.js

Notes
In base.html

  • Not all JS code was extracted because it requires some jinja statements and variables

    image

  • 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 queries

    image

  • The 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

@curibe curibe changed the title Separate js from html #785 [WIP] Separate js from html #785 Jul 22, 2021
@curibe curibe changed the title [WIP] Separate js from html #785 Separate js from html #785 Jul 22, 2021
@carlinmack
Copy link
Collaborator

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 scholia.js. Thoughts?

@curibe
Copy link
Collaborator Author

curibe commented Jul 26, 2021

@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 %}

@carlinmack
Copy link
Collaborator

Hmm sorry just looking at scholia.js now, it seems that most of it is useful for working with Q-number pages? I thought it was more general. In that case it's probably better to go with this PR and then later on see if scholia.js can be dropped from the non-Q-number pages (like the home page).

@curibe curibe linked an issue Jul 29, 2021 that may be closed by this pull request
@curibe curibe force-pushed the separate-js-from-html-785 branch from e9fb46f to 1a1ed42 Compare August 4, 2021 19:02
@curibe curibe requested review from fnielsen and carlinmack August 5, 2021 20:06
Copy link
Collaborator

@carlinmack carlinmack left a 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 Show resolved Hide resolved
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\') }}"' +
Copy link
Collaborator

@carlinmack carlinmack Aug 7, 2021

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.

Copy link
Collaborator

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

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

image

Please, let me know what do you think about that.

scholia/app/static/scholia.js Outdated Show resolved Hide resolved
@curibe curibe requested a review from carlinmack August 9, 2021 19:37
@curibe curibe force-pushed the separate-js-from-html-785 branch from c95402d to e6ec48e Compare August 9, 2021 20:40
Comment on lines 125 to 129
// Add anchor links to all headings
var headers = document.querySelectorAll('h2[id], h3[id]')
if (headers) {
headers.forEach(element => {
var title = element.innerText;
Copy link
Collaborator Author

@curibe curibe Aug 9, 2021

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

Copy link
Collaborator

@carlinmack carlinmack Aug 17, 2021

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)

Copy link
Collaborator Author

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!

@carlinmack
Copy link
Collaborator

carlinmack commented Aug 18, 2021

Could you have function workWithQ(q, q2) {...} so that q2 can be accessed inside workWithQ? For #1552, I need to have consecutive ajax calls

$.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

@curibe
Copy link
Collaborator Author

curibe commented Aug 19, 2021

Could you have function workWithQ(q, q2) {...} so that q2 can be accessed inside workWithQ? For #1552, I need to have consecutive ajax calls

$.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.

curibe added 3 commits August 26, 2021 00:20
the js code is now implemented as a macro
Remove jinja expressions and pass some as arguments
@curibe curibe force-pushed the separate-js-from-html-785 branch from e6ec48e to 31bceea Compare August 26, 2021 05:25
Q2 was added as a argument of functions workWithQ,
wikiItemAsDifferentAspect,wikiItemSubpage in case of its value
could be required for later
@curibe
Copy link
Collaborator Author

curibe commented Aug 26, 2021

Could you have function workWithQ(q, q2) {...} so that q2 can be accessed inside workWithQ? For #1552, I need to have consecutive ajax calls

$.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 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 wikiItemAsDifferentAspect or wikiItemSubpage. In any case, I added Q2 to these three functions: 384affe

@fnielsen
Copy link
Collaborator

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.

@fnielsen fnielsen closed this Oct 25, 2021
@fnielsen
Copy link
Collaborator

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.

@fnielsen fnielsen reopened this Oct 25, 2021
@fnielsen fnielsen added the needs rebasing label for pull requests that are sent back for rebasing on the latest master (by author or other) label Oct 25, 2021
@egonw egonw self-assigned this Oct 30, 2021
@egonw
Copy link
Collaborator

egonw commented Oct 30, 2021

@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 :(

@egonw egonw assigned curibe and unassigned egonw Oct 30, 2021
@curibe
Copy link
Collaborator Author

curibe commented Nov 1, 2021

Hi @egonw and @fnielsen . Ok, I'm going to take a look at it. Probably there were several changes in the js code since the PR was created, which makes the conflicts appear. Especially because the PR extracts the code out of the html.

}

function wikiItemSubpage(query, q, q2, urlfor) {
var endpointUrl = 'https://query.wikidata.org/sparql';

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"

Suggested change
var endpointUrl = 'https://query.wikidata.org/sparql';
var endpointUrl = "https://query.wikidata.org/sparql";

strictlanguage: true
}
return query
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


var endpointUrl = 'https://query.wikidata.org/sparql';
settings = {
headers: { Accept: 'application/sparql-results+json' },

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"

Suggested change
headers: { Accept: 'application/sparql-results+json' },
headers: { Accept: "application/sparql-results+json" },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs rebasing label for pull requests that are sent back for rebasing on the latest master (by author or other)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JavaScript development setup
5 participants