Skip to content

Commit

Permalink
Wrap in anonymous function to prevent polution of the global namespace.
Browse files Browse the repository at this point in the history
Fixes issues:
- zurb#22

Satisfies pull requests:
- zurb#28
  • Loading branch information
NicholasRBowers committed Apr 15, 2015
1 parent 0d34bc6 commit c9adf6a
Showing 1 changed file with 63 additions and 59 deletions.
122 changes: 63 additions & 59 deletions responsive-tables.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,71 @@
$(document).ready(function() {
var switched = false;
var updateTables = function() {
if (($(window).width() < 767) && !switched ){
switched = true;
$("table.responsive").each(function(i, element) {
splitTable($(element));
});
return true;
}
else if (switched && ($(window).width() > 767)) {
switched = false;
$("table.responsive").each(function(i, element) {
unsplitTable($(element));
});
}
};

$(window).load(updateTables);
$(window).on("redraw",function(){switched=false;updateTables();}); // An event to listen for
$(window).on("resize", updateTables);


function splitTable(original)
{
original.wrap("<div class='table-wrapper' />");

var copy = original.clone();
copy.find("td:not(:first-child), th:not(:first-child)").css("display", "none");
copy.removeClass("responsive");

original.closest(".table-wrapper").append(copy);
copy.wrap("<div class='pinned' />");
original.wrap("<div class='scrollable' />");
(function($) {

$(document).ready(function() {
var switched = false;
var updateTables = function() {
if (($(window).width() < 767) && !switched ){
switched = true;
$("table.responsive").each(function(i, element) {
splitTable($(element));
});
return true;
}
else if (switched && ($(window).width() > 767)) {
switched = false;
$("table.responsive").each(function(i, element) {
unsplitTable($(element));
});
}
};

$(window).load(updateTables);
$(window).on("redraw",function(){switched=false;updateTables();}); // An event to listen for
$(window).on("resize", updateTables);


setCellHeights(original, copy);
}

function unsplitTable(original) {
original.closest(".table-wrapper").find(".pinned").remove();
original.unwrap();
original.unwrap();
}
function splitTable(original)
{
original.wrap("<div class='table-wrapper' />");

function setCellHeights(original, copy) {
var tr = original.find('tr'),
tr_copy = copy.find('tr'),
heights = [];
var copy = original.clone();
copy.find("td:not(:first-child), th:not(:first-child)").css("display", "none");
copy.removeClass("responsive");

tr.each(function (index) {
var self = $(this),
tx = self.find('th, td');
original.closest(".table-wrapper").append(copy);
copy.wrap("<div class='pinned' />");
original.wrap("<div class='scrollable' />");

setCellHeights(original, copy);
}

function unsplitTable(original) {
original.closest(".table-wrapper").find(".pinned").remove();
original.unwrap();
original.unwrap();
}

function setCellHeights(original, copy) {
var tr = original.find('tr'),
tr_copy = copy.find('tr'),
heights = [];

tr.each(function (index) {
var self = $(this),
tx = self.find('th, td');

tx.each(function () {
var height = $(this).outerHeight(true);
heights[index] = heights[index] || 0;
if (height > heights[index]) heights[index] = height;
});

tx.each(function () {
var height = $(this).outerHeight(true);
heights[index] = heights[index] || 0;
if (height > heights[index]) heights[index] = height;
});

});
tr_copy.each(function (index) {
$(this).height(heights[index]);
});
}

tr_copy.each(function (index) {
$(this).height(heights[index]);
});
}
});

});
})(jQuery);

0 comments on commit c9adf6a

Please sign in to comment.