@castlabs/ui 7.21.2 → 7.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,20 +43,21 @@ declare module '@castlabs/ui/dist/castlabs-ui.module.js' {
43
43
  order?: string,
44
44
  caseSensitive?: boolean
45
45
  ): object[]
46
- export function clTableSorter (
46
+ export type ClTableSorter<Type> = {
47
+ col: number
48
+ order: 'ASC' | 'DESC' | 'NONE'
49
+ set: (col: number, order: string) => void
50
+ get: (col: number) => string
51
+ sorted: Type[]
52
+ sorter: () => any
53
+ sort: (col?: number, order?: string) => 'ASC' | 'DESC' | 'NONE'
54
+ }
55
+ export function clTableSorter<Type> (
47
56
  sort: (col: number, order: 'ASC' | 'DESC') => { sorted: object[], sortedOrder: 'ASC' | 'DESC' },
48
57
  initialCol?: number,
49
58
  initialOrder?: string
50
- ): any
51
- export function clTableSorterObjects<Type> (dataCallback: () => Type[], keys: string[], initialCol?: number, pageNoCallback?: () => number, pageSizeCallback?: () => number): {
52
- col: number,
53
- order: 'ASC' | 'DESC' | 'NONE',
54
- set: (col: number, order: string) => void,
55
- get: (col: number) => string,
56
- sorted: Type[],
57
- sorter: () => any,
58
- sort: (col?: number, order?: string) => 'ASC' | 'DESC' | 'NONE',
59
- }
59
+ ): ClTableSorter<Type>
60
+ export function clTableSorterObjects<Type> (dataCallback: () => Type[], keys: string[], initialCol?: number, pageNoCallback?: () => number, pageSizeCallback?: () => number): ClTableSorter<Type>
60
61
  export function clDebounce (callback: any, ms?: number, key?: string): void
61
62
  export function clSanitizeId (id: string): string
62
63
 
@@ -1,4 +1,4 @@
1
- /* @castlabs/ui v7.21.2 */
1
+ /* @castlabs/ui v7.23.0 */
2
2
 
