|
| 1 | +package org.jetbrains.kotlinx.dataframe.api |
| 2 | + |
| 3 | +import org.jetbrains.kotlinx.dataframe.ColumnsSelector |
| 4 | +import org.jetbrains.kotlinx.dataframe.DataFrame |
| 5 | +import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath |
| 6 | +import org.jetbrains.kotlinx.dataframe.columns.toColumnSet |
| 7 | +import org.jetbrains.kotlinx.dataframe.impl.api.formatHeaderImpl |
| 8 | + |
| 9 | +// region docs |
| 10 | + |
| 11 | +/** |
| 12 | + * A lambda used to format a column header (its displayed name) when rendering a dataframe to HTML. |
| 13 | + * |
| 14 | + * The lambda runs in the context of [FormattingDsl] and receives the [ColumnWithPath] of the header to format. |
| 15 | + * Return a [CellAttributes] (or `null`) describing the CSS you want to apply to the header cell. |
| 16 | + * |
| 17 | + * Examples: |
| 18 | + * - Center the header: `attr("text-align", "center")` |
| 19 | + * - Make it bold: `bold` |
| 20 | + * - Set a custom color: `textColor(rgb(10, 10, 10))` |
| 21 | + */ |
| 22 | +public typealias HeaderColFormatter<C> = FormattingDsl.(col: ColumnWithPath<C>) -> CellAttributes? |
| 23 | + |
| 24 | +/** |
| 25 | + * An intermediate class used in the header-format operation [formatHeader]. |
| 26 | + * |
| 27 | + * This class itself does nothing—it represents a selection of columns whose headers will be formatted. |
| 28 | + * Finalize this step by calling [with] to produce a new [FormattedFrame]. |
| 29 | + * |
| 30 | + * Header formatting is additive and supports nested column groups: styles specified for a parent group |
| 31 | + * are inherited by its child columns unless overridden for the child. |
| 32 | + */ |
| 33 | +public class HeaderFormatClause<T, C>( |
| 34 | + internal val df: DataFrame<T>, |
| 35 | + internal val columns: ColumnsSelector<T, C> = { all().cast() }, |
| 36 | + internal val oldHeaderFormatter: HeaderColFormatter<C>? = null, |
| 37 | + internal val oldCellFormatter: RowColFormatter<T, *>? = null, |
| 38 | +) { |
| 39 | + override fun toString(): String = |
| 40 | + "HeaderFormatClause(df=$df, columns=$columns, oldHeaderFormatter=$oldHeaderFormatter, oldCellFormatter=$oldCellFormatter)" |
| 41 | +} |
| 42 | + |
| 43 | +// endregion |
| 44 | + |
| 45 | +// region DataFrame.formatHeader |
| 46 | + |
| 47 | +/** |
| 48 | + * **Experimental API. It may be changed in the future.** |
| 49 | + * |
| 50 | + * Selects [columns] whose headers should be formatted. If unspecified, all columns will be formatted. |
| 51 | + * |
| 52 | + * This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] |
| 53 | + * which must be finalized using [HeaderFormatClause.with]. |
| 54 | + */ |
| 55 | +public fun <T, C> DataFrame<T>.formatHeader(columns: ColumnsSelector<T, C>): HeaderFormatClause<T, C> = |
| 56 | + HeaderFormatClause(this, columns) |
| 57 | + |
| 58 | +/** |
| 59 | + * **Experimental API. It may be changed in the future.** |
| 60 | + * |
| 61 | + * Selects [columns] whose headers should be formatted. |
| 62 | + * |
| 63 | + * This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] |
| 64 | + * which must be finalized using [HeaderFormatClause.with]. |
| 65 | + */ |
| 66 | +public fun <T> DataFrame<T>.formatHeader(vararg columns: String): HeaderFormatClause<T, Any?> = |
| 67 | + formatHeader { columns.toColumnSet() } |
| 68 | + |
| 69 | +/** |
| 70 | + * **Experimental API. It may be changed in the future.** |
| 71 | + * |
| 72 | + * Selects all columns for header formatting. |
| 73 | + * |
| 74 | + * This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] |
| 75 | + * which must be finalized using [HeaderFormatClause.with]. |
| 76 | + */ |
| 77 | +public fun <T> DataFrame<T>.formatHeader(): HeaderFormatClause<T, Any?> = HeaderFormatClause(this) |
| 78 | + |
| 79 | +// endregion |
| 80 | + |
| 81 | +// region FormattedFrame.formatHeader |
| 82 | + |
| 83 | +/** |
| 84 | + * **Experimental API. It may be changed in the future.** |
| 85 | + * |
| 86 | + * Selects [columns] whose headers should be formatted. |
| 87 | + * |
| 88 | + * This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] |
| 89 | + * which must be finalized using [HeaderFormatClause.with]. |
| 90 | + */ |
| 91 | +public fun <T, C> FormattedFrame<T>.formatHeader(columns: ColumnsSelector<T, C>): HeaderFormatClause<T, C> = |
| 92 | + HeaderFormatClause( |
| 93 | + df = df, |
| 94 | + columns = columns, |
| 95 | + oldHeaderFormatter = headerFormatter as HeaderColFormatter<C>?, |
| 96 | + oldCellFormatter = formatter, |
| 97 | + ) |
| 98 | + |
| 99 | +/** |
| 100 | + * **Experimental API. It may be changed in the future.** |
| 101 | + * |
| 102 | + * Selects [columns] whose headers should be formatted. |
| 103 | + * |
| 104 | + * This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] |
| 105 | + * which must be finalized using [HeaderFormatClause.with]. |
| 106 | + */ |
| 107 | +public fun <T> FormattedFrame<T>.formatHeader(vararg columns: String): HeaderFormatClause<T, Any?> = |
| 108 | + formatHeader { columns.toColumnSet() } |
| 109 | + |
| 110 | +/** |
| 111 | + * **Experimental API. It may be changed in the future.** |
| 112 | + * |
| 113 | + * Selects all columns for header formatting. |
| 114 | + * |
| 115 | + * This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] |
| 116 | + * which must be finalized using [HeaderFormatClause.with]. |
| 117 | + */ |
| 118 | +public fun <T> FormattedFrame<T>.formatHeader(): HeaderFormatClause<T, Any?> = |
| 119 | + HeaderFormatClause( |
| 120 | + df = df, |
| 121 | + oldHeaderFormatter = headerFormatter, |
| 122 | + oldCellFormatter = formatter, |
| 123 | + ) |
| 124 | + |
| 125 | +// endregion |
| 126 | + |
| 127 | +// region terminal operations |
| 128 | + |
| 129 | +/** |
| 130 | + * **Experimental API. It may be changed in the future.** |
| 131 | + * |
| 132 | + * Creates a new [FormattedFrame] that uses the specified [HeaderColFormatter] to format the selected headers. |
| 133 | + * |
| 134 | + * Header formatting is additive: attributes from already-applied header formatters are combined with the newly |
| 135 | + * |
| 136 | + * returned attributes using [CellAttributes.and]. If a parent column group is selected, its attributes are |
| 137 | + * applied to its children unless explicitly overridden. |
| 138 | + */ |
| 139 | +@Suppress("UNCHECKED_CAST") |
| 140 | +public fun <T, C> HeaderFormatClause<T, C>.with(formatter: HeaderColFormatter<C>): FormattedFrame<T> = |
| 141 | + formatHeaderImpl(formatter) |
| 142 | + |
| 143 | +// endregion |
0 commit comments