Skip to content

Instantly share code, notes, and snippets.

@markdaugherty
Created September 17, 2018 16:26
Show Gist options
  • Save markdaugherty/7bb313a1de64a33f7a5591902e895197 to your computer and use it in GitHub Desktop.
Save markdaugherty/7bb313a1de64a33f7a5591902e895197 to your computer and use it in GitHub Desktop.
import com.citytechinc.cq.component.annotations.Component
import com.citytechinc.cq.component.annotations.DialogField
import com.citytechinc.cq.component.annotations.Option
import com.citytechinc.cq.component.annotations.widgets.PathField
import com.citytechinc.cq.component.annotations.widgets.Selection
import com.citytechinc.cq.component.annotations.widgets.Switch
import com.citytechinc.cq.component.annotations.widgets.TextField
import com.icfolson.aem.library.core.components.AbstractComponent
import com.icfolson.aem.library.core.constants.PathConstants
import com.icfolson.aem.multicompositeaddon.widget.MultiCompositeField
import org.apache.sling.api.SlingHttpServletRequest
import org.apache.sling.api.resource.Resource
import org.apache.sling.models.annotations.Default
import org.apache.sling.models.annotations.DefaultInjectionStrategy
import org.apache.sling.models.annotations.Model
import javax.inject.Inject
/**
* Idiomatic Groovy component class. Note that fields are defined with default visibility, which is {@code public}
* for Groovy classes. This eliminates the need to define additional getters for each field.
*/
@Component(value = "Groovy Component")
@Model(adaptables = [Resource, SlingHttpServletRequest], defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
class GroovyIdioms extends AbstractComponent {
enum HeadingSize {
@Option(text = "Heading 1")
H1,
@Option(text = "Heading 2", selected = true)
H2,
@Option(text = "Heading 3")
H3,
@Option(text = "Heading 4")
H4,
@Option(text = "Heading 5")
H5,
@Option(text = "Heading 6")
H6
}
@Model(adaptables = [Resource, SlingHttpServletRequest],
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
class LinkItem {
@DialogField(fieldLabel = "Link Title")
@TextField
@Inject
String title
@DialogField(fieldLabel = "Link")
@PathField(rootPath = PathConstants.PATH_CONTENT)
String link
}
// define a text field and accessors for a property value stored on the current resource
/**
* Define a text field and inject a property value stored on the current resource.
*/
@DialogField(fieldLabel = "Title")
@TextField
@Inject
@Default(values = "Default Title")
String title
/**
* Use the underlying value map getter to retrieve the property value. No {@code @Inject} annotation needed.
*/
@DialogField(fieldLabel = "Title")
@TextField
String getTitle() {
get("title", "Default Title")
}
// define a switch/boolean field for a property value stored on the current resource
@DialogField(fieldLabel = "Enabled?")
@Switch(offText = "No", onText = "Yes")
@Inject
@Default(booleanValues = false)
Boolean enabled
@DialogField(fieldLabel = "Enabled?")
@Switch(offText = "No", onText = "Yes")
Boolean isEnabled() {
get("enabled", false)
}
// define a select field using an enum to provide selection options
@DialogField(fieldLabel = "Heading Size", value = "H2")
@Selection(type = Selection.SELECT)
@Inject
HeadingSize size = HeadingSize.H2 // assign default value
// define a multicomposite field
/**
* Inject list of composite resources. To support inheritance, use {@code @InheritInject} instead.
*/
@DialogField(fieldLabel = "Link Items")
@MultiCompositeField(limit = 3)
@Inject
// @InheritInject
List<LinkItem> linkItems
/**
* Same as above, but use the underlying method to get the list of nodes and transform the list by adapting each
* resource to the composite model class.
*/
@DialogField(fieldLabel = "Link Items")
@MultiCompositeField(limit = 3)
List<LinkItem> getLinkItems() {
getComponentNodes("linkItems").collect { node ->
node.resource.adaptTo(LinkItem)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment