Skip to content

Commit

Permalink
selection.table.RowModel: update record based annotations (in case th…
Browse files Browse the repository at this point in the history
…ey exist) on manual selections #6347
  • Loading branch information
tobiu committed Jan 30, 2025
1 parent 81b3416 commit 56f13cf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 34 deletions.
13 changes: 6 additions & 7 deletions src/grid/View.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -816,13 +816,12 @@ class GridView extends Component {
* @param {Object} opts.record
*/
onStoreRecordChange({fields, record}) {
let me = this,
fieldNames = fields.map(field => field.name),
needsUpdate = false,
{gridContainer} = me,
rowIndex = me.store.indexOf(record),
{selectionModel} = gridContainer.view,
{vdom} = me,
let me = this,
fieldNames = fields.map(field => field.name),
needsUpdate = false,
{gridContainer} = me,
rowIndex = me.store.indexOf(record),
{selectionModel, vdom} = me,
cellId, cellNode, cellStyle, cellVdom, column, columnIndex;

if (fieldNames.includes(me.colspanField)) {
Expand Down
5 changes: 3 additions & 2 deletions src/selection/grid/RowModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ class RowModel extends BaseModel {
let me = this,
{view} = me,
{store} = view,
countRecords = store.getCount(),
currentIndex = 0,
newIndex, record, rowId;

if (me.hasSelection()) {
currentIndex = store.indexOf(view.getRecordByRowId(me.items[0]))
}

newIndex = (currentIndex + step) % store.getCount();
newIndex = (currentIndex + step) % countRecords;

while (newIndex < 0) {
newIndex += store.getCount()
newIndex += countRecords
}

record = store.getAt(newIndex);
Expand Down
74 changes: 57 additions & 17 deletions src/selection/table/RowModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class RowModel extends BaseModel {
cls: 'neo-selection-rowmodel'
}

/**
* @param {Record} record
* @returns {Boolean}
*/
hasAnnotations(record) {
return !!Object.getOwnPropertyDescriptor(record.__proto__, this.view.selectedRecordField)
}

/**
*
*/
Expand Down Expand Up @@ -64,28 +72,31 @@ class RowModel extends BaseModel {
let me = this,
{view} = me,
{store} = view,
countRecords = store.getCount(),
currentIndex = 0,
newIndex, newRecord, rowId;
newIndex, record, rowId;

if (me.hasSelection()) {
currentIndex = store.indexOf(view.getRecordByRowId(me.items[0]))
}

newIndex = (currentIndex + step) % store.getCount();
newIndex = (currentIndex + step) % countRecords;

while (newIndex < 0) {
newIndex += store.getCount()
newIndex += countRecords
}

newRecord = store.getAt(newIndex);
rowId = view.getRowId(newRecord);
record = store.getAt(newIndex);

if (rowId) {
me.select(rowId);
if (me.hasAnnotations(record)) {
me.updateAnnotations(record)
} else {
rowId = view.getRowId(record);

view.fire('select', {
record: store.getAt(newIndex)
})
if (rowId) {
me.select(rowId);
view.fire('select', {record})
}
}
}

Expand All @@ -99,16 +110,19 @@ class RowModel extends BaseModel {
isSelected, record;

if (id) {
me.toggleSelection(id);
record = view.getRecord(id);

if (me.hasAnnotations(record)) {
me.updateAnnotations(record)
} else {
me.toggleSelection(id);

isSelected = me.isSelected(id);
record = view.getRecord(id);
isSelected = me.isSelected(id);

!isSelected && view.onDeselect?.(record);
!isSelected && view.onDeselect?.(record);

view.fire(isSelected ? 'select' : 'deselect', {
record
})
view.fire(isSelected ? 'select' : 'deselect', {record})
}
}
}

Expand Down Expand Up @@ -139,6 +153,32 @@ class RowModel extends BaseModel {

super.unregister()
}

/**
* @param {Record} record
*/
updateAnnotations(record) {
let me = this,
{view} = me,
rowId = view.getRowId(record),
isSelected = me.isSelected(rowId),
annotationsField = view.selectedRecordField;

if (me.singleSelect) {
if (isSelected) {
record[annotationsField] = false
} else {
me.items.forEach(rowId => {
// We can use setSilent(), since the last change will trigger a view update
view.getRecordByRowId(rowId).setSilent({[annotationsField]: false})
});

record[annotationsField] = true
}
} else {
record[annotationsField] = !record[annotationsField]
}
}
}

export default Neo.setupClass(RowModel);
15 changes: 7 additions & 8 deletions src/table/View.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,16 @@ class View extends Component {
* @param {Object} opts.record
*/
onStoreRecordChange({fields, model, record}) {
let me = this,
fieldNames = fields.map(field => field.name),
needsUpdate = false,
tableContainer = me.parent,
rowIndex = me.store.indexOf(record),
{selectionModel} = tableContainer,
{vdom} = me,
let me = this,
fieldNames = fields.map(field => field.name),
needsUpdate = false,
tableContainer = me.parent,
rowIndex = me.store.indexOf(record),
{selectionModel, vdom} = me,
cellId, cellNode, cellVdom, column, columnIndex, scope;

if (fieldNames.includes(me.colspanField)) {
me.vdom.cn[index] = me.createRow({record, rowIndex});
me.vdom.cn[rowIndex] = me.createRow({record, rowIndex});
me.update()
} else {
fields.forEach(field => {
Expand Down

0 comments on commit 56f13cf

Please sign in to comment.