canvas-datagrid 0.4.5 → 0.4.7

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.
package/lib/defaults.js CHANGED
@@ -61,6 +61,7 @@ export default function (self) {
61
61
  ['persistantSelectionMode', false],
62
62
  ['removeFilterOptionText', 'Remove filter on %s'],
63
63
  ['reorderDeadZone', 3],
64
+ ['resizeAfterDragged', false],
64
65
  ['resizeScrollZone', 20],
65
66
  ['rowGrabZoneSize', 5],
66
67
  ['columnGrabZoneSize', 30],
@@ -336,6 +337,8 @@ export default function (self) {
336
337
  ['reorderMarkerBorderWidth', 1.25],
337
338
  ['reorderMarkerIndexBorderColor', 'rgba(66, 133, 244, 1)'],
338
339
  ['reorderMarkerIndexBorderWidth', 2.75],
340
+ ['resizeMarkerColor', 'rgba(0, 0, 0, 0.2)'],
341
+ ['resizeMarkerSize', 2],
339
342
  ['rowGroupColumnWidth', 25],
340
343
  ['rowHeaderCellBackgroundColor', 'rgba(240, 240, 240, 1)'],
341
344
  ['rowHeaderCellBorderColor', 'rgba(200, 200, 200, 1)'],
package/lib/docs.js CHANGED
@@ -82,6 +82,7 @@
82
82
  * @param {string} [args.rowGroupIndicatorPosition='bottom'] - Ths position of the row group indicator(icon), available values: 'top' and 'bottom'.
83
83
  * @param {boolean} [args.showOrderByOption=true] - Show an option on the context menu sort rows.
84
84
  * @param {boolean} [args.showPaste=false] - Show the paste option in the context menu when applicable.
85
+ * @param {boolean} [args.resizeAfterDragged] - Resize after the handle is dropped, similar to Excel.
85
86
  * @param {array} [args.schema=[]] - Sets the schema. See {@link canvasDatagrid.schema}.
86
87
  * @param {array} [args.data=[]] - Sets the data. See {@link canvasDatagrid.data}.
87
88
  * @param {number} [args.touchReleaseAnimationDurationMs=2000] - How long the ease animation runs after touch release.
@@ -306,8 +307,8 @@
306
307
  * @property {string} [activeCellSelectedBackgroundColor=rgba(236, 243, 255, 1)] - Style of activeCellSelectedBackgroundColor.
307
308
  * @property {string} [activeCellSelectedColor=rgba(0, 0, 0, 1)] - Style of activeCellSelectedColor.
308
309
  * @property {string} [activeCellVerticalAlignment=center] - Style of activeCellVerticalAlignment.
309
- * @property {string} [activeHeaderCellBackgroundColor=rgba(225, 225, 225, 1)] - Style of activeHeaderCellBackgroundColor.
310
- * @property {string} [activeHeaderCellColor=rgba(0, 0, 0, 1)] - Style of activeHeaderCellColor.
310
+ * @property {string} [activeColumnHeaderCellBackgroundColor=rgba(225, 225, 225, 1)] - Style of activeColumnHeaderCellBackgroundColor.
311
+ * @property {string} [activeColumnHeaderCellColor=rgba(0, 0, 0, 1)] - Style of activeColumnHeaderCellColor.
311
312
  * @property {string} [activeRowHeaderCellBackgroundColor=rgba(225, 225, 225, 1)] - Style of activeRowHeaderCellBackgroundColor.
312
313
  * @property {string} [activeRowHeaderCellColor=rgba(0, 0, 0, 1)] - Style of activeRowHeaderCellColor.
313
314
  * @property {number} [autocompleteBottomMargin=60] - Style of autocompleteBottomMargin.
@@ -400,9 +401,12 @@
400
401
  * @property {number} [gridBorderWidth=1] - Style of gridBorderWidth.
401
402
  * @property {string} [groupIndicatorColor=rgba(155, 155, 155, 1)'] - Foreground color of the group indicator(icon).
402
403
  * @property {string} [groupIndicatorBackgroundColor=rgba(255, 255, 255, 1)'] - Background color of the group indicator(icon).
403
- * @property {string} [columnHeaderCellCapBackgroundColor=rgba(240, 240, 240, 1)] - Style of columnHeaderCellBackgroundColor.
404
- * @property {string} [columnHeaderCellCapBorderColor=rgba(172, 172, 172, 1)] - Style of columnHeaderCellBackgroundColor.
405
- * @property {number} [columnHeaderCellCapBorderWidth=1] - Style of columnHeaderCellBackgroundColor.
404
+ * @property {string} [columnHeaderCellBackgroundColor=rgba(240, 240, 240, 1)] - Style of columnHeaderCellBackgroundColor.
405
+ * @property {string} [columnHeaderCellBorderColor=rgba(172, 172, 172, 1)] - Style of columnHeaderCellBackgroundColor.
406
+ * @property {number} [columnHeaderCellBorderWidth=1] - Style of columnHeaderCellBackgroundColor.
407
+ * @property {string} [columnHeaderCellCapBackgroundColor=rgba(240, 240, 240, 1)] - Style of columnHeaderCellCapBackgroundColor.
408
+ * @property {string} [columnHeaderCellCapBorderColor=rgba(172, 172, 172, 1)] - Style of columnHeaderCellCapBackgroundColor.
409
+ * @property {number} [columnHeaderCellCapBorderWidth=1] - Style of columnHeaderCellCapBackgroundColor.
406
410
  * @property {string} [columnHeaderCellBorderColor=rgba(152, 152, 152, 1)] - Style of columnHeaderCellBorderColor.
407
411
  * @property {number} [columnHeaderCellBorderWidth=0.25] - Style of columnHeaderCellBorderWidth.
408
412
  * @property {string} [columnHeaderCellColor=rgba(50, 50, 50, 1)] - Style of columnHeaderCellColor.
@@ -426,6 +430,7 @@
426
430
  * @property {number} [columnHeaderOrderByArrowWidth=13] - Style of columnHeaderOrderByArrowWidth.
427
431
  * @property {number} [rowHeaderCellWidth=57] - Style of rowHeaderCellWidth.
428
432
  * @property {number} [minColumnWidth=45] - Style of minColumnWidth.
433
+ * @property {(number|string)} [height=auto] - Style of height.
429
434
  * @property {number} [minHeight=24] - Style of minHeight.
430
435
  * @property {number} [minRowHeight=24] - Style of minRowHeight.
431
436
  * @property {string} [name=default] - Style of name.
@@ -434,6 +439,8 @@
434
439
  * @property {number} [reorderMarkerBorderWidth=1.25] - Style of reorderMarkerBorderWidth.
435
440
  * @property {string} [reorderMarkerIndexBorderColor=rgba(66, 133, 244, 1)] - Style of reorderMarkerIndexBorderColor.
436
441
  * @property {number} [reorderMarkerIndexBorderWidth=2.75] - Style of reorderMarkerIndexBorderWidth.
442
+ * @property {string} [resizeMarkerColor=rgba(0, 0, 0, 0.2)] - Style of resizeMarkerColor.
443
+ * @property {number} [resizeMarkerSize=2] - Style of resizeMarkerSize.
437
444
  * @property {number} [rowGroupColumnWidth=25] - Style of rowGroupColumnWidth.
438
445
  * @property {string} [rowHeaderCellBackgroundColor=rgba(240, 240, 240, 1)] - Style of rowHeaderCellBackgroundColor.
439
446
  * @property {string} [rowHeaderCellBorderColor=rgba(152, 152, 152, 1)] - Style of rowHeaderCellBorderColor.
@@ -493,6 +500,7 @@
493
500
  * @property {number} [frozenMarkerWidth=1] - Style of frozenMarkerWidth.
494
501
  * @property {string} [overflowY=auto] - When set to hidden, vertical scroll bar will be hidden. When set to auto vertical scroll bar will appear when data overflows the height. When set to scroll the vertical scrollbar will always be visible.
495
502
  * @property {string} [overflowX=auto] - When set to hidden, horizontal scroll bar will be hidden. When set to auto horizontal scroll bar will appear when data overflows the width. When set to scroll the horizontal scrollbar will always be visible.
503
+ * @property {(number|string)} [width=auto] - Style of width.
496
504
 
497
505
  ['', 'normal'],
498
506
  */
package/lib/dom.js CHANGED
@@ -755,6 +755,7 @@ export default function (self) {
755
755
  Object.keys(css[className]).map(function (prop) {
756
756
  el.style[prop] = css[className][prop];
757
757
  });
758
+ el.className = className;
758
759
  }
759
760
  return;
760
761
  };
@@ -803,7 +804,7 @@ export default function (self) {
803
804
  self.eventParent.addEventListener('mousedown', self.mousedown, false);
804
805
  self.eventParent.addEventListener('dblclick', self.dblclick, false);
805
806
  self.eventParent.addEventListener('click', self.click, false);
806
- window.addEventListener('mouseup', self.mouseup, false);
807
+ self.eventParent.addEventListener('mouseup', self.mouseup, false);
807
808
  window.addEventListener('mousemove', self.mousemove);
808
809
  self[self.isChildGrid ? 'parentGrid' : 'eventParent'].addEventListener(
809
810
  'wheel',
package/lib/draw.js CHANGED
@@ -2,6 +2,8 @@
2
2
  /*globals XMLSerializer: false, define: true, Blob: false, MutationObserver: false, requestAnimationFrame: false, performance: false, btoa: false*/
3
3
  'use strict';
4
4
 
5
+ import { isCellSelected } from './selections/util';
6
+
5
7
  export default function (self) {
6
8
  var perfCounters = [],
7
9
  cachedImagesDrawn = false,
@@ -995,21 +997,23 @@ export default function (self) {
995
997
  propPrefix,
996
998
  offsetPoint,
997
999
  ) {
998
- offsetPoint = offsetPoint || { x: 0, y: 0 };
999
1000
  cell.selectionBorder = '';
1000
- if (
1001
- !cell.isRowHeader &&
1002
- self.selections[cell.rowIndex + -offsetPoint.y] &&
1003
- self.selections[cell.rowIndex + -offsetPoint.y].indexOf(
1004
- cell.columnIndex + -offsetPoint.x,
1005
- ) !== -1
1006
- ) {
1001
+ // TODO: optimize it by `getSelectionStateFromCells`
1002
+ const isSelected = offsetPoint
1003
+ ? (row, col) =>
1004
+ isCellSelected(
1005
+ self.selections,
1006
+ row - offsetPoint.y,
1007
+ col - offsetPoint.x,
1008
+ )
1009
+ : (row, col) => isCellSelected(self.selections, row, col);
1010
+ const lastColumnIndex = Math.max(schema.length - 1, 0);
1011
+ const lastRowIndex = Math.max(viewData.length - 1, 0);
1012
+ const { isRowHeader, rowIndex, columnIndex } = cell;
1013
+
1014
+ if (!isRowHeader && isSelected(rowIndex, columnIndex)) {
1007
1015
  if (
1008
- (!self.selections[cell.rowIndex - 1 + -offsetPoint.y] ||
1009
- self.selections[cell.rowIndex - 1 + -offsetPoint.y].indexOf(
1010
- cell.columnIndex + -offsetPoint.x,
1011
- ) === -1 ||
1012
- cell.rowIndex === 0) &&
1016
+ (!isSelected(rowIndex - 1, columnIndex) || rowIndex === 0) &&
1013
1017
  !cell.isHeader
1014
1018
  ) {
1015
1019
  drawArray.push([cell, 't']);
@@ -1017,32 +1021,21 @@ export default function (self) {
1017
1021
  cell[propPrefix + 'Border'] += 't';
1018
1022
  }
1019
1023
  if (
1020
- !self.selections[cell.rowIndex + 1 + -offsetPoint.y] ||
1021
- self.selections[cell.rowIndex + 1 + -offsetPoint.y].indexOf(
1022
- cell.columnIndex + -offsetPoint.x,
1023
- ) === -1
1024
+ rowIndex >= lastRowIndex ||
1025
+ !isSelected(rowIndex + 1, columnIndex)
1024
1026
  ) {
1025
1027
  drawArray.push([cell, 'b']);
1026
1028
  cell[propPrefix + 'BorderBottom'] = true;
1027
1029
  cell[propPrefix + 'Border'] += 'b';
1028
1030
  }
1029
- if (
1030
- !self.selections[cell.rowIndex + -offsetPoint.y] ||
1031
- cell.columnIndex === 0 ||
1032
- self.selections[cell.rowIndex + -offsetPoint.y].indexOf(
1033
- cell.columnIndex - 1 + -offsetPoint.x,
1034
- ) === -1
1035
- ) {
1031
+ if (columnIndex === 0 || !isSelected(rowIndex, columnIndex - 1)) {
1036
1032
  drawArray.push([cell, 'l']);
1037
1033
  cell[propPrefix + 'BorderLeft'] = true;
1038
1034
  cell[propPrefix + 'Border'] += 'l';
1039
1035
  }
1040
1036
  if (
1041
- !self.selections[cell.rowIndex + -offsetPoint.y] ||
1042
- cell.columnIndex === schema.length ||
1043
- self.selections[cell.rowIndex + -offsetPoint.y].indexOf(
1044
- cell.columnIndex + 1 + -offsetPoint.x,
1045
- ) === -1
1037
+ columnIndex >= lastColumnIndex ||
1038
+ !isSelected(rowIndex, columnIndex + 1)
1046
1039
  ) {
1047
1040
  drawArray.push([cell, 'r']);
1048
1041
  cell[propPrefix + 'BorderRight'] = true;
@@ -1051,6 +1044,10 @@ export default function (self) {
1051
1044
  }
1052
1045
  }
1053
1046
  function drawCell(rowData, rowOrderIndex, rowIndex) {
1047
+ const isActiveRowHeader =
1048
+ self.orders.rows[self.activeCell.rowIndex] === rowOrderIndex ||
1049
+ self.isRowSelected(rowOrderIndex);
1050
+
1054
1051
  return function drawEach(header, headerIndex, columnOrderIndex) {
1055
1052
  if (header.hidden) {
1056
1053
  return 0;
@@ -1066,9 +1063,12 @@ export default function (self) {
1066
1063
  self.filterable.rows.includes(rowIndex) &&
1067
1064
  self.filterable.columns.includes(headerIndex),
1068
1065
  wrap = self.style.cellWhiteSpace !== 'nowrap',
1069
- selected =
1070
- self.selections[rowOrderIndex] &&
1071
- self.selections[rowOrderIndex].indexOf(columnOrderIndex) !== -1,
1066
+ // TODO: optimize it by `getSelectionStateFromCells`
1067
+ selected = isCellSelected(
1068
+ self.selections,
1069
+ rowOrderIndex,
1070
+ columnOrderIndex,
1071
+ ),
1072
1072
  hovered =
1073
1073
  self.hovers.rowIndex === rowOrderIndex &&
1074
1074
  (self.attributes.hoverMode === 'row' ||
@@ -1082,17 +1082,6 @@ export default function (self) {
1082
1082
  isColumnHeaderCellCap = cellStyle === 'columnHeaderCellCap',
1083
1083
  rawValue = rowData ? rowData[header.name] : undefined,
1084
1084
  isGrid = header.type === 'canvas-datagrid',
1085
- activeHeader =
1086
- (self.orders.rows[self.activeCell.rowIndex] === rowOrderIndex ||
1087
- self.selectedRows.indexOf(rowOrderIndex) >= 0 ||
1088
- self.orders.columns[self.activeCell.columnIndex] ===
1089
- headerIndex ||
1090
- self.selectedColumns.indexOf(headerIndex) >= 0) &&
1091
- (columnOrderIndex === -1 || rowOrderIndex === -1)
1092
- ? isRowHeader
1093
- ? 'activeRowHeaderCell'
1094
- : 'activeColumnHeaderCell'
1095
- : false,
1096
1085
  val,
1097
1086
  f = self.formatters[header.type || 'string'],
1098
1087
  orderByArrowSize = 0,
@@ -1103,6 +1092,19 @@ export default function (self) {
1103
1092
  row: rowData,
1104
1093
  header: header,
1105
1094
  };
1095
+
1096
+ const isActiveColumnHeader =
1097
+ self.orders.columns[self.activeCell.columnIndex] === headerIndex ||
1098
+ self.isColumnSelected(self.activeCell.viewColumnIndex);
1099
+ let activeHeader = false;
1100
+ if (
1101
+ (isActiveColumnHeader || isActiveRowHeader) &&
1102
+ (columnOrderIndex === -1 || rowOrderIndex === -1)
1103
+ )
1104
+ activeHeader = isRowHeader
1105
+ ? 'activeRowHeaderCell'
1106
+ : 'activeColumnHeaderCell';
1107
+
1106
1108
  if (isColumnHeaderCellCap) {
1107
1109
  cellWidth = w - x;
1108
1110
  }
@@ -2447,10 +2449,7 @@ export default function (self) {
2447
2449
  self.ctx.lineWidth = self.style.reorderMarkerBorderWidth;
2448
2450
  self.ctx.strokeStyle = self.style.reorderMarkerBorderColor;
2449
2451
  if (self.dragMode === 'row-reorder') {
2450
- for (var k = 0; k < self.selections.length; k++) {
2451
- if (!self.selections[k] || k == self.reorderObject.rowIndex) continue;
2452
- b.height += self.getRowHeight(k);
2453
- }
2452
+ b.height = self.getSelectedRowsHeight();
2454
2453
  b.width = w;
2455
2454
  b.x = 0;
2456
2455
  m.width = w;
@@ -2461,7 +2460,7 @@ export default function (self) {
2461
2460
  self.ctx.lineWidth = self.style.reorderMarkerIndexBorderWidth;
2462
2461
  self.ctx.strokeStyle = self.style.reorderMarkerIndexBorderColor;
2463
2462
  if (
2464
- self.selectedRows.indexOf(self.reorderTarget.sortRowIndex) === -1 &&
2463
+ !self.isRowSelected(self.reorderTarget.sortRowIndex) &&
2465
2464
  self.currentCell.rowIndex !== self.reorderObject.rowIndex &&
2466
2465
  self.currentCell.rowIndex > -1 &&
2467
2466
  self.currentCell.rowIndex < l
@@ -2474,14 +2473,7 @@ export default function (self) {
2474
2473
  );
2475
2474
  }
2476
2475
  } else if (self.dragMode === 'column-reorder' && self.reorderObject) {
2477
- var selectedColumns = self.selections[0];
2478
- if (selectedColumns) {
2479
- for (var k = 1; k < selectedColumns.length; k++) {
2480
- b.width += self.getColumnWidth(
2481
- self.orders.columns[selectedColumns[k]],
2482
- );
2483
- }
2484
- }
2476
+ b.width = self.getSelectedColumnsWidth();
2485
2477
  b.height = h;
2486
2478
  b.y = 0;
2487
2479
  m.height = h;
@@ -2493,8 +2485,7 @@ export default function (self) {
2493
2485
  self.ctx.lineWidth = self.style.reorderMarkerIndexBorderWidth;
2494
2486
  self.ctx.strokeStyle = self.style.reorderMarkerIndexBorderColor;
2495
2487
  if (
2496
- self.selectedColumns.indexOf(self.reorderTarget.sortColumnIndex) ===
2497
- -1 &&
2488
+ !self.isColumnSelected(self.reorderTarget.sortColumnIndex) &&
2498
2489
  self.currentCell.sortColumnIndex !==
2499
2490
  self.reorderObject.sortColumnIndex &&
2500
2491
  self.reorderTarget.columnIndex !== self.reorderObject.columnIndex &&
@@ -2510,6 +2501,31 @@ export default function (self) {
2510
2501
  }
2511
2502
  }
2512
2503
  }
2504
+ function drawResizeMarkers() {
2505
+ if (
2506
+ !self.attributes.resizeAfterDragged ||
2507
+ !self.draggingItem ||
2508
+ !self.pendingDragResize ||
2509
+ (self.dragMode != 'ew-resize' && self.dragMode != 'ns-resize')
2510
+ ) {
2511
+ return;
2512
+ }
2513
+
2514
+ const resizingRow = self.dragMode == 'ns-resize';
2515
+ const height = resizingRow ? self.style.resizeMarkerSize : self.height;
2516
+ const width = resizingRow ? self.width : self.style.resizeMarkerSize;
2517
+ const minX =
2518
+ self.dragStart.x -
2519
+ (self.resizingStartingWidth - self.style.minColumnWidth);
2520
+ const minY =
2521
+ self.dragStart.y -
2522
+ (self.resizingStartingHeight - self.style.minRowHeight);
2523
+ const x = resizingRow ? 0 : Math.max(self.pendingDragResize.x, minX);
2524
+ const y = !resizingRow ? 0 : Math.max(self.pendingDragResize.y, minY);
2525
+
2526
+ self.ctx.fillStyle = self.style.resizeMarkerColor;
2527
+ fillRect(x, y, width, height);
2528
+ }
2513
2529
  function drawBorder() {
2514
2530
  self.ctx.lineWidth = self.style.gridBorderWidth;
2515
2531
  self.ctx.strokeStyle = self.style.gridBorderColor;
@@ -3064,6 +3080,7 @@ export default function (self) {
3064
3080
  drawSelectionHandles();
3065
3081
  drawReorderMarkers();
3066
3082
  drawMoveMarkers();
3083
+ drawResizeMarkers();
3067
3084
  drawBorder();
3068
3085
  drawSelectionBorders();
3069
3086
  drawScrollBars();