Skip to content

Commit

Permalink
WIP: layer to extend the Magewire object (#133)
Browse files Browse the repository at this point in the history
Co-authored-by: Willem Poortman <[email protected]>
  • Loading branch information
wpoortman and Willem Poortman authored Oct 25, 2023
1 parent c2c1fa6 commit 8bd856a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/view/frontend/layout/default_hyva.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@
</argument>
</arguments>

<container name="magewire.directive.scripts" as="magewire.directives">
<block name="magewire.directive.select"
template="Magewirephp_Magewire::page/js/magewire/directive/select.phtml"/>
</container>

<container name="magewire.plugin.scripts" as="magewire.plugins">
<!-- Inject the Magewire core Loader plugin -->
<block name="magewire.plugin.loader"
Expand Down Expand Up @@ -121,6 +116,16 @@
</arguments>
</block>
</container>

<container name="magewire.directive.scripts" as="magewire.directives">
<block name="magewire.directive.select"
template="Magewirephp_Magewire::page/js/magewire/directive/select.phtml"/>
</container>

<container name="magewire.extend.scripts" as="magewire.extends">
<block name="magewire.extend.plugin"
template="Magewirephp_Magewire::page/js/magewire/extend/plugin.phtml"/>
</container>
</block>
</referenceContainer>

Expand Down
9 changes: 7 additions & 2 deletions src/view/frontend/templates/page/js/magewire-initialize.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ $magewireScripts = $block->getViewModel();

<?php /* Custom plugin registration. */ ?>
Magewire.plugins = {}
<?php /* Allow component.initialize hooks to be registered before the components are initialized. */ ?>
Magewire.dispatchEvent('available')
</script>

<?php /* Include Magewire plugin scripts. */ ?>
<?= /* @noEscape */ $block->getChildHtml('magewire.plugins') ?>
<?php /* Include a space specified to extend the Magewire object with addition methods. */ ?>
<?= /* @noEscape */ $block->getChildHtml('magewire.extends') ?>

<script>
Magewire.dispatchEvent('available')
</script>

<?php /* Include custom Magewire directives. */ ?>
<?= /* @noEscape */ $block->getChildHtml('magewire.directives') ?>
<?php endif ?>
15 changes: 15 additions & 0 deletions src/view/frontend/templates/page/js/magewire/extend/plugin.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
'use strict';

Magewire.hasPlugin = function(name) {
const plugins = Magewire.plugins

if (typeof plugins !== 'object' || plugins.length === 0) {
return false;
} else if (typeof name === 'object') {
return name.every((name) => Magewire.hasPlugin(name))
}

return name in plugins
}
</script>

0 comments on commit 8bd856a

Please sign in to comment.