Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
McFizh committed Aug 2, 2024
1 parent 2922939 commit 609f51c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 89 deletions.
7 changes: 6 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### Changelog

- **v2.0.1** - 3.8.2024

- Fixed getter (gave out non-existent property)
- Multiple initialization of plugin doesn't now break it

- **v2.0.0** - 2.8.2024

- Refactored folder structure
Expand All @@ -17,7 +22,7 @@
- **v1.3.0** - 14.3.2016

- Plugin structure is rewritten to support state getter. Now you can create a method to save table state to localstorage or similar.
- Once again had to increase target size from 6kB to 8kB :((
- Once again had to increase target size from 6kB to 8kB :(

- **v1.2.7** - 8.3.2016

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Test](https://github.com/mcfizh/slimtable/actions/workflows/test-workflow.yml/badge.svg)

SlimTable is a plugin for jQuery, which creates sortable and pageable tables from existing table data. Plugin is quite lightweight, as it only takes 8kB when minified (3kB gzipped).
SlimTable is a plugin for jQuery, which creates sortable and pageable tables from existing table data. Plugin is quite lightweight, as it only takes 9kB when minified (3kB gzipped).

Official homepage of slimtable: [https://slimtable.mcfish.org](https://slimtable.mcfish.org)

Expand Down
4 changes: 2 additions & 2 deletions dist/js/slimtable.min.js

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "slimtable",
"description": "jQuery plugin to create sortable and pageable table.",
"version": "2.0.0",
"version": "2.0.1",
"author": "Pekka Harjamäki <[email protected]>",
"homepage": "https://slimtable.mcfish.org",
"license": "MIT",
Expand Down
106 changes: 45 additions & 61 deletions src/slimtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
*
* Licensed under MIT license.
*
* @version 2.0.0
* @version 2.0.1
* @author Pekka Harjamäki
*/
// eslint-disable-next-line no-extra-semi
;(function($) {

"use strict";

var SlimTable = {
const SlimTable = {

/* ******************************************************************* *
* Class initializer
* ******************************************************************* */
init: function($el, options) {

$el.data("slimTableObj",this);

this.settings = $.extend({
tableData: null,
dataUrl: null,
Expand Down Expand Up @@ -114,7 +111,7 @@
},

returnRowType: function(data) {
var patt_01 = /[^0-9]/g,
const patt_01 = /[^0-9]/g,
patt_02 = /^[0-9]+([.,][0-9]+)?$/,
patt_03 = /^([0-9]+([.,][0-9]+)?)\s*[%$£e]?$/,
patt_04 = /^[0-9]{1,2}[.-/][0-9]{1,2}[.-/][0-9]{4}$/;
Expand Down Expand Up @@ -148,7 +145,7 @@
* Read data from ajax url / array / table
* ******************************************************************* */
readTable: function() {
var l1, l2, matchArr, tObj, tRow, tAttr;
let l1, l2, matchArr, tObj, tRow, tAttr;

// Get data either from table, pre defined array or ajax url
if( this.settings.dataUrl && this.settings.dataUrl.length > 2 )
Expand Down Expand Up @@ -260,7 +257,7 @@
},

processData: function(data) {
var l1, l2, tRow;
let l1, l2, tRow;

this.state.tblData = [];

Expand All @@ -277,7 +274,7 @@
* Create table body / header
* ******************************************************************* */
createTableHead: function() {
var l1, t_item1, t_item2;
let l1, t_item1, t_item2;

for(l1=0; l1<this.state.tableHeads.length; l1++)
{
Expand All @@ -304,10 +301,9 @@
},

createTableBody: function() {
var end_pos = this.state.pagingStart + this.state.itemsPerPage,
t_cobj, t_obj1, t_obj2,
pages = Math.ceil( this.state.tblData.length / this.state.itemsPerPage ),
l1, l2, l3;
const pages = Math.ceil( this.state.tblData.length / this.state.itemsPerPage );
let end_pos = this.state.pagingStart + this.state.itemsPerPage;
let t_cobj, t_obj1, t_obj2, l1, l2, l3;

//
this.state.tableBody.empty();
Expand Down Expand Up @@ -369,14 +365,11 @@

this.state.tableHeads.each(function(index) {
$(this).attr("unselectable","on");

let tCfg = self.state.colSettings[index];
const tCfg = self.state.colSettings[index];

if(tCfg && tCfg.enableSort)
{
var tObj = $("<span></span>").
attr("unselectable","on").
addClass("slimtable-sprites");
const tObj = $("<span></span>").attr("unselectable","on").addClass("slimtable-sprites");

if( tCfg.sortDir === "asc" )
{
Expand All @@ -387,10 +380,7 @@
tObj.addClass("slimtable-sortboth");
}

$(this).
prepend(tObj).
css({ cursor: "pointer" }).
on("click",{ self: self},self.handleHeaderClick);
$(this).prepend(tObj).css({ cursor: "pointer" }).on("click",{ self: self},self.handleHeaderClick);
} else {
$(this).addClass("slimtable-unsortable");
}
Expand Down Expand Up @@ -448,15 +438,12 @@

//
if(this.state.sortList.length>0)

this.state.tblData.sort(function(a,b) {
var t1, ta, tb, l1,
slistLength=self.state.sortList.length,
same_item;
let ta, tb, l1, same_item;
const slistLength=self.state.sortList.length;

for(l1=0; l1<slistLength; l1++)
{
t1 = self.state.sortList[l1];
for(l1=0; l1<slistLength; l1++) {
const t1 = self.state.sortList[l1];

// Swap variables, if sortdir = ascending
if( self.state.colSettings[t1].sortDir === "desc" )
Expand Down Expand Up @@ -487,10 +474,7 @@
continue;

// Compare values
if ( self.state.colSettings[t1].rowType === 0 )
return( ta.localeCompare(tb) );
else
return( ta - tb );
return self.state.colSettings[t1].rowType === 0 ? ta.localeCompare(tb) : ta - tb;
}

});
Expand Down Expand Up @@ -551,22 +535,21 @@
},

handleIppChange: function(e) {
var self = e.data.self;
const self = e.data.self;
self.state.itemsPerPage = parseInt(this.value);
self.state.pagingStart = 0;
self.createTableBody();
},

handlePageChange: function(event) {
var num = parseInt($(this).text())-1,
self = event.data.self,
pages = Math.ceil( self.state.tblData.length / self.state.itemsPerPage );
const num = parseInt($(this).text()) - 1;
const self = event.data.self;
const pages = Math.ceil( self.state.tblData.length / self.state.itemsPerPage );

if(num<0 || num>=pages)
return;

self.state.pagingStart = num * self.state.itemsPerPage;
self.createTableBody();
if(num>=0 && num<pages) {
self.state.pagingStart = num * self.state.itemsPerPage;
self.createTableBody();
}
},

/* ******************************************************************* *
Expand All @@ -575,7 +558,7 @@
sanityCheck: function() {
return !(
this.state.tblData.length > 0 &&
this.state.colSettings.length !== this.state.tblData[0].data.length
this.state.colSettings.length !== this.state.tblData[0].data.length
);
},

Expand All @@ -586,19 +569,13 @@
/* ******************************************************************* *
* Status getters
* ******************************************************************* */
getState: function($el) {
const slimObj = $el.data("slimTableObj");

if(!slimObj || !slimObj.state)
return {};

const state = slimObj.state;
getState: function() {
const state = this.state;
return {
colNumber: state.colNumber,
itemsPerPage: state.itemsPerPage,
pagingStart: state.pagingStart,
colSettings: state.colSettings,
sortList: state.sortList
sortList: state.sortList,
pagingStart: state.pagingStart,
itemsPerPage: state.itemsPerPage
};
}

Expand All @@ -608,13 +585,20 @@
* Plugin main
* ******************************************************************* */
$.fn.slimtable = function(options) {
if( typeof(options) !== "undefined" && options === "getState" )
return SlimTable.getState( $(this[0]) );

return this.each(function(){
const tbl = Object.create(SlimTable);
tbl.init( $(this) , options);
});
const key = "plugin_slimtable";

if(options === undefined || typeof options === "object") {
return this.each( function() {
if(!$.data(this, key)) {
const tbl = Object.create(SlimTable);
tbl.init($(this), options);
$.data(this, key, tbl);
}
});
} else if(typeof options === "string" && options === "getState") {
const instance = $(this).data(key);
return instance ? instance.getState() : {};
}
};

}(jQuery));
19 changes: 8 additions & 11 deletions tests/unitTest2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,28 @@ QUnit.test("basic test", (assert) => {
//
$.ajax({ url: "test.json", dataType: "json" })
.done(function(data) {

$("#testTable").slimtable({
tableData: data
});

$("#testTable").slimtable({ tableData: data });
assert.ok(true, "Test resumed after ajax call");

// Table should have 5 TH elements with attributes 'unselectable'='on'
// Table should have 8 TH elements with attributes 'unselectable'='on'
$("#testTable").find("thead").each(function(){
$(this).find("th").each(function(){
if( $(this).attr("unselectable") === "on" )
thfound++;
});
});

assert.equal(thfound, 8, "Proper number of th's found");

// Table should have 10 Tr elements
// Table should have 9 Tr elements
$("#testTable").find("tbody").each(function(){
$(this).find("tr").each(function(){
trfound++;
});
$(this).find("tr").each(function(){ trfound++; });
});
assert.equal(trfound, 9, "Proper number of tr's found");

// GetState command should return sane values
const state = $("#testTable").slimtable("getState");
assert.propContains(state, { itemsPerPage: 10, pagingStart: 0 });

done1();
})
.fail(function(param1,param2) {
Expand Down

0 comments on commit 609f51c

Please sign in to comment.