-
Notifications
You must be signed in to change notification settings - Fork 319
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
Tidy up and refactor the Character Count JavaScript #2807
Conversation
This abstraction isn't useful as the defaults can't be overridden anywhere, the two properties are only used once, and it means we need to jump around the code to understand what's going on.
We already read all of the `data-*` attributes into the `this.options` object, so use them instead of using `getAttribute`. This also avoids implying that `this.options.maxwords` and `$module.getAttribute('data-maxwords')` are different things – they just represent the same data attribute on the module in different ways. Avoid the indirection introduced by the countAttribute variable by setting this.maxLength conditionally based on whether `this.options.maxwords` is set. Merge with the return statement if neither maxwords nor maxlength are set.
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.
LGTM!
A few small formatting comments, but nothing blocking.
@@ -1,5 +1,5 @@ | |||
import '../../vendor/polyfills/Function/prototype/bind.mjs' | |||
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normaliziation | |||
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normalization |
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 could be super pedantic and say this should even be "normalisation"...
@@ -10,7 +10,9 @@ function CharacterCount ($module) { | |||
this.lastInputTimestamp = null | |||
} | |||
|
|||
// Initialize component | |||
/** | |||
* Initialise component |
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.
Indentation
* fields by directly changing its `value`. These changes don't trigger events | ||
* in JavaScript, so we need to poll to handle when and if they occur. | ||
* | ||
* Once the and a keyup event hasn't been detected for at least 1000 ms (1s), |
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.
* Once the and a keyup event hasn't been detected for at least 1000 ms (1s), | |
* Once the keyup event hasn't been detected for at least 1000 ms (1s), |
/** | ||
* Bind change events | ||
* | ||
* Set up events on the $textarea so that the counts update when the user |
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 makes it sound like internal counts are updating I think.
"... count messages update..."?
Also rename some functions to clarify what they actually do: - `formattedUpdateMessage` => `getCountMessage` - `checkIfValueChanged` => `updateIfValueChanged` Rename `element` to `$element` in the `getDataset` method to reflect the fact that it's an HTMLElement. Return directly from `count` function to avoid the unneccesary `length` variable.
Try to put the methods in a logical order so that methods that call each other are in close proximity and roughly in the order that they will be called.
1d9fa8b
to
67ef4a1
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.
Changes look good to me!
Tidying this up with the aim of making it easier to set config (e.g. maxwords, threshold) by passing a config object at initialisation in the future.
Suggest reviewing commit by commit as quite a lot of the code has been moved around.