Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Convert to CoffeeScript.

  • Loading branch information...
commit 5fd845dbd35a9491f479653ef92c9126de6fcbfe 1 parent e720b80
@metaskills authored
Showing with 106 additions and 147 deletions.
  1. +106 −0 jquery.tablesort.coffee
  2. +0 −147 jquery.tablesort.js
View
106 jquery.tablesort.coffee
@@ -0,0 +1,106 @@
+###
+ 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'
+ tbodySelector: 'tbody:first'
+ trSelector: 'tr'
+
+ constructor: (table, settings={}) ->
+ @table = $(table)
+ @settings = $.extend {}, @constructor.defaults, settings
+ @tableBody = @table.find @settings.tbodySelector
+ @tableHeaders = @table.find('thead th')
+ @tableHeaders.bind 'click.tablesort', @headerClicked
+ @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', [@]
+
+ 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) =>
+ th = $(event.target)
+ 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
+
+
View
147 jquery.tablesort.js
@@ -1,147 +0,0 @@
-/*
- A simple, lightweight jQuery plugin for creating sortable tables.
- https://github.com/kylefox/jquery-tablesort
- Version 0.0.1
-*/
-
-$(function() {
-
- var $ = window.jQuery;
-
- function sortValueForCell(th, td, sorter) {
- if(th.data().sortBy) {
- var sortBy = th.data().sortBy;
- return (typeof sortBy === 'function') ? sortBy(th, td, sorter) : sortBy;
- }
- if(td.data().sortValue) {
- return td.data().sortValue;
- } else {
- return td.text();
- }
- }
-
- $.tablesort = function ($table, settings) {
- var self = this;
- this.$table = $table;
- this.settings = $.extend({}, $.tablesort.defaults, settings);
- this.$table.find('thead th').bind('click.tablesort', function() {
- self.sort($(this));
- });
- this.index = null;
- this.$th = null;
- this.direction = null;
- };
-
- $.tablesort.prototype = {
-
- sort: function(th, direction) {
- var start = new Date(),
- self = this,
- table = this.$table,
- rows = table.find('tbody tr'),
- aRow,
- bRow,
- aIndex,
- bIndex,
- cache = [];
-
- if(rows.length === 0)
- return;
-
- self.$table.find('thead th').removeClass(self.settings.asc + ' ' + self.settings.desc);
-
- this.index = th.index();
- this.$th = th;
-
- if(direction !== 'asc' && direction !== 'desc')
- this.direction = this.direction === 'asc' ? 'desc' : 'asc';
- else
- this.direction = direction;
-
- direction = this.direction == 'asc' ? 1 : -1;
-
- self.$table.trigger('tablesort:start', [self]);
- self.log("Sorting by " + this.index + ' ' + this.direction);
-
- rows.sort(function(a, b) {
- aRow = $(a);
- bRow = $(b);
- aIndex = aRow.index();
- bIndex = bRow.index();
-
- // Sort value A
- if(cache[aIndex]) {
- a = cache[aIndex];
- } else {
- a = sortValueForCell(th, self.cellToSort(a), self);
- cache[aIndex] = a;
- }
-
- // Sort Value B
- if(cache[bIndex]) {
- b = cache[bIndex];
- } else {
- b = sortValueForCell(th, self.cellToSort(b), self);
- cache[bIndex]= b;
- }
-
- if(a > b) {
- return 1 * direction;
- } else if(a < b) {
- return -1 * direction;
- } else {
- return 0;
- }
- });
-
- rows.each(function(i, tr) {
- table.append(tr);
- });
-
- th.addClass(self.settings[self.direction]);
-
- self.log('Sort finished in ' + ((new Date()).getTime() - start.getTime()) + 'ms');
- self.$table.trigger('tablesort:complete', [self]);
-
- },
-
- cellToSort: function(row) {
- return $($(row).find('td').get(this.index));
- },
-
-
- log: function(msg) {
- if(($.tablesort.DEBUG || this.settings.debug) && console && console.log) {
- console.log('[tablesort] ' + msg);
- }
- },
-
- destroy: function() {
- this.$table.find('thead th').unbind('click.tablesort');
- this.$table.data('tablesort', null);
- return null;
- }
-
- };
-
- $.tablesort.DEBUG = false;
-
- $.tablesort.defaults = {
- debug: $.tablesort.DEBUG,
- asc: 'sorted ascending',
- desc: 'sorted descending'
- };
-
- $.fn.tablesort = function(settings) {
- var table, sortable, previous;
- return this.each(function() {
- table = $(this);
- previous = table.data('tablesort');
- if(previous) {
- previous.destroy();
- }
- table.data('tablesort', new $.tablesort(table, settings));
- });
- };
-
-});
Please sign in to comment.
Something went wrong with that request. Please try again.