-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.js
124 lines (112 loc) · 4.13 KB
/
cards.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var containersToFill = [];
function reAddDefinition(cardinfo, container) {
var dt = document.createElement("dt");
dt.innerHTML = cardinfo.date;
container.appendChild(dt);
var dd = document.createElement("dd");
dd.innerHTML = cardinfo.body;
container.appendChild(dd);
}
function addCardToColumn(cardinfo, column) {
card = document.createElement("div");
card.className = "row card";
column.appendChild(card);
var bodySegment = document.createElement("div");
bodySegment.className = "col card-body s12";
card.appendChild(bodySegment);
var dateSegment = document.createElement("div");
dateSegment.className = "card-date";
dateSegment.innerHTML = cardinfo.date;
bodySegment.appendChild(dateSegment);
for (var f = 0; f < cardinfo.featured.length; f++) {
var image = document.createElement("img");
image.src = cardinfo.featured[f].img;
image.className = "card-feature-image";
if (cardinfo.featured[f].uri) {
var link = document.createElement("a");
link.href = cardinfo.featured[f].uri;
link.className = "card-feature-link";
link.appendChild(image);
bodySegment.appendChild(link);
} else {
bodySegment.appendChild(image);
}
}
var parSegment = document.createElement("p");
parSegment.innerHTML = cardinfo.body;
bodySegment.appendChild(parSegment);
}
function fillWithCards(container_id, cardWidth, cardPadding) {
var container = document.getElementById(container_id);
var lastDate = "";
var cards = [];
while (container.firstChild) {
var child = container.firstChild;
if (child.tagName == "DT") {
// Remember the date.
lastDate = child.innerHTML;
} else if (child.tagName == "DD") {
// Make a card.
cardinfo = {date: lastDate, featured: []};
cardinfo.body = child.innerHTML;
links = child.getElementsByTagName("a");
for (var l = 0; l < links.length; l++) {
var link = links[l];
if (link.hasAttribute("img")) {
src = link.getAttribute("img");
cardinfo.featured.push({img: src, uri: link.href});
}
}
cards.push(cardinfo);
}
container.removeChild(container.firstChild);
}
containersToFill.push({box: container, cards: cards,
cardWidth: cardWidth, cardPadding: cardPadding});
}
function createLayouts() {
for (var container_id = 0; container_id < containersToFill.length;
container_id++) {
container_data = containersToFill[container_id];
var container = container_data.box;
var cards = container_data.cards;
var cardWidth = container_data.cardWidth;
var cardPadding = container_data.cardPadding;
var width = container.offsetWidth;
while (container.lastChild) {
container.removeChild(container.lastChild);
}
columns = [];
var numColumns = Math.floor(width / (cardWidth + 2 * cardPadding));
var padding = Math.floor((width -
numColumns * (cardWidth + cardPadding)) /
(2 * numColumns));
for (var i = 0; i < numColumns; i++) {
var column = document.createElement("div");
column.className = "cards-column";
column.style.position = "relative";
column.style.left = padding * (1 + i) + "px";
columns.push(column);
container.appendChild(column);
}
if (columns.length > 0) {
for (var i = 0; i < cards.length; i++) {
var min_col;
var min_height = Math.pow(2, 32);
for (var c = 0; c < columns.length; c++) {
if (columns[c].offsetHeight < min_height) {
min_height = columns[c].offsetHeight;
min_col = c;
}
}
addCardToColumn(cards[i], columns[min_col])
}
} else {
// No space for cards, just add the text back.
for (var i = 0; i < cards.length; i++) {
reAddDefinition(cards[i], container)
}
}
}
}
window.onresize = createLayouts;