Skip to content

Commit

Permalink
Item8646: ready for release
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/JQTablePlugin@6615 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Mar 2, 2010
1 parent 5fa999d commit 694970c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 57 deletions.
31 changes: 20 additions & 11 deletions data/System/JQTablePlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,43 @@

%TOC%

This *work-in-progress* _aims_ to be a 99% replacement for !TablePlugin. It requires Javascript
to be enabled in the browser, and does *not* fall back to TablePlugin, so only use it if you
are sure all users will have Javascript enabled.

This is a work in progress and it's not perfect yet; some notable omissions:
1 Now sort icon in table headers (just click the header to sort)
1 Some issues with sorting when there are multiple header rows
1 Sort direction and initcolumns controls don't work
This plugin does two things:
1 If TablePlugin is enabled, it enhances TablePlugin with client-side column sorting (if Javascript is enabled)
1 If !TablePlugin is _not_ enabled, it performs the same processing as TablePlugin except it does it in the browser.

Some notable omissions:
* Sorting doesn't work correctly if you have multiple header rows

*when used without TablePlugin*
* No sort icon in table headers (just click the header to sort)
* The sorted data background doesn't alternate if more than one colour is given.

*when used with TablePlugin:*
* Sorted column background doesn't work

You can help improve it, please feel free to do so.

Sorting is done using the jQuery tablesorter plugin written by Christian Bach,
which is included.

---++ Usage

Install it; disable TablePlugin and enable this plugin. Click on column headers to sort.
If used with TablePlugin: Click on column headers to sort.

If used without TablePlugin: Behaviour should be identical to TablePlugin.

See TablePlugin for other usage.
See TablePlugin for more information.

---++ Installation Instructions

%$INSTALL_INSTRUCTIONS%

---++ Info
Another great Foswiki extension from the <a style="text-decoration:none" href="http://wikiring.com"><img src="%ATTACHURLPATH%/wikiringlogo20x20.png" alt="" /> *WikiRing* </a> - working together to improve your wiki experience!

