Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #288 from fourkitchens/attach_library-fixes
Browse files Browse the repository at this point in the history
move drupal js loading to head, reload scripts
  • Loading branch information
Evan Willhite authored Dec 4, 2018
2 parents 51ec226 + c99f4b7 commit 1c39e48
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
12 changes: 11 additions & 1 deletion components/_meta/_00-head.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
<title>{{ title }}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width" />

<!-- Component styles -->
<link rel="stylesheet" href="../../../../dist/style.css?{{ cacheBuster }}" media="all" />

<!-- Drupal-specific usage -->
<!-- example using symlinking -->
<!-- cd web/themes/custom/emulsify/components/js -->
<!-- ln -s ../../../../../core/assets/vendor/domready/ready.min.js ./ -->
<!-- ln -s ../../../../../core/misc/drupal.js ./ -->

<!-- UNCOMMENT AS NECESSARY -->
<!-- <script src="../../js/ready.min.js"></script> -->
<!-- <script src="../../js/drupal.js"></script> -->

<!-- Begin Pattern Lab (Required for Pattern Lab to run properly) -->
{{ patternLabHead | raw }}
<!-- End Pattern Lab -->
Expand Down
14 changes: 2 additions & 12 deletions components/_meta/_01-foot.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@
<!--DO NOT REMOVE-->
{{ patternLabFoot | raw }}

<!-- Drupal-specific usage -->
<!-- example using symlinking -->
<!-- cd web/themes/custom/emulsify/components/js -->
<!-- ln -s ../../../../../core/assets/vendor/domready/ready.min.js ./ -->
<!-- ln -s ../../../../../core/misc/drupal.js ./ -->

<!-- UNCOMMENT AS NECESSARY -->
<!-- <script src="../../js/ready.min.js"></script> -->
<!-- <script src="../../js/drupal.js"></script> -->

<!-- If using Drupal.behaviors, uncomment for them to work in Pattern Lab -->
<!-- <script>Drupal.attachBehaviors();</script> -->
<!-- This makes attach_library work wherever it is in the document flow. -->
<script src="../../js/reload-scripts.js"></script>

</body>
</html>
44 changes: 44 additions & 0 deletions components/js/reload-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Reload Scripts on page (required for attach_library)
*
*/
const d = document;
// Get body tag.
const head = d.getElementsByTagName('body')[0];
// Get scripts within the body.
const scripts = head.querySelectorAll('script');
const scriptsArray = [];

function async(src, callback) {
const script = document.createElement('script');
script.src = src;
if (callback) {
script.addEventListener(
"load",
e => {
callback(null, e);
},
false
);
}
head.appendChild(script);
}

scripts.forEach(element => {
// If the script has the data-name attribute.
if (element.dataset.name) {
const scriptSrc = element.dataset.src;
if (!scriptsArray.includes(scriptSrc)) {
async(scriptSrc, () => {
if (typeof Drupal === 'object' && typeof Drupal.attachBehaviors === 'function') {
const behaviors = Object.values(Drupal.behaviors);
// Only run attachBehaviors once.
behaviors.forEach(behavior => {
Drupal.attachBehaviors();
});
}
});
scriptsArray.push(scriptSrc);
}
}
});

0 comments on commit 1c39e48

Please sign in to comment.