canvas-datagrid 0.4.1 → 0.4.4

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/dist/types.d.ts CHANGED
@@ -463,16 +463,16 @@ declare class canvasDatagrid {
463
463
  * @param dontDraw - Suppress the draw method after the selection change.
464
464
  */
465
465
  static selectAll(dontDraw: boolean): void;
466
- /**
467
- * Returns true if indices of columns next to the selected columnIndex is selected on every row.
468
- * @param columnIndex - The column index to check.
469
- */
470
- static isMultiColumnsSelected(columnIndex: number): void;
471
466
  /**
472
467
  * Returns true if the selected columnIndex is selected on every row.
473
468
  * @param columnIndex - The column index to check.
474
469
  */
475
470
  static isColumnSelected(columnIndex: number): void;
471
+ /**
472
+ * Returns true if the selected rowIndex is selected on every column.
473
+ * @param rowIndex - The row index to check.
474
+ */
475
+ static isRowSelected(rowIndex: number): void;
476
476
  /**
477
477
  * Runs the defined method on each selected cell.
478
478
  * @param fn - The function to execute. The signature of the function is: (data, rowIndex, columnName).
@@ -516,6 +516,22 @@ declare class canvasDatagrid {
516
516
  * @returns header with the selected name, or undefined.
517
517
  */
518
518
  static getHeaderByName(name: string): header;
519
+ /**
520
+ * Hide column/columns
521
+ */
522
+ static hideColumns(beginColumnOrderIndex: number, endColumnOrderIndex?: number): void;
523
+ /**
524
+ * Unihde column/columns
525
+ */
526
+ static unhideColumns(beginColumnOrderIndex: number, endColumnOrderIndex?: number): void;
527
+ /**
528
+ * Hide rows
529
+ */
530
+ static hideRows(beginRowIndex: number, endRowIndex: number): void;
531
+ /**
532
+ * Unhide rows
533
+ */
534
+ static unhideRows(beginRowIndex: number, endRowIndex: number): void;
519
535
  /**
520
536
  * Resizes a column to fit the longest value in the column. Call without a value to resize all columns.
521
537
  * Warning, can be slow on very large record sets (1m records ~3-5 seconds on an i7).
@@ -620,6 +636,12 @@ declare class canvasDatagrid {
620
636
  * @returns cell at the selected location.
621
637
  */
622
638
  static getVisibleCellByIndex(x: number, y: number): cell;
639
+ /**
640
+ * Get an unhide indicator at grid pixel coordinate x and y.
641
+ * @param x - Number of pixels from the left.
642
+ * @param y - Number of pixels from the top.
643
+ */
644
+ static getUnhideIndicator(x: number, y: number): void;
623
645
  /**
624
646
  * Get a column group at grid pixel coordinate x and y.
625
647
  * @param x - Number of pixels from the left.
@@ -2186,3 +2208,10 @@ declare namespace canvasDatagrid {
2186
2208
  }
2187
2209
  }
2188
2210
 
2211
+ /**
2212
+ * Merge a new hidden row range into existed ranges array
2213
+ * @param hiddenRowRanges - tuples: Array<[bgeinRowIndex, endRowIndex]>
2214
+ * @param newRange - tuple: [beginRowIndex, endRowIndex]
2215
+ */
2216
+ declare function mergeHiddenRowRanges(hiddenRowRanges: any[], newRange: number[]): boolean;
2217
+
@@ -538,8 +538,114 @@ export default function (self) {
538
538
  });
539
539
  }
540
540
  }
