From e44488bceb69af0bb3e79dde5977c82a0efb5d28 Mon Sep 17 00:00:00 2001
From: Christophe Gourmelon Fork me on Github fork me on githubLight Javascript Table Sorter
+ light javascript table sorter
-
-
Name
- Email
- Phone
- Price
+ name
+ email
+ phone
+ price
-
- John Doe
- john.doe@gmail.com
- 0123456789
- 99
-
-
- Jane Vanda
- jane@vanda.org
- 9876543210
- 349
-
-
+ Alferd Penyworth
- alfred@batman.com
- 6754328901
- 199
- john doe
+ john.doe@gmail.com
+ 0123456789
+ 99
+
+
+
+ jane vanda
+ jane@vanda.org
+ 9876543210
+ 349
+
+
+ Jane Vanda
+ jane@vanda.org
+ 9876543210
+ 999
+
+
alferd penyworth
+ alfred@batman.com
+ 6754328901
+ 199
+ Light Javascript Table Sorter
+
diff --git a/light-table-sorter.coffee b/light-table-sorter.coffee
new file mode 100644
index 0000000..631bc2a
--- /dev/null
+++ b/light-table-sorter.coffee
@@ -0,0 +1,50 @@
+LightTableSorter = do ->
+
+ _th = null
+ _cellIndex = null
+ _order = ''
+
+ _text = (row) ->
+ return row.cells.item(_cellIndex).textContent.toLowerCase()
+
+ _sort = (a, b) ->
+ textA = _text(a)
+ textB = _text(b)
+ n = parseInt(textA, 10)
+ if n
+ textA = n
+ textB = parseInt(textB, 10)
+ if textA > textB then return 1
+ if textA < textB then return -1
+ return 0
+
+ _toggle = ->
+ c = if _order isnt 'asc' then 'asc' else 'desc'
+ _th.className = (_th.className.replace(_order, '') + ' ' + c).trim()
+ _order = c
+
+ _reset = ->
+ _th.className = _th.className.replace('asc', '').replace('desc', '')
+ _order = ''
+
+ _onClickEvent = (e) ->
+ _reset() if _th and (_cellIndex isnt e.target.cellIndex)
+ _th = e.target
+ if _th.nodeName.toLowerCase() is 'th'
+ _cellIndex = _th.cellIndex
+ tbody = _th.offsetParent.getElementsByTagName('tbody')[0]
+ rows = tbody.rows
+ if rows
+ rows = Array.prototype.slice.call(rows, 0)
+ rows = Array.prototype.sort.call(rows, _sort)
+ Array.prototype.reverse.call(rows) if _order is 'asc'
+ _toggle()
+ tbody.innerHtml = ''
+ tbody.appendChild(row) for row in rows
+ return
+
+
+ return init: ->
+ ths = document.getElementsByTagName('th')
+ th.onclick = _onClickEvent for th in ths
+
diff --git a/light-table-sorter.js b/light-table-sorter.js
index e5857ec..4f3aa4c 100644
--- a/light-table-sorter.js
+++ b/light-table-sorter.js
@@ -1,67 +1,75 @@
-(function(document) {
- 'use strict';
+var LightTableSorter;
- var LightTableSorter = (function(Arr) {
-
- var _th, _cellIndex, _order = '';
-
- function _text(row) {
- return row.cells.item(_cellIndex).textContent.toLowerCase();
- }
-
- function _sort(a, b) {
- var va = _text(a), vb = _text(b), n = parseInt(va, 10);
- if (n) {
- va = n;
- vb = parseInt(vb, 10);
- }
- return va > vb ? 1 : va < vb ? -1 : 0;
- }
-
- function _toggle() {
- var c = _order !== 'asc' ? 'asc' : 'desc';
- _th.className = (_th.className.replace(_order, '') + ' ' + c).trim();
- _order = c;
- }
-
- function _reset() {
- _th.className = _th.className.replace('asc', '').replace('desc', '');
- _order = '';
- }
-
- function onClickEvent(e) {
- if (_th && _cellIndex !== e.target.cellIndex) {
- _reset();
- }
- _th = e.target;
- if (_th.nodeName.toLowerCase() === 'th') {
- _cellIndex = _th.cellIndex;
- var tbody = _th.offsetParent.getElementsByTagName('tbody')[0],
- rows = tbody.rows;
- if (rows) {
- rows = Arr.sort.call(Arr.slice.call(rows, 0), _sort);
- if (_order === 'asc') {
- Arr.reverse.call(rows);
- }
- _toggle();
- tbody.innerHtml = '';
- Arr.forEach.call(rows, function(row) { tbody.appendChild(row); });
- }
- }
- }
-
- return {
- init: function() {
- var ths = document.getElementsByTagName('th');
- Arr.forEach.call(ths, function(th) { th.onclick = onClickEvent; });
- }
- };
- })(Array.prototype);
-
- document.addEventListener('readystatechange', function() {
- if (document.readyState === 'complete') {
- LightTableSorter.init();
- }
- });
-
-})(document);
\ No newline at end of file
+LightTableSorter = (function() {
+ var _cellIndex, _onClickEvent, _order, _reset, _sort, _text, _th, _toggle;
+ _th = null;
+ _cellIndex = null;
+ _order = '';
+ _text = function(row) {
+ return row.cells.item(_cellIndex).textContent.toLowerCase();
+ };
+ _sort = function(a, b) {
+ var n, textA, textB;
+ textA = _text(a);
+ textB = _text(b);
+ n = parseInt(textA, 10);
+ if (n) {
+ textA = n;
+ textB = parseInt(textB, 10);
+ }
+ if (textA > textB) {
+ return 1;
+ }
+ if (textA < textB) {
+ return -1;
+ }
+ return 0;
+ };
+ _toggle = function() {
+ var c;
+ c = _order !== 'asc' ? 'asc' : 'desc';
+ _th.className = (_th.className.replace(_order, '') + ' ' + c).trim();
+ return _order = c;
+ };
+ _reset = function() {
+ _th.className = _th.className.replace('asc', '').replace('desc', '');
+ return _order = '';
+ };
+ _onClickEvent = function(e) {
+ var row, rows, tbody, _i, _len;
+ if (_th && (_cellIndex !== e.target.cellIndex)) {
+ _reset();
+ }
+ _th = e.target;
+ if (_th.nodeName.toLowerCase() === 'th') {
+ _cellIndex = _th.cellIndex;
+ tbody = _th.offsetParent.getElementsByTagName('tbody')[0];
+ rows = tbody.rows;
+ if (rows) {
+ rows = Array.prototype.slice.call(rows, 0);
+ rows = Array.prototype.sort.call(rows, _sort);
+ if (_order === 'asc') {
+ Array.prototype.reverse.call(rows);
+ }
+ _toggle();
+ tbody.innerHtml = '';
+ for (_i = 0, _len = rows.length; _i < _len; _i++) {
+ row = rows[_i];
+ tbody.appendChild(row);
+ }
+ }
+ }
+ };
+ return {
+ init: function() {
+ var th, ths, _i, _len, _results;
+ ths = document.getElementsByTagName('th');
+ _results = [];
+ for (_i = 0, _len = ths.length; _i < _len; _i++) {
+ th = ths[_i];
+ _results.push(th.onclick = _onClickEvent);
+ }
+ return _results;
+ }
+ };
+})();
diff --git a/light-table-sorter.min.js b/light-table-sorter.min.js
index 2fb0d8c..0a63684 100644
--- a/light-table-sorter.min.js
+++ b/light-table-sorter.min.js
@@ -1 +1 @@
-(function(a){"use strict";var b=function(b){function f(a){return a.cells.item(d).textContent.toLowerCase()}function g(a,b){var c=f(a),d=f(b),e=parseInt(c,10);return e&&(c=e,d=parseInt(d,10)),c>d?1:d>c?-1:0}function h(){var a="asc"!==e?"asc":"desc";c.className=(c.className.replace(e,"")+" "+a).trim(),e=a}function i(){c.className=c.className.replace("asc","").replace("desc",""),e=""}function j(a){if(c&&d!==a.target.cellIndex&&i(),c=a.target,"th"===c.nodeName.toLowerCase()){d=c.cellIndex;var f=c.offsetParent.getElementsByTagName("tbody")[0],j=f.rows;j&&(j=b.sort.call(b.slice.call(j,0),g),"asc"===e&&b.reverse.call(j),h(),f.innerHtml="",b.forEach.call(j,function(a){f.appendChild(a)}))}}var c,d,e="";return{init:function(){var c=a.getElementsByTagName("th");b.forEach.call(c,function(a){a.onclick=j})}}}(Array.prototype);a.addEventListener("readystatechange",function(){"complete"===a.readyState&&b.init()})})(document);
\ No newline at end of file
+var LightTableSorter;LightTableSorter=function(){var e,t,r,a,n,l,c,o;return c=null,e=null,r="",l=function(t){return t.cells.item(e).textContent.toLowerCase()},n=function(e,t){var r,a,n;return a=l(e),n=l(t),r=parseInt(a,10),r&&(a=r,n=parseInt(n,10)),a>n?1:n>a?-1:0},o=function(){var e;return e="asc"!==r?"asc":"desc",c.className=(c.className.replace(r,"")+" "+e).trim(),r=e},a=function(){return c.className=c.className.replace("asc","").replace("desc",""),r=""},t=function(t){var l,s,i,u,p;if(c&&e!==t.target.cellIndex&&a(),c=t.target,"th"===c.nodeName.toLowerCase()&&(e=c.cellIndex,i=c.offsetParent.getElementsByTagName("tbody")[0],s=i.rows))for(s=Array.prototype.slice.call(s,0),s=Array.prototype.sort.call(s,n),"asc"===r&&Array.prototype.reverse.call(s),o(),i.innerHtml="",u=0,p=s.length;p>u;u++)l=s[u],i.appendChild(l)},{init:function(){var e,r,a,n,l;for(r=document.getElementsByTagName("th"),l=[],a=0,n=r.length;n>a;a++)e=r[a],l.push(e.onclick=t);return l}}}();
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a1e67e6
--- /dev/null
+++ b/package.json
@@ -0,0 +1,11 @@
+{
+ "name": "Light-Table-Sorter",
+ "version": "0.0.1",
+ "devDependencies": {
+ "coffee-script": "^1.7.1",
+ "gulp": "^3.6.0",
+ "gulp-coffee": "^1.4.1",
+ "gulp-rename": "^1.2.0",
+ "gulp-uglify": "^0.2.1"
+ }
+}
diff --git a/readme.md b/readme.md
index a2a4c0b..faafb1e 100644
--- a/readme.md
+++ b/readme.md
@@ -8,14 +8,13 @@ Here is a simple implementation [demo](http://roparz.me/play/light-javascript-ta
## How to use it?
-Installation is very simple. Just add the script in your html (head or foot, whatever):
-```html
-
-```
-
-Or the minified version:
+Installation is very simple. Just add the script at the end of your html (just before `