3
3
  /*!
4
4
  * Bootstrap v5.3.8 (https://getbootstrap.com/)
@@ -216,9 +216,9 @@ function clMatomoSetup (siteId = 1, domain = 'castlabs.com') {
216
216
  })()
217
217
  }
218
218
 
219
- export function clTableSetupResize (id) {
219
+ export function clTableSetupResize (id, auto = false) {
220
220
  document.querySelectorAll(`#${id} .cl-resizer`).forEach(resizer => {
221
- clTableSetResizeListeners(resizer)
221
+ clTableSetResizeListeners(resizer, auto)
222
222
  })
223
223
  }
224
224
 
@@ -249,12 +249,15 @@ export function clTableSortStatic (id, col) {
249
249
  let lastX = 0
250
250
  let nextCol = null
251
251
 
252
- function clTableSetResizeListeners (resizer) {
252
+ function clTableSetResizeListeners (resizer, auto = false) {
253
253
  lastX = 0
254
254
  nextCol = null
255
255
  resizer.addEventListener('mousedown', mousedown)
256
256
  document.addEventListener('mousemove', mousemove)
257
257
  document.addEventListener('mouseup', mouseup)
258
+ if (auto) {
259
+ document.addEventListener('click', mouseclick)
260
+ }
258
261
  }
259
262
 
260
263
  const mousedown = mousedown => {
@@ -269,28 +272,25 @@ const mousemove = mousemove => {
269
272
  const delta = mousemove.pageX - lastX
270
273
  lastX = mousemove.pageX
271
274
 
272
- const colIndex = getColIndex(nextCol)
275
+ const colIndex = getColIndex(nextCol) - 1
273
276
  const id = mousemove.target?.closest('table')?.id ?? 'unknown'
274
277
  const nowrap = !!document.querySelector(`#${id}.cl-table-nowrap`) // are we in nowrap mode?
275
278
  if (nowrap) {
276
- // in nowrap mode we only adjust the first col
277
- document
278
- .querySelectorAll(
279
- `#${id} tr th:nth-child(${colIndex - 1})` // , #${id} tr td:nth-child(${colIndex - 1})
280
- )
281
- .forEach(cel => {
282
- const width =
283
- (cel.style.maxWidth === '' ? cel.offsetWidth : parseInt(cel.style.maxWidth)) + delta
284
- cel.style.width = (width > 32 ? width : 32) + 'px'
285
- cel.style.maxWidth = (width > 32 ? width : 32) + 'px'
286
- })
279
+ // in nowrap mode we only adjust the thead col
280
+ document.querySelectorAll(`#${id} tr th:nth-child(${colIndex})`).forEach(cel => {
281
+ const width =
282
+ (cel.style.maxWidth === '' ? cel.offsetWidth : parseInt(cel.style.maxWidth)) + delta
283
+ cel.style.width = (width > 32 ? width : 32) + 'px'
284
+ cel.style.maxWidth = (width > 32 ? width : 32) + 'px'
285
+ cel.clWidth = 0
286
+ })
287
287
  } else {
288
288
  // in regular mode we shrink one and enlarge the other col
289
- document.querySelectorAll(`#${id} tr td:nth-child(${colIndex - 1})`).forEach(cel => {
289
+ document.querySelectorAll(`#${id} tr td:nth-child(${colIndex})`).forEach(cel => {
290
290
  const width = cel.offsetWidth + delta
291
291
  cel.style.width = (width > 32 ? width : 32) + 'px'
292
292
  })
293
- document.querySelectorAll(`#${id} tr td:nth-child(${colIndex})`).forEach(cel => {
293
+ document.querySelectorAll(`#${id} tr td:nth-child(${colIndex + 1})`).forEach(cel => {
294
294
  const width = cel.offsetWidth - delta
295
295
  cel.style.width = (width > 32 ? width : 32) + 'px'
296
296
  })
@@ -306,8 +306,52 @@ const mouseup = mouseup => {
306
306
  }
307
307
  }
308
308
 
309
+ const mouseclick = click => {
310
+ if (click.detail === 2) {
311
+ click.preventDefault()
312
+
313
+ const table = click.target?.closest('table')
314
+ const id = table?.id ?? 'unknown'
315
+ const th = click.target?.closest('th')
316
+ const colIndex = Array.from(th.parentElement.children).indexOf(th)
317
+
318
+ // set all to something small so scrollWidth is correct for all-smaller items (head only)
319
+ document.querySelectorAll(`#${id} th:nth-child(${colIndex})`).forEach(cel => {
320
+ cel.style.width = '1px'
321
+ cel.style.maxWidth = '1px'
322
+ })
323
+
324
+ // iterate over all rows and col index (thead + tbody), find max cel width
325
+ let maxCellWidth = 0
326
+ document
327
+ .querySelectorAll(`#${id} th:nth-child(${colIndex}), #${id} td:nth-child(${colIndex})`)
328
+ .forEach(cel => {
329
+ if ((cel.colSpan ?? 1) === 1) {
330
+ const cellWidth = cel.scrollWidth
331
+ maxCellWidth = cellWidth > maxCellWidth ? cellWidth : maxCellWidth
332
+ }
333
+ })
334
+
335
+ // set all outer width to inner (head only)
336
+ document.querySelectorAll(`#${id} th:nth-child(${colIndex})`).forEach(cel => {
337
+ if (cel.clWidth === maxCellWidth) {
338
+ // revert to un-maximized
339
+ cel.clWidth = 0
340
+ cel.style.removeProperty('width')
341
+ cel.style.removeProperty('max-width')
342
+ } else {
343
+ // maximize
344
+ const padding = 8 * 2
345
+ cel.clWidth = maxCellWidth
346
+ cel.style.width = `${cel.clWidth + padding}px`
347
+ cel.style.maxWidth = `${cel.clWidth + padding}px`
348
+ }
349
+ })
350
+ }
351
+ }
352
+
309
353
  function getColIndex (col) {
310
- // calculate index of a col in a row
354
+ // calculate index of a col in a row, 1-based
311
355
  let colNo = 0
312
356
  while (col) {
313
357
  colNo++