"Once" is not yet in core. See #2402103: Add once.js to core

This task comes from a code review in #73 in #3111409: Add new Olivero frontend theme to Drupal 9.1 core as beta

Olivero will be able to use the vanilla once script once it's in core, for now it's ok to leave it like that. It doesn't make sense to require jquery for such a small thing. the once feature is handled Drupal 6 style. good enough for now.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mherchel created an issue. See original summary.

mherchel’s picture

Title: Refactor JS Drupal behaviors to use vanilla version of .once() when available » Refactor Olivero's JavaScript Drupal behaviors to use vanilla version of .once() when available
Project: Olivero » Drupal core
Version: 8.x-1.x-dev » 9.1.x-dev
Component: Code » Olivero theme
Related issues: +#2402103: Add once.js to core
nod_’s picture

To do right after this one lands

nod_’s picture

Status: Postponed » Active

once js is in core now :)

nod_’s picture

Title: Refactor Olivero's JavaScript Drupal behaviors to use vanilla version of .once() when available » Refactor Olivero's JavaScript Drupal behaviors to use once()
mherchel’s picture

Version: 9.1.x-dev » 9.2.x-dev
Status: Active » Needs review
FileSize
7.74 KB

First pass at this is attached. Seems super easy to use!

nod_’s picture

Status: Needs review » Needs work
  1. +++ b/core/themes/olivero/js/messages.es6.js
    @@ -114,8 +112,9 @@
    +        context.querySelectorAll('.messages'),
    

    you can simplify with once('olivero-messages', '.messages', context) :)

  2. +++ b/core/themes/olivero/js/navigation.es6.js
    @@ -103,13 +103,13 @@
    +        context.getElementById(navWrapperId),
    

    same here: once('olivero-navigation', `#${navWrapperId}`, context)

  3. +++ b/core/themes/olivero/js/tabs.es6.js
    @@ -32,9 +32,11 @@
    +        context.querySelectorAll('[data-drupal-nav-tabs]'),
    

    same

  4. +++ b/core/themes/olivero/js/tabs.es6.js
    @@ -32,9 +32,11 @@
    +      tabs.forEach((el) => init(el));
    

    tabs.forEach(init) is sufficient

mherchel’s picture

Status: Needs work » Needs review
FileSize
3.32 KB
7.61 KB

Thanks for the quick review. Here's the changes requested.

nod_’s picture

Status: Needs review » Needs work
  1. +++ b/core/themes/olivero/js/messages.es6.js
    @@ -114,13 +112,11 @@
    +      const messages = once('olivero-messages', '.messages', context);
     
           messages.forEach((message) => {
             closeMessage(message);
           });
    

    Here you can simplify as well since the messages const is not used later:

    once('olivero-messages', '.messages', context).forEach(closeMessage);
    
  2. +++ b/core/themes/olivero/js/navigation.es6.js
    @@ -103,13 +103,14 @@
    +      const navWrapper = once(
    +        'olivero-navigation',
    +        `#${navWrapperId}`,
    +        context,
           );
    

    This one doesn't work right? Once always returns an array and because later you use navWrapper as a DOM element you can do either:

    // It's 'pretty' but babel adds a lot of code when using that.
    const [navWrapper] = once();
    // This works just as well and no extra babel code.
    const navWrapper = once().shift();
    
  3. +++ b/core/themes/olivero/js/tabs.es6.js
    @@ -32,9 +32,8 @@
    +      const tabs = once('olivero-tabs', '[data-drupal-nav-tabs]', context);
    +      tabs.forEach(init);
    

    Here as well the tabs const is not used later so it can be a little shorter:

    once('olivero-tabs', '[data-drupal-nav-tabs]', context).forEach(init);
    
droplet’s picture

It seems like a bug to me

In what case the `Drupal.theme.message` is called but no `closeMessage`?
https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/themes/olive...

If we have a special custom function without the `closeMessage`, should we inject it?

mherchel’s picture

Status: Needs work » Needs review
FileSize
7.6 KB
3.18 KB

Changes from review in #9 attached

nod_’s picture

#10 when a message is displayed by the backend (when saving a node for example) the message is on the page but Drupal.theme.message was not called to display it so we need the utility function (the naming is confusing though). Or did I miss something else?

Patch is good for me.

droplet’s picture

#10,

OK. What blocks us to do it on client-side only? (and must be client-side only?)
https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/themes/olive...

OK. So my best guess is the `click handler` and we don't want to repeat the code again on serverside.

EDIT:
and if I read the code correctly, #11 won't prevent an extra call on `closeMessage` (if we called it from clientside)

mglaman’s picture

  1. +++ b/core/themes/olivero/js/messages.es6.js
    @@ -3,7 +3,7 @@
    -((Drupal) => {
    
    @@ -28,8 +28,6 @@
         closeBtn.addEventListener('click', () => {
    
    +++ b/core/themes/olivero/js/messages.js
    @@ -63,10 +62,7 @@
    \ No newline at end of file
    ...
    \ No newline at end of file
    
    +++ b/core/themes/olivero/js/navigation.js
    @@ -98,4 +96,4 @@
    \ No newline at end of file
    ...
    \ No newline at end of file
    
    +++ b/core/themes/olivero/js/tabs.js
    @@ -37,9 +37,7 @@
    \ No newline at end of file
    ...
    \ No newline at end of file
    

    Uber nits

  2. +++ b/core/themes/olivero/js/messages.js
    @@ -31,7 +30,7 @@
         var messageWrapper = document.createElement('div');
    -    messageWrapper.setAttribute('class', "messages-list__item messages messages--".concat(type, " messages-processed"));
    +    messageWrapper.setAttribute('class', "messages-list__item messages messages--".concat(type));
    

    EDIT: I didn't realize this was the generated code. Review item is moot.

    The diff doesn't give much for clarity, but is there a reason to have concat here anymore, if it's just `type`? Would

    `messages-list__item messages messages--${type}` be more appropriate?

nod_’s picture

for 2. the patch is using that in the es6.js file, it's the compiled code that uses concat, so it's all good on that front :)

mglaman’s picture

d'oh! Thanks, @nod_. I tried to make sure I avoided reviewing the compiled code.

mherchel’s picture

Re-roll attached.

@mglaman you mention core/themes/olivero/js/messages.es6.js in #14, but I don't see anything wrong. Is there an issue there? If not, does this patch look good to you?

mglaman’s picture

Status: Needs review » Reviewed & tested by the community

Yeah, sorry. My review is moot. The compiled JS doesn't have a new line and my second thing was also compiled CSS. Lookin' great to me!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: 3173900-17-reroll.patch, failed testing. View results

benjifisher’s picture

Please do not ask the testbot to try again until #3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest is fixed.

kuldeep_mehra27’s picture

Status: Needs work » Needs review
FileSize
7.53 KB
Spokje’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
7.62 KB

Not quite sure what's going on with patch #21.

As @benjifisher explained in #20 the RTBC patch #17 was failing due to an error in HEAD.

#3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest was committed. Re-uploaded the original patch #17 so it will be retested on a 2 daily base, ordered retest and put this issue back to RTBC per #18.

@kuldeep_mehra27 Please provide an interdiff with updated patches.

  • lauriii committed 4814a98 on 9.2.x
    Issue #3173900 by mherchel, kuldeep_mehra27, Spokje, nod_, mglaman,...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 4814a98 and pushed to 9.2.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.