-
Notifications
You must be signed in to change notification settings - Fork 194
Vanilla Staticman JS & HTML reorganization #245
Conversation
Haven't forgotten. Should have more free time in the coming weeks! |
Initial review looks great - all of your comments seem to be good and logical. I will start a full review this evening. |
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 assume that this is the portion that you want to redo to clean up that block of the JS.
hugo-future-imperfect-slim/layouts/_default/comments.html
Lines 53 to 57 in 21d42da
{{/* Start comment form alert messaging */}} | |
<div class='submit-notice'> | |
<strong class='submit-notice-text submit-success hidden'>{{ i18n "success_msg" }}</strong> | |
<strong class='submit-notice-text submit-failed hidden'>{{ i18n "error_msg" }}</strong> | |
</div> |
If this is true (and the only thing), then I don't feel that is necessary for this PR. Unless I am misunderstanding the problem, I don't see a way to reduce the redundancy. Either we now have language-specific CSS files (since i18n would have to take effect at build), or we are still stuck with both options in the HTML so it builds and can run i18n.
A possible solution to clean it up is using msg
from the if statement below to build the query selector.
hugo-future-imperfect-slim/assets/js/staticman.js
Lines 66 to 78 in 21d42da
function showAlert(msg) { | |
if (msg == 'success') { | |
form.querySelector('.submit-notice').classList.add('submit-success') | |
form.querySelector('.submit-success').classList.remove('hidden'); // show submit success message | |
form.querySelector('.submit-failed').classList.add('hidden'); // hide submit failed message | |
} else { | |
form.querySelector('.submit-notice').classList.add('submit-failed') | |
form.querySelector('.submit-success').classList.add('hidden'); // hide submit success message | |
form.querySelector('.submit-failed').classList.remove('hidden'); // show submit failed message | |
} | |
form.querySelector('input[type="submit"]:enabled').classList.remove('hidden'); // show "submit" | |
form.querySelector('input[type="submit"]:disabled').classList.add('hidden'); // hide "submitted" | |
} |
Something like
if (msg == 'success') {
var clear = 'failed' }
else {
var clear = 'success'}
form.querySelector(',submit-notice).classList.add('submit-'+msg);
form.querySelector(`submit-`+msg).classList.remove('hidden');
form.querySelector('submit-'+clear).classList.add(hidden);
Obviously not optimized, but a potential approach.
Looking at #178 again I came across fancyapps/fancybox#2581 - Looks like a Vanilla JS version of Fancybox should be ready soon. |
Agreed and thank you for your thoughts. The "critique" is just like the antithesis and synthesis combined. With the use of custom
The last two points would involve playing with the strings "success" and "failed" in JavaScript. That wouldn't simplify the code. More lines would be added—IMHO, my current "redundant" approach is more readable to amateurs. |
Description
HTML reorganization:
In my original PR for Staticman's nested comment support Staticman nested comments support from Huginn #69, I attempted to stored the post's
$threadID
with a hidden<span>
—that's not the HTML5 way. I wish I knew the use ofdata-
attributes earilier.hugo-future-imperfect-slim/layouts/post/staticman.html
Lines 72 to 83 in 118943a
Each comment is wrapped by an
<article>
tag with class namecomment
andid
received from the data file. Each comment reply is nested inside a main comment. I've added the classcomments-container
to the<div>
for JS event delegation (for details, see next point). I don't touch the commentedTODO
introduced in Reduce Theme Specificity #154 as I don't know much about the look yet—with the fixed JavaScript, the retrieval of reply target info should be OK.I changed the tag name surrounding the Hugo code for Disqus from
<article>
to<div>
.I observed that the styles are controlled by the
post
class in the CSS, so changing the tag name toarticle
should be no impact on the styles.The quotes for HTML syntax in
layout/_defaults/comments.html
are unified to single quotes. I left the double quotes in the Hugo code untouched.assets/js/staticman.js
in Vanilla JSjQuery syntax for HTML element selection
$.(...)
replaced bydocument.querySelector(...)
in most cases.jQuery method
ready()
replaced by self-executing JS function(function(){...})();
some other method translation in the same sense:
val()
,addClass()
, etc.event delegation: Vanilla JS's
addEventListner(evt, handler)
method only allows adding listener to one single HTML element at a time, while jQuery allows adding that to a class of HTML elements$('comments').on(evt, handler)
. To write readable and maintainable code, I favored adding a new CSS class "comments-container" inlayouts/_default/comments.html
rather than using JS code to navigate the DOM structure likeform.parentNode.nextSibling
, which can be easily broken in case that some siblings are inserted in between.hugo-future-imperfect-slim/assets/js/staticman.js
Lines 96 to 104 in 21d42da
jQuery method
ajax()
replaced with XHR.hugo-future-imperfect-slim/assets/js/staticman.js
Lines 35 to 49 in 21d42da
jQuery method
serialize()
for converting form data to URL encoded string replaced with JSON-friendly string.hugo-future-imperfect-slim/assets/js/staticman.js
Lines 17 to 32 in 21d42da
I've hard-coded numerical values
6
,7
and11
representing the length offields
,options
andreCaptcha][
respectively for substring extraction from form input fields' name likefields[email]
andoptions[reCaptcha][siteKey]
. The goal is to construct a JSON-friendly string like Understand MISSING_PARAMS eduardoboucas/staticman#412.replaced all instances of
var
tolet
to limit the scope of the variables to inside the function only: to avoid the following variables, espaciallyurl
andapi
, from overwriting variables in other files with the same name.hugo-future-imperfect-slim/assets/js/staticman.js
Lines 9 to 15 in 21d42da
Motivation and Context
It's motivated by the goal to replace jQuery with Vanilla in #231 (comment). This resolves #242 and resolves #243.
Screenshots (if appropriate):
[You can use
Windows+Shift+S
orControl+Command+Shift+4
to add a screenshot to your clipboard and then paste it here.]Example
Checklist:
theme.toml
, as applicable.Critique
The JS code is repetitive. From an OO point of view, directly changing the state of the HTML element should give more concise JS code, and the l10n of button texts can be stored in CSS files. I'm not sure if
{{ i18n ... }}
can be called inassets/css/*.css
. Even though that's possible, Hugo experts might prefer putting UI text in the Go-HTML templatelayout/**/*.html
.hugo-future-imperfect-slim/assets/js/staticman.js
Lines 66 to 78 in 21d42da