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.
@@ -420,259 +420,7 @@ export default function (self) {
420
420
  columnIndex: x,
421
421
  };
422
422
  };
423
- /**
424
- * Removes the selection.
425
- * @memberof canvasDatagrid
426
- * @name selectNone
427
- * @param {boolean} dontDraw Suppress the draw method after the selection change.
428
- * @method
429
- */
430
- self.selectNone = function (dontDraw) {
431
- self.selections = [];
432
- self.selectedRows = [];
433
- self.selectedColumns = [];
434
- self.dispatchEvent('selectionchanged', {
435
- selectedData: self.getSelectedData(),
436
- selections: self.selections,
437
- selectionBounds: self.selectionBounds,
438
- selectedCells: self.getSelectedCells(),
439
- });
440
- if (dontDraw) {
441
- return;
442
- }
443
- self.draw();
444
- };
445
- /**
446
- * Selects every visible cell.
447
- * @memberof canvasDatagrid
448
- * @name selectAll
449
- * @param {boolean} dontDraw Suppress the draw method after the selection change.
450
- * @method
451
- */
452
- self.selectAll = function (dontDraw) {
453
- self.selectArea({
454
- top: 0,
455
- left: -1,
456
- right: self.getSchema().length - 1,
457
- bottom: self.viewData.length - 1,
458
- });
459
- if (dontDraw) {
460
- return;
461
- }
462
- self.draw();
463
- };
464
- /**
465
- * Returns true if the selected columnIndex is selected on every row.
466
- * @memberof canvasDatagrid
467
- * @name isColumnSelected
468
- * @method
469
- * @param {number} columnIndex The column index to check.
470
- */
471
- self.isColumnSelected = function (columnIndex) {
472
- var colIsSelected = true;
473
- self.viewData.forEach(function (row, rowIndex) {
474
- if (
475
- !self.selections[rowIndex] ||
476
- self.selections[rowIndex].indexOf(columnIndex) === -1
477
- ) {
478
- colIsSelected = false;
479
- }
480
- });
481
- return colIsSelected;
482
- };
483
- /**
484
- * Returns true if the selected rowIndex is selected on every column.
485
- * @memberof canvasDatagrid
486
- * @name isRowSelected
487
- * @method
488
- * @param {number} rowIndex The row index to check.
489
- */
490
-
491
- self.isRowSelected = function (rowIndex) {
492
- if (!self.selections[rowIndex]) return false;
493
- var rowIsSelected = true;
494
- var schema = self.getVisibleSchema();
495
- schema.forEach(function (col, index) {
496
- if (
497
- !self.selections[rowIndex] ||
498
- self.selections[rowIndex].indexOf(index) === -1
499
- ) {
500
- rowIsSelected = false;
501
- }
502
- });
503
- return rowIsSelected;
504
- };
505
423
 
