@zeedhi/teknisa-components-common 1.77.2 → 1.78.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.
@@ -675,6 +675,18 @@ class ReportFilter {
675
675
  }
676
676
  }
677
677
 
678
+ /**
679
+ * Delete rows error
680
+ */
681
+ class TekGridDeleteRowsError extends Error {
682
+ constructor() {
683
+ super('TekGrid can\'t automatically delete rows when selectAllPages property is true.'
684
+ + ' You should delete them manually using the beforeDelete event and preventing'
685
+ + ' the default execution');
686
+ this.name = 'TekGridDeleteRowsError';
687
+ }
688
+ }
689
+
678
690
  class GridBase {
679
691
  constructor(grid) {
680
692
  this.exportConfigButtons = [];
@@ -925,6 +937,9 @@ class GridBase {
925
937
  deleteButtonClick({ event }) {
926
938
  this.grid.callEvent('beforeDelete', { component: this.grid, event });
927
939
  if (!event.defaultPrevented) {
940
+ if (this.grid.selectAllPages) {
941
+ throw new TekGridDeleteRowsError();
942
+ }
928
943
  this.grid.deleteRows();
929
944
  this.grid.callEvent('afterDelete', { component: this.grid });
930
945
  }
@@ -1598,10 +1613,18 @@ class GridController {
1598
1613
  return !this.isEditing;
1599
1614
  }
1600
1615
  get disableDeleteButton() {
1601
- return !((this.grid.deleteButton === 'selection'
1602
- && this.grid.selectedRows.length > 0)
1603
- || (this.grid.deleteButton === 'currentRow'
1604
- && this.grid.datasource.currentRow[this.grid.datasource.uniqueKey]));
1616
+ if (this.grid.deleteButton === 'selection') {
1617
+ if (this.grid.selectAllPages) {
1618
+ const { allSelected, except } = this.grid.selectionState;
1619
+ return (allSelected && except.length === this.grid.datasource.total)
1620
+ || (!allSelected && except.length === 0);
1621
+ }
1622
+ return this.grid.selectedRows.length === 0;
1623
+ }
1624
+ if (this.grid.deleteButton === 'currentRow') {
1625
+ return !this.grid.datasource.currentRow[this.grid.datasource.uniqueKey];
1626
+ }
1627
+ return false;
1605
1628
  }
1606
1629
  }
1607
1630
 
@@ -1967,13 +1990,35 @@ class TekMemoryDatasource extends MemoryDatasource {
1967
1990
  * Updates filtered data
1968
1991
  */
1969
1992
  updateFilteredData() {
1970
- super.updateFilteredData();
1993
+ // first apply filters (simple and dynamic)
1994
+ this.filteredData = Object.keys(this.filter).length
1995
+ ? this.allData.filter((row) => this.getRowByFilter(row))
1996
+ : Array.from(this.allData);
1971
1997
  if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
1972
1998
  this.filteredData = this.filteredData.filter((row) => this.getRowByDynamicFilter(row));
1973
1999
  }
2000
+ const searchWithoutSearchJoin = (row) => {
2001
+ const searchRow = Object.assign({}, row);
2002
+ if (this.searchJoin) {
2003
+ // do not search on columns with searchJoin
2004
+ Object.keys(this.searchJoin).forEach((key) => delete searchRow[key]);
2005
+ }
2006
+ return this.getRowBySearch(searchRow);
2007
+ };
2008
+ // only after do the search
2009
+ const searchData = this.search
2010
+ ? this.filteredData.filter(searchWithoutSearchJoin)
2011
+ : this.filteredData;
2012
+ let searchIds = searchData.map((row) => row[this.uniqueKey]);
1974
2013
  if (this.searchJoin && Object.keys(this.searchJoin).length) {
1975
- this.filteredData = this.filteredData.filter((row) => this.getRowBySearchJoin(row));
1976
- }
2014
+ const searchJoinData = this.filteredData.filter((row) => this.getRowBySearchJoin(row));
2015
+ // get the ids from search and searchJoin
2016
+ searchIds = searchIds
2017
+ .concat(searchJoinData.map((row) => row[this.uniqueKey]))
2018
+ .sort();
2019
+ }
2020
+ // filter filteredData using searchIds
2021
+ this.filteredData = this.allData.filter((row) => searchIds.indexOf(row[this.uniqueKey]) !== -1);
1977
2022
  }
1978
2023
  getRowByDynamicFilter(row) {
1979
2024
  let filtered;
@@ -680,6 +680,18 @@
680
680
  }
681
681
  }
682
682
 
683
+ /**
684
+ * Delete rows error
685
+ */
686
+ class TekGridDeleteRowsError extends Error {
687
+ constructor() {
688
+ super('TekGrid can\'t automatically delete rows when selectAllPages property is true.'
689
+ + ' You should delete them manually using the beforeDelete event and preventing'
690
+ + ' the default execution');
691
+ this.name = 'TekGridDeleteRowsError';
692
+ }
693
+ }
694
+
683
695
  class GridBase {
684
696
  constructor(grid) {
685
697
  this.exportConfigButtons = [];
@@ -930,6 +942,9 @@
930
942
  deleteButtonClick({ event }) {
931
943
  this.grid.callEvent('beforeDelete', { component: this.grid, event });
932
944
  if (!event.defaultPrevented) {
945
+ if (this.grid.selectAllPages) {
946
+ throw new TekGridDeleteRowsError();
947
+ }
933
948
  this.grid.deleteRows();
934
949
  this.grid.callEvent('afterDelete', { component: this.grid });
935
950
  }
@@ -1603,10 +1618,18 @@
1603
1618
  return !this.isEditing;
1604
1619
  }
1605
1620
  get disableDeleteButton() {
1606
- return !((this.grid.deleteButton === 'selection'
1607
- && this.grid.selectedRows.length > 0)
1608
- || (this.grid.deleteButton === 'currentRow'
1609
- && this.grid.datasource.currentRow[this.grid.datasource.uniqueKey]));
1621
+ if (this.grid.deleteButton === 'selection') {
1622
+ if (this.grid.selectAllPages) {
1623
+ const { allSelected, except } = this.grid.selectionState;
1624
+ return (allSelected && except.length === this.grid.datasource.total)
1625
+ || (!allSelected && except.length === 0);
1626
+ }
1627
+ return this.grid.selectedRows.length === 0;
1628
+ }
1629
+ if (this.grid.deleteButton === 'currentRow') {
1630
+ return !this.grid.datasource.currentRow[this.grid.datasource.uniqueKey];
1631
+ }
1632
+ return false;
1610
1633
  }
1611
1634
  }
1612
1635
 
@@ -1972,13 +1995,35 @@
1972
1995
  * Updates filtered data
1973
1996
  */
1974
1997
  updateFilteredData() {
1975
- super.updateFilteredData();
1998
+ // first apply filters (simple and dynamic)
1999
+ this.filteredData = Object.keys(this.filter).length
2000
+ ? this.allData.filter((row) => this.getRowByFilter(row))
2001
+ : Array.from(this.allData);
1976
2002
  if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
1977
2003
  this.filteredData = this.filteredData.filter((row) => this.getRowByDynamicFilter(row));
1978
2004
  }
2005
+ const searchWithoutSearchJoin = (row) => {
2006
+ const searchRow = Object.assign({}, row);
2007
+ if (this.searchJoin) {
2008
+ // do not search on columns with searchJoin
2009
+ Object.keys(this.searchJoin).forEach((key) => delete searchRow[key]);
2010
+ }
2011
+ return this.getRowBySearch(searchRow);
2012
+ };
2013
+ // only after do the search
2014
+ const searchData = this.search
2015
+ ? this.filteredData.filter(searchWithoutSearchJoin)
2016
+ : this.filteredData;
2017
+ let searchIds = searchData.map((row) => row[this.uniqueKey]);
1979
2018
  if (this.searchJoin && Object.keys(this.searchJoin).length) {
1980
- this.filteredData = this.filteredData.filter((row) => this.getRowBySearchJoin(row));
1981
- }
2019
+ const searchJoinData = this.filteredData.filter((row) => this.getRowBySearchJoin(row));
2020
+ // get the ids from search and searchJoin
2021
+ searchIds = searchIds
2022
+ .concat(searchJoinData.map((row) => row[this.uniqueKey]))
2023
+ .sort();
2024
+ }
2025
+ // filter filteredData using searchIds
2026
+ this.filteredData = this.allData.filter((row) => searchIds.indexOf(row[this.uniqueKey]) !== -1);
1982
2027
  }
