Skip to content

Commit

Permalink
Create jquery.magiccolumns.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cousins committed May 6, 2014
1 parent cf28a74 commit 9107df6
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions jquery.magiccolumns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module.
define(['jquery'], factory);
} else {
// Browser globals.
factory(jQuery);
}
}(function ($) {

$.fn.magiccolumns = function() {

// if we're called on junk or a non-table non-junk, run away
if (this[0] == undefined || this[0].tagName !== 'TABLE') {
console.log('.magiccolumns must be called on a <table> element.');
return this; // chaining!
}

var table = this[0];

if (!$(table).data('magiccolumns')) {
$(table).data({magiccolumns: true});

$( window ).resize(function() {
update(table);
});
}

// get the highest priority
var maxpriority = 0;
var headers = $(table).find('th');

//sizebaker(this);

headers.each(function(){
if ($(this).data('priority') !== undefined && $(this).data('priority') > maxpriority) {
maxpriority = $(this).data('priority');
};
});

if (maxpriority == 0) {
setTimeout(function(){
$(table).magiccolumns();
}, 500);
return this;
}

$(table).data({maxpriority: maxpriority});

update(table);

return this; // chaining!

}

var update = function(table) {
// Is the table too big? either left edge or right edge needs to be off-screen
var maxRight = $(window).width();

// Let's try this... show everything, and then hide stuff until it fits.
$(table).find('th').css({display: 'table-cell'});
$(table).find('td').css({display: 'table-cell'});

var lastpriority = $(table).data('maxpriority');

while ($(table).offset().left < 0 ||
$(table).offset().left + $(table).outerWidth() > maxRight) {

var numHidden = 0;
$(table).find('th').each(function() {
if ($(this).data('priority') == lastpriority) {
$(this).css({display: 'none'});
numHidden++;
}
});

$(table).find('td').each(function() {
if ($(this).data('priority') == lastpriority) {
$(this).css({display: 'none'});
numHidden++;
}
});

// walk down priority in case we're still too large
lastpriority -= 1;
if (lastpriority <= 0) break;
}

};

}));

0 comments on commit 9107df6

Please sign in to comment.