541
+
542
+ /**
543
+ * Return a tuple if the user selected contiguous columns, otherwise `null`.
544
+ * Info: Because the user may reorder the columns,
545
+ * the schemaIndex of the first item may be greater than the schemaIndex of the second item,
546
+ * but the columnIndex of the firs item must less than the columnIndex of the second item.
547
+ * @param {any[]} schema from `self.getSchema()`
548
+ * @returns {any[]} column schemas tuple (each schema has an additional field `schemaIndex`)
549
+ */
550
+ function getSelectedContiguousColumns(ev, schema) {
551
+ const memoKey = '__contiguousColumns';
552
+ if (Array.isArray(ev[memoKey]) || ev[memoKey] === null) return ev[memoKey];
553
+ ev[memoKey] = null;
554
+
555
+ if (!Array.isArray(self.selections) || self.selections.length === 0) return;
556
+ const selection = self.selections[0];
557
+ if (!selection || selection.length === 0) return;
558
+ for (let rowIndex = 0; rowIndex < self.viewData.length; rowIndex++) {
559
+ const row = self.viewData[rowIndex];
560
+ if (!row) continue;
561
+ const compare = self.selections[rowIndex];
562
+ if (!compare) return;
563
+ if (compare.length !== selection.length) return;
564
+ for (let i = 0; i < selection.length; i++)
565
+ if (selection[i] !== compare[i]) return;
566
+ }
567
+ selection.sort((a, b) => a - b);
568
+
569
+ /** @type {number[][]} */
570
+ const ranges = [];
571
+ let begin = selection[0];
572
+ let end = selection[0];
573
+ for (let i = 1; i < selection.length; i++) {
574
+ const orderIndex = selection[i];
575
+ if (orderIndex === end + 1) {
576
+ end = orderIndex;
577
+ continue;
578
+ }
579
+ ranges.push([begin, end]);
580
+ begin = orderIndex;
581
+ end = orderIndex;
582
+ }
583
+ ranges.push([begin, end]);
584
+
585
+ const currentOrderIndex = ev.cell.columnIndex;
586
+ const matchedRange = ranges.find(
587
+ (range) =>
588
+ currentOrderIndex >= range[0] &&
589
+ currentOrderIndex <= range[1] &&
590
+ range[0] !== range[1],
591
+ );
592
+ if (!matchedRange) return;
593
+
594
+ /** @type {number[]} orders[index] => columnIndex */
595
+ const orders = self.orders.columns;
596
+ if (!Array.isArray(orders)) return;
597
+
598
+ const matchedSchema = matchedRange.map((orderIndex) => {
599
+ const schemaIndex = orders[orderIndex];
600
+ const thisSchema = schema[schemaIndex];
601
+ return Object.assign({}, thisSchema, { orderIndex });
602
+ });
603
+ if (matchedSchema.findIndex((it) => !it) >= 0) return;
604
+ return (ev[memoKey] = matchedSchema);
605
+ }
606
+ /**
607
+ * @param {boolean} [allowOnlyOneRow]
608
+ * @returns {number[]} a rowIndex tuple. It can contains one row index or two row indexes.
609
+ */
610
+ function getSelectedContiguousRows(allowOnlyOneRow) {
611
+ let range = [];
612
+ let prev = -2;
613
+ let ok = true;
614
+ self.selections.forEach(function (row, index) {
615
+ if (!ok) return;
616
+ if (prev < -1) {
617
+ prev = index;
618
+ range[0] = index;
619
+ return;
620
+ }
621
+ if (index !== prev + 1 || !row || row.length === 0) {
622
+ ok = false;
623
+ return;
624
+ }
625
+ prev = index;
626
+ range[1] = index;
627
+ });
628
+ if (ok) {
629
+ if (range.length === 1) return allowOnlyOneRow ? range : null;
630
+ return range;
631
+ }
632
+ }
541
633
  function addDefaultContextMenuItem(e) {
542
- var isNormalCell =
634
+ const schema = self.getSchema();
635
+ /**
636
+ * A map between columnIndex and column data
637
+ * @type {Map<string,any>}
638
+ */
639
+ let columns;
640
+ const getColumnsMap = () => {
641
+ if (!columns)
642
+ columns = new Map(schema.map((_col) => [_col.columnIndex, _col]));
643
+ return columns;
644
+ };
645
+ const isSorting =
646
+ self.orderings.columns && self.orderings.columns.length > 0;
647
+
648
+ const isNormalCell =
543
649
  !(
544
650
  e.cell.isBackground ||
545
651
  e.cell.isColumnHeaderCellCap ||
@@ -614,20 +720,30 @@ export default function (self) {
614
720
  const columnOrderIndex = e.cell.columnIndex;
615
721
  const columnIndex = self.orders.columns[columnOrderIndex];
616
722
 
723
+ const contiguousColumns = getSelectedContiguousColumns(e, schema);
724
+ let title = '';
725
+ if (contiguousColumns) {
726
+ title = contiguousColumns
727
+ .map((col) => col.title || col.name)
728
+ .join('-');
729
+ } else {
730
+ const column = schema[columnIndex];
731
+ if (column) title = column.title || column.name;
732
+ }
617
733
  e.items.push({
618
- title: self.attributes.hideColumnText.replace(
619
- /%s/gi,
620
- e.cell.header.title || e.cell.header.name,
621
- ),
734
+ title: self.attributes.hideColumnText.replace(/%s/gi, title),
622
735
  click: function (ev) {
623
- self.getSchema()[columnIndex].hidden = true;
624
736
  ev.preventDefault();
625
737
  self.stopPropagation(ev);
626
738
  self.disposeContextMenu();
627
- self.setStorageData();
628
- setTimeout(function () {
629
- self.resize(true);
630
- }, 10);
739
+ if (contiguousColumns) {
740
+ self.hideColumns(
741
+ contiguousColumns[0].orderIndex,
742
+ contiguousColumns[1].orderIndex,
743
+ );
744
+ } else {
745
+ self.hideColumns(columnOrderIndex);
746
+ }
631
747
  },
632
748
  });
633
749
  }
@@ -689,6 +805,51 @@ export default function (self) {
689
805
  },
690
806
  });
