Skip to content

Commit

Permalink
Update: Split separate types for rendering
Browse files Browse the repository at this point in the history
This enables a cache entry for plain embed
  • Loading branch information
jonnitto committed May 24, 2023
1 parent 53c7489 commit 6137d00
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
29 changes: 0 additions & 29 deletions Resources/Private/Fusion/Component/Form.fusion

This file was deleted.

23 changes: 23 additions & 0 deletions Resources/Private/Fusion/Component/Form/Form.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
prototype(Garagist.Mautic:Component.Form) < prototype(Neos.Fusion:Component) {
@propTypes {
id = ${PropTypes.integer}
url = ${PropTypes.string}
apiUrl = ${PropTypes.string}
embedType = ${PropTypes.oneOf(['javascript', 'iframe', 'plain'])}
iframeClass = ${PropTypes.anyOf( PropTypes.string, PropTypes.arrayOf( PropTypes.string ) )}
waitMessage = ${PropTypes.string}
}

id = ${q(node).property('mauticFormId')}
url = ${Configuration.Setting('Garagist.Mautic.publicUrl')}
apiUrl = ${Configuration.Setting('Garagist.Mautic.api.baseUrl') || this.url}
embedType = ${Configuration.Setting('Garagist.Mautic.embedType')}
iframeClass = null
replacements = Neos.Fusion:DataStructure

renderer = afx`
<Garagist.Mautic:Component.Form.Fragment.Javascript @if={props.embedType == 'javascript'} {...props} />
<Garagist.Mautic:Component.Form.Fragment.Iframe @if={props.embedType == 'iframe'} {...props} />
<Garagist.Mautic:Component.Form.Fragment.Plain @if={props.embedType == 'plain'} {...props} />
`
}
17 changes: 17 additions & 0 deletions Resources/Private/Fusion/Component/Form/Fragment/Iframe.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
prototype(Garagist.Mautic:Component.Form.Fragment.Iframe) < prototype(Neos.Fusion:Component) {
@propTypes {
id = ${PropTypes.integer}
url = ${PropTypes.string}
class = ${PropTypes.anyOf( PropTypes.string, PropTypes.arrayOf( PropTypes.string ) )}
}

id = null
url = null
class = ${this.iframeClass}

@if.hasUrl_Id = ${this.url && this.id}

renderer = afx`
<iframe src={props.url + "/form/" + props.id} class={props.class}></iframe>
`
}
15 changes: 15 additions & 0 deletions Resources/Private/Fusion/Component/Form/Fragment/Javascript.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
prototype(Garagist.Mautic:Component.Form.Fragment.Javascript) < prototype(Neos.Fusion:Component) {
@propTypes {
id = ${PropTypes.integer}
url = ${PropTypes.string}
}

id = null
url = null

@if.hasUrl_Id = ${this.url && this.id}

renderer = afx`
<script type="text/javascript" src={props.url + "/form/generate.js?id=" + props.id} defer></script>
`
}
51 changes: 51 additions & 0 deletions Resources/Private/Fusion/Component/Form/Fragment/Plain.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
prototype(Garagist.Mautic:Component.Form.Fragment.Plain) < prototype(Neos.Fusion:Component) {
@propTypes {
id = ${PropTypes.integer}
url = ${PropTypes.string}
apiUrl = ${PropTypes.string}
waitMessage = ${PropTypes.string}
}

waitMessage = ${Translation.translate('pleaseWait', 'Please wait…', [], 'Main', 'Garagist.Mautic')}

id = null
url = null
apiUrl = null
removeStyles = true

@if.hasUrl_Id_ApiUrl = ${this.url && this.id && this.apiUrl}

_javascript = ${this.url + '/media/js/mautic-form.js'}
_embededFile = ${File.readFile(this.apiUrl + "/form/embed/" + this.id)}
[email protected] = ${this.removeStyles && value ? String.pregReplace(value, '~<style([.\s\S]*?)</style>~', ''): value}
_hasReplacements = ${Carbon.Array.check(this.replacements)}
_globalVariables = ${'window.MauticDomain="' + this.url + '";window.MauticLang={submittingMessage:"' + this.waitMessage + '"}'}

renderer = afx`
<script data-slipstream>{props._globalVariables}</script>
<script src={props._javascript} data-slipstream defer onload="MauticSDK.onLoad()"></script>
{props._hasReplacements ? '' : props._embededFile}
<Neos.Fusion:Reduce
@if={props._hasReplacements}
items={props.replacements}
initialValue={props._embededFile}
itemReducer={String.replace(carry, item.search, item.replace)}
/>
`

@context.cacheEntryIdentifier = ${'MauticFormPlain'+ this.url + this.id + this.apiUrl}

@cache {
mode = 'cached'
// Maximum lifetime of this cache entry in seconds. Defaults to 300 seconds (5 minutes)
maximumLifetime = 300
entryIdentifier {
node = ${node}
cacheEntryIdentifier = ${cacheEntryIdentifier}
}
entryTags {
1 = ${Neos.Caching.nodeTag(node)}
2 = 'Garagist_Mautic_Form'
}
}
}

0 comments on commit 6137d00

Please sign in to comment.