Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!12230
Resolve
#3526267
"ajax commands with htmx"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Resolve
#3526267
"ajax commands with htmx"
issue/drupal-3526267:3526267-htmx-onload-attribute
into
11.x
Overview
1
Commits
16
Pipelines
14
Changes
10
Open
Théodore Biadala
requested to merge
issue/drupal-3526267:3526267-htmx-onload-attribute
into
11.x
1 week ago
Overview
1
Commits
16
Pipelines
14
Changes
10
Expand
Closes
#3526267
0
0
Merge request reports
Compare
11.x
version 13
50662bf0
1 week ago
version 12
9b6d55fc
1 week ago
version 11
48120e16
1 week ago
version 10
2cb16450
1 week ago
version 9
6b0b2073
1 week ago
version 8
4675cd01
1 week ago
version 7
b2918b84
1 week ago
version 6
b6efd67c
1 week ago
version 5
5991cb8d
1 week ago
version 4
11442b4e
1 week ago
version 3
c0ac79b5
1 week ago
version 2
79fbff4b
1 week ago
version 1
2145e03e
1 week ago
11.x (HEAD)
and
latest version
latest version
0d44e87c
16 commits,
2 days ago
version 13
50662bf0
15 commits,
1 week ago
version 12
9b6d55fc
14 commits,
1 week ago
version 11
48120e16
13 commits,
1 week ago
version 10
2cb16450
12 commits,
1 week ago
version 9
6b0b2073
11 commits,
1 week ago
version 8
4675cd01
10 commits,
1 week ago
version 7
b2918b84
9 commits,
1 week ago
version 6
b6efd67c
8 commits,
1 week ago
version 5
5991cb8d
7 commits,
1 week ago
version 4
11442b4e
6 commits,
1 week ago
version 3
c0ac79b5
5 commits,
1 week ago
version 2
79fbff4b
4 commits,
1 week ago
version 1
2145e03e
3 commits,
1 week ago
10 files
+
364
−
118
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
10
Search (e.g. *.vue) (Ctrl+P)
core/misc/htmx/htmx-assets.js
+
19
−
89
Options
@@ -7,7 +7,7 @@
* page.
*/
(
function
(
Drupal
,
drupalSettings
,
loadjs
,
htmx
)
{
(
function
(
Drupal
,
drupalSettings
,
htmx
)
{
// Disable htmx loading of script tags since we're handling it.
htmx
.
config
.
allowScriptTags
=
false
;
@@ -21,47 +21,6 @@
*/
const
requestAssetsLoaded
=
new
WeakMap
();
/**
* Helper function to merge two objects recursively.
*
* @param current
* The object to receive the merged values.
* @param sources
* The objects to merge into current.
*
* @return object
* The merged object.
*
* @see https://youmightnotneedjquery.com/#deep_extend
*/
function
mergeSettings
(
current
,
...
sources
)
{
if
(
!
current
)
{
return
{};
}
sources
.
filter
((
obj
)
=>
Boolean
(
obj
))
.
forEach
((
obj
)
=>
{
Object
.
entries
(
obj
).
forEach
(([
key
,
value
])
=>
{
switch
(
Object
.
prototype
.
toString
.
call
(
value
))
{
case
'
[object Object]
'
:
current
[
key
]
=
current
[
key
]
||
{};
current
[
key
]
=
mergeSettings
(
current
[
key
],
value
);
break
;
case
'
[object Array]
'
:
current
[
key
]
=
mergeSettings
(
new
Array
(
value
.
length
),
value
);
break
;
default
:
current
[
key
]
=
value
;
}
});
});
return
current
;
}
/**
* Send the current ajax page state with each request.
*
@@ -94,6 +53,10 @@
// Custom event to detach behaviors.
htmx
.
trigger
(
detail
.
elt
,
'
htmx:drupal:unload
'
);
if
(
!
detail
.
xhr
)
{
return
;
}
// We need to parse the response to find all the assets to load.
// htmx cleans up too many things to be able to rely on their dom fragment.
let
responseHTML
=
Document
.
parseHTMLUnsafe
(
detail
.
serverResponse
);
@@ -104,71 +67,38 @@
'
:is(head, body) > script[type="application/json"][data-drupal-selector="drupal-settings-json"]
'
,
);
if
(
settingsElement
!==
null
)
{
mergeSettings
(
drupalSettings
,
JSON
.
parse
(
settingsElement
.
textContent
));
Drupal
.
htmx
.
mergeSettings
(
drupalSettings
,
JSON
.
parse
(
settingsElement
.
textContent
),
);
}
// Load all assets files. We sent ajax_page_state in the request so this is only the diff with the current page.
const
assetsTags
=
responseHTML
.
querySelectorAll
(
'
link[rel="stylesheet"][href], script[src]
'
,
);
const
bundleIds
=
Array
.
from
(
assetsTags
)
.
filter
(({
href
,
src
})
=>
!
loadjs
.
isDefined
(
href
??
src
))
.
map
(({
href
,
src
,
type
,
attributes
})
=>
{
const
bundleId
=
href
??
src
;
let
prefix
=
'
css!
'
;
if
(
src
)
{
prefix
=
type
===
'
module
'
?
'
module!
'
:
'
js!
'
;
}
loadjs
(
prefix
+
bundleId
,
bundleId
,
{
// JS files are loaded in order, so this needs to be false when 'src'
// is defined.
async
:
!
src
,
// Copy asset tag attributes to the new element.
before
(
path
,
element
)
{
// This allows all attributes to be added, like defer, async and
// crossorigin.
Object
.
values
(
attributes
).
forEach
((
attr
)
=>
{
element
.
setAttribute
(
attr
.
name
,
attr
.
value
);
});
},
});
return
bundleId
;
// Transform the data from the DOM into an ajax command like format.
const
data
=
Array
.
from
(
assetsTags
).
map
(({
attributes
})
=>
{
const
attrs
=
{};
Object
.
values
(
attributes
).
forEach
(({
name
,
value
})
=>
{
attrs
[
name
]
=
value
;
});
return
attrs
;
});
// Helps with memory management.
responseHTML
=
null
;
// Nothing to load, we resolve the promise right away.
let
assetsLoaded
=
Promise
.
resolve
();
// If there are assets to load, use loadjs to manage this process.
if
(
bundleIds
.
length
)
{
// Trigger the event once all the dependencies have loaded.
assetsLoaded
=
new
Promise
((
resolve
,
reject
)
=>
{
loadjs
.
ready
(
bundleIds
,
{
success
:
resolve
,
error
(
depsNotFound
)
{
const
message
=
Drupal
.
t
(
`The following files could not be loaded: @dependencies`
,
{
'
@dependencies
'
:
depsNotFound
.
join
(
'
,
'
)
},
);
reject
(
message
);
},
});
});
}
requestAssetsLoaded
.
set
(
detail
.
xhr
,
assetsLoaded
);
requestAssetsLoaded
.
set
(
detail
.
xhr
,
Drupal
.
htmx
.
addAssets
(
data
));
});
// Trigger the Drupal processing once all assets have been loaded.
// @see https://htmx.org/events/#htmx:afterSettle
htmx
.
on
(
'
htmx:afterSettle
'
,
({
detail
})
=>
{
requestAssetsLoaded
.
get
(
detail
.
xhr
).
then
(()
=>
{
(
requestAssetsLoaded
.
get
(
detail
.
xhr
)
||
Promise
.
resolve
())
.
then
(()
=>
{
htmx
.
trigger
(
detail
.
elt
,
'
htmx:drupal:load
'
);
// This should be automatic but don't wait for the garbage collector.
requestAssetsLoaded
.
delete
(
detail
.
xhr
);
});
});
})(
Drupal
,
drupalSettings
,
loadjs
,
htmx
);
})(
Drupal
,
drupalSettings
,
htmx
);
Loading