691
807
  }
808
+
809
+ //#region hide rows
810
+ const canHideRows = !isSorting && e.cell.isRowHeader && e.cell.header;
811
+ if (canHideRows) {
812
+ const range = getSelectedContiguousRows(true);
813
+ if (range) {
814
+ const boundRowIndexes = range.map((viewRowIndex) =>
815
+ self.getBoundRowIndexFromViewRowIndex(viewRowIndex),
816
+ );
817
+ let title;
818
+ if (boundRowIndexes.length === 1) {
819
+ if (typeof boundRowIndexes[0] === 'number')
820
+ title = boundRowIndexes[0] + 1;
821
+ else title = range[0] + 1;
822
+
823
+ title = self.attributes.showHideRow.replace('%s', title);
824
+ // hide one row
825
+ e.items.push({
826
+ title,
827
+ click: function (ev) {
828
+ ev.preventDefault();
829
+ self.hideRows(boundRowIndexes[0], boundRowIndexes[0]);
830
+ },
831
+ });
832
+ } else if (boundRowIndexes[0] <= boundRowIndexes[1]) {
833
+ title = boundRowIndexes
834
+ .map((it, index) => {
835
+ if (typeof it === 'number') return it + 1;
836
+ return range[index] + 1;
837
+ })
838
+ .join('-');
839
+ title = self.attributes.showHideRows.replace('%s', title);
840
+ // hide rows
841
+ e.items.push({
842
+ title,
843
+ click: function (ev) {
844
+ ev.preventDefault();
845
+ self.hideRows(boundRowIndexes[0], boundRowIndexes[1]);
846
+ },
847
+ });
848
+ }
849
+ }
850
+ }
851
+ //#endregion hide rows
852
+
692
853
  //#region group/ungroup columns
693
854
  const groupAreaHeight = self.getColumnGroupAreaHeight();
694
855
  const groupAreaWidth = self.getRowGroupAreaWidth();