| Author(s): | Crawford Currie http://c-dot.co.uk |
| Copyright: | Copyright &copy; 2010 Crawford Currie |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| License: | [http://www.gnu.org/licenses/old-licenses/gpl-2.0.html][Gnu General Public License v2]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
Expand Down
37 changes: 27 additions & 10 deletions lib/Foswiki/Plugins/JQTablePlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package Foswiki::Plugins::JQTablePlugin;
use strict;
use Assert;

use JSON;

use Foswiki::Func (); # The plugins API
use Foswiki::Plugins (); # For the API version
use Foswiki::Plugins::JQueryPlugin ();
Expand All @@ -17,6 +15,7 @@ our $NO_PREFS_IN_TOPIC = 1;
our $DEFAULT_TABLE_SETTINGS =
'tableborder="1" valign="top" headercolor="#fff" headerbg="#687684" headerbgsorted="#345" databg="#fff,#edf4f9" databgsorted="#f1f7fc,#ddebf6" tablerules="rows" headerrules="cols"';
our %pluginAttributes;
our $tpRegistered = 0;

sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;
Expand All @@ -26,34 +25,52 @@ sub initPlugin {
Foswiki::Func::addToZone('body', 'JQTABLEPLUGIN::JS', <<HERE, 'JQUERYPLUGIN::FOSWIKI');
<script type="text/javascript" src="%PUBURLPATH%/%SYSTEMWEB%/JQTablePlugin/tables$src.js"></script>
HERE

my $sort = Foswiki::Func::getPreferencesValue('TABLEPLUGIN_SORT')
|| 'all';
Foswiki::Func::addToZone('head', 'JQTABLEPLUGIN::SORT', <<HERE);
<meta name="JQTABLEPLUGINSORT" content="$sort" />
HERE
my $plugin = Foswiki::Plugins::JQueryPlugin::registerPlugin(
'tablesorter', 'Foswiki::Plugins::JQTablePlugin::TableSorter');
if ($sort =~ /^(all|attachments)$/) {
# In this special case, the tablesorter is always required
Foswiki::Plugins::JQueryPlugin::registerPlugin(
'tablesorter', 'Foswiki::Plugins::JQTablePlugin::TableSorter');
Foswiki::Plugins::JQueryPlugin::createPlugin('tablesorter');
$tpRegistered = 1;
}

Foswiki::Func::registerTagHandler( 'TABLE', \&_TABLE );
# if the TablePlugin isn't there to handle table formatting, then
# enable JS to do it.
if (!$Foswiki::cfg{Plugins}{TablePlugin}{Enabled}) {
Foswiki::Func::registerTagHandler( 'TABLE', \&_TABLE );
}
return 1;
}

sub _TABLE {
my($session, $params, $theTopic, $theWeb) = @_;
my($session, $params, $topic, $web) = @_;

unless ($tpRegistered) {
Foswiki::Plugins::JQueryPlugin::registerPlugin(
'tablesorter', 'Foswiki::Plugins::JQTablePlugin::TableSorter');
Foswiki::Plugins::JQueryPlugin::createPlugin('tablesorter');
$tpRegistered = 1;
}
_readPluginSettings() if !%pluginAttributes;
Foswiki::Plugins::JQueryPlugin::createPlugin('tablesorter');
my %p = %pluginAttributes;
while (my ($k, $v) = each %$params) {
next if $k =~ /^_/ && $k ne '_DEFAULT';
$p{$k} = $v;
}
return '<textarea class="jqtp_process" style="display:none">'
. JSON::to_json(\%p)
. '</textarea>';
return CGI::div(
{
class => 'jqtp_process',
style => 'display:none',
# Note: manual JSON generator, due to amazing slowness
# of CPAN JSON module
title => join(',', map { "'$_':'$p{$_}'" } keys %p),
},
'');
}

# Copied from TablePlugin
Expand Down
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/JQTablePlugin/DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# Time::ParseDate,>=2003.0211,cpan,Required.
Foswiki::Plugins::JQueryPlugin,>=0,perl,Required


1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/JQTablePlugin/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub/System/JQTablePlugin/tables.js 0644 Javascript module
pub/System/JQTablePlugin/tables.js.gz 0644 Javascript module
pub/System/JQTablePlugin/jquery.tablesorter.min.js 0644 Javascript module
pub/System/JQTablePlugin/jquery.tablesorter.min.js.gz 0644 Javascript module
pub/System/JQTablePlugin/wikiringlogo20x20.png 0644 Javascript module

131 changes: 95 additions & 36 deletions pub/System/JQTablePlugin/tables_src.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ jQuery(document).ready(
// Process elements marked with "jqtp_process". These are generated
// when %TABLE tags are expanded.
process : function() {
$(".jqtp_process").each(
function() {
// Find the next table in the DOM
var pdata = $(this).text();
var params = eval('('+pdata+')');
var table = jqtp.nextTable(this);
$(this).remove();
if (!table)
return;
table = $(table);
jqtp.doTable(params, table);
});
var pdata = '({' + $(this).attr('title') + '})';
var params = eval(pdata);
var table = jqtp.nextTable(this);
if (!table)
return;
$(table).attr("jqtp_params", pdata);
jqtp.doTable(params, $(table));
$(this).remove();
},

// Find the next TABLE tag after the given element. This matches
Expand Down Expand Up @@ -79,6 +75,19 @@ jQuery(document).ready(
var hrc = p.headerrows;
var frc = p.footerrows;

jqtp.cleanHeadAndFoot(t, hrc, frc);

jqtp.colours(p, t);
jqtp.borders(p, t);
jqtp.layout(p, t);
jqtp.align(p, t);
if (p.sort == "on" && (p.disableallsort != "on")) {
t.addClass("jqtp_sortable");
}
},

// try and pull out head and foot
cleanHeadAndFoot : function(t, hrc, frc) {
var bodies = t.find('tbody');
var b = bodies[0];
var h = t.find('thead');
Expand Down Expand Up @@ -108,10 +117,28 @@ jQuery(document).ready(
}

var f = t.find('tfoot');
if (f.length == 0 && $(b).children().length > 0 && frc > 0) {
if (f.length != 0 && f.children().length == 0) {
// There's a bug in Render.pm that makes it generate
// an empty tfoot even if there are footer rows
// Remove empty tfoot and recompute
f.remove();
f = [];
}
if (f.length == 0 && $(b).children().length > 0) {
// No TFOOT, are there enough rows in the body?
if (frc > $(b).children().length)
frc = $(b).children().length;
if (frc != undefined) {
if (frc > $(b).children().length)
frc = $(b).children().length;
} else {
// Footer rows not explicitly defined - see
// if we can find footer rows by groping
frc = 0;
var lc = b.lastChild;
while (lc && lc.firstChild.tagName == 'TH') {
frc++;
lc = lc.previousSibling;
}
}
if (frc > 0) {
bodies.after("<tfoot></tfoot>");
f = t.find('tfoot');
Expand All @@ -121,23 +148,6 @@ jQuery(document).ready(
}
}
}

jqtp.colours(p, t);
jqtp.borders(p, t);
jqtp.layout(p, t);
jqtp.align(p, t);
if (p.sort == "on" && (p.disableallsort != "on")) {
t.addClass("jqtp_sortable");
jqtp.sorts(p, t);
}
},

// handle sort options
sorts : function(p, t) {
var sortcol = p.initSort;
if (sortcol <= 0) sortcol = 1;
var sortdir = p.initdirection;
var sortcolbg = p.databgsorted
},

// handle colour options
Expand Down Expand Up @@ -245,22 +255,71 @@ jQuery(document).ready(
t.find("tbody > tr > td")
.add(t.find("tbody > tr > th"))
.css("text-align", p.headeralign);
},

// handle sort options; cache them on the table for picking up when
// we init tablesorter
makeSortable : function() {
var sortOpts;

var pdata = $(this).attr("jqtp_params");
if (pdata != undefined) {
$(this).removeAttr("jqtp_params");
var p = eval(pdata);
var sortcol = [0, 0];

if (p.initSort != undefined) {
if (!sortOpts) sortOpts = {};
sortcol[0] = p.initSort - 1;
sortOpts.sortList = [sortcol];
}
if (p.initdirection != undefined) {
if (!sortOpts) sortOpts = {};
sortcol[1] = (p.initdirection == "down") ? 1 : 0;
sortOpts.sortList = [sortcol];
}

if (p.databgsorted != undefined) {
if (!sortOpts) sortOpts = {};

var className = 'jqtp_databgsorted_'
+ p.databgsorted.replace(/\W/g, '_');

/* Simplification; rather than pissing about colouring
alternate rows, paint all rows the same colour. */
var cols = p.databgsorted.split(/\s*,\s*/);
col = cols[0];

$("body").append('<style type="text/css">.' + className
+ '{background-color:' + col
+ '}</style>');
sortOpts.cssAsc = className;
sortOpts.cssDesc = className;
}
}
if (!$(this).find("thead").length) {
jqtp.cleanHeadAndFoot($(this));
}

$(this).tablesorter(sortOpts);
}

};

// Process tables with a %TABLE tag
jqtp.process();
$(".jqtp_process").each(jqtp.process);

// If sort is all, attach the sortable class to all tables
var sort = $("meta[name='JQTABLEPLUGINSORT']").attr("content");
if (sort) {
if (sort == 'all')
if (sort == 'all') {
$("table").addClass("jqtp_sortable");
else if (sort == 'attachments')
} else if (sort == 'attachments') {
// Just attachments
$(".foswikiAttachments table").addClass("jqtp_sortable");
}
}

// Process tables marked as sortable
$(".jqtp_sortable").tablesorter();
$(".jqtp_sortable").each(jqtp.makeSortable);
});
Binary file added pub/System/JQTablePlugin/wikiringlogo20x20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 694970c

Please sign in to comment.