diff --git a/modules/backend/widgets/table/assets/js/table.datasource.base.js b/modules/backend/widgets/table/assets/js/table.datasource.base.js index c6712bed6..ed911de0b 100644 --- a/modules/backend/widgets/table/assets/js/table.datasource.base.js +++ b/modules/backend/widgets/table/assets/js/table.datasource.base.js @@ -68,13 +68,15 @@ * Deletes a record with the specified key. * * - key - the record key in the dataset (primary key, etc). + * - newRecordData - replacement record to add to the dataset if the deletion + * empties it. * - offset - the current page's first record key (zero-based) * - count - number of records to return * - onSuccess - a callback function to execute when the updated data gets available. * * The onSuccess callback parameters: records, totalCount. */ - Base.prototype.deleteRecord = function(key, offset, count, onSuccess) { + Base.prototype.deleteRecord = function(key, newRecordData, offset, count, onSuccess) { onSuccess([], 0) } diff --git a/modules/backend/widgets/table/assets/js/table.datasource.client.js b/modules/backend/widgets/table/assets/js/table.datasource.client.js index 01ff174c4..98c5690e7 100644 --- a/modules/backend/widgets/table/assets/js/table.datasource.client.js +++ b/modules/backend/widgets/table/assets/js/table.datasource.client.js @@ -101,6 +101,32 @@ throw new Error('Record with they key '+key+ ' is not found in the data set') } + /* + * Deletes a record with the specified key. + * + * - key - the record key in the dataset (primary key, etc). + * - newRecordData - replacement record to add to the dataset if the deletion + * empties it. + * - offset - the current page's first record key (zero-based) + * - count - number of records to return + * - onSuccess - a callback function to execute when the updated data gets available. + * + * The onSuccess callback parameters: records, totalCount. + */ + Base.prototype.deleteRecord = function(key, newRecordData, offset, count, onSuccess) { + var recordIndex = this.getIndexOfKey(key) + + if (recordIndex !== -1) { + this.data.splice(recordIndex, 1) + + if (this.data.length == 0) + this.data.push(newRecordData) + + this.getRecords(offset, count, onSuccess) + } else + throw new Error('Record with they key '+key+ ' is not found in the data set') + } + Client.prototype.getIndexOfKey = function(key) { return this.data.map(function(record) { return record.__key diff --git a/modules/backend/widgets/table/assets/js/table.helper.navigation.js b/modules/backend/widgets/table/assets/js/table.helper.navigation.js index fae6fec64..190d4a813 100644 --- a/modules/backend/widgets/table/assets/js/table.helper.navigation.js +++ b/modules/backend/widgets/table/assets/js/table.helper.navigation.js @@ -152,6 +152,10 @@ this.tableObj.updateDataTable(onSuccess) } + Navigation.prototype.getRowCountOnPage = function(cellElement) { + return this.tableObj.getDataTableBody().children.length + } + Navigation.prototype.getNewRowPage = function(placement, currentRowIndex) { var curRecordCount = this.getRecordCount() @@ -173,6 +177,13 @@ return this.pageIndex } + Navigation.prototype.getPageAfterDeletion = function(currentRowIndex) { + if (currentRowIndex == 0 && this.getRowCountOnPage() == 1) + return this.pageIndex == 0 ? 0 : this.pageIndex-1 + + return this.pageIndex + } + // KEYBOARD NAVIGATION // ============================ diff --git a/modules/backend/widgets/table/assets/js/table.js b/modules/backend/widgets/table/assets/js/table.js index b615509f4..4bcd6314e 100644 --- a/modules/backend/widgets/table/assets/js/table.js +++ b/modules/backend/widgets/table/assets/js/table.js @@ -52,8 +52,8 @@ // Navigation helper this.navigation = null - // Number of records added during the session - this.recordsAdded = 0 + // Number of records added or deleted during the session + this.recordsAddedOrDeleted = 0 // // Initialization @@ -350,11 +350,11 @@ this.unfocusTable() - this.recordsAdded++ + this.recordsAddedOrDeleted++ // New records have negative keys var recordData = { - __key: -1*this.recordsAdded + __key: -1*this.recordsAddedOrDeleted }, self = this @@ -363,7 +363,6 @@ this.options.recordsPerPage, function onAddRecordDataTableSuccess(records, totalCount) { self.buildDataTable(records, totalCount) - self.navigation.focusCell('bottom', 0) var row = self.findRowByKey(recordData.__key) if (!row) @@ -376,6 +375,58 @@ ) } + Table.prototype.deleteRecord = function() { + if (!this.activeCell) + return + + var currentRowIndex = this.getCellRowIndex(this.activeCell), + key = this.getCellRowKey(this.activeCell), + self = this, + paginationEnabled = this.navigation.paginationEnabled(), + currentPageIndex = this.navigation.pageIndex, + currentCellIndex = this.activeCell.cellIndex + + if (paginationEnabled) + this.navigation.pageIndex = this.navigation.getPageAfterDeletion(currentRowIndex) + + this.recordsAddedOrDeleted++ + + // New records have negative keys + var newRecordData = { + __key: -1*this.recordsAddedOrDeleted + } + + this.dataSource.deleteRecord(key, + newRecordData, + this.navigation.getPageFirstRowOffset(), + this.options.recordsPerPage, + function onDeleteRecordDataTableSuccess(records, totalCount) { + self.buildDataTable(records, totalCount) + + if (!paginationEnabled) { + if (currentRowIndex == 0) + self.navigation.focusCell('top', currentCellIndex) + else { + var focusRow = self.findRowByIndex(currentRowIndex) + + if (!focusRow) + focusRow = self.findRowByIndex(currentRowIndex-1) + + if (focusRow) + self.navigation.focusCell(focusRow, currentCellIndex) + else + self.navigation.focusCell('top', currentCellIndex) + } + } + + // TODO - implement focusing for the pagination + // TODO - profile memory usage + + self = null + } + ) + } + // EVENT HANDLERS // ============================ @@ -408,6 +459,14 @@ return } + if (ev.keyCode == 68 && ev.altKey) { + // alt+d - delete record + this.deleteRecord('above') + + this.stopEvent(ev) + return + } + if (this.navigation.onKeydown(ev) === false) return } @@ -504,6 +563,10 @@ return this.dataTable.querySelector('tbody tr[data-row="'+key+'"]') } + Table.prototype.findRowByIndex = function(index) { + return this.getDataTableBody().children[index] + } + Table.prototype.getCellRowIndex = function(cellElement) { return parseInt(cellElement.parentNode.rowIndex) }