@@ -760,28 +921,13 @@ export default function (self) {
760
921
  const canUngroupColumns =
761
922
  self.attributes.allowGroupingColumns && e.cell.isColumnHeader;
762
923
  const canGroupByRows =
763
- self.attributes.allowGroupingRows && e.cell.isRowHeader && e.cell.header;
924
+ !isSorting &&
925
+ self.attributes.allowGroupingRows &&
926
+ e.cell.isRowHeader &&
927
+ e.cell.header;
764
928
  const canUngroupRows =
765
929
  self.attributes.allowGroupingRows && e.cell.isRowHeader;
766
930
 
767
- /**
768
- * The value for storing the return value from `self.getSchema()`
769
- * @type {any[]}
770
- */
771
- let schema;
772
- /**
773
- * A map between columnIndex and column data
774
- * @type {Map<string,any>}
775
- */
776
- let columns;
777
- const getColumnsMap = () => {
778
- if (!columns) {
779
- if (!schema) schema = self.getSchema();
780
- columns = new Map(schema.map((_col) => [_col.columnIndex, _col]));
781
- }
782
- return columns;
783
- };
784
-
785
931
  if (canGroupByColumns) {
786
932
  /** @type {number[]} */
787
933
  const groupIndexes = [];
@@ -863,23 +1009,7 @@ export default function (self) {
863
1009
  }
864
1010
  }
865
1011
  if (canGroupByRows) {
866
- let range = [];
867
- let prev = -2;
868
- let ok = true;
869
- self.selections.forEach(function (row, index) {
870
- if (!ok) return;
871
- if (prev < -1) {
872
- prev = index;
873
- range[0] = index;
874
- return;
875
- }
876
- if (index !== prev + 1 || !row || row.length === 0) {
877
- ok = false;
878
- return;
879
- }
880
- prev = index;
881
- range[1] = index;
882
- });
1012
+ const range = getSelectedContiguousRows(false) || [];
883
1013
  const rangeTitle = range
884
1014
  .map((rowIndex) => {
885
1015
  const index = self.getBoundRowIndexFromViewRowIndex(rowIndex);
@@ -888,7 +1018,6 @@ export default function (self) {
888
1018
  })
889
1019
  .join('-');
890
1020
  if (
891
- ok &&
892
1021
  range.length === 2 &&
893
1022
  self.isNewGroupRangeValid(self.groupedRows, range[0], range[1])
894
1023
  ) {
@@ -999,7 +1128,7 @@ export default function (self) {
999
1128
  function createDisposeEvent() {
1000
1129
  requestAnimationFrame(function () {
1001
1130
  document.addEventListener('click', self.disposeContextMenu);
1002
- document.removeEventListener('mouseup', createDisposeEvent);
1131
+ window.removeEventListener('mouseup', createDisposeEvent);
1003
1132
  });
1004
1133
  }
1005
1134
  var contextPosition,
@@ -1047,7 +1176,7 @@ export default function (self) {
1047
1176
  }
1048
1177
  self.contextMenu = createContextMenu(ev, contextPosition, items);
1049
1178
  if (e.type == 'mousedown') {
1050
- document.addEventListener('mouseup', createDisposeEvent);
1179
+ window.addEventListener('mouseup', createDisposeEvent);
1051
1180
  } else {
1052
1181
  createDisposeEvent();
1053
1182
  }
package/lib/defaults.js CHANGED
@@ -45,6 +45,10 @@ export default function (self) {
45
45
  ['filterFrozenRows', true],
46
46
  ['globalRowResize', false],
47
47
  ['hideColumnText', 'Hide %s'],
48
+ ['showUnhideColumnsIndicator', false],
49
+ ['showUnhideRowsIndicator', false],
50
+ ['showHideRow', 'Hide row %s'],
51
+ ['showHideRows', 'Hide rows %s'],
48
52
  ['hoverMode', 'cell'],
49
53
  ['keepFocusOnMouseOut', false],
50
54
  ['maxAutoCompleteItems', 200],
@@ -291,14 +295,16 @@ export default function (self) {
291
295
  ['filterButtonHoverBackgroundColor', 'rgba(235, 235, 235, 1)'],
292
296
  ['filterButtonMenuOffsetTop', 10],
293
297
  ['filterButtonWidth', 20],
294
- ['frozenMarkerHoverColor', 'rgba(236, 243, 255, 1)'],
295
- ['frozenMarkerHoverBorderColor', 'rgba(110, 168, 255, 1)'],
296
- ['frozenMarkerActiveColor', 'rgba(236, 243, 255, 1)'],
297
- ['frozenMarkerActiveBorderColor', 'rgba(110, 168, 255, 1)'],
298
- ['frozenMarkerColor', 'rgba(222, 222, 222, 1)'],
299
- ['frozenMarkerBorderColor', 'rgba(168, 168, 168, 1)'],
298
+ ['frozenMarkerHoverColor', 'rgba(165, 198, 254, 1)'],
299
+ ['frozenMarkerHoverBorderColor', 'rgba(165, 198, 254, 1)'],
300
+ ['frozenMarkerActiveColor', 'rgba(165, 198, 254, 0.2)'],
301
+ ['frozenMarkerActiveBorderColor', 'rgba(165, 198, 254, 0.5)'],
302
+ ['frozenMarkerActiveHeaderColor', 'rgba(165, 198, 254, 1)'],
303
+ ['frozenMarkerColor', 'rgba(221, 221, 221, 1)'],
304
+ ['frozenMarkerBorderColor', 'rgba(221, 221, 221, 1)'],
300
305
  ['frozenMarkerBorderWidth', 1],
301
- ['frozenMarkerWidth', 2],
306
+ ['frozenMarkerHeaderColor', 'rgba(188, 188, 188, 1)'],
307
+ ['frozenMarkerWidth', 4],
302
308
  ['groupingAreaBackgroundColor', 'rgba(240, 240, 240, 1)'],
303
309
  ['gridBackgroundColor', 'rgba(240, 240, 240, 1)'],
304
310
  ['gridBorderCollapse', 'collapse'],
@@ -377,6 +383,10 @@ export default function (self) {
377
383
  ['treeArrowMarginTop', 6],
378
384
  ['treeArrowWidth', 13],
379
385
  ['treeGridHeight', 250],
386
+ ['unhideIndicatorColor', 'rgba(0, 0, 0, 1)'],
387
+ ['unhideIndicatorBackgroundColor', 'rgba(255, 255, 255, 1)'],
388
+ ['unhideIndicatorBorderColor', 'rgba(174, 193, 232, 1)'],
389
+ ['unhideIndicatorSize', 16],
380
390
  ['width', 'auto'],
381
391
  ],
382
392
  };
package/lib/docs.js CHANGED
@@ -973,6 +973,22 @@
973
973
  * @param {object} e.ctx Canvas context.
974
974
  * @param {canvasDatagrid.cell} e.cell The cell being moved out of.
975
975
  */
976
+ /**
977
+ * Fires column(s) are being hidden
978
+ * @event
979
+ * @name canvasDatagrid#columnhide
980
+ * @param {object} e Event object.
981
+ * @param {object} e.ctx Canvas context.
982
+ * @param {columnIndex} e.columnIndex Index of column being hidden.
983
+ */
984
+ /**
985
+ * Fires column(s) are being unhidden
986
+ * @event
987
+ * @name canvasDatagrid#columnunhide
988
+ * @param {object} e Event object.
989
+ * @param {object} e.ctx Canvas context.
990
+ * @param {columnIndex} e.columnIndex Index of column being unhidden.
991
+ */
976
992
  /**
977
993
  * Fires when the mouse enters a row.
978
994
  * @event
package/lib/dom.js CHANGED
@@ -370,6 +370,19 @@ export default function (self) {
370
370
  var shouldClearCellValue = cellValueIsEmpty || inEnterMode;
371
371
 
372
372
  self.input.value = shouldClearCellValue ? '' : cell.value;
373
+ let width = Math.round(
374
+ self.input.value.length *
375
+ parseInt(self.style.editCellFontSize, 10) *
376
+ 0.7 *
377
+ self.scale,
378
+ );
379
+ if (width + self.style.cellPaddingLeft < cell.width) {
380
+ width = cell.width - self.style.cellPaddingLeft;
381
+ } else if (width > self.width - cell.x - self.style.scrollBarWidth) {
382
+ width = self.width - cell.x - self.style.scrollBarWidth;
383
+ self.input.style.height = 'auto';
384
+ }
385
+ self.input.style.width = width + 'px';
373
386
  self.input.focus();
374
387
  self.input.addEventListener('click', self.stopPropagation);
375
388
  self.input.addEventListener('dblclick', self.stopPropagation);
@@ -450,6 +463,20 @@ export default function (self) {
450
463
  }
451
464
  self.scrollIntoView(nx, ny);
452
465
  self.beginEditAt(nx, ny, e);
466
+ } else {
467
+ let width = Math.round(
468
+ self.input.value.length *
469
+ parseInt(self.style.editCellFontSize, 10) *
470
+ 0.7 *
471
+ self.scale,
472
+ );
473
+ if (width + self.style.cellPaddingLeft < cell.width) {
474
+ width = cell.width - self.style.cellPaddingLeft;
475
+ } else if (width > self.width - cell.x - self.style.scrollBarWidth) {
476
+ width = self.width - cell.x - self.style.scrollBarWidth;
477
+ self.input.style.height = 'auto';
478
+ }
479
+ self.input.style.width = width + 'px';
453
480
  }
454
481
  });
455
482
 
@@ -773,11 +800,11 @@ export default function (self) {
773
800
  });
774
801
  self.eventParent.addEventListener('scroll', self.resize, false);
775
802
  self.eventParent.addEventListener('touchstart', self.touchstart, false);
776
- self.eventParent.addEventListener('mouseup', self.mouseup, false);
777
803
  self.eventParent.addEventListener('mousedown', self.mousedown, false);
778
804
  self.eventParent.addEventListener('dblclick', self.dblclick, false);
779
805
  self.eventParent.addEventListener('click', self.click, false);
780
- document.body.addEventListener('mousemove', self.mousemove);
806
+ window.addEventListener('mouseup', self.mouseup, false);
807
+ window.addEventListener('mousemove', self.mousemove);
781
808
  self[self.isChildGrid ? 'parentGrid' : 'eventParent'].addEventListener(
782
809
  'wheel',
783
810
  self.scrollWheel,