1983
2028
  getRowByDynamicFilter(row) {
1984
2029
  let filtered;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeedhi/teknisa-components-common",
3
- "version": "1.77.2",
3
+ "version": "1.78.0",
4
4
  "description": "Teknisa Components Common",
5
5
  "author": "Zeedhi <zeedhi@teknisa.com>",
6
6
  "license": "ISC",
@@ -31,5 +31,5 @@
31
31
  "peerDependencies": {
32
32
  "@zeedhi/core": "*"
33
33
  },
34
- "gitHead": "a8154284d8e6d69b2f4757eb1272a2bd0f2cb997"
34
+ "gitHead": "84ea35b74e9d917e6ea29e012a01bc8a525f7e4f"
35
35
  }
@@ -342,6 +342,7 @@ describe('TekMemoryDatasource', () => {
342
342
  instance.searchJoin = {
343
343
  name: ['name 1', 'name 2'],
344
344
  };
345
+ instance.search = 'aaa';
345
346
  await instance.get();
346
347
  expect(instance.data.length).toBe(2);
347
348
  });
@@ -660,6 +660,56 @@ describe('TekGrid', () => {
660
660
  }
661
661
  spy.mockReset();
662
662
  });
663
+
664
+ it('when deleteButton is clicked with selectAllPages, should throw', () => {
665
+ const instance = new TekGrid({
666
+ name: 'grid_deleteButtonClick1',
667
+ component: 'TekGrid',
668
+ deleteButton: 'selection',
669
+ selectAllPages: true,
670
+ datasource: {
671
+ data: [{ id: '1' }],
672
+ },
673
+ });
674
+
675
+ instance.onCreated();
676
+ const notEditingSpan = instance.toolbarSlot[2];
677
+ if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
678
+ const deleteTooltip = notEditingSpan.children[1];
679
+ if (deleteTooltip && deleteTooltip.children && deleteTooltip.children.length > 0) {
680
+ const deleteButton = deleteTooltip.children[0];
681
+ const button = new Button(deleteButton);
682
+ const event = new Event('click');
683
+ expect(button.isVisible).toBeTruthy();
684
+ expect(button.disabled).toBeTruthy();
685
+ instance.selectAll(true);
686
+ expect(button.disabled).toBeFalsy();
687
+ expect(() => button.click(event, {} as HTMLElement)).toThrow();
688
+ }
689
+ }
690
+ });
691
+ });
692
+
693
+ describe('disableDeleteButton', () => {
694
+ it('should not disable deleteButton when deleteButton equals none', () => {
695
+ const instance = new TekGrid({
696
+ name: 'grid_deleteButtonClick1',
697
+ component: 'TekGrid',
698
+ deleteButton: 'none',
699
+ });
700
+
701
+ instance.onCreated();
702
+ const notEditingSpan = instance.toolbarSlot[2];
703
+ if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
704
+ const deleteTooltip = notEditingSpan.children[1];
705
+ if (deleteTooltip && deleteTooltip.children && deleteTooltip.children.length > 0) {
706
+ const deleteButton = deleteTooltip.children[0];
707
+ const button = new Button(deleteButton);
708
+ expect(button.isVisible).toBeFalsy();
709
+ expect(button.disabled).toBeFalsy();
710
+ }
711
+ }
712
+ });
663
713
  });
664
714
 
665
715
  describe('navigation', () => {
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Delete rows error
3
+ */
4
+ export declare class TekGridDeleteRowsError extends Error {
5
+ constructor();
6
+ }