506
- /**
507
- * Runs the defined method on each selected cell.
508
- * @memberof canvasDatagrid
509
- * @name forEachSelectedCell
510
- * @method
511
- * @param {number} fn The function to execute. The signature of the function is: (data, rowIndex, columnName).
512
- * @param {number} expandToRow When true the data in the array is expanded to the entire row.
513
- */
514
- self.forEachSelectedCell = function (fn, expandToRow) {
515
- var d = [],
516
- s = expandToRow ? self.getSchema() : self.getVisibleSchema(),
517
- l = self.viewData.length;
518
- self.selections.forEach(function (row, index) {
519
- if (index === l) {
520
- return;
521
- }
522
- if (row.length === 0) {
523
- d[index] = null;
524
- return;
525
- }
526
- d[index] = {};
527
- row.forEach(function (col) {
528
- if (col === -1 || !s[col]) {
529
- return;
530
- }
531
- fn(self.viewData, index, s[col].name);
532
- });
533
- });
534
- };
535
- /**
536
- * Selects a column.
537
- * @memberof canvasDatagrid
538
- * @name selectColumn
539
- * @method
540
- * @param {number} columnIndex The column index to select.
541
- * @param {boolean} toggleSelectMode When true, behaves as if you were holding control/command when you clicked the column.
542
- * @param {boolean} shift When true, behaves as if you were holding shift when you clicked the column.
543
- * @param {boolean} supressSelectionchangedEvent When true, prevents the selectionchanged event from firing.
544
- */
545
- self.selectColumn = function (columnIndex, ctrl, shift, supressEvent) {
546
- var s, e, x;
547
- self.isMultiRowsSelected = false;
548
- self.isMultiColumnsSelected = false;
549
- function addCol(i) {
550
- self.viewData.forEach(function (row, rowIndex) {
551
- self.selections[rowIndex] = self.selections[rowIndex] || [];
552
- if (self.selections[rowIndex].indexOf(i) === -1) {
553
- self.selections[rowIndex].push(i);
554
- }
555
- });
556
- }
557
- function removeCol(i) {
558
- self.viewData.forEach(function (row, rowIndex) {
559
- self.selections[rowIndex] = self.selections[rowIndex] || [];
560
- if (self.selections[rowIndex].indexOf(i) !== -1) {
561
- self.selections[rowIndex].splice(
562
- self.selections[rowIndex].indexOf(i),
563
- 1,
564
- );
565
- }
566
- });
567
- }
568
- if (shift) {
569
- if (!self.activeCell) {
570
- return;
571
- }
572
- s = Math.min(self.activeCell.columnIndex, columnIndex);
573
- e = Math.max(self.activeCell.columnIndex, columnIndex);
574
- for (x = s; e > x; x += 1) {
575
- addCol(x);
576
- if (self.selectedColumns.indexOf(x) === -1)
577
- self.selectedColumns.push(x);
578
- }
579
- if (s != e) self.isMultiColumnsSelected = true;
580
- }
581
- if (!ctrl && !shift) {
582
- self.selections = [];
583
- self.selectedColumns = [];
584
- self.activeCell.columnIndex = columnIndex;
585
- self.activeCell.rowIndex = self.scrollIndexTop;
586
- }
587
- if (ctrl && self.isColumnSelected(columnIndex)) {
588
- removeCol(columnIndex);
589
- self.selectedColumns.splice(columnIndex, 1);
590
- } else {
591
- addCol(columnIndex);
592
- self.selectedColumns.push(columnIndex);
593
- }
594
- if (supressEvent) {
595
- return;
596
- }
597
- self.dispatchEvent('selectionchanged', {
598
- selectedData: self.getSelectedData(),
599
- selections: self.selections,
600
- selectionBounds: self.getSelectionBounds(),
601
- selectedCells: self.getSelectedCells(),
602
- });
603
- };
604
- /**
605
- * Selects a row.
606
- * @memberof canvasDatagrid
607
- * @name selectRow
608
- * @method
609
- * @param {number} rowIndex The row index to select.
610
- * @param {boolean} ctrl When true, behaves as if you were holding control/command when you clicked the row.
611
- * @param {boolean} shift When true, behaves as if you were holding shift when you clicked the row.
612
- * @param {boolean} supressSelectionchangedEvent When true, prevents the selectionchanged event from firing.
613
- */
614
- self.selectRow = function (rowIndex, ctrl, shift, supressEvent) {
615
- var x,
616
- st,
617
- en,
618
- s = self.getVisibleSchema();
619
- self.isMultiRowsSelected = false;
620
- function de() {
621
- if (supressEvent) {
622
- return;
623
- }
624
- self.dispatchEvent('selectionchanged', {
625
- selectedData: self.getSelectedData(),
626
- selections: self.selections,
627
- selectionBounds: self.selectionBounds,
628
- selectedCells: self.getSelectedCells(),
629
- });
630
- }
631
- function addRow(ri) {
632
- self.selections[ri] = [];
633
- self.selections[ri].push(-1);
634
- s.forEach(function (col, index) {
635
- self.selections[ri].push(self.orders.columns.indexOf(col.index));
636
- if (
637
- col.index !== -1 &&
638
- self.selectedColumns.indexOf(
639
- self.orders.columns.indexOf(col.index),
640
- ) === -1
641
- )
642
- self.selectedColumns.push(self.orders.columns.indexOf(col.index));
643
- });
644
- }
645
- if (self.dragAddToSelection === false || self.dragObject === undefined) {
646
- if (
647
- self.selections[rowIndex] &&
648
- self.selections[rowIndex].length - 1 === s.length
649
- ) {
650
- if (ctrl) {
651
- self.selections[rowIndex] = [];
652
- de();
653
- return;
654
- }
655
- }
656
- }
657
- if (self.dragAddToSelection === true || self.dragObject === undefined) {
658
- if (shift && self.dragObject === undefined) {
659
- if (!self.activeCell) {
660
- return;
661
- }
662
- st = Math.min(self.activeCell.rowIndex, rowIndex);
663
- en = Math.max(self.activeCell.rowIndex, rowIndex);
664
- for (x = st; en >= x; x += 1) {
665
- addRow(x);
666
- self.selectedRows.push(x);
667
- }
668
- } else {
669
- addRow(rowIndex);
670
- self.selectedRows = [];
671
- self.selectedRows.push(rowIndex);
672
- }
673
- }
674
- de();
675
- };
676
424
  /**
677
425
  * Collapse a tree grid by row index.
678
426
  * @memberof canvasDatagrid
@@ -953,6 +701,10 @@ export default function (self) {
953
701
  sortFunction,
954
702
  dontSetStorageData,
955
703
  ) {
704
+ if (!self.attributes.allowSorting) {
705
+ self.refresh();
706
+ return;
707
+ }
956
708
  var f,
957
709
  c = self.getSchema().filter(function (col) {
958
710
  return col.name === columnName;
@@ -1242,49 +994,7 @@ export default function (self) {
1242
994
  }
1243
995
  return true;
1244
996
  };
1245
- /**
1246
- * Moves the current selection relative to the its current position. Note: this method does not move the selected data, just the selection itself.
1247
- * @memberof canvasDatagrid
1248
- * @name moveSelection
1249
- * @method
1250
- * @param {number} offsetX The number of columns to offset the selection.
1251
- * @param {number} offsetY The number of rows to offset the selection.
1252
- */
1253
- self.moveSelection = function (offsetX, offsetY) {
1254
- var sel = [];
1255
- self.selectedRows = [];
1256
- self.selectedColumns = [];
1257
- self.selections.forEach(function (row, rowIndex) {
1258
- sel[rowIndex + offsetY] = [];
1259
- self.selectedRows.push(rowIndex + offsetY);
1260
- row.forEach(function (colIndex) {
1261
- sel[rowIndex + offsetY].push(colIndex + offsetX);
1262
- if (self.selectedColumns.indexOf(colIndex + offsetX) === -1)
1263
- self.selectedColumns.push(colIndex + offsetX);
1264
- });
1265
- });
1266
- self.selections = sel;
1267
- };
1268
- /**
1269
- * Deletes currently selected data.
1270
- * @memberof canvasDatagrid
1271
- * @name deleteSelectedData
1272
- * @method
1273
- * @param {boolean} dontDraw Suppress the draw method after the selection change.
1274
- */
1275
- self.deleteSelectedData = function (dontDraw) {
1276
- const affectedCells = self.clearSelectedCells();
1277
997
 
1278
- self.dispatchEvent('afterdelete', {
1279
- cells: affectedCells,
1280
- });
1281
-
1282
- if (dontDraw) {
1283
- return;
1284
- }
1285
-
1286
- requestAnimationFrame(() => self.draw());
1287
- };
1288
998
  /**
1289
999
  * Moves data in the provided selection to another position in the grid. Moving data off the edge of the schema (columns/x) will truncate data.
1290
1000
  * @memberof canvasDatagrid
@@ -1295,6 +1005,11 @@ export default function (self) {
1295
1005
  * @param {number} y The row index to start inserting the selection at.
1296
1006
  */
