-
Notifications
You must be signed in to change notification settings - Fork 6
/
from_quote_file.js
160 lines (126 loc) · 3.93 KB
/
from_quote_file.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
const quoteSeperator = /\n----\n/;
const authorSeperator = /\n\s{4}(.*?)$/;
const emptyLine = /^(\s+)?\n+$/
const quoteFileAddress = 'quotes.txt';
const quoteBodyHTMLElement = 'quote-body';
const fromHTMLElement = 'from';
const totalHTMLElement = 'total';
const currentQuoteIndexHTMLElement = 'currentQuoteIndex';
class Quotes {
constructor() {
this.htmlFields = {
quoteBody: document.getElementById(quoteBodyHTMLElement),
from: document.getElementById(fromHTMLElement),
total: document.getElementById(totalHTMLElement),
currentQuoteIndex: document.getElementById(currentQuoteIndexHTMLElement),
};
this.url = {
hash: '',
args: [],
}
this.quoteIndex = -1;
this.quotesContainer = [];
this.parseUrl(window.location.href);
this.getFile();
window.onpopstate = this.onHistoryChanged.bind(this);
}
getFile() {
fetch(quoteFileAddress)
.then(response => {
response.text().then(
content => this.parseContent(content)
)
})
.catch(e => alert("Unable to get quote file: " + e));
}
parseContent(content) {
const lines = content.split(quoteSeperator);
let len = lines.length;
if (lines[len - 1] =~ emptyLine) {
lines.splice(-1, 1);
len = lines.length;
}
lines.forEach(item => {
const quote = { text: '', author: ''};
if (authorSeperator.test(item)) {
quote.author = item.match(authorSeperator)[1].replace(/\n/, ' ');
quote.text = item.replace(authorSeperator, '').replace(/\n/, ' ');
} else {
delete quote.author
quote.text = item.replace(/\n/, ' ');
}
this.quotesContainer.push(quote);
});
this.htmlFields.total.innerText = `${this.quotesContainer.length}`;
this.getQuote();
}
getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
calcFontSize(textLength) {
const baseSize = 4;
if (textLength >= baseSize) {
textLength = baseSize - 2;
}
const fontSize = baseSize - textLength;
return `${fontSize}vw`;
}
getCurrentQuote() {
return this.quotesContainer[this.quoteIndex];
}
extractUrlQuoteNumber() {
let quoteIndex = -1;
this.url.args.forEach( (value) => {
if (value.key === "quote") {
quoteIndex = parseInt(value.value, 10);
return;
}
} );
return quoteIndex;
}
getQuote(clicked) {
let quoteNumber = null
if (!clicked) {
if (this.url.hash != '') {
quoteNumber = parseInt(this.url.hash, 10);
} else if (this.url.args.length > 0) {
quoteNumber = this.extractUrlQuoteNumber();
}
}
if (quoteNumber === null ||
isNaN(quoteNumber) ||
quoteNumber >= this.quotesContainer.length) {
this.getRandomQuote();
} else {
this.quoteIndex = quoteNumber;
}
const quote = this.quotesContainer[this.quoteIndex];
this.htmlFields.quoteBody.innerText = quote.text;
this.htmlFields.quoteBody.style.fontSize = this.calcFontSize(quote.text.length);
this.htmlFields.from.innerText = ''
if (quote.hasOwnProperty('author')) {
this.htmlFields.from.innerText = '—' + quote.author
}
this.htmlFields.currentQuoteIndex.innerText = `${this.quoteIndex || 0}`;
// NOTE: This is a small bug of twice having the same history
// TODO: fix it :D
window.history.pushState({quote: this.quoteIndex}, document.head.title,
`?quote=${this.quoteIndex}`);
}
getRandomQuote() {
this.quoteIndex = this.getRandomInt(this.quotesContainer.length);
}
parseUrl(urlString) {
let url = new URL(urlString || window.location.href);
if (url.hash != '') {
this.url.hash = url.hash.replace('#', '');
}
this.url.args = [];
url.searchParams.forEach((value, key) => this.url.args.push({ key, value }));
}
onHistoryChanged(e) {
this.parseUrl(window.location.href);
this.getQuote();
}
}
const quote = new Quotes();