Skip to content

Instantly share code, notes, and snippets.

@jtompkins
Created February 17, 2014 19:46
Show Gist options
  • Save jtompkins/9057583 to your computer and use it in GitHub Desktop.
Save jtompkins/9057583 to your computer and use it in GitHub Desktop.
This is code I regret writing. It's inline in the page, it pollutes the global namespace, it switches randomly between underscore.js and jquery, and it uses both badly.
<script type="text/javascript">
function createColumn() {
return $('<div class="column"></div>');
}
function createBlock() {
return $('<div class="block"></div>');
}
function isLarge(tile) {
return $(tile).hasClass("large");
}
function isSmall(tile) {
return $(tile).hasClass("small");
}
function isFavorite(tile) {
return $(tile).hasClass("favorite");
}
function resetLayout() {
$(".group").each(function (index) {
var group = $(this);
$(".column .tile", group).each(function (index) {
var tile = $(this);
group.append(tile);
});
});
$(".block").remove();
$(".column").remove();
}
function handleLayout() {
// this is the height of the scrollbar, as defined in Home.scss, plus the bottom
//padding of #tiles, plus spacing from the bottom of the viewport.
var SCROLL_BAR_HEIGHT = 33;
var ROW_HEIGHT = 130;
var container = $("#tiles");
var windowHeight = $(window).height();
var containerOffset = container.offset().top;
var titleHeight = $(".group h2").outerHeight(true);
var availableHeight = windowHeight - (containerOffset + titleHeight);
var availableRows = Math.floor(availableHeight / ROW_HEIGHT);
if (availableRows <= 1)
return;
//loop through the tiles, building columns and blocks
var containerWidth = 0;
$(".group").each(function (index) {
var group = $(this);
var tiles = _.toArray($(".tile", group));
var column = null;
var partialColumn = false;
var rows = 0;
while (tiles.length > 0) {
//if we don't have a column, or we need a new column,
//go ahead and create it.
if (column == null || rows >= availableRows) {
rows = 0;
column = createColumn();
group.append(column);
}
//peek at the first tile in the array
var tile = $(tiles[0]);
if (isLarge(tile)) {
partialColumn = false;
rows += 2;
//this is a corner case; a large tile can cause a column
//wrap even if we had space for a single row.
if (rows > availableRows)
continue;
//if the tile will fit, pop it off of the queue
column.append(tile);
tiles.shift();
} else if (isFavorite(tile)) {
partialColumn = false;
rows++;
column.append(tile);
tiles.shift();
} else if (isSmall(tile)) {
if (partialColumn) {
partialColumn = false;
rows++;
} else {
partialColumn = true;
}
//if we've encountered a small tile, we need to build a
//containing block to hold the tile. since a containing block
//can hold up to four small tiles, we'll peek ahead to see if
//the next blocks are small, and, if so, we'll pop them off
//of the queue and place them into the block.
var block = createBlock();
block.append(tile);
tiles.shift();
var i = 1;
while (i < 5 && $(tiles[0]).hasClass("small")) {
var smallTile = $(tiles[0]);
//smallTile.unwrap();
block.append(smallTile);
tiles.shift();
i++;
}
column.append(block);
} else {
if (partialColumn) {
partialColumn = false;
rows++;
} else {
partialColumn = true;
}
column.append(tile);
tiles.shift();
}
}
var groupWidth = _.reduce($(".column", group), function (sum, col) { return sum + $(col).outerWidth(true); }, 0);
group.width(groupWidth);
containerWidth += group.outerWidth(true);
});
//set the height and width of the container
var containerHeight = windowHeight - containerOffset - SCROLL_BAR_HEIGHT;
container.height(containerHeight);
container.width(containerWidth);
}
function animateTileContent(tile) {
var container = $(".live-content", tile);
var item = $(".tile-content", container).first();
var offset = item.width() * -1;
item.animate({ marginLeft: offset }, 500, function () {
item.css("marginLeft", 0);
container.append(item);
window.setTimeout(function() {
animateTileContent(tile);
}, 8000);
});
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
$(function () {
handleLayout();
$(".favorite, .large").each(function (index) {
var tile = $(this);
var randomDelay = getRandomInt(5000, 10000);
window.setTimeout(function() {
animateTileContent(tile);
}, randomDelay);
});
$("#accounts-tile").click(function() {
window.location = '@Url.Action("Index", "Accounts")';
});
$(window).resize(function () {
resetLayout();
handleLayout();
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment