-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.html
executable file
·277 lines (226 loc) · 7.89 KB
/
stack.html
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
<!DOCTYPE html>
{% load i18n %}
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{{ STATIC_URL }}/css/normalize.css">
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/fonts.css">
<link rel="stylesheet/less" type="text/css" href="{{ STATIC_URL }}/less/style.less">
<script src="{{ STATIC_URL }}js/vendor/less-1.3.0.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="{{ STATIC_URL }}/css/stack.css">
<style>
.big {
font-size: 128px;
}
.caption {
font-size: 96px;
text-align: center;
text-shadow: 0px 2px 6px rgba(0,0,0,.5);
}
</style>
</head>
<body class="overview stack-enabled">
<header >
<a href="{% url walt_home %}">walt.</a>
<a href="{% url walt_series %}">series</a>
{% if user.is_authenticated %}
<a href="{% url walt_logout %}">{% trans "logout" %} {{ user.username }}</a>
{% else %}
<a href="{% url walt_login %}">{% trans "login" %}</a>
{% endif %}
<!-- {{ tags}} -->
</div>
</header>
<section>
<h1>{{ serie.title }}</h1>
{% with authors=serie.authors.all %}
<p>a composition of {{ serie.frames.count }} fragments</p>
<div class="authors">
{% trans "created by" %}
{% for u in authors %}
<a href="{% url walt_spiff u.username %}">{{ u.username }}</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</div>
{% endwith %}
<p class="grey">Scroll down to continue.
</section>
{% for frame in serie.frames.all %}
<section>
<h1>{{ frame.pin.title }}</h1>
</section>
{% endfor %}
<section>
<p>Stack is made by <a href="http://bost.ocks.org/mike/" rel="author">Mike Bostock</a>
<br>and is available on <a href="https://github.com/mbostock/stack">GitHub</a>
<br>under the <a href="https://github.com/mbostock/stack/blob/gh-pages/LICENSE">BSD license</a>.
<p class="grey">View source to get started.
</section>
<script src="{{ STATIC_URL }}/js/vendor/d3.v2.js"></script>
<script>
var stack = (function() {
var stack = {},
event = d3.dispatch("activate", "deactivate"),
section = d3.selectAll("section"),
self = d3.select(window),
body = document.body,
root = document.documentElement,
timeout,
duration = 250,
ease = "cubic-in-out",
screenY,
size,
yActual,
yFloor,
yTarget,
yMax,
yOffset,
n = section[0].length;
// Invert the z-index so the earliest slides are on top.
section.classed("stack", true).style("z-index", function(d, i) { return n - i; });
// Detect the slide height (by showing an active slide).
section.classed("active", true);
size = section.node().getBoundingClientRect().height;
section.classed("active", false);
// Sets the stack position.
stack.position = function(y1) {
console.log("slide changed")
var y0 = body.scrollTop / size;
if (arguments.length < 1) return y0;
// clamp and round
if (y1 >= n) y1 = n - 1;
else if (y1 < 0) y1 = Math.max(0, n + y1);
y1 = Math.floor(y1);
if (y0 - y1) {
self.on("scroll.stack", null);
leap(y1);
d3.select(body).transition()
.duration(duration)
.ease(ease)
.tween("scrollTop", tween(yTarget = y1))
.each("end", function() { yTarget = null; self.on("scroll.stack", scroll); });
}
location.replace("#" + y1);
return stack;
};
// Don't do anything fancy for iOS.
if (section.style("display") == "block") return;
self
.on("keydown.stack", keydown)
.on("resize.stack", resize)
.on("scroll.stack", scroll)
.on("mousemove.stack", snap)
.on("hashchange.stack", hashchange);
resize();
scroll();
hashchange();
// if scrolling up, jump to edge of previous slide
function leap(yNew) {
if ((yActual < n - 1) && (yActual == yFloor) && (yNew < yActual)) {
event.deactivate.call(section[0][yFloor], yFloor);
event.activate.call(section[0][--yFloor], yFloor);
yActual -= .5 - yOffset / size / 2;
scrollTo(0, yActual * size);
return true;
}
}
function resize() {
yOffset = (window.innerHeight - size) / 2;
yMax = 1 + yOffset / size;
d3.select(body)
.style("margin-top", yOffset + "px")
.style("margin-bottom", yOffset + "px")
.style("height", (n - .5) * size + yOffset + "px");
}
function hashchange() {
var hash = +location.hash.slice(1);
console.log("hashchange")
if (!isNaN(hash)) stack.position(hash);
}
function keydown() {
var delta;
switch (d3.event.keyCode) {
case 39: // right arrow
if (d3.event.metaKey) return;
case 40: // down arrow
case 34: // page down
delta = d3.event.metaKey ? Infinity : 1; break;
case 37: // left arrow
if (d3.event.metaKey) return;
case 38: // up arrow
case 33: // page up
delta = d3.event.metaKey ? -Infinity : -1; break;
case 32: // space
delta = d3.event.shiftKey ? -1 : 1;
break;
default: return;
}
if (timeout) timeout = clearTimeout(timeout);
if (yTarget == null) yTarget = (delta > 0 ? Math.floor : Math.ceil)(yActual == yFloor ? yFloor : yActual + (.5 - yOffset / size / 2));
stack.position(yTarget = Math.max(0, Math.min(n - 1, yTarget + delta)));
d3.event.preventDefault();
}
function scroll() {
// Detect whether to scroll with documentElement or body.
if (body !== root && root.scrollTop) body = root;
var yNew = Math.max(0, body.scrollTop / size);
if (yNew >= n - 1.51 + yOffset / size) yNew = n - 1;
// if scrolling up, jump to edge of previous slide
if (leap(yNew)) return;
var yNewFloor = Math.max(0, Math.floor(yActual = yNew)),
yError = Math.min(yMax, (yActual % 1) * 2);
if (yFloor != yNewFloor) {
if (yFloor != null) event.deactivate.call(section[0][yFloor], yFloor);
event.activate.call(section[0][yNewFloor], yNewFloor);
yFloor = yNewFloor;
}
section
.classed("active", false);
d3.select(section[0][yFloor])
.style("-webkit-transform", yError ? "translate3d(0," + (-yError * size) + "px,0)" : null)
.style("-o-transform", yError ? "translate(0," + (-yError * size) + "px)" : null)
.style("-moz-transform", yError ? "translate(0," + (-yError * size) + "px)" : null)
.style("transform", yError ? "translate(0," + (-yError * size) + "px)" : null)
.classed("active", yError != yMax);
d3.select(section[0][yFloor + 1])
.style("-webkit-transform", yError ? "translate3d(0,0,0)" : null)
.style("-o-transform", yError ? "translate(0,0)" : null)
.style("-moz-transform", yError ? "translate(0,0)" : null)
.style("transform", yError ? "translate(0,0)" : null)
.classed("active", yError > 0);
}
function snap() {
var y = d3.event.clientY;
if (y === screenY) return; // ignore move on scroll
screenY = y;
if (yTarget != null) return; // don't snap if already snapping
var y0 = stack.position(),
y1 = Math.max(0, Math.round(y0 + .25));
// if we're before the first slide, or after the last slide, do nothing
if (y0 <= 0 || y0 >= n - 1.51 + yOffset / size) return;
// if the previous slide is not visible, immediate jump
if (y1 > y0 && y1 - y0 < .5 - yOffset / size) scrollTo(0, y1 * size);
// else transition
else if (y1 !== y0) stack.position(y1);
}
function tween(y) {
return function() {
var i = d3.interpolateNumber(this.scrollTop, y * size);
return function(t) { scrollTo(0, i(t)); scroll(); };
};
}
stack.duration = function(_) {
if (!arguments.length) return duration;
duration = _;
return stack;
};
stack.ease = function(_) {
if (!arguments.length) return ease;
ease = _;
return stack;
};
d3.rebind(stack, event, "on");
return stack;
})();
</script>
</body>