|
| 1 | +package dev.bombinating.bianmen.factory |
| 2 | + |
| 3 | +import dev.bombinating.bianmen.ComponentExt.config |
| 4 | +import dev.bombinating.bianmen.context.AjaxLinkContext |
| 5 | +import dev.bombinating.bianmen.context.AjaxLinkContextImpl |
| 6 | +import dev.bombinating.bianmen.context.ComponentReferenceType |
| 7 | +import org.apache.wicket.ajax.AjaxRequestTarget |
| 8 | +import org.apache.wicket.ajax.markup.html.AjaxLink |
| 9 | +import org.apache.wicket.model.IModel |
| 10 | + |
| 11 | +/** |
| 12 | + * Factory methods for creating [AjaxLink]s. |
| 13 | + */ |
| 14 | +public object AjaxLinkFactory { |
| 15 | + /** |
| 16 | + * Creates a new [AjaxLink] based on the parameters. |
| 17 | + * |
| 18 | + * @param T type of [AjaxLink] to create |
| 19 | + * @param id Wicket id of the [AjaxLink] |
| 20 | + * @param model the model for the [AjaxLink] |
| 21 | + * @param refType how the [AjaxLink] may be updated from the server |
| 22 | + * @param visibleWhen model of when the [AjaxLink] is visible |
| 23 | + * @param enabledWhen model of when the [AjaxLink] is enabled |
| 24 | + * @param renderBodyOnly whether to only render the body of the [AjaxLink] |
| 25 | + * @param escapeModelStrings whether to escape the [AjaxLink]'s model string |
| 26 | + * @param config lambda for configuring the [AjaxLink] |
| 27 | + */ |
| 28 | + @Suppress("LongParameterList") |
| 29 | + public fun <T> ajaxLink( |
| 30 | + id: String, |
| 31 | + model: IModel<T>?, |
| 32 | + refType: ComponentReferenceType? = null, |
| 33 | + visibleWhen: IModel<Boolean>? = null, |
| 34 | + enabledWhen: IModel<Boolean>? = null, |
| 35 | + renderBodyOnly: Boolean? = null, |
| 36 | + escapeModelStrings: Boolean? = null, |
| 37 | + config: (AjaxLinkContext<T>.() -> Unit)? = null |
| 38 | + ): AjaxLink<T> { |
| 39 | + val context = AjaxLinkContextImpl<T>() |
| 40 | + config?.let { it(context) } |
| 41 | + val clickHandler = context.onClick |
| 42 | + return object : AjaxLink<T>(id, model) { |
| 43 | + override fun onClick(target: AjaxRequestTarget) { |
| 44 | + clickHandler?.invoke(this, target) |
| 45 | + } |
| 46 | + }.config( |
| 47 | + refType = refType, |
| 48 | + visibleWhen = visibleWhen, |
| 49 | + enabledWhen = enabledWhen, |
| 50 | + renderBodyOnly = renderBodyOnly, |
| 51 | + escapeModelStrings = escapeModelStrings, |
| 52 | + behaviors = context.behaviors |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new [AjaxLink] based on the parameters. |
| 58 | + * |
| 59 | + * @param T type of [AjaxLink] to create |
| 60 | + * @param id Wicket id of the [AjaxLink] |
| 61 | + * @param refType how the [AjaxLink] may be updated from the server |
| 62 | + * @param visibleWhen model of when the [AjaxLink] is visible |
| 63 | + * @param enabledWhen model of when the [AjaxLink] is enabled |
| 64 | + * @param renderBodyOnly whether to only render the body of the [AjaxLink] |
| 65 | + * @param escapeModelStrings whether to escape the [AjaxLink]'s model string |
| 66 | + * @param config lambda for configuring the [AjaxLink] |
| 67 | + */ |
| 68 | + @Suppress("LongParameterList") |
| 69 | + public fun ajaxLink( |
| 70 | + id: String, |
| 71 | + refType: ComponentReferenceType? = null, |
| 72 | + visibleWhen: IModel<Boolean>? = null, |
| 73 | + enabledWhen: IModel<Boolean>? = null, |
| 74 | + renderBodyOnly: Boolean? = null, |
| 75 | + escapeModelStrings: Boolean? = null, |
| 76 | + config: (AjaxLinkContext<*>.() -> Unit)? = null |
| 77 | + ): AjaxLink<*> = ajaxLink<Any?>( |
| 78 | + id = id, model = null, refType = refType, visibleWhen = visibleWhen, |
| 79 | + enabledWhen = enabledWhen, renderBodyOnly = renderBodyOnly, escapeModelStrings = escapeModelStrings, |
| 80 | + config = config |
| 81 | + ) |
| 82 | +} |
0 commit comments