Allows custom toolbar buttons to be injected

Removes the assumption that all anchors are pagination links
Make code easier to read in places
Sorry, had to use jQuery in some places too
This commit is contained in:
Samuel Georges 2015-10-01 18:13:15 +10:00
parent 97595a631e
commit f97b9a6831
4 changed files with 39 additions and 21 deletions

View File

@ -288,6 +288,8 @@ if(!this.validate()){ev.preventDefault()
return}
var fieldName=this.options.alias.indexOf('[')>-1?this.options.alias+'[TableData]':this.options.alias+'TableData';data.options.data[fieldName]=this.dataSource.getAllData()}}
Table.prototype.onToolbarClick=function(ev){var target=this.getEventTarget(ev),cmd=target.getAttribute('data-cmd')
if(!cmd)
return
switch(cmd){case'record-add-below':this.addRecord('below')
break
case'record-add-above':this.addRecord('above')
@ -397,12 +399,11 @@ var paginationContainer=this.tableObj.getElement().querySelector('.pagination'),
this.pageCount=this.calculatePageCount(recordCount,this.tableObj.options.recordsPerPage)
if(!paginationContainer){paginationContainer=document.createElement('div')
paginationContainer.setAttribute('class','pagination')
newPaginationContainer=true}else
curRecordCount=this.getRecordCount(paginationContainer)
newPaginationContainer=true}
else{curRecordCount=this.getRecordCount(paginationContainer)}
if(newPaginationContainer||curRecordCount!=recordCount){paginationContainer.setAttribute('data-record-count',recordCount)
var pageList=this.buildPaginationLinkList(recordCount,this.tableObj.options.recordsPerPage,this.pageIndex)
if(!newPaginationContainer)
paginationContainer.replaceChild(pageList,paginationContainer.children[0])
if(!newPaginationContainer){paginationContainer.replaceChild(pageList,paginationContainer.children[0])}
else{paginationContainer.appendChild(pageList)
this.tableObj.getElement().appendChild(paginationContainer)}}else{this.markActiveLinkItem(paginationContainer,this.pageIndex)}}
Navigation.prototype.calculatePageCount=function(recordCount,recordsPerPage){var pageCount=Math.ceil(recordCount/recordsPerPage)
@ -415,6 +416,7 @@ Navigation.prototype.buildPaginationLinkList=function(recordCount,recordsPerPage
for(var i=0;i<pageCount;i++){var item=document.createElement('li'),link=document.createElement('a')
if(i==pageIndex)
item.setAttribute('class','active')
$(item).addClass('pagination-link')
link.innerText=i+1
link.setAttribute('data-page-index',i)
link.setAttribute('href','#')
@ -423,8 +425,7 @@ pageList.appendChild(item)}
return pageList}
Navigation.prototype.markActiveLinkItem=function(paginationContainer,pageIndex){var activeItem=paginationContainer.querySelector('.active'),list=paginationContainer.children[0]
activeItem.setAttribute('class','')
for(var i=0,len=list.children.length;i<len;i++){if(i==pageIndex)
list.children[i].setAttribute('class','active')}}
for(var i=0,len=list.children.length;i<len;i++){if(i==pageIndex){list.children[i].setAttribute('class','active')}}}
Navigation.prototype.gotoPage=function(pageIndex,onSuccess){this.tableObj.unfocusTable()
if(!this.tableObj.validate())
return
@ -450,7 +451,8 @@ return
var row=this.tableObj.activeCell.parentNode,newRow=!ev.shiftKey?row.nextElementSibling:row.parentNode.children[row.parentNode.children.length-1],cellIndex=forceCellIndex!==undefined?forceCellIndex:this.tableObj.activeCell.cellIndex
if(newRow){var cell=newRow.children[cellIndex]
if(cell)
this.tableObj.focusCell(cell)}else{if(!this.paginationEnabled())
this.tableObj.focusCell(cell)}
else{if(!this.paginationEnabled())
return
if(this.pageIndex<this.pageCount-1){var self=this
this.gotoPage(this.pageIndex+1,function navDownPageSuccess(){self.focusCell('top',cellIndex)
@ -524,7 +526,7 @@ return this.navigateRight(ev)
if(ev.keyCode==9)
return this.navigateNext(ev)}
Navigation.prototype.onClick=function(ev){var target=this.tableObj.getEventTarget(ev,'A')
if(!target)
if(!target||!$(target).hasClass('pagination-link'))
return
var pageIndex=parseInt(target.getAttribute('data-page-index'))
if(pageIndex===null)
@ -552,7 +554,8 @@ this.data=JSON.parse(dataString)};Client.prototype=Object.create(BaseProto)
Client.prototype.constructor=Client
Client.prototype.dispose=function(){BaseProto.dispose.call(this)
this.data=null}
Client.prototype.getRecords=function(offset,count,onSuccess){if(!count){onSuccess(this.data,this.data.length)}else{onSuccess(this.data.slice(offset,offset+count),this.data.length)}}
Client.prototype.getRecords=function(offset,count,onSuccess){if(!count){onSuccess(this.data,this.data.length)}
else{onSuccess(this.data.slice(offset,offset+count),this.data.length)}}
Client.prototype.createRecord=function(recordData,placement,relativeToKey,offset,count,onSuccess){if(placement==='bottom'){this.data.push(recordData)}
else if(placement=='above'||placement=='below'){var recordIndex=this.getIndexOfKey(relativeToKey)
if(placement=='below')

View File

@ -49,7 +49,8 @@
if (!count) {
// Return all records
onSuccess(this.data, this.data.length)
} else {
}
else {
// Return a subset of records
onSuccess(this.data.slice(offset, offset+count), this.data.length)
}

View File

@ -66,19 +66,24 @@
paginationContainer = document.createElement('div')
paginationContainer.setAttribute('class', 'pagination')
newPaginationContainer = true
} else
}
else {
curRecordCount = this.getRecordCount(paginationContainer)
}
// Generate the new page list only if the record count has changed
if (newPaginationContainer || curRecordCount != recordCount) {
paginationContainer.setAttribute('data-record-count', recordCount)
var pageList = this.buildPaginationLinkList(recordCount,
this.tableObj.options.recordsPerPage,
this.pageIndex)
var pageList = this.buildPaginationLinkList(
recordCount,
this.tableObj.options.recordsPerPage,
this.pageIndex
)
if (!newPaginationContainer)
if (!newPaginationContainer) {
paginationContainer.replaceChild(pageList, paginationContainer.children[0])
}
else {
paginationContainer.appendChild(pageList)
this.tableObj.getElement().appendChild(paginationContainer)
@ -120,6 +125,8 @@
if (i == pageIndex)
item.setAttribute('class', 'active')
$(item).addClass('pagination-link')
link.innerText = i+1
link.setAttribute('data-page-index', i)
link.setAttribute('href', '#')
@ -141,8 +148,9 @@
activeItem.setAttribute('class', '')
for (var i=0, len = list.children.length; i < len; i++) {
if (i == pageIndex)
if (i == pageIndex) {
list.children[i].setAttribute('class', 'active')
}
}
}
@ -200,9 +208,9 @@
return
var row = this.tableObj.activeCell.parentNode,
newRow = !ev.shiftKey ?
row.nextElementSibling :
row.parentNode.children[row.parentNode.children.length - 1],
newRow = !ev.shiftKey
? row.nextElementSibling
: row.parentNode.children[row.parentNode.children.length - 1],
cellIndex = forceCellIndex !== undefined ?
forceCellIndex :
this.tableObj.activeCell.cellIndex
@ -212,7 +220,8 @@
if (cell)
this.tableObj.focusCell(cell)
} else {
}
else {
// Try to switch to the next page if that's possible
if (!this.paginationEnabled())
@ -377,8 +386,10 @@
return this.navigateUp(ev)
else if (ev.keyCode == 37)
return this.navigateLeft(ev)
if (ev.keyCode == 39)
return this.navigateRight(ev)
if (ev.keyCode == 9)
return this.navigateNext(ev)
}
@ -389,7 +400,7 @@
var target = this.tableObj.getEventTarget(ev, 'A')
if (!target)
if (!target || !$(target).hasClass('pagination-link'))
return
var pageIndex = parseInt(target.getAttribute('data-page-index'))

View File

@ -766,6 +766,9 @@
var target = this.getEventTarget(ev),
cmd = target.getAttribute('data-cmd')
if (!cmd)
return
switch (cmd) {
case 'record-add-below':
this.addRecord('below')