Skip to content

Commit 054888c

Browse files
committed
refactor: Simplify code. Remove legacy code.
1 parent 5210661 commit 054888c

File tree

4 files changed

+97
-124
lines changed

4 files changed

+97
-124
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
},
1010
rules: {
1111
"no-console": 0,
12-
"prefer-const": 2,
12+
"prefer-const": 0,
1313
"no-var": 2
1414
}
1515
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Big
22

3+
[Get started on Glitch](https://glitch.com/edit/#!/remix/clone-from-repo?REPO_URL=https://github.com/tmcw/big.git)
4+
35
<p align="center">
46
<img width="540" src='.github/logo.png' />
57
</p>
@@ -231,4 +233,4 @@ At the very least, themes are CSS files. You can pick a theme by picking one in
231233
- **light**: like dark, but flipped.
232234
- **white**: instead of tastefully off-white and off-black, this theme uses stark, literal black & white colors.
233235

234-
[![](https://circleci.com/gh/tmcw/big/tree/gh-pages.svg?style=shield&circle-token=2963848e42fe67b8a66a2ad2d6dd99d05bdde6a4 "Circle CI")](https://circleci.com/gh/tmcw/big/tree/gh-pages)
236+
[![](https://circleci.com/gh/tmcw/big/tree/gh-pages.svg?style=shield&circle-token=2963848e42fe67b8a66a2ad2d6dd99d05bdde6a4 "Circle CI")](https://circleci.com/gh/tmcw/big/tree/gh-pages)

big.js

Lines changed: 89 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
1-
const ASPECT_RATIO =
2-
window.BIG_ASPECT_RATIO === undefined ? 1.6 : window.BIG_ASPECT_RATIO;
1+
let ASPECT_RATIO = window.BIG_ASPECT_RATIO === undefined ? 1.6 : window.BIG_ASPECT_RATIO;
32

43
function parseHash() {
54
return parseInt(window.location.hash.substring(1), 10);
65
}
76

7+
function emptyNode(node) {
8+
while (node.hasChildNodes()) node.removeChild(node.lastChild);
9+
}
10+
811
function ce(type, klass = "") {
9-
const element = document.createElement(type);
12+
let element = document.createElement(type);
1013
element.className = klass;
1114
return element;
1215
}
1316

1417
addEventListener("load", function() {
15-
let slideDivs = Array.from(document.querySelectorAll("body > div")),
16-
timeoutInterval;
17-
18-
const forward = () => go(big.current + 1);
19-
const reverse = () => go(big.current - 1);
20-
const { body } = document,
21-
{ className: initialBodyClass } = body,
22-
notes = slideDivs.map(slide =>
23-
Array.from(slide.querySelectorAll("notes"), noteElement => {
24-
noteElement.parentNode.removeChild(noteElement);
25-
return noteElement.innerHTML.trim();
26-
})
27-
),
28-
big = {
29-
current: -1,
30-
mode: "talk",
31-
forward: forward,
32-
reverse: reverse,
33-
go: go,
34-
length: slideDivs.length
35-
},
36-
pc = body.appendChild(ce("div", "presentation-container"));
37-
38-
slideDivs = slideDivs.map(slide => {
18+
let slideDivs = Array.from(document.querySelectorAll("body > div"));
19+
let pc = document.body.appendChild(ce("div", "presentation-container"));
20+
slideDivs = slideDivs.map((slide, _i) => {
3921
slide.setAttribute("tabindex", 0);
4022
slide.classList.add("slide");
4123

4224
if (slide.hasAttribute("data-background-image")) {
43-
const preloadLink = document.createElement("link");
25+
let preloadLink = document.createElement("link");
4426
preloadLink.href = slide.getAttribute("data-background-image");
4527
preloadLink.rel = "preload";
4628
preloadLink.as = "image";
4729
document.head.appendChild(preloadLink);
4830
}
4931

50-
const sc = pc.appendChild(ce("div", "slide-container"));
32+
let sc = pc.appendChild(ce("div", "slide-container"));
5133
sc.appendChild(slide);
52-
return sc;
34+
return Object.assign(sc, {
35+
_notes: Array.from(slide.querySelectorAll("notes"), noteElement => {
36+
noteElement.parentNode.removeChild(noteElement);
37+
return noteElement.innerHTML.trim();
38+
}),
39+
_i
40+
});
5341
});
42+
let timeoutInterval,
43+
{ body } = document,
44+
{ className: initialBodyClass } = body,
45+
big = (window.big = {
46+
current: -1,
47+
mode: "talk",
48+
forward: forward,
49+
reverse: reverse,
50+
go: go,
51+
length: slideDivs.length
52+
});
53+
function forward() {
54+
go(big.current + 1);
55+
}
56+
function reverse() {
57+
go(big.current - 1);
58+
}
5459

5560
body.className = "talk-mode " + initialBodyClass;
5661
window.matchMedia("print").addListener(onPrint);
@@ -60,18 +65,13 @@ addEventListener("load", function() {
6065
addEventListener("hashchange", onHashChange);
6166
addEventListener("resize", onResize);
6267
window.big = big;
63-
console.log(
64-
"This is a big presentation. You can: \n\n* press j to jump to a slide\n" +
65-
"* press p to see the print view\n* press t to go back to the talk view"
66-
);
68+
console.log("This is a big presentation. You can: \n\n* press j to jump to a slide\n" + "* press p to see the print view\n* press t to go back to the talk view");
6769
go(parseHash() || big.current);
6870

6971
function useDataImageAsBackground(sc) {
70-
const { firstChild } = sc;
72+
let { firstChild } = sc;
7173
if (firstChild.hasAttribute("data-background-image")) {
72-
sc.style.backgroundImage = `url("${firstChild.getAttribute(
73-
"data-background-image"
74-
)}")`;
74+
sc.style.backgroundImage = `url("${firstChild.getAttribute("data-background-image")}")`;
7575
return firstChild.classList.add("imageText");
7676
}
7777
sc.style.backgroundImage = "";
@@ -82,59 +82,40 @@ addEventListener("load", function() {
8282
n = Math.max(0, Math.min(big.length - 1, n));
8383
if (!force && big.current === n) return;
8484
big.current = n;
85-
const sc = slideDivs[n];
86-
const slideDiv = sc.firstChild;
87-
if (notes[n].length) {
85+
let sc = slideDivs[n],
86+
slideDiv = sc.firstChild;
87+
if (sc._notes.length) {
8888
console.group(n);
89-
for (const note of notes[n]) {
90-
console.log(
91-
"%c%s",
92-
"padding:5px;font-family:serif;font-size:18px;line-height:150%;",
93-
note
94-
);
95-
}
89+
for (let note of sc._notes) console.log("%c%s", "padding:5px;font-family:serif;font-size:18px;line-height:150%;", note);
9690
console.groupEnd();
9791
}
9892
slideDivs.forEach((slide, i) => {
9993
slide.style.display = i === n ? "flex" : "none";
10094
});
101-
body.className = `talk-mode ${slideDiv.getAttribute("data-bodyclass") ||
102-
""} ${initialBodyClass}`;
95+
body.className = `talk-mode ${slideDiv.getAttribute("data-bodyclass") || ""} ${initialBodyClass}`;
10396
useDataImageAsBackground(sc);
104-
if (timeoutInterval !== undefined) {
105-
window.clearInterval(timeoutInterval);
106-
timeoutInterval = undefined;
107-
}
108-
if (slideDiv.hasAttribute("data-time-to-next")) {
109-
const timeToNext = parseFloat(slideDiv.getAttribute("data-time-to-next"));
110-
timeoutInterval = window.setTimeout(forward, timeToNext * 1000);
111-
}
112-
slideDiv.focus();
97+
window.clearInterval(timeoutInterval);
98+
if (slideDiv.hasAttribute("data-time-to-next")) timeoutInterval = window.setTimeout(forward, parseFloat(slideDiv.getAttribute("data-time-to-next")) * 1000);
11399
onResize();
114100
if (window.location.hash !== n) window.location.hash = n;
115-
document.title = slideDiv.textContent || slideDiv.innerText;
101+
document.title = slideDiv.textContent;
116102
}
117103

118104
function resizeTo(sc, width, height) {
119-
const slideDiv = sc.firstChild;
120-
const padding = Math.min(width * 0.04);
121-
let fontSize = height;
105+
let slideDiv = sc.firstChild,
106+
padding = Math.min(width * 0.04),
107+
fontSize = height;
122108
sc.style.width = `${width}px`;
123109
sc.style.height = `${height}px`;
124110
slideDiv.style.padding = `${padding}px`;
125-
if (getComputedStyle(slideDiv).display === "grid")
126-
slideDiv.style.height = `${height - padding * 2}px`;
127-
for (const step of [100, 50, 10, 2]) {
111+
if (getComputedStyle(slideDiv).display === "grid") slideDiv.style.height = `${height - padding * 2}px`;
112+
for (let step of [100, 50, 10, 2]) {
128113
for (; fontSize > 0; fontSize -= step) {
129114
slideDiv.style.fontSize = `${fontSize}px`;
130115
if (
131116
slideDiv.scrollWidth <= width &&
132117
slideDiv.offsetHeight <= height &&
133-
Array.from(slideDiv.querySelectorAll("div")).every(
134-
elem =>
135-
elem.scrollWidth <= elem.clientWidth &&
136-
elem.scrollHeight <= elem.clientHeight
137-
)
118+
Array.from(slideDiv.querySelectorAll("div")).every(elem => elem.scrollWidth <= elem.clientWidth && elem.scrollHeight <= elem.clientHeight)
138119
) {
139120
break;
140121
}
@@ -143,31 +124,24 @@ addEventListener("load", function() {
143124
}
144125
}
145126

146-
function emptyNode(node) {
147-
while (node.hasChildNodes()) node.removeChild(node.lastChild);
148-
}
149-
150127
function onPrint() {
151128
if (big.mode === "print") return;
152129
body.className = `print-mode ${initialBodyClass}`;
153130
emptyNode(pc);
154-
slideDivs.forEach((sc, i) => {
155-
const subContainer = pc.appendChild(ce("div", "sub-container")),
156-
sbc = subContainer.appendChild(
157-
ce("div", sc.firstChild.getAttribute("data-bodyclass") || "")
158-
);
131+
for (let sc of slideDivs) {
132+
let subContainer = pc.appendChild(ce("div", "sub-container")),
133+
sbc = subContainer.appendChild(ce("div", sc.firstChild.getAttribute("data-bodyclass") || ""));
159134
sbc.appendChild(sc);
160135
sc.style.display = "flex";
161136
useDataImageAsBackground(sc);
162137
resizeTo(sc, 512, 320);
163-
if (notes[i].length) {
164-
const notesUl = subContainer.appendChild(ce("ul", "notes-list"));
165-
for (const note of notes[i]) {
166-
const li = notesUl.appendChild(ce("li"));
167-
li.innerText = note;
168-
}
138+
if (!sc._notes.length) return;
139+
let notesUl = subContainer.appendChild(ce("ul", "notes-list"));
140+
for (let note of sc._notes) {
141+
let li = notesUl.appendChild(ce("li"));
142+
li.innerText = note;
169143
}
170-
});
144+
}
171145
big.mode = "print";
172146
}
173147

@@ -176,29 +150,25 @@ addEventListener("load", function() {
176150
big.mode = "talk";
177151
body.className = `talk-mode ${initialBodyClass}`;
178152
emptyNode(pc);
179-
for (const sc of slideDivs) pc.appendChild(sc);
180-
let goTo = big.current;
181-
if (typeof i === "number") goTo = i;
182-
go(goTo, true);
153+
for (let sc of slideDivs) pc.appendChild(sc);
154+
go(i, true);
183155
}
184156

185157
function onJump() {
186158
if (big.mode === "jump") return;
187159
big.mode = "jump";
188160
body.className = "jump-mode " + initialBodyClass;
189161
emptyNode(pc);
190-
slideDivs.forEach((sc, i) => {
191-
const subContainer = pc.appendChild(ce("div", "sub-container"));
162+
slideDivs.forEach(sc => {
163+
let subContainer = pc.appendChild(ce("div", "sub-container"));
192164
subContainer.addEventListener("keypress", e => {
193165
if (e.key !== "Enter") return;
194166
subContainer.removeEventListener("click", onClickSlide);
195167
e.stopPropagation();
196168
e.preventDefault();
197-
onTalk(i);
169+
onTalk(sc._i);
198170
});
199-
const sbc = subContainer.appendChild(
200-
ce("div", sc.firstChild.getAttribute("data-bodyclass") || "")
201-
);
171+
let sbc = subContainer.appendChild(ce("div", sc.firstChild.getAttribute("data-bodyclass") || ""));
202172
sbc.appendChild(sc);
203173
sc.style.display = "flex";
204174
useDataImageAsBackground(sc);
@@ -207,7 +177,7 @@ addEventListener("load", function() {
207177
subContainer.removeEventListener("click", onClickSlide);
208178
e.stopPropagation();
209179
e.preventDefault();
210-
onTalk(i);
180+
onTalk(sc._);
211181
}
212182
subContainer.addEventListener("click", onClickSlide);
213183
});
@@ -236,17 +206,17 @@ addEventListener("load", function() {
236206
onPrint();
237207
break;
238208
case "t":
239-
onTalk();
209+
onTalk(big.current);
240210
break;
241211
case "j":
242212
onJump();
243213
break;
244214
}
245215
if (big.mode !== "jump") return;
246-
const { activeElement } = document;
216+
let { activeElement } = document;
247217
if (activeElement && activeElement.classList.contains("slide")) {
248-
const startIndex = slideDivs.indexOf(activeElement.parentNode);
249-
const columnIndexes = getColumnIndexes(activeElement);
218+
let startIndex = slideDivs.indexOf(activeElement.parentNode),
219+
columnIndexes = getColumnIndexes(activeElement);
250220
jumpFocus(
251221
e,
252222
{
@@ -262,8 +232,8 @@ addEventListener("load", function() {
262232
}
263233

264234
function getColumnIndexes(activeElement) {
265-
const { left } = activeElement.getBoundingClientRect();
266-
let lastIndex,
235+
let { left } = activeElement.getBoundingClientRect(),
236+
lastIndex,
267237
foundSelf = false;
268238
for (let i = 0; i < slideDivs.length; i++) {
269239
if (slideDivs[i].firstChild.getBoundingClientRect().left === left) {
@@ -279,24 +249,25 @@ addEventListener("load", function() {
279249
}
280250

281251
function jumpFocus(e, i) {
282-
if (slideDivs[i]) {
283-
e.preventDefault();
284-
slideDivs[i].firstChild.focus();
285-
}
252+
if (!slideDivs[i]) return;
253+
e.preventDefault();
254+
slideDivs[i].firstChild.focus();
286255
}
287256

288257
function onTouchStart(e) {
289258
if (big.mode !== "talk") return;
290-
const { pageX: startingPageX } = e.changedTouches[0];
291-
document.addEventListener("touchend", onTouchEnd, { once: true });
292-
293-
function onTouchEnd(e2) {
294-
const distanceTraveled = e2.changedTouches[0].pageX - startingPageX;
295-
// Don't navigate if the person didn't swipe by fewer than 4 pixels
296-
if (Math.abs(distanceTraveled) < 4) return;
297-
if (distanceTraveled < 0) forward();
298-
else reverse();
299-
}
259+
let { pageX: startingPageX } = e.changedTouches[0];
260+
document.addEventListener(
261+
"touchend",
262+
function(e2) {
263+
let distanceTraveled = e2.changedTouches[0].pageX - startingPageX;
264+
// Don't navigate if the person didn't swipe by fewer than 4 pixels
265+
if (Math.abs(distanceTraveled) < 4) return;
266+
if (distanceTraveled < 0) forward();
267+
else reverse();
268+
},
269+
{ once: true }
270+
);
300271
}
301272

302273
function onHashChange() {

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' />
66
<title>Big</title>
77
<link href='big.css' rel='stylesheet' type='text/css' />
8-
<style>
9-
.new-shiny { background: #aaaaaa; }
10-
</style>
118
<script src='big.js'></script>
129
</head>
1310
<body class='light'>
14-
<div>Your presentation goes here</div>
11+
<div>
12+
Your presentation goes here
13+
<notes>Speakers notes go here.</notes>
14+
</div>
1515
</body>
1616
</html>

0 commit comments

Comments
 (0)