Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Part of the JavaScript selectors clean-up effort.
#1574470: Selectors clean-up
#1415788: Javascript winter clean-up
Comment | File | Size | Author |
---|---|---|---|
#45 | 1751308-45.patch | 14.23 KB | Kiphaas7 |
#45 | interdiff.txt | 5.66 KB | Kiphaas7 |
#40 | 1751308-40.patch | 14.68 KB | Kiphaas7 |
#40 | interdiff.txt | 3.96 KB | Kiphaas7 |
#38 | 1751308-38.patch | 14.21 KB | Kiphaas7 |
Comments
Comment #1
nod_No big issues with the selectors, some can use some help and get rid of sizzle-specific selectors.
More importantly, this needs some work to make this more readable.
Comment #2
Kiphaas7 CreditAttribution: Kiphaas7 commentedclaiming this one for now.
Comment #3
Kiphaas7 CreditAttribution: Kiphaas7 commentedInitial stab at it. Still 1 @todo left, comments should be improved, and the code needs some serious reviewing :).
Comment #4
Kiphaas7 CreditAttribution: Kiphaas7 commentedUnassigning for the moment. Might get back to it as I find time for it.
Comment #5
Kiphaas7 CreditAttribution: Kiphaas7 commentedReviewing my own patch:
Should be without trailing ','.
Leaving at 'needs review' for testbot.
Comment #6
Kiphaas7 CreditAttribution: Kiphaas7 commentedgrmbl. Stupid status switch. Might as well update the patch then. Ran it through JSHint, and fixed some errors/warnings stemming from that.
Comment #7
Kiphaas7 CreditAttribution: Kiphaas7 commentedGRMBL. Trailing space.
Comment #8
Kiphaas7 CreditAttribution: Kiphaas7 commentedCouple of pointers regarding the patch:
Comment #9
Kiphaas7 CreditAttribution: Kiphaas7 commentedAddendum to #8:
Patch basically needs to be extensively tested for:
Comment #10
Kiphaas7 CreditAttribution: Kiphaas7 commentedThis is similar to the old code, but a possible improvement would be only updating the selectAll if it doesn't have the
state
it should have. Could be done by simply keeping track of the selectAll state in the object.Something like this:
constructor:
createSelectAll:
updateSelectAll:
Comment #11
Kiphaas7 CreditAttribution: Kiphaas7 commentedGoing to fix the @todo and add the comment in #10 somewhere today. Also going to see if I can make the initial creation and state of the tableselect more robust (ie for the edgecase where all checkboxes are already checked at pageload).
Comment #12
Kiphaas7 CreditAttribution: Kiphaas7 commentedComment #13
Kiphaas7 CreditAttribution: Kiphaas7 commentedWhitespace issues again. Lucky number 13!
Comment #14
Kiphaas7 CreditAttribution: Kiphaas7 commentedBy the way: I realize that it looks like there are over 4 times as much lines added than removed; after minification this will change to a little less than 2x the number of characters (1500 old minified file vs ~2900 characters new minified file). I'd call the code increase due to having a bit more explicite coding style, more comments, ability to handle multiple tableselects on the same page, and a detach event.
Comment #15
Kiphaas7 CreditAttribution: Kiphaas7 commented+ if ($selectAll.length) {
+ if ($table.find('td input:checkbox').length !== 0) {
+ if ($stickyTable.length === 1) {
Is there a convention how to check for existence of jQuery elements? I'm using
if($foo.length)
andif($foo.length !== 0)
, what should be used?Here, and in some other places, I choose to use an
if/else
structure. Is the shorthandvar a = b ? c : d;
allowed or even recommended?Comment #17
Kiphaas7 CreditAttribution: Kiphaas7 commentedAnd would it be more straight forward to use
e.target
orthis
? I'm leaning towardsthis
, since this seems to be suggested by the jQuery docs.Comment #18
seutje CreditAttribution: seutje commentedif ($foo.length) { }
if (!$foo.length) { }
suffices for me, but I don't think there's an agreed-on standard right now, perhaps we should look into creating one.
jQuery uses this internally when checking for the presence of an array value, which is essentially what we're doing, and it is a pretty widely used standard within the JavaScript/jQuery community.
Where the use of a ternary can easily lead to confusion, a full
if
statement with curly braces is recommended.In the case where you're storing exactly the value of your condition I would be fine with doing it directly, but with braces around the condition, like:
a = (b === c);
Constructs like the following seem overly verbose to me, but again, there's no standard set in stone or anything and they might help readability for some people.
Comment #19
Kiphaas7 CreditAttribution: Kiphaas7 commentedWorks for me!
Comment #20
Kiphaas7 CreditAttribution: Kiphaas7 commentedInterdiff against version 19. Actually forgot to remove the item from cache in detach.
Comment #21
Kiphaas7 CreditAttribution: Kiphaas7 commentedheckbox -> checkbox
Haven't tested the detach code _at all_.
Comment #22
Kiphaas7 CreditAttribution: Kiphaas7 commentedCould probably be
Probably has to be changed to
TableSelect.tables.splice(i, 1);
, and has to be moved outside the loop: removing an item like this forces the array to be reindexed, which makes the i parameter unreliable at best. The only way I see it (while still supporting IE8, which does not supportindexOf
orfilter
):Setting to needs work for #21 and this comment.
Comment #23
Kiphaas7 CreditAttribution: Kiphaas7 commentedComment #25
Kiphaas7 CreditAttribution: Kiphaas7 commentedDamm you whitespace!
Comment #27
Kiphaas7 CreditAttribution: Kiphaas7 commented#25: 1751308-24.patch queued for re-testing.
Comment #29
seutje CreditAttribution: seutje commentedshould be
But that's not why the test is failing.
Comment #30
Kiphaas7 CreditAttribution: Kiphaas7 commentedI thought I copied that from tableheader.js.
Yes I did: http://drupalcode.org/project/drupal.git/blob/refs/heads/8.x:/core/misc/...
So I'll fix it there as well in the next patch.
Comment #31
Kiphaas7 CreditAttribution: Kiphaas7 commented#25: 1751308-24.patch queued for re-testing.
Comment #32
Kiphaas7 CreditAttribution: Kiphaas7 commentedI added some basic *created/*removed events in this patch. I think it would be incredibly helpful if these events had extra parameters containing the index of the instance and a reference to the instance. The second parameter is easy to add, the first might take some thinking if it is _really_ necessary.
Testbot came back green, setting back to needs work for #29/#30 and the above comment.
Comment #33
Kiphaas7 CreditAttribution: Kiphaas7 commentedComment #34
seutje CreditAttribution: seutje commentedSorry, I was wrong, apparently
}(jQuery, Drupal));
was the agreed on structure.Comment #35
Kiphaas7 CreditAttribution: Kiphaas7 commentedinterdiff vs 25
Comment #36
Kiphaas7 CreditAttribution: Kiphaas7 commented.removeOnce()
should be used instead of manually removing the class added by.once()
.Comment #37
Kiphaas7 CreditAttribution: Kiphaas7 commentedWhile fixing #36, I thought of an obscure bug: tableselect and tableheader are applied: no biggie, it works. tableselect gets detached and attached while tableheader is left alone: stickyTableCreated will not fire. Plus, the checkbox from the cloned header is removed.
Tried to fix it, but it took more lines than I'm comfortable with :/. Any thoughts/ideas how to fix this more elegantly are more than welcome.
Comment #38
Kiphaas7 CreditAttribution: Kiphaas7 commentedIgnore the attachments of #37. Forgot to commit on my dev branch.
Comment #39
seutje CreditAttribution: seutje commentedI'm not too keen on having "on" in handler names, and would prefer not having sizzle-specific selectors, but other than that, I don't have any real objections.
Comment #40
Kiphaas7 CreditAttribution: Kiphaas7 commentedFixed both. I think.
Comment #41
Kiphaas7 CreditAttribution: Kiphaas7 commentedSo basically, I think this is ready....
But: I'd love some input from either seutje of nod_ (or anyone else who wants to weigh in) on how to handle the sticky table/tableselect interaction nicely.
At this point, there is slightly awkward code to keep the sticky table in sync. Because the following situations can occur:
Comment #42
Kiphaas7 CreditAttribution: Kiphaas7 commentedBAH. I need to stop being sloppy.
Should they start with lowercase because they are event names, or uppercase because they reference to an instance of a constructor?
Comment #43
seutje CreditAttribution: seutje commentedSorry for being a bit unclear, by "not having sizzle-specific selectors", I meant changing
:checkbox
to[type=checkbox]
, which speeds up things on qSA-capable browsers (which is all we support anyway) (perf case -> http://jsperf.com/sizzle-vs-qsa ). The loop-check you changed it too seems a bit awkward and error-prone (it bails as soon as it detects an input that isn't a checkbox?)On the issue raised in #42: I would say lowercase, as they are custom event names and don't really reference the constructor instances.
Haven't yet properly looked into the issue raised in #41 though
Comment #44
Kiphaas7 CreditAttribution: Kiphaas7 commentedSeutje, will change as soon as I have some time. Thanks for the review.
Comment #45
Kiphaas7 CreditAttribution: Kiphaas7 commentedNeeds testing, think I fixed it correctly. #41 still stands.
Comment #46
nod_tag
Comment #47
nod_#45: 1751308-45.patch queued for re-testing.
Comment #49
pixelmord CreditAttribution: pixelmord at Wunder commentedHere is a related issue that I created, so please consider including this here in the refactored code as well:
https://www.drupal.org/node/2604912
Comment #56
huzooka