Problem/Motivation

Drupal.js mixes responsibilities: it provides the API, it initializes it and it contains the behaviors attachment/dettachment.

Let's split those for having clearer dependencies and providing a way to contrib for switching out dependencies on domready, mess with how things are initialized.

Proposed resolution

  • drupal.init.js performs initialization of the Drupal JS API when dom is ready and adds the js class needed for styling.
  • drupal.js contains the Drupal API.

Remaining tasks

Reviews + Manual testing

User interface changes

None.

API changes

Split JS, compatibility break with existing code only if sites had already mangled with Drupal core JS (potentially noone).

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Disruption Non disruptive, splitted JS files, but if you depended on the library nothing breaks. We can consider this an internal refactoring.
Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Manually test the patch Novice Instructions Yes - see #34
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

penyaskito’s picture

Status: Active » Needs review
Issue tags: +Needs manual testing
FileSize
14.72 KB

We need to add the dependency in several other places, but I want to get feedback on the direction.
Everything works now as long as the contextual module is active.

Wim Leers’s picture

Assigned: Unassigned » nod_

I have doubts about this.

Anything using Drupal.t() would have to declare a dependency. This would be very useful for locale module actually, to know which files contain translations.

Let's see what nod_ thinks.

Gábor Hojtsy’s picture

It would need to be Strings.t(), etc. then no? I know JS does not limit us to keep all Drupal.* in one library, but for consistency we are supposed to no?

penyaskito’s picture

If that's the case, as a big disruption this should be postponed to D9. I think it would be useful still to split, let's see what nod_ thinks.

nod_’s picture

Status: Needs review » Needs work

I'm behind the split. How things are split can be discussed a tiny bit more.

For the naming: we can't use Strings.XXX that's much too close to the JS string object, that would be confusing on top of adding a new global that would confuse the hell out of non drupal people. As for naming it's pretty much free for all. We have mobile.install.js next to nav-tabs.js and there is no official relations between how the file is named and what's in there.

Moving string functions is good, but we still have api related functions next to behaviors stuff: url, theme, encode path, those manipulate strings as well. What could be a good split is to move initialization-related code and function in their own file like a drupal.init.js and leave the rest in drupal.js and add a dependency on drupal.init. We don't have to mess all the other libs that way. As for splitting translation related function (and not just string related) that could be good as wim points out.

At the very least, this needs work because we use 2 space indent, not 4.

nod_’s picture

Assigned: nod_ » Unassigned
penyaskito’s picture

Status: Needs work » Needs review
FileSize
15.16 KB

Let's see what do you think about this:

I splitted drupal.js into drupal.init.js, drupal.domready.js and drupal.js:

  • drupal.init.js defines the Drupal object, and the attachBehaviors and detachBehaviors.
  • drupal.domready.js ensures that the behaviors are attached on domready (I think this deserves to be isolated).
  • drupal.js contains the rest.

Tested different scenarios manually and looks like I didn't break anything. I was forced to use weights, because the domready lib and drupal.js have that, but once they are removed the dependencies should resolve correctly.

Gábor Hojtsy’s picture

Title: Split string functions (Drupal.t, Drupal.checkPlain, Drupal.formatPlural...) from drupal.js » Split drupal.js
Issue tags: -D8MI, -sprint, -language-ui

Not sure this has something to do with D8MI at this point?

Wim Leers’s picture

Status: Needs review » Needs work

Thanks! Since that looks to be in accordance with the directives from our JS maintainer in #5: looking great :)

Still using 4-space indentation everywhere though.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
14.71 KB

Shame on me, fixed my IDE settings so it does not happen again.

penyaskito’s picture

Issue summary: View changes

Added beta evaluation and fixed proposed resolution with new scope. We will (or not) split the string/translation functions on separate issue.

Fabianx’s picture

+++ b/core/misc/drupal.js
@@ -1,16 +1,3 @@
-/**
- * Base framework for Drupal-specific JavaScript, behaviors, and settings.
- */

I think this should get a new @file header?

Jeff Burnz’s picture

Can we have a change notice please.

penyaskito’s picture

@nod_ wanted to have some time to think about this one. Further work should wait for a decision.

