javascript-data-set
Version:
powerfull recordset class, supporting sorting, filtering, subsets, loading data from json, array and html markup
277 lines (203 loc) • 6.74 kB
text/coffeescript
window = window || {}
window.Francodacosta = window.Francodacosta || {}
window.Francodacosta.DataSet = window.Francodacosta.DataSet || {}
class window.Francodacosta.DataSet.dataset
#
# Initlializes class
#
# array data
# array columns
#
#
constructor: (data, columns) ->
data = data || []
columns = columns || []
= []
= []
clearFilters: () ->
= []
= undefined
clearFiltersForColumn: (column) ->
# [column] = undefined
# .splice(column,1)
delete [column]
= undefined
addFilter: (column, fn, filterTerm) ->
if typeof fn != "function"
throw new Error('Filter: Expecting function, got ' + fn)
if not
throw new Error('Column does not exist ' + column)
if not (column of )
[column] = []
[column].push [fn, filterTerm]
= undefined
clearSorting: ->
= []
=undefined
filter: (data) ->
if 0 == Object.keys( ).length
return data
filteredData = []
for record in data
for column of
match = false
columnFilters = [column]
for filterData in columnFilters
filter = filterData[0]
filterTerm = filterData[1] || null
value = record.get(column)
if not filter(value, filterTerm)
break;
match = true
if match
filteredData.push record
return filteredData
setSorting: (column, direction) ->
if not
throw new Error('Unknown column ' + column)
direction = ("" + direction).toUpperCase()
if direction != 'DESC'
direction = 'ASC'
.push [column, direction]
= undefined
sort: (data)->
if !
return data
L = .length
if !L
return data
alphaSortAsc = (a, b) ->
if a == b
return 0
if a > b
return 1
else
return -1;
alphaSortDesc = (a, b) ->
if a == b
return 0;
if a < b
return 1
else
return -1;
me = @
data.sort(
(a, b) ->
tem= 0
indx=0;
while (tem==0 && indx<L)
itm=me.sortingInfo[indx][0]
if 'DESC' == me.sortingInfo[indx][1]
tem = alphaSortDesc(a.get(itm), b.get(itm))
else
tem= alphaSortAsc(a.get(itm), b.get(itm));
indx+=1;
return tem;
)
return data
hasColumn: (name) ->
for column in
if column.getName() == name
return true
return false
#
# returns a column definition by its name
#
# string name
getColumn: (name) ->
for column in
if column.getName() == name
return column
throw new Error('Unknown column ' + name)
#
# Sets column definitions
# Columns can either be an array fo strings or an an aray of objects
#
# array columns
setColumns: (columns) ->
= []
if Object.prototype.toString.call( columns ) != '[object Array]'
throw nwe Error('Column definition should be an array of objects or strings')
for columnDef in columns
if Object.prototype.toString.call( columnDef ) == '[object Object]'
name = columnDef.name
title = columnDef.title || columnDef.name
options = columnDef.options || {}
else
name = columnDef
title = undefined
options = undefined
= undefined
#
# Adds a column definition
#
# string name
# string title
# Object options
addColumn: (name, title, options) ->
.push new window.Francodacosta.DataSet.Column(name, title, options)
= undefined
#
# retuns the current column definition
#
# [Column]
getColumns: =>
return
#
# sets the record set data
#
# array|object
#
setData:(data) ->
= []
= undefined
for record in data
if Object.prototype.toString.call( record ) == '[object Array]'
record =
else if Object.prototype.toString.call( record ) == '[object Object]'
record =
else
throw new Error ('Could not parse data')
_normalizeDataArray: (record) ->
tmpRecord = {}
for col, index in
if not record[index]
value = undefined
else
value = record[index]
tmpRecord[col.getName()] = value
return new window.Francodacosta.DataSet.Record(tmpRecord)
_normalizeDataObject: (record) ->
tmpRecord = {}
for col in
if not col.getName() of record
value = undefined
else
value = record[col.getName()]
tmpRecord[col.getName()] = value
return new window.Francodacosta.DataSet.Record(tmpRecord)
getData: (start, offset) ->
start = start || 0
offset = offset || .length
if not
=
return
_processData: (start, offset) ->
# filter
=
=
=
# sort
# slice
return .slice(start, start + offset)
add: (record) ->
if not record instanceof window.Francodacosta.DataSet.Record
throw new Error('record must be a instance of Record')
.push record
# reset working data
= undefined
# module.exports = Dataset