forked from kylefox/jquery-tablesort
/
jquery.tablesort.coffee
113 lines (91 loc) · 3.23 KB
/
jquery.tablesort.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
###
A simple, lightweight jQuery plugin for creating sortable tables.
Original: https://github.com/kylefox/jquery-tablesort
CoffeeScript: https://github.com/metaskills/jquery-tablesort
###
$ = window.jQuery
class $.tablesort
@DEBUG = false
@defaults =
debug: @DEBUG
asc: 'sorted ascending'
desc: 'sorted descending'
theadSelector: 'thead'
thSelector: 'th'
tbodySelector: 'tbody:first'
trSelector: 'tr'
constructor: (table, settings={}) ->
@table = $(table)
@settings = $.extend {}, @constructor.defaults, settings
@tableBody = @table.find @settings.tbodySelector
@tableHeaders = @table.find(@settings.theadSelector).find(@settings.thSelector)
@tableHeaders.bind 'click.tablesort', @headerClicked
@tableHeaders.addClass 'sortable'
@sortedHeader = null
@sortedDirection = null
@sortedIndex = null
sort: (th, direction) ->
@setCurrentSorted th, @getDefaultOrReverseDirection(th, direction)
start = new Date()
rows = $.makeArray @tableBody.find(@settings.trSelector)
return if rows.length is 0
@tableHeaders.removeClass "#{@settings.asc} #{@settings.desc}"
@table.trigger 'tablesort:start', [@]
@log "Sorting by #{@sortedHeader.text()} #{@sortedDirection}"
@tableBody.append row for row in rows.sort(@rowSorter)
@sortedHeader.addClass @settings[@sortedDirection]
@log "Sort finished in #{(new Date()).getTime() - start.getTime()} ms"
@table.trigger 'tablesort:complete', [@]
resort: ->
@sort(@sortedHeader, @sortedDirection) if @sortedHeader and @sortedDirection
destroy: ->
@tableHeaders.unbind 'click.tablesort'
@table.data 'tablesort', null
# Private
rowSorter: (a, b) =>
aRow = $(a)
bRow = $(b)
aValue = @sortValueForCell @sortedHeader, @cellToSort(aRow)
bValue = @sortValueForCell @sortedHeader, @cellToSort(bRow)
if aValue > bValue
1 * @dirMultiplier()
else if aValue < bValue
-1 * @dirMultiplier()
else
0
dirMultiplier: ->
if @sortedDirection is 'asc' then 1 else -1
setCurrentSorted: (th, direction) ->
@sortedHeader = th
@sortedDirection = direction
@sortedIndex = @tableHeaders.index(th)
headerClicked: (event) =>
event.stopPropagation()
th = $(event.currentTarget)
dir = @getDefaultOrReverseDirection(th)
@sort th, dir
getDefaultOrReverseDirection: (th, preferedDir) ->
return preferedDir if preferedDir
if @sortedHeader?.text() is th.text()
@reverseDirection()
else
'asc'
reverseDirection: (direction) ->
dir = direction or @sortedDirection
if dir is 'asc' then 'desc' else 'asc'
log: (msg) ->
debugging = @constructor.DEBUG or @settings.debug
logability = window.console?.log?
console.log "[tablesort] #{msg}" if debugging and logability
cellToSort: (row) ->
$(row.find('td').get(@sortedIndex))
sortValueForCell: (th, td) ->
if sortBy = th.data().sortBy
return if typeof sortBy is 'function' then sortBy(th, td, @) else sortBy
td.data().sortValue or td.text()
$.fn.tablesort = (settings) ->
@each ->
table = $(this)
table.data('tablesort')?.destroy()
tablesort = new $.tablesort table, settings
table.data 'tablesort', tablesort