Skip to content

Commit

Permalink
Make lint clean
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartlangridge committed Jun 14, 2016
1 parent be901a4 commit 85a7672
Showing 1 changed file with 57 additions and 52 deletions.
109 changes: 57 additions & 52 deletions sorttable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
This basically means: do what you want with it.
*/

/* jshint -W051, -W083, -W027 */

var stIsIE = /*@cc_on!@*/false;

Expand All @@ -40,15 +41,15 @@ sorttable = {
},

makeSortable: function(table) {
if (table.getElementsByTagName('thead').length == 0) {
if (table.getElementsByTagName('thead').length === 0) {
// table doesn't have a tHead. Since it should have, create one and
// put the first table row in it.
the = document.createElement('thead');
the.appendChild(table.rows[0]);
table.insertBefore(the,table.firstChild);
}
// Safari doesn't support table.tHead, sigh
if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
if (table.tHead === null) table.tHead = table.getElementsByTagName('thead')[0];

if (table.tHead.rows.length != 1) return; // can't cope with two header rows

Expand All @@ -63,33 +64,33 @@ sorttable = {
}
}
if (sortbottomrows) {
if (table.tFoot == null) {
if (table.tFoot === null) {
// table doesn't have a tfoot. Create one.
tfo = document.createElement('tfoot');
table.appendChild(tfo);
}
for (var i=0; i<sortbottomrows.length; i++) {
for (i=0; i<sortbottomrows.length; i++) {
tfo.appendChild(sortbottomrows[i]);
}
delete sortbottomrows;
}

// work through each column and calculate its type
headrow = table.tHead.rows[0].cells;
for (var i=0; i<headrow.length; i++) {
for (i=0; i<headrow.length; i++) {
// manually override the type with a sorttable_type attribute
if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
if (mtch) { override = mtch[1]; }
if (mtch && typeof sorttable["sort_"+override] == 'function') {
headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
} else {
headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
}
// make it clickable to sort
headrow[i].sorttable_columnindex = i;
headrow[i].sorttable_tbody = table.tBodies[0];
dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) {
if (mtch && typeof sorttable["sort_"+override] == 'function') {
headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
} else {
headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
}
// make it clickable to sort
headrow[i].sorttable_columnindex = i;
headrow[i].sorttable_tbody = table.tBodies[0];
dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) {

if (this.className.search(/\bsorttable_sorted\b/) != -1) {
// if we're already sorted by this column, just
Expand Down Expand Up @@ -137,29 +138,29 @@ sorttable = {
sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
this.appendChild(sortfwdind);

// build an array to sort. This is a Schwartzian transform thing,
// i.e., we "decorate" each row with the actual sort key,
// sort based on the sort keys, and then put the rows back in order
// which is a lot faster because you only do getInnerText once per row
row_array = [];
col = this.sorttable_columnindex;
rows = this.sorttable_tbody.rows;
for (var j=0; j<rows.length; j++) {
row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
}
/* If you want a stable sort, uncomment the following line */
//sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
/* and comment out this one */
row_array.sort(this.sorttable_sortfunction);

tb = this.sorttable_tbody;
for (var j=0; j<row_array.length; j++) {
tb.appendChild(row_array[j][1]);
}

delete row_array;
});
}
// build an array to sort. This is a Schwartzian transform thing,
// i.e., we "decorate" each row with the actual sort key,
// sort based on the sort keys, and then put the rows back in order
// which is a lot faster because you only do getInnerText once per row
row_array = [];
col = this.sorttable_columnindex;
rows = this.sorttable_tbody.rows;
for (var j=0; j<rows.length; j++) {
row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
}
/* If you want a stable sort, uncomment the following line */
//sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
/* and comment out this one */
row_array.sort(this.sorttable_sortfunction);

tb = this.sorttable_tbody;
for (j=0; j<row_array.length; j++) {
tb.appendChild(row_array[j][1]);
}

delete row_array;
});
}
}
},

