Implementing record deletion feature, in progress

This commit is contained in:
alekseybobkov 2014-11-26 23:14:46 -08:00
parent 8c5cd78143
commit 839c686bb3
4 changed files with 108 additions and 6 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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
// ============================

View File

@ -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)
}