(here is when I try to assign it to nod_ but I don't have enough permissions)

Fabianx’s picture

Assigned: Unassigned » nod_

.

penyaskito’s picture

@nod_, could you share your concerns about this patch so we can try to move further?

nod_’s picture

Status: Needs review » Needs work

All right had some time to think about it.

I'm not comfortable with the proposed split because we're splitting things that are related with things that are not. Maybe it's because of the naming of the files that it's a little confusing.

Here is my though process: drupal.js contains the Drupal API (not just "the rest" :p) and this API needs to be initialized, hence it depends on a drupal.init.js file that comes after drupal.js. We don't have to add more libraries, we can just say that the core/drupal lib is actually 2 js files. That way contrib can switch out dependencies on domready, mess with how things are initialized and we don't have to rename a huge amount of things.

  1. we need to put window.Drupal = {behaviors: {}}; in drupal.js (can be inside the closure, no need to separate it).
  2. Attach/DetachBehavior-related function should be back in drupal.js, it's part of the Drupal API after all.
  3. content of drupal.domready.js should go in drupal.init.js, that init file would essentially contain the domready stuff, the js class stuff and the jQuery no conflict stuff.

Just one little comment:

+++ b/core/misc/drupal.js
@@ -1,16 +1,3 @@
 (function (domready, Drupal, drupalSettings, drupalTranslations) {

don't need domready here anymore.

nod_’s picture

Assigned: nod_ » Unassigned
nod_’s picture

Issue tags: +Novice

Things should be clear in #17

penyaskito’s picture

Status: Needs work » Needs review
FileSize
2.04 KB

Started from scratch looking for the indications at #17.
Manually tested frontpage, quickedit, views and tours. No problems introduced.

I didn't change header files, will appreciate suggestions if we need to.

penyaskito’s picture

Status: Needs review » Needs work

@nod_'s feedback on IRC:

19:17 < nod_> penyaskito: so what I would do is put the js class stuff in the init
19:19 < nod_> penyaskito: and maybe add a weight to the drupal.init file, -17. because I'd rather keep that init file close to the drupal.js one
19:20 < nod_> penyaskito: otherwise it's cool :)

penyaskito’s picture

Status: Needs work » Needs review
FileSize
2.23 KB
1.34 KB

Fixed feedback from #21.
I found that the js class was being added twice. An anonymous closure fixed the issue.

The last submitted patch, 20: core-js-lib-diet-1945262-20.patch, failed testing.

YesCT’s picture

per request from @penyaskito, canceled #20 since head had been failing, so the test was postponed, and there is a new one on this issue anyway.

penyaskito’s picture

Issue summary: View changes

Updated issue summary after new point of view.
Added novice contributor tasks for "Needs manual testing"

penyaskito’s picture

FileSize
2.19 KB
521 bytes

Per @nod_ on IRC: #22 was wrong, the other js was added by modernizr and its fine.
So new patch, this should be good to go.

nod_’s picture

I apologize about the "diet" patch name, it was a poor attempt at wit. https://the-pastry-box-project.net/marc-drummond/2015-december-21

droplet’s picture

It's over-optimizing. We can introduce a global var to on/off the domready init.

droplet’s picture

Version: 8.0.x-dev » 8.1.x-dev
Related issues: +#2484623: Move all JS in modules to a js/ folder

whatever, can't do it 8.0.x :)

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

MobliMic’s picture

Issue tags: +Dublin2016

Tried to test on simplytest.me against 8.3.x and patch failed. Will reroll

MobliMic’s picture

Adding rerolled patch for testing

Merge conflict in drupal.js

MobliMic’s picture

Tested on simplytest.me. Patch applied correctly.

Manually clicked around the install. Everything seems to load correctly and the attachBehaviors function is firing correctly. Checked that ajax on tags field was working and quick edit for an article. Ordering of files appears to be correct when combined.

Chrome and IE11

MobliMic’s picture

Issue summary: View changes

Updating tasks table in issue summary

nod_’s picture

Status: Needs review » Reviewed & tested by the community

Little nitpick,

drupal/core/misc/drupal.init.js
  14:3  error  Strings must use singlequote  quotes

✖ 1 problem (1 error, 0 warnings)

Can be fixed during commit I guess

catch’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs change record

This looks fine to me for 8.3.x, but needs a change record on the unlikely chance that someone is using library_override.

Also we should clarify the bc policy for location of js files - I think saying libraries_override is @internal would be fine, but don't think we say that yet.

penyaskito’s picture

Uploading reroll for #36.

I don't get how we should say that libraries_override is @internal, as it's actually an extension API that we don't have a replacement for AFAIK.
It's actually documented at https://www.drupal.org/node/2497313.

If only the change record is needed, I can do that. Just need to be aware if we should do anything with libraries_override.

penyaskito’s picture

Created a draft change record: https://www.drupal.org/node/2830647

nod_’s picture

Status: Needs review » Reviewed & tested by the community

That looks good to me. Thanks!

Everything still works.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: split_drupal_js-2422017-38.patch, failed testing.

nod_’s picture

Status: Needs work » Reviewed & tested by the community

testbot fail

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: split_drupal_js-2422017-38.patch, failed testing.

nod_’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs manual testing, -Needs change record

Another random failure

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: split_drupal_js-2422017-38.patch, failed testing.

The last submitted patch, 38: split_drupal_js-2422017-38.patch, failed testing.

nod_’s picture

Status: Needs work » Reviewed & tested by the community
catch’s picture

@penyaskito libraries_override itself isn't @internal - we're not going to remove support for it in 8.x

However we can't guarantee that a specific file in a library is going to be override-able in the same way for the entirety of the 8.x cycle (especially not if we commit patches like this).

This is in the same way that hook_form_alter() is an API, but we specifically say the form array passed to the hook can and will change during the cycle.

penyaskito’s picture

@catch Thanks for the clarification! I reviewed again that the change record draft is clear about this.

  • catch committed 765c10b on 8.3.x
    Issue #2422017 by penyaskito, MobliMic, nod_: Split drupal.js
    
catch’s picture

Committed/pushed to 8.3.x, thanks!

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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