forked from LeaVerou/regexplained
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexplained.js
218 lines (187 loc) · 4.96 KB
/
regexplained.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
// Remove spaces in syntax breakdown and add classes to the ones that are towards the end
$$('.syntax-breakdown h1 code').forEach(function(code){
code.innerHTML = code.innerHTML
.replace(/[\t\r\n]/g, '');
var text = code.textContent;
$$('span', code).forEach(function(span){
span.classList.add('delayed');
if(text.indexOf(span.textContent) > text.length/2) {
// FIXME will break when there are duplicates
span.classList.add('after-middle');
}
});
});
$$('.regex-test.slide').forEach(function(slide){
slide.tester = new RegExpTester(slide);
});
(function(){
var _ = window.TwitterSearch = function(q, details) {
this.q = q;
this.details = details;
}
_.maxId = 0;
_.prototype = {
send: function(q) {
window.twitterSearch = this;
$u.script('http://search.twitter.com/search.json?' +
'callback=twitterSearch.callback' +
'&q=' + encodeURIComponent(this.q) +
'&since_id=' + _.maxId +
'&rpp=100' +
'&result_type=recent');
},
callback: function(data) {
delete window.twitterSearch;
_.maxId = +data.max_id;
this.onload && this.onload(data);
}
};
_.dateOffset = function (date) {
var seconds = Math.round((+new Date - new Date(date))/1000);
if (seconds >= 3600) {
var hours = Math.round(seconds/3600);
return hours + ' hour' + (hours===1? '' : 's') + ' ago'
}
if(seconds > 60) {
var minutes = Math.round(seconds/60);
return minutes + ' minute' + (minutes===1? '' : 's') + ' ago'
}
return seconds + ' seconds ago';
}
})();
$$('.slide[data-type="Challenge"]').forEach(function(slide){
var minutes = slide.getAttribute('data-duration') || 1,
duration = 60 * minutes,
SVGNamespace = 'http://www.w3.org/2000/svg';
var animate = $u.element.create({
tag: 'animate',
namespace: SVGNamespace,
attributes: {
attributeName: 'stroke-dasharray',
values: '0,314.1593%;314.1593%,314.1593%',
dur: 2*duration + 's', // No fucking idea why it needs double the time!!
begin: 'indefinite',
fill: 'freeze'
}
});
var timer = $u.element.create({
tag: 'div',
properties: {
className: 'timer'
},
attributes: {
'data-duration': minutes
},
contents: [{
tag: 'svg',
namespace: SVGNamespace,
contents: {
tag: 'circle',
namespace: SVGNamespace,
attributes: {
cx: '50%',
cy: '50%',
r: '25%',
"stroke-dasharray": '0,314.1593%'
},
contents: animate
}
}, {
tag: 'button',
contents: 'Go!',
properties: {
type: 'button',
onclick: function(){
animate.beginElement();
var running = true;
timer.classList.add('running');
var search = new TwitterSearch('#regexplained', details);
search.onload = function(data) {
// Process data
var results = data.results,
list = $('div', search.details),
summary = $('summary', search.details);
for (var i=results.length, tweet; tweet = results[--i];) {
// Don’t add the same tweets twice
// Twitter Search API is fucked
if($('#t' + tweet.id, list)) {
results.splice(i, 1);
continue;
}
$u.element.create('article', {
properties: {
id: 't' + tweet.id
},
contents: [{
tag: 'img',
properties: {
src: tweet.profile_image_url
}
}, {
tag: 'h1',
contents: [{
tag: 'a',
properties: {
href: 'http://twitter.com/' + tweet.from_user,
target: '_blank'
},
contents: '@' + tweet.from_user
}, ' ', {
tag: 'a',
properties: {
href: 'http://twitter.com/' + tweet.from_user + '/status/' + tweet.id,
target: '_blank'
},
contents: {
tag: 'time',
attributes: {
datetime: (new Date(tweet.created_at)).toISOString()
}
}
}]
}, {
tag: 'p',
properties: {
innerHTML: tweet.text.replace(/[@#]regexplained/g, '')
}
}],
start: list
});
}
// Fix relative dates
$$('time', list).forEach(function(time) {
time.textContent = TwitterSearch.dateOffset(time.getAttribute('datetime'));
});
summary.innerHTML = (parseInt(summary.innerHTML) || 0) + results.length + ' tweets';
// Schedule next fetch
if(running) {
setTimeout(function() {
search.send();
}, 5000);
}
}
search.send();
setTimeout(function(){
// Time’s up!
running = false;
timer.classList.remove('running');
}, (duration + 20) * 1000); // +20s to account for the twitter lag
}
}
}],
inside: slide
});
var details = $u.element.create('details', {
tag: 'details',
properties: {
className: 'tweets'
},
contents: [{
tag: 'summary',
contents: '0 tweets'
},{
tag: 'div'
}],
inside: slide
});
});