| 1 |
import { TextRenderer, CellRenderer, GraphicsContext } from '@lumino/datagrid'; |
| 2 |
|
| 3 |
import { Theme } from '../utils'; |
| 4 |
|
| 5 |
import { UniqueValueStateManager, InteractiveFilterDialog } from './filterMenu'; |
| 6 |
|
| 7 |
/** |
| 8 |
* A custom cell renderer for displaying the unique values of a column. |
| 9 |
*/ |
| 10 |
export class FilterValueRenderer extends TextRenderer { |
| 11 |
constructor(options: FilterValueRenderer.IOptions) { |
| 12 |
super({ |
| 13 |
textColor: options.textColor, |
| 14 |
backgroundColor: options.backgroundColor, |
| 15 |
}); |
| 16 |
this._stateManager = options.stateManager; |
| 17 |
this._dialog = options.dialog; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Draw the text for the cell. |
| 22 |
* |
| 23 |
* @param gc - The graphics context to use for drawing. |
| 24 |
* |
| 25 |
* @param config - The configuration data for the cell. |
| 26 |
*/ |
| 27 |
drawText(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 28 |
// Resolve the font for the cell. |
| 29 |
const font = CellRenderer.resolveOption(this.font, config); |
| 30 |
|
| 31 |
// Bail if there is no font to draw. |
| 32 |
if (!font) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Resolve the text color for the cell. |
| 37 |
const color = CellRenderer.resolveOption(this.textColor, config); |
| 38 |
|
| 39 |
// Bail if there is no text color to draw. |
| 40 |
if (!color) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Format the cell value to text. |
| 45 |
const format = this.format; |
| 46 |
const text = format(config); |
| 47 |
|
| 48 |
// Bail if there is no text to draw. |
| 49 |
if (!text) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Resolve the vertical and horizontal alignment. |
| 54 |
const vAlign = CellRenderer.resolveOption(this.verticalAlignment, config); |
| 55 |
const hAlign = CellRenderer.resolveOption(this.horizontalAlignment, config); |
| 56 |
|
| 57 |
// Compute the padded text box height for the specified alignment. |
| 58 |
const boxHeight = config.height - (vAlign === 'center' ? 1 : 2); |
| 59 |
|
| 60 |
// Bail if the text box has no effective size. |
| 61 |
if (boxHeight <= 0) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Compute the text height for the gc font. |
| 66 |
const textHeight = TextRenderer.measureFontHeight(font); |
| 67 |
|
| 68 |
// Set up the text position variables. |
| 69 |
let textX: number; |
| 70 |
let textY: number; |
| 71 |
|
| 72 |
// Compute the Y position for the text. |
| 73 |
switch (vAlign) { |
| 74 |
case 'top': |
| 75 |
textY = config.y + 2 + textHeight; |
| 76 |
break; |
| 77 |
case 'center': |
| 78 |
textY = config.y + config.height / 2 + textHeight / 2; |
| 79 |
break; |
| 80 |
case 'bottom': |
| 81 |
textY = config.y + config.height - 2; |
| 82 |
break; |
| 83 |
default: |
| 84 |
throw 'unreachable'; |
| 85 |
} |
| 86 |
|
| 87 |
// Compute the X position for the text. |
| 88 |
switch (hAlign) { |
| 89 |
case 'left': |
| 90 |
textX = config.x + 2; |
| 91 |
break; |
| 92 |
case 'center': |
| 93 |
textX = config.x + config.width / 2; |
| 94 |
break; |
| 95 |
case 'right': |
| 96 |
textX = config.x + config.width - 3; |
| 97 |
break; |
| 98 |
default: |
| 99 |
throw 'unreachable'; |
| 100 |
} |
| 101 |
|
| 102 |
// Clip the cell if the text is taller than the text box height. |
| 103 |
if (textHeight > boxHeight) { |
| 104 |
gc.beginPath(); |
| 105 |
gc.rect(config.x, config.y, config.width, config.height - 1); |
| 106 |
gc.clip(); |
| 107 |
} |
| 108 |
|
| 109 |
// Set the gc state. |
| 110 |
gc.font = font; |
| 111 |
gc.fillStyle = color; |
| 112 |
gc.textAlign = hAlign; |
| 113 |
gc.textBaseline = 'bottom'; |
| 114 |
|
| 115 |
// Draw the text |
| 116 |
gc.fillText(text, textX + 25, textY); |
| 117 |
gc.fillStyle = Theme.getBorderColor(1); |
| 118 |
|
| 119 |
const BOX_OFFSET = 5; |
| 120 |
|
| 121 |
gc.fillStyle = '#ffffff'; |
| 122 |
gc.fillRect(config.x + BOX_OFFSET, config.y + BOX_OFFSET, 10, 10); |
| 123 |
|
| 124 |
gc.fillStyle = '#000000'; |
| 125 |
gc.strokeRect(config.x + BOX_OFFSET, config.y + BOX_OFFSET, 10, 10); |
| 126 |
|
| 127 |
// Check state to display checkbox |
| 128 |
if (this._getCheckedState(config)) { |
| 129 |
gc.beginPath(); |
| 130 |
gc.strokeStyle = '#000000'; |
| 131 |
gc.moveTo(config.x + BOX_OFFSET + 3, config.y + BOX_OFFSET + 5); |
| 132 |
gc.lineTo(config.x + BOX_OFFSET + 4, config.y + BOX_OFFSET + 8); |
| 133 |
gc.lineTo(config.x + BOX_OFFSET + 8, config.y + BOX_OFFSET + 2); |
| 134 |
gc.lineWidth = 2; |
| 135 |
gc.stroke(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private _getCheckedState(config: CellRenderer.CellConfig): boolean { |
| 140 |
return ( |
| 141 |
this._stateManager.has( |
| 142 |
this._dialog.region, |
| 143 |
this._dialog.column, |
| 144 |
config.value, |
| 145 |
) || |
| 146 |
(!this._dialog.hasFilter && !this._dialog.userInteractedWithDialog) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
private _stateManager: UniqueValueStateManager; |
| 151 |
private _dialog: InteractiveFilterDialog; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* The namespace for the `HeaderRenderer` class statics. |
| 156 |
*/ |
| 157 |
export namespace FilterValueRenderer { |
| 158 |
/** |
| 159 |
* An options object for initializing a renderer. |
| 160 |
*/ |
| 161 |
export interface IOptions { |
| 162 |
/** |
| 163 |
* The `UniqueValueStateManager` to be used by this renderer. |
| 164 |
*/ |
| 165 |
readonly stateManager: UniqueValueStateManager; |
| 166 |
|
| 167 |
/** |
| 168 |
* The `InteractiveFilterDialog` this renderer is used by. |
| 169 |
*/ |
| 170 |
readonly dialog: InteractiveFilterDialog; |
| 171 |
|
| 172 |
/** |
| 173 |
* The CSS color for drawing the text. |
| 174 |
*/ |
| 175 |
readonly backgroundColor: string; |
| 176 |
|
| 177 |
/** |
| 178 |
* The CSS color for drawing the text. |
| 179 |
*/ |
| 180 |
readonly textColor: string; |
| 181 |
} |
| 182 |
} |