@castlabs/ui 5.4.3 → 5.5.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.
@@ -214,7 +214,7 @@ function clMatomoSetup () {
214
214
 
215
215
  function clTableSetupResize (id) {
216
216
  document.querySelectorAll(`#${id} .cl-resizer`).forEach(resizer => {
217
- clTableSetResizeListeners(resizer)
217
+ clTableSetResizeListeners(resizer, id)
218
218
  })
219
219
  }
220
220
 
@@ -243,37 +243,69 @@ function clTableSortStatic (id, col) {
243
243
  }
244
244
 
245
245
  let lastX = 0
246
- let column = null
246
+ let nextCol = null
247
+
248
+ function clTableSetResizeListeners (resizer, id) {
249
+ const nowrap = !!document.querySelector(`#${id}.cl-table-nowrap`) // are we in nowrap mode?
247
250
 
248
- function clTableSetResizeListeners (resizer) {
249
251
  resizer.addEventListener('mousedown', mousedown => {
250
252
  mousedown.preventDefault()
251
- column = mousedown.target ? mousedown.target.parentElement : null
253
+ nextCol = mousedown.target ? mousedown.target.parentElement : null
252
254
  lastX = mousedown.pageX
253
255
  })
254
256
 
255
257
  document.addEventListener('mousemove', mousemove => {
256
- if (column !== null) {
258
+ if (nextCol !== null) {
257
259
  mousemove.preventDefault()
258
260
  const delta = mousemove.pageX - lastX
259
261
  lastX = mousemove.pageX
260
- column.style.width = column.offsetWidth - delta + 'px'
261
- const prevElement = column.previousElementSibling
262
- if (prevElement != null) {
263
- prevElement.style.width = prevElement.offsetWidth + delta + 'px'
262
+
263
+ const colIndex = getColIndex(nextCol)
264
+ if (nowrap) {
265
+ // in nowrap mode we only adjust the first col
266
+ document
267
+ .querySelectorAll(
268
+ `#${id} tr th:nth-child(${colIndex - 1}), #${id} tr td:nth-child(${colIndex - 1})`
269
+ )
270
+ .forEach(cel => {
271
+ const width =
272
+ (cel.style.maxWidth === '' ? cel.offsetWidth : parseInt(cel.style.maxWidth)) + delta
273
+ cel.style.width = (width > 32 ? width : 32) + 'px'
274
+ cel.style.maxWidth = (width > 32 ? width : 32) + 'px'
275
+ })
276
+ } else {
277
+ // in regular mode we shrink one and enlarge the other col
278
+ document.querySelectorAll(`#${id} tr td:nth-child(${colIndex - 1})`).forEach(cel => {
279
+ const width = cel.offsetWidth + delta
280
+ cel.style.width = (width > 32 ? width : 32) + 'px'
281
+ })
282
+ document.querySelectorAll(`#${id} tr td:nth-child(${colIndex})`).forEach(cel => {
283
+ const width = cel.offsetWidth - delta
284
+ cel.style.width = (width > 32 ? width : 32) + 'px'
285
+ })
264
286
  }
265
287
  }
266
288
  })
267
289
 
268
290
  document.addEventListener('mouseup', mouseup => {
269
- if (column !== null) {
291
+ if (nextCol !== null) {
270
292
  mouseup.preventDefault()
271
293
  lastX = 0
272
- column = null
294
+ nextCol = null
273
295
  }
274
296
  })
275
297
  }
276
298
 
299
+ function getColIndex (col) {
300
+ // calculate index of a col in a row
301
+ let colNo = 0
302
+ while (col) {
303
+ colNo++
304
+ col = col.previousElementSibling
305
+ }
306
+ return colNo
307
+ }
308
+
277
309
 
278
310
 
279
311
  // -----------------------------------------------------------------------------