1297
1007
  self.moveTo = function (sel, x, y) {
1008
+ // TODO: optimize this function,
1009
+ // we just transform the `sel` to obselete selection matrix
1010
+ sel = self.getObsoleteSelectionMatrix(sel);
1011
+
1012
+ // TODO: this function is not migrated done
1298
1013
  var selectedData = self.getSelectedData(),
1299
1014
  visibleSchema = self.getVisibleSchema(),
1300
1015
  selectionLength = sel.length,
@@ -1773,32 +1488,6 @@ export default function (self) {
1773
1488
  isBackground: true,
1774
1489
  };
1775
1490
  };
1776
- /**
1777
- * Gets the bounds of current selection.
1778
- * @returns {rect} selection.
1779
- * @memberof canvasDatagrid
1780
- * @name getSelectionBounds
1781
- * @method
1782
- */
1783
- self.getSelectionBounds = function () {
1784
- var low = { x: Infinity, y: Infinity },
1785
- high = { x: -Infinity, y: -Infinity };
1786
- self.selections.forEach(function (row, rowIndex) {
1787
- var maxCol, minCol;
1788
- low.y = rowIndex < low.y ? rowIndex : low.y;
1789
- high.y = rowIndex > high.y ? rowIndex : high.y;
1790
- maxCol = Math.max.apply(null, row);
1791
- minCol = Math.min.apply(null, row);
1792
- low.x = minCol < low.x ? minCol : low.x;
1793
- high.x = maxCol > high.x ? maxCol : high.x;
1794
- });
1795
- return {
1796
- top: low.y,
1797
- left: low.x,
1798
- bottom: high.y,
1799
- right: high.x,
1800
- };
1801
- };
1802
1491
  /**
1803
1492
  * Returns an auto generated schema based on data structure.
1804
1493
  * @memberof canvasDatagrid
@@ -1845,64 +1534,6 @@ export default function (self) {
1845
1534
  self.clearChangeLog = function () {
1846
1535
  self.changes = [];
1847
1536
  };
1848
- /**
1849
- * Selects an area of the grid.
1850
- * @memberof canvasDatagrid
1851
- * @name selectArea
1852
- * @method
1853
- * @param {rect} bounds A rect object representing the selected values.
1854
- */
1855
- self.selectArea = function (bounds, ctrl) {
1856
- self.selectionBounds = bounds || self.selectionBounds;
1857
- var ev,
1858
- x,
1859
- y,
1860
- s = self.getSchema();
1861
- if (!ctrl) {
1862
- self.selections = [];
1863
- self.selectedRows = [];
1864
- self.selectedColumns = [];
1865
- }
1866
- if (
1867
- self.selectionBounds.top < -1 ||
1868
- self.selectionBounds.bottom > self.viewData.length ||
1869
- self.selectionBounds.left < -1 ||
1870
- self.selectionBounds.right > s.length
1871
- ) {
1872
- throw new Error('Impossible selection area');
1873
- }
1874
- for (
1875
- x = self.selectionBounds.top;
1876
- x <= self.selectionBounds.bottom;
1877
- x += 1
1878
- ) {
1879
- self.selections[x] = [];
1880
- self.selectedRows.push(x);
1881
- for (
1882
- y = self.selectionBounds.left;
1883
- y <= self.selectionBounds.right;
1884
- y += 1
1885
- ) {
1886
- if (self.selections[x].indexOf(y) === -1) {
1887
- self.selections[x].push(y);
1888
- }
1889
- if (y !== -1 && self.selectedColumns.indexOf(y) === -1) {
1890
- self.selectedColumns.push(y);
1891
- }
1892
- }
1893
- }
1894
- ev = {
1895
- selections: self.selections,
1896
- selectionBounds: self.selectionBounds,
1897
- selectedCells: self.getSelectedCells(),
1898
- };
1899
- Object.defineProperty(ev, 'selectedData', {
1900
- get: function () {
1901
- return self.getSelectedData();
1902
- },
1903
- });
1904
- self.dispatchEvent('selectionchanged', ev);
1905
- };
1906
1537
  /**
1907
1538
  * Returns the maximum text width for a given column by column name.
1908
1539
  * @memberof canvasDatagrid