Expand All @@ -168,18 +169,18 @@ sorttable = {
sortfn = sorttable.sort_alpha;
for (var i=0; i<table.tBodies[0].rows.length; i++) {
text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
if (text != '') {
if (text !== '') {
if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
return sorttable.sort_numeric;
}
// check for a date: dd/mm/yyyy or dd/mm/yy
// can have / or . or - as separator
// can be mm/dd as well
possdate = text.match(sorttable.DATE_RE)
possdate = text.match(sorttable.DATE_RE);
if (possdate) {
// looks like a date
first = parseInt(possdate[1]);
second = parseInt(possdate[2]);
first = parseInt(possdate[1], 10);
second = parseInt(possdate[2], 10);
if (first > 12) {
// definitely dd/mm
return sorttable.sort_ddmm;
Expand Down Expand Up @@ -208,7 +209,7 @@ sorttable = {
hasInputs = (typeof node.getElementsByTagName == 'function') &&
node.getElementsByTagName('input').length;

if (node.nodeType == 1 && node.getAttribute("sorttable_customkey") != null) {
if (node.nodeType == 1 && node.getAttribute("sorttable_customkey") !== null) {
return node.getAttribute("sorttable_customkey");
}
else if (typeof node.textContent != 'undefined' && !hasInputs) {
Expand All @@ -226,6 +227,7 @@ sorttable = {
if (node.nodeName.toLowerCase() == 'input') {
return node.value.replace(/^\s+|\s+$/g, '');
}
break;
case 4:
return node.nodeValue.replace(/^\s+|\s+$/g, '');
break;
Expand All @@ -249,7 +251,7 @@ sorttable = {
for (var i=0; i<tbody.rows.length; i++) {
newrows[newrows.length] = tbody.rows[i];
}
for (var i=newrows.length-1; i>=0; i--) {
for (i=newrows.length-1; i>=0; i--) {
tbody.appendChild(newrows[i]);
}
delete newrows;
Expand All @@ -267,9 +269,11 @@ sorttable = {
},
sort_alpha: function(a,b) {
return a[0].localeCompare(b[0]);
/*
if (a[0]==b[0]) return 0;
if (a[0]<b[0]) return -1;
return 1;
*/
},
sort_ddmm: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
Expand Down Expand Up @@ -309,30 +313,31 @@ sorttable = {
var b = 0;
var t = list.length - 1;
var swap = true;
var q;

while(swap) {
swap = false;
for(var i = b; i < t; ++i) {
if ( comp_func(list[i], list[i+1]) > 0 ) {
var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
q = list[i]; list[i] = list[i+1]; list[i+1] = q;
swap = true;
}
} // for
t--;

if (!swap) break;

for(var i = t; i > b; --i) {
for(i = t; i > b; --i) {
if ( comp_func(list[i], list[i-1]) < 0 ) {
var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
q = list[i]; list[i] = list[i-1]; list[i-1] = q;
swap = true;
}
} // for
b++;

} // while(swap)
}
}
};

/* ******************************************************************
Supporting functions: bundled here to avoid depending on a library
Expand Down Expand Up @@ -396,7 +401,7 @@ function dean_addEvent(element, type, handler) {
// assign a global event handler to do all the work
element["on" + type] = handleEvent;
}
};
}
// a counter used to create unique IDs
dean_addEvent.guid = 1;

Expand All @@ -409,7 +414,7 @@ function removeEvent(element, type, handler) {
delete element.events[type][handler.$$guid];
}
}
};
}

function handleEvent(event) {
var returnValue = true;
Expand All @@ -425,20 +430,20 @@ function handleEvent(event) {
}
}
return returnValue;
};
}

function fixEvent(event) {
// add W3C standard event methods
event.preventDefault = fixEvent.preventDefault;
event.stopPropagation = fixEvent.stopPropagation;
return event;
};
}
fixEvent.preventDefault = function() {
this.returnValue = false;
};
fixEvent.stopPropagation = function() {
this.cancelBubble = true;
}
};

// Dean's forEach: http://dean.edwards.name/base/forEach.js
/*
Expand Down

0 comments on commit 85a7672

Please sign in to comment.