-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathscript.js
283 lines (254 loc) · 9.84 KB
/
script.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var isGithubDemo = isGithubDemo || false; // This is for GitHub demo only. Remove it in your project
void function(window, document, undefined) {
// ES5 strict mode
"user strict";
var MIN_COLUMN_COUNT = 3; // minimal column count
var COLUMN_WIDTH = 220; // cell width: 190, padding: 14 * 2, border: 1 * 2
var CELL_PADDING = 26; // cell padding: 14 + 10, border: 1 * 2
var GAP_HEIGHT = 15; // vertical gap between cells
var GAP_WIDTH = 15; // horizontal gap between cells
var THRESHOLD = 2000; // determines whether a cell is too far away from viewport (px)
var columnHeights; // array of every column's height
var columnCount; // number of columns
var noticeDelay; // popup notice timer
var resizeDelay; // resize throttle timer
var scrollDelay; // scroll throttle timer
var managing = false; // flag for managing cells state
var loading = false; // flag for loading cells state
var noticeContainer = document.getElementById('notice');
var cellsContainer = document.getElementById('cells');
// Cross-browser compatible event handler.
var addEvent = function(element, type, handler) {
if(element.addEventListener) {
addEvent = function(element, type, handler) {
element.addEventListener(type, handler, false);
};
} else if(element.attachEvent) {
addEvent = function(element, type, handler) {
element.attachEvent('on' + type, handler);
};
} else {
addEvent = function(element, type, handler) {
element['on' + type] = handler;
};
}
addEvent(element, type, handler);
};
// Get the minimal value within an array of numbers.
var getMinVal = function(arr) {
return Math.min.apply(Math, arr);
};
// Get the maximal value within an array of numbers.
var getMaxVal = function(arr) {
return Math.max.apply(Math, arr);
};
// Get index of the minimal value within an array of numbers.
var getMinKey = function(arr) {
var key = 0;
var min = arr[0];
for(var i = 1, len = arr.length; i < len; i++) {
if(arr[i] < min) {
key = i;
min = arr[i];
}
}
return key;
};
// Get index of the maximal value within an array of numbers.
var getMaxKey = function(arr) {
var key = 0;
var max = arr[0];
for(var i = 1, len = arr.length; i < len; i++) {
if(arr[i] > max) {
key = i;
max = arr[i];
}
}
return key;
};
// Pop notice tag after user liked or marked an item.
var updateNotice = function(event) {
clearTimeout(noticeDelay);
var e = event || window.event;
var target = e.target || e.srcElement;
if(target.tagName == 'SPAN') {
var targetTitle = target.parentNode.tagLine;
noticeContainer.innerHTML = (target.className == 'like' ? 'Liked ' : 'Marked ') + '<strong>' + targetTitle + '</strong>';
noticeContainer.className = 'on';
noticeDelay = setTimeout(function() {
noticeContainer.className = 'off';
}, 2000);
}
};
// Calculate column count from current page width.
var getColumnCount = function() {
return Math.max(MIN_COLUMN_COUNT, Math.floor((document.body.offsetWidth + GAP_WIDTH) / (COLUMN_WIDTH + GAP_WIDTH)));
};
// Reset array of column heights and container width.
var resetHeights = function(count) {
columnHeights = [];
for(var i = 0; i < count; i++) {
columnHeights.push(0);
}
cellsContainer.style.width = (count * (COLUMN_WIDTH + GAP_WIDTH) - GAP_WIDTH) + 'px';
};
// Fetch JSON string via Ajax, parse to HTML and append to the container.
var appendCells = function(num) {
if(loading) {
// Avoid sending too many requests to get new cells.
return;
}
var xhrRequest = new XMLHttpRequest();
var fragment = document.createDocumentFragment();
var cells = [];
var images;
xhrRequest.open('GET', 'json.php?n=' + num, true);
xhrRequest.onreadystatechange = function() {
if(xhrRequest.readyState == 4 && xhrRequest.status == 200) {
images = JSON.parse(xhrRequest.responseText);
for(var j = 0, k = images.length; j < k; j++) {
var cell = document.createElement('div');
cell.className = 'cell pending';
cell.tagLine = images[j].title;
cells.push(cell);
cell.innerHTML = `
<p><a href="#"><img src="img/${images[j].src}.jpg" height="${images[j].height}" width="${images[j].width}" /></a></p>
<h2><a href="#">${images[j].title}</a></h2>
<span class="like">Like!</span>
<span class="mark">Mark!</span>
`
fragment.appendChild(cell);
}
cellsContainer.appendChild(fragment);
loading = false;
adjustCells(cells);
}
};
loading = true;
xhrRequest.send(null);
};
// Fake mode, only for GitHub demo. Delete this function in your project.
var appendCellsDemo = function(num) {
if(loading) {
// Avoid sending too many requests to get new cells.
return;
}
var fragment = document.createDocumentFragment();
var cells = [];
var images = [0, 286, 143, 270, 143, 190, 285, 152, 275, 285, 285, 128, 281, 242, 339, 236, 157, 286, 259, 267, 137, 253, 127, 190, 190, 225, 269, 264, 272, 126, 265, 287, 269, 125, 285, 190, 314, 141, 119, 274, 274, 285, 126, 279, 143, 266, 279, 600, 276, 285, 182, 143, 287, 126, 190, 285, 143, 241, 166, 240, 190];
for(var j = 0; j < num; j++) {
var key = Math.floor(Math.random() * 60) + 1;
var cell = document.createElement('div');
cell.className = 'cell pending';
cell.tagLine = 'demo picture ' + key;
cells.push(cell);
cell.innerHTML = `
<p><a href="#"><img src="img/${key}.jpg" height="${images[key]}" width="190" /></a></p>
<h2><a href="#">demo picture ${key}</a></h2>
<span class="like">Like!</span>
<span class="mark">Mark!</span>
`
fragment.appendChild(cell);
}
// Faking network latency.
setTimeout(function() {
loading = false;
cellsContainer.appendChild(fragment);
adjustCells(cells);
}, 2000);
};
// Position the newly appended cells and update array of column heights.
var adjustCells = function(cells, reflow) {
var columnIndex;
var columnHeight;
for(var j = 0, k = cells.length; j < k; j++) {
// Place the cell to column with the minimal height.
columnIndex = getMinKey(columnHeights);
columnHeight = columnHeights[columnIndex];
cells[j].style.height = (cells[j].offsetHeight - CELL_PADDING) + 'px';
cells[j].style.left = columnIndex * (COLUMN_WIDTH + GAP_WIDTH) + 'px';
cells[j].style.top = columnHeight + 'px';
columnHeights[columnIndex] = columnHeight + GAP_HEIGHT + cells[j].offsetHeight;
if(!reflow) {
cells[j].className = 'cell ready';
}
}
cellsContainer.style.height = getMaxVal(columnHeights) + 'px';
manageCells();
};
// Calculate new column data if it's necessary after resize.
var reflowCells = function() {
// Calculate new column count after resize.
columnCount = getColumnCount();
if(columnHeights.length != columnCount) {
// Reset array of column heights and container width.
resetHeights(columnCount);
adjustCells(cellsContainer.children, true);
} else {
manageCells();
}
};
// Toggle old cells' contents from the DOM depending on their offset from the viewport, save memory.
// Load and append new cells if there's space in viewport for a cell.
var manageCells = function() {
// Lock managing state to avoid another async call. See {Function} delayedScroll.
managing = true;
var cells = cellsContainer.children;
var viewportTop = (document.body.scrollTop || document.documentElement.scrollTop) - cellsContainer.offsetTop;
var viewportBottom = (window.innerHeight || document.documentElement.clientHeight) + viewportTop;
// Remove cells' contents if they are too far away from the viewport. Get them back if they are near.
// TODO: remove the cells from DOM should be better :<
for(var i = 0, l = cells.length; i < l; i++) {
if((cells[i].offsetTop - viewportBottom > THRESHOLD) || (viewportTop - cells[i].offsetTop - cells[i].offsetHeight > THRESHOLD)) {
if(cells[i].className === 'cell ready') {
cells[i].fragment = cells[i].innerHTML;
cells[i].innerHTML = '';
cells[i].className = 'cell shadow';
}
} else {
if(cells[i].className === 'cell shadow') {
cells[i].innerHTML = cells[i].fragment;
cells[i].className = 'cell ready';
}
}
}
// If there's space in viewport for a cell, request new cells.
if(viewportBottom > getMinVal(columnHeights)) {
// Remove the if/else statement in your project, just call the appendCells function.
if(isGithubDemo) {
appendCellsDemo(columnCount);
} else {
appendCells(columnCount);
}
}
// Unlock managing state.
managing = false;
};
// Add 500ms throttle to window scroll.
var delayedScroll = function() {
clearTimeout(scrollDelay);
if(!managing) {
// Avoid managing cells for unnecessity.
scrollDelay = setTimeout(manageCells, 500);
}
};
// Add 500ms throttle to window resize.
var delayedResize = function() {
clearTimeout(resizeDelay);
resizeDelay = setTimeout(reflowCells, 500);
};
// Initialize the layout.
var init = function() {
// Add other event listeners.
addEvent(cellsContainer, 'click', updateNotice);
addEvent(window, 'resize', delayedResize);
addEvent(window, 'scroll', delayedScroll);
// Initialize array of column heights and container width.
columnCount = getColumnCount();
resetHeights(columnCount);
// Load cells for the first time.
manageCells();
};
// Ready to go!
addEvent(window, 'load', init);
}(window, document);