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

Replacing jQuery to Vanilla JavaScript #1388

Merged
merged 1 commit into from
Aug 4, 2022

Conversation

peregrineshahin
Copy link
Contributor

@peregrineshahin peregrineshahin commented Jul 27, 2022

Now that we don't need it in Bootstrap 5

@ppigazzini ppigazzini added enhancement server server side changes refactoring non functional code improvement labels Jul 27, 2022
@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 27, 2022

DEV updated.

image

application.js:

  const tasksCollapse = new bootstrap.Collapse(tasks, {
      toggle: true,
    });

@peregrineshahin
Copy link
Contributor Author

peregrineshahin commented Jul 28, 2022

Refactored all jQuery from the templates, and fixed bootstrap collapse instance,
I think this is ready to be tested again, EDIT: (not really)

@ppigazzini
Copy link
Collaborator

DEV updated.

$ git log --oneline -n 15
fc8bcb6 (HEAD -> master) Merge commit 'refs/pull/1388/head' of https://github.com/glinscott/fishtest
3646fd1 Merge branch 'task_duration_180' of https://github.com/ppigazzini/fishtest
1ea774e missing parentheses
2a32868 fix typo in document.head
b4831c7 Update tests_view.mak
3f8f595 Bootstrap 5 collapse instance
28217ad Update tests_run.mak
7d780a8 Update base.mak
b66085f Update actions.mak
0317aaa convert jQ to JS
917a17c Replace legacy `var` with ES6 `let` and `const`
d54aa7a Convert jQuery to JavaScript
423f4c1 (origin/master, origin/HEAD) Update forms Update events log form to bootstrap 5 Display the selected values after submitting in events log Make SPRT forms mobile friendly Update action buttons and forms in test view Update CSS forms for dark theme
7303459 Raise worker version to 175 (also server side)
e198edb Use ZipFile context manager

Not working:

  • all show/hide buttons (it works only for workers but after a browser refresh, not with a clean start)
  • diff missing in test page
  • theme switch
  • login "check_csrf_token(): Invalid token"

Solved:

  • SPSA graph

Working:

  • column content sorting
  • SPRT calc
  • liveElo graph (not tested yet the live updated)
  • show/hide default nets

@peregrineshahin
Copy link
Contributor Author

seems like one problem in the bootstrap collapse instance is making the script stop resulting is other problems,
and seems like I'm assigning a bootstrap instance to the divs target before they are loaded;

will have a look on how I can solve this

@ppigazzini
Copy link
Collaborator

Take your time, I know very little javascript so my help is limited to testing the PR.

@ppigazzini
Copy link
Collaborator

DEV updated.

@ppigazzini
Copy link
Collaborator

