| 1 |
const d3Color: any = require('d3-color'); |
| 2 |
import { CellGroup } from '@lumino/datagrid'; |
| 3 |
import { DataSource } from './datasource'; |
| 4 |
|
| 5 |
export namespace ArrayUtils { |
| 6 |
/** |
| 7 |
* Returns the locations of multi-index column arrays. |
| 8 |
* @param model the datamodel instance. |
| 9 |
*/ |
| 10 |
export function generateMultiIndexArrayLocations(model: any): number[] { |
| 11 |
// Removing the primary key uuid from the primary key array |
| 12 |
// as it is not used in the multi index location generation |
| 13 |
// process. |
| 14 |
const primaryKey = model._dataset.schema.primaryKey.slice( |
| 15 |
0, |
| 16 |
model._dataset.schema.primaryKey.length - 1, |
| 17 |
); |
| 18 |
const dataFields = model._dataset.schema.fields; |
| 19 |
const multiIndexLocationArr: number[] = []; |
| 20 |
// Subtracting one from the length of dataFields to account for |
| 21 |
// (remove) the invisible primary key uuid column. |
| 22 |
for (let i = 0; i < dataFields.length - 1; i++) { |
| 23 |
if (!primaryKey.includes(dataFields[i].name)) { |
| 24 |
multiIndexLocationArr.push(i); |
| 25 |
} |
| 26 |
} |
| 27 |
return multiIndexLocationArr; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns an array of [mergedCellSiblingGroup]. Each element represents a row of columns. |
| 32 |
* The 0th row will be the top level group, and the n-th will be the last. |
| 33 |
* @param model the data model. |
| 34 |
* @param multiIndexArrayLocations index-based locations of all mutli-index columns. |
| 35 |
*/ |
| 36 |
export function generateColMergedCellLocations( |
| 37 |
model: any, |
| 38 |
multiIndexArrayLocations: number[], |
| 39 |
): number[][][][] { |
| 40 |
// Terminating if no locations are passed. |
| 41 |
if (multiIndexArrayLocations.length === 0) { |
| 42 |
return []; |
| 43 |
} |
| 44 |
|
| 45 |
const dataFields = model._dataset.schema.fields; |
| 46 |
// The data grid doesn't count corner-headers when indexing column-headers, so if a given |
| 47 |
// datagrid has 5 columns, 2 of which are corner-headers, the index of the first column |
| 48 |
// header will not be 2, but 0. columnStartIndexOffset calculaates that offset so we |
| 49 |
// can correctly identify indices corresponding to column-headers. |
| 50 |
// Subtracting one from the start index offset to account for (remove) any |
| 51 |
// references to the primary key uuid column. |
| 52 |
const columnStartIndexOffset = model._dataset.schema.primaryKey.length - 1; |
| 53 |
const retArr: any[] = []; |
| 54 |
let curRow: any[] = []; |
| 55 |
const firstIndex = multiIndexArrayLocations[0]; |
| 56 |
|
| 57 |
let prevVal: string | number | undefined = undefined; |
| 58 |
for (let i = 0; i < dataFields[firstIndex].rows.length; i++) { |
| 59 |
let curMergedRange: number[][] = []; |
| 60 |
for (const j of multiIndexArrayLocations) { |
| 61 |
const curVal = dataFields[j].rows[i]; |
| 62 |
if (curMergedRange.length == 0 || prevVal == curVal) { |
| 63 |
curMergedRange.push([i, j - columnStartIndexOffset]); |
| 64 |
} else { |
| 65 |
curRow.push(curMergedRange); |
| 66 |
curMergedRange = [[i, j - columnStartIndexOffset]]; |
| 67 |
} |
| 68 |
prevVal = curVal; |
| 69 |
} |
| 70 |
curRow.push(curMergedRange); |
| 71 |
retArr.push(curRow); |
| 72 |
curRow = []; |
| 73 |
} |
| 74 |
return retArr; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns an array of [mergedCellSiblingGroup]. Each element represents a column of rows. |
| 79 |
* The 0th row will be the top level group, and the n-th will be the last. |
| 80 |
* @param model data model with schema and data fields |
| 81 |
* @returns index-based locations of all mutli-index rows. |
| 82 |
*/ |
| 83 |
export function generateRowMergedCellLocations(model: any): number[][][][] { |
| 84 |
const dataset = model._dataset as DataSource; |
| 85 |
|
| 86 |
// Removing internal primary key identifier. |
| 87 |
const primaryKey = dataset.schema.primaryKey.slice( |
| 88 |
0, |
| 89 |
dataset.schema.primaryKey.length - 1, |
| 90 |
); |
| 91 |
|
| 92 |
// Terminate if we're not dealing with nested row headers. |
| 93 |
if (!(primaryKey.length > 1)) { |
| 94 |
return []; |
| 95 |
} |
| 96 |
|
| 97 |
const data = dataset.data; |
| 98 |
const retArr = []; |
| 99 |
let curCol = []; |
| 100 |
|
| 101 |
for (let i = 0; i < primaryKey.length; i++) { |
| 102 |
let prevVal = undefined; |
| 103 |
let curMergedRange: any = []; |
| 104 |
for (let j = 0; j < dataset.length; j++) { |
| 105 |
const curVal = data[primaryKey[i]][j]; |
| 106 |
// if (curMergedRange.length == 0 || prevVal == curVal) { |
| 107 |
const [parentGroupStart, parentGroupEnd] = getParentGroupPosition( |
| 108 |
retArr, |
| 109 |
j, |
| 110 |
i, |
| 111 |
); |
| 112 |
if ( |
| 113 |
curMergedRange.length == 0 || |
| 114 |
(prevVal == curVal && |
| 115 |
curMergedRange[0][0] >= parentGroupStart && |
| 116 |
j <= parentGroupEnd) |
| 117 |
) { |
| 118 |
curMergedRange.push([j, i]); |
| 119 |
} else { |
| 120 |
curCol.push(curMergedRange); |
| 121 |
curMergedRange = [[j, i]]; |
| 122 |
} |
| 123 |
prevVal = curVal; |
| 124 |
} |
| 125 |
curCol.push(curMergedRange); |
| 126 |
retArr.push(curCol); |
| 127 |
curCol = []; |
| 128 |
} |
| 129 |
|
| 130 |
return retArr; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns [startRow, endRow] of parent column at the same row position. |
| 135 |
* The 0th row will be the top level group, and the n-th will be the last. |
| 136 |
* @param retArr array of merge cellgroups in all previous columns |
| 137 |
* @param rowNum current row number |
| 138 |
* @param colNum current column number |
| 139 |
* @returns.[startRow, endRow] of previous column |
| 140 |
*/ |
| 141 |
function getParentGroupPosition( |
| 142 |
retArr: any, |
| 143 |
rowNum: number, |
| 144 |
colNum: number, |
| 145 |
): number[] { |
| 146 |
if (colNum === 0) { |
| 147 |
return [0, rowNum]; |
| 148 |
} |
| 149 |
for (let i = 0; i < retArr[colNum - 1].length; i++) { |
| 150 |
// iterate mergegroups of previous row |
| 151 |
const curMergedGroup = retArr[colNum - 1][i]; |
| 152 |
const curMergedGroupLen = curMergedGroup.length; |
| 153 |
const firstRow = curMergedGroup[0][0]; |
| 154 |
const lastRow = curMergedGroup[curMergedGroupLen - 1][0]; |
| 155 |
if (rowNum >= firstRow && rowNum <= lastRow) { |
| 156 |
return [firstRow!, lastRow!]; |
| 157 |
} |
| 158 |
} |
| 159 |
return []; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Checks whether the merged cell ranges conform to a valid hierarchy. |
| 164 |
* @param retVal boolean |
| 165 |
*/ |
| 166 |
export function validateMergingHierarchy(retVal: number[][][][]): boolean { |
| 167 |
let prevLevelLength; |
| 168 |
for (const mergeRange of retVal) { |
| 169 |
// First element - setting up the value of prevLevelLength |
| 170 |
if (prevLevelLength === undefined) { |
| 171 |
prevLevelLength = mergeRange.length; |
| 172 |
continue; |
| 173 |
} |
| 174 |
// If the current merged range list has a length that is less than |
| 175 |
// the previous range, it means the current level is a larger merged |
| 176 |
// range, which violates the hierarchy. The function terminates here |
| 177 |
// and returns false. |
| 178 |
if (mergeRange.length < prevLevelLength) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
prevLevelLength = mergeRange.length; |
| 182 |
} |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Generates a list of merged column cell groups for use in the data model. |
| 188 |
* @param indexLists index-based location of merged columns |
| 189 |
* @returns CellGroup[] |
| 190 |
*/ |
| 191 |
export function generateColumnCellGroups( |
| 192 |
indexLists: number[][][][], |
| 193 |
): CellGroup[] { |
| 194 |
const columnGroup = []; |
| 195 |
for (let curRow = 0; curRow < indexLists.length; curRow++) { |
| 196 |
for (let groupNum = 0; groupNum < indexLists[curRow].length; groupNum++) { |
| 197 |
if (indexLists[curRow][groupNum].length > 1) { |
| 198 |
const groupLength = indexLists[curRow][groupNum].length; |
| 199 |
const c1 = indexLists[curRow][groupNum][0][1]; |
| 200 |
const c2 = indexLists[curRow][groupNum][groupLength - 1][1]; |
| 201 |
columnGroup.push({ r1: curRow, c1, r2: curRow, c2 }); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return columnGroup; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Generates a list of merged row cell groups for use in the data model. |
| 211 |
* @param indexLists index-based location of merged rows. |
| 212 |
* @returns CellGroup[] |
| 213 |
*/ |
| 214 |
export function generateRowCellGroups( |
| 215 |
indexLists: number[][][][], |
| 216 |
): CellGroup[] { |
| 217 |
const rowGroup = []; |
| 218 |
for (let curCol = 0; curCol < indexLists.length; curCol++) { |
| 219 |
for (let groupNum = 0; groupNum < indexLists[curCol].length; groupNum++) { |
| 220 |
if (indexLists[curCol][groupNum].length > 1) { |
| 221 |
const groupLength = indexLists[curCol][groupNum].length; |
| 222 |
const r1 = indexLists[curCol][groupNum][0][0]; |
| 223 |
const r2 = indexLists[curCol][groupNum][groupLength - 1][0]; |
| 224 |
rowGroup.push({ r1: r1, c1: curCol, r2: r2, c2: curCol }); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return rowGroup; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Scalar type |
| 234 |
export type Scalar = null | boolean | number | string; |
| 235 |
|
| 236 |
export namespace Scalar { |
| 237 |
export function isScalar(x: any): x is Scalar { |
| 238 |
return ( |
| 239 |
x === null || |
| 240 |
typeof x === 'boolean' || |
| 241 |
typeof x === 'number' || |
| 242 |
typeof x === 'string' |
| 243 |
); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
export namespace Theme { |
| 248 |
function applyOpacity(color: string, opacity: number) { |
| 249 |
const c = d3Color.rgb(color); |
| 250 |
c.opacity = opacity; |
| 251 |
|
| 252 |
return c.formatRgb(); |
| 253 |
} |
| 254 |
|
| 255 |
export function getComputedColor(name: string) { |
| 256 |
const el = document.querySelector('.ipydatagrid-widget') || document.body; |
| 257 |
const color = window.getComputedStyle(el).getPropertyValue(name); |
| 258 |
if (color) { |
| 259 |
return color; |
| 260 |
} |
| 261 |
|
| 262 |
return '#ffffff'; |
| 263 |
} |
| 264 |
|
| 265 |
export function getCSSColor(name: string, opacity = 1) { |
| 266 |
const color = getComputedColor(name); |
| 267 |
return applyOpacity(color, opacity); |
| 268 |
} |
| 269 |
|
| 270 |
export function getBackgroundColor(index = 0, opacity = 1) { |
| 271 |
return getCSSColor('--ipydatagrid-layout-color' + index, opacity); |
| 272 |
} |
| 273 |
|
| 274 |
export function getFontColor(index = 0, opacity = 1) { |
| 275 |
return getCSSColor('--ipydatagrid-ui-font-color' + index, opacity); |
| 276 |
} |
| 277 |
|
| 278 |
export function getBorderColor(index = 0, opacity = 1) { |
| 279 |
return getCSSColor('--ipydatagrid-border-color' + index, opacity); |
| 280 |
} |
| 281 |
|
| 282 |
export function getBrandColor(index = 0, opacity = 1) { |
| 283 |
return getCSSColor('--ipydatagrid-brand-color' + index, opacity); |
| 284 |
} |
| 285 |
} |