This throws error when not in homepage:

  const machinesButton = document.querySelector("#machines-button");
  machinesButton?.addEventListener("click", (e) => {

This throws error when in homepage:

  const tasksButton = document.querySelector("#tasks-button");
  tasksButton?.addEventListener("click", (e) => {

Commenting out the second one, and loading the homepage, the theme switch works fine.

@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 28, 2022

DEV updated, the collapse.js error in console seems disappeared in every page.

In a test page (eg https://dfts-0.pigazzini.it/tests/view/62e0ba678e4fa6ae4726947e) there are 2 SyntaxErrors:

image

@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 28, 2022

Login works now!
(use "user01"/"user01" to login as normal user and "user00"/"user00" as admin)
Error in the new test page.

image

@ppigazzini
Copy link
Collaborator

DEV updated, I was able to submit a new SPRT test.
However, I was not able to change the type of test (eg NumGames, SPSA) or TC.

@ppigazzini
Copy link
Collaborator

@ppigazzini
Copy link
Collaborator

DEV updated. In homepage the show/hide buttons are changing status, to load the table require a F5, though.

@peregrineshahin
Copy link
Contributor Author

I will try to fix that lastly as I'm thinking about using CSS classes and not using this bootstrap instance instead, will see,

there are still some errors I still see jQuery even in the last commit and there are a couple of syntax errors,

@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 28, 2022

I'm thinking about this PR as a marathon, not a 100m dash. There are so much things to be well tested before the merge. Take your time.

@ppigazzini ppigazzini added the wip work in progress label Jul 28, 2022
@ppigazzini
Copy link
Collaborator

DEV updated.
2 Uncaught SyntaxError in console for a test page.

@ppigazzini
Copy link
Collaborator

DEV updated.

@ppigazzini
Copy link
Collaborator

DEV updated

@ppigazzini
Copy link
Collaborator

Updated

@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 29, 2022

Updated and added by hand in tests.mak, tests_view.mak:

<%
from fishtest.util import get_cookie
%>

All show/hide buttons seems to work fine in a fast test.

EDIT_000: all the show/hide button (but the "workers" one) have the label reversed (eg "show" when the table is already shown)

@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 29, 2022

I'm a bit confused to why we even need this in here

The code for show/hide buttons was at first wrote in application.js, see the version from 18 months ago,
https://github.com/glinscott/fishtest/blame/27bb5700ae26d0e95c1d3d61dcb96078d2eaac5b/server/fishtest/static/js/application.js

Please mind that the function get_cookie() was added as workaround for a bug with chrome, see
fdee74e

@ppigazzini
Copy link
Collaborator

Updated.
In tests_view.mak the import of get_cookie can be simplified in this way:

<%
from fishtest.util import get_cookie, worker_name

if 'spsa' in run['args']:
  import json
  spsa_data = json.dumps(run["args"]["spsa"])
%>

@ppigazzini
Copy link
Collaborator

ppigazzini commented Jul 29, 2022

Pyramid works in this way.
In views.py we set the mako template to be used to render the page and the variables passed to that template, eg

https://github.com/glinscott/fishtest/blob/7a732ef837612cca4113540e4a4dfcdd8d2ff434/server/fishtest/views.py#L1294-L1325

Please mind the last lines, where we read a cookie to set a variable used in the mako template.

Here is some documentation about pyramid request, views etc.

https://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#request-module
https://docs.pylonsproject.org/projects/pyramid/en/latest/glossary.html#term-request
https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/views.html

Here is some doc about mako template (default template in Pyramid):
https://www.makotemplates.org/
https://docs.makotemplates.org/en/latest/

To inspect the variables content in a page simply add a field in the mako template:

<div>
  some_variable = ${some_variable} 
</div>

EDIT_000: in a mako template we can set a javascript variable using the python variable, eg:
https://github.com/glinscott/fishtest/blob/7a732ef837612cca4113540e4a4dfcdd8d2ff434/server/fishtest/templates/base.mak#L11-L13
https://github.com/glinscott/fishtest/blob/7a732ef837612cca4113540e4a4dfcdd8d2ff434/server/fishtest/templates/tests_view.mak#L13-L21

@ppigazzini
Copy link
Collaborator

DEV updated.

@dav1312
Copy link
Contributor

dav1312 commented Jul 29, 2022

Instead of using querySelector() for IDs why not just use getElementById() ?

@ppigazzini
Copy link
Collaborator

DEV updated.

@ppigazzini
Copy link
Collaborator

Updated:

  • non default nns cookie works fine
  • diff in test page is now broken Uncaught SyntaxError: Unexpected token '}' (at 62e1b66c8e4fa6ae4726bbfe:2247:3)

image

I don't recall someone reporting the rate limit message in the diff window (hard to be ignored because the long one line message pushes the "Actions", "Stats", "Time" fields in the far right). I triggered the rate limit because I created a very high number of new runs to test the vanilla js code.
If a proper solution is hard to be coded or is against the best practice, for me me is fine to revert 517999b

@dav1312
Copy link
Contributor

dav1312 commented Aug 2, 2022

What about checking if the user has reached its rate limit (or that it has enough left) before making a request? https://api.github.com/rate_limit

@ppigazzini
Copy link
Collaborator

What about checking if the user has reached its rate limit before making a request? https://api.github.com/rate_limit

Nice idea but IMO better to no add new features to this PR (BTW I started requesting new features so I'm to blame here :) )

@peregrineshahin
Copy link
Contributor Author

Please notice that the jQuery Ajax solution(master) also has the same problem of 403 (the rate limit problem)with diff box, I have been experiencing it since a very long time, when you refresh enough times on a particular test diff doesn't show anymore,
if you tried the same test on the master you will also get the same error.

@ppigazzini
Copy link
Collaborator

I think to have hit a bug that IMO already affects master.

I do know, I wrote it in my old post.

@peregrineshahin
Copy link
Contributor Author

I think this should make it equivalent to master
didn't notice at first that there are nested fetching in the code

@ppigazzini
Copy link
Collaborator

DEV Updated. Looks good to me!

PROD updated by hand to test the PR with the sharks :)

@peregrineshahin peregrineshahin changed the title Draft for replacing jQuery to JavaScript Replacing jQuery to Vanilla JavaScript Aug 2, 2022
@ppigazzini ppigazzini removed the wip work in progress label Aug 2, 2022
@ppigazzini
Copy link
Collaborator

ppigazzini commented Aug 2, 2022

I will run PROD with this PR for few days to collects feedbacks from the users.

@peregrineshahin please at your convenient time squash the commits or in one commit or in few logical commits, writing a/some final commit message/s (I use git rebase -i master to do the work, ymmv)

EDIT_000: we auto format the front end code with prettier, see #634 (comment):

npx prettier --write "server/fishtest/static/{css/*.css,html/*.html,js/*[!highlight.diff.min].js}"

I can do it during the merge, let me know.

@vdbergh
Copy link
Contributor

vdbergh commented Aug 2, 2022

Very substantial PR!

@ppigazzini
Copy link
Collaborator

DEV updated, I submitted 11 tests in a row using the back button, it seems to work (with the previous code I got a random true/false value for form_submitted when pushing the back button).
PROD updated by hand to have a bigger number of testers.

@peregrineshahin
Copy link
Contributor Author

Just for the record, I don't think you get random as garbage memory random before the fix, I think the randomity comes from if the back button triggered a full reload or rendered the cached page and that depends on how fast you click it - it seems (bc I'm still not able to reproduce it)-

please @ppigazzini feel free to format the PR with Prettier when merging, and I think I can squash the commits after learning how git commands, tonight or tomorrow

@ppigazzini
Copy link
Collaborator

ppigazzini commented Aug 3, 2022

@peregrineshahin squashing to only one commit it's very easy, change all the "pick" to "squash" but the first one.

git checkout jQ-to-JS
git checkout -b jQ-to-JS-backup
git checkout master
git fetch -p upstream
git reset --hard upstream master
git checkout jQ-to-JS
git rebase -i master
# change all the "pick" to "squash" but the commits that you want to keep, save and exit
# write the final commit messages for the commits, save and exit
# if a commit doesn't depend on previous ones, you can change the order moving the line
# you can rebase several times to make little refactoring of the commits
git push -f origin jQ-to-JS

@ppigazzini
Copy link
Collaborator

I test the code on PROD for a few more hours to collect the users feedback.

server/fishtest/templates/run_table.mak Outdated Show resolved Hide resolved
server/fishtest/templates/tests_run.mak Outdated Show resolved Hide resolved
server/fishtest/templates/tests_run.mak Outdated Show resolved Hide resolved
server/fishtest/templates/tests_run.mak Outdated Show resolved Hide resolved
Bootstrap v5.x has dropped the dependency from jQuery

this PR includes:
- converting all jQuery code to native JS including the abandonment of jQuery Cookie and jQuery Style functions
- using the bootstrap data attribute for toggling and move toggling inside its fitting mako templates and updating bootstrap to the latest version,

- use a proper naming convention and reformat old js code

The format was made manually (Copy & Paste) using Prettier v2.7.1 on their website https://prettier.io/playground/, using Babel option using  --print-width set to 80 (the default)
it formats ```.js``` files and only the <script> tags inside the Mako templates
@ppigazzini
Copy link
Collaborator

PROD and DEV updated.

@ppigazzini ppigazzini merged commit 911213f into official-stockfish:master Aug 4, 2022
@ppigazzini
Copy link
Collaborator

Merged, PROD updated, thank you @peregrineshahin :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement refactoring non functional code improvement server server side changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants