-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuilder.html
561 lines (515 loc) · 15.9 KB
/
builder.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<!DOCTYPE html>
<html>
<head>
<title>Build an episode</title>
<link rel="stylesheet" media="all" href="style.css" type="text/css"/>
<script src="js/stuquery.js"></script>
<script type="text/javascript">
// Return array of string values, or NULL if CSV string not well formed.
function CSVtoArray(text) {
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
var re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;
// Return NULL if input string is not well formed CSV string.
if (!re_valid.test(text)) return null;
var a = []; // Initialize array to receive values.
text.replace(re_value, // "Walk" the string using replace with callback.
function(m0, m1, m2, m3) {
// Remove backslash from \' in single quoted values.
if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'"));
// Remove backslash from \" in double quoted values.
else if (m2 !== undefined) a.push(m2.replace(/\\"/g, '"'));
else if (m3 !== undefined) a.push(m3);
return ''; // Return empty string.
});
// Handle special case of empty last value.
if (/,\s*$/.test(text)) a.push('');
return a;
};
var shows = {};
var showlookup = {};
var show = "";
var db = new Array();
var ids = {};
var lookup = {};
var keys = {};
// When the DOM is ready to be interacted with
S(document).ready(function(){
// Select all the text in the textarea when focus switches to it
S('#textarea').on('focus',function(){
this.e[0].select();
});
// Function to update the textarea based on the people added to the DOM
function updateList(reset){
// Find all the "person" DOM elements
var p = S('.person');
var str = "";
// Loop over the DOM elements
for(var i = 0; i < p.e.length; i++){
// Get a stuQuery object for the current element
el = S(p.e[i]);
// Find the ID stored in the "data-id" attribute
id = el.attr('data-id');
// Build the string to show in the textarea
str += db[id].id+':'+db[id].name+' ('+(db[id].gender == "male" ? "Himself" : (db[id].gender == "female" ? "Herself" : db[id].gender))+');';
}
now = new Date();
// Build the missing bits
if(S('#textarea .extract').length == 0){
S('#textarea').html("<div class=\"line\">Episode,Date,Guests (Himself/Herself - Role),References</div><div class=\"line extract\"></div><div class=\"line\"><input type=\"text\" id=\"episode-output\" class=\"episode-output\" value=\"AxB,"+now.toISOString().substr(0,10)+","+str+",\" /></div>");
// Add select event
S('#episode-output').on('focus',function(e){
e.currentTarget.select();
}).on('keyup',function(e){
adjustWidthOfInput();
});
}else{
// Update line
val = S('#episode-output')[0].value;
val = val.replace(/^([^\,]*)\,([^\,]*)\,([^\,]*)\,/,function(m,p1,p2,p3){ return p1+","+p2+","+str+","; });
S('#episode-output')[0].value = val;
}
adjustWidthOfInput();
html = "";
if(show) html = '<a href="https://github.com/slowe/panelshows/edit/master/shows/data/'+show+'.csv" target="_github">Edit the file for '+shows[show].title+' on Github</a>'
S('#notes').html(html);
if(reset) getShowFile(show);
}
function getWidthOfInput() {
var tmp = document.createElement("span");
tmp.className = "episode-output";
tmp.innerHTML = S('#episode-output')[0].value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
S('#textarea')[0].appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
S('#textarea')[0].removeChild(tmp);
return theWidth;
}
function adjustWidthOfInput(){ S('#episode-output')[0].style.width = (getWidthOfInput()+5) + "px"; }
function getShowFile(show){
S().ajax("shows/data/"+show+".csv",{
"dataType": "text",
"success": function(data){
var lines = data.split(/[\n\r]/);
olines = lines.slice(-3);
var html = (lines.length > 3 ? '<div>...</div>':'');
for(var i = 0; i < olines.length; i++) html += '<div>'+linkify(olines[i])+'</div>'
S('.extract').html(html);
},
'error': function(e,attr){
console.error('Unable to load '+attr.file)
}
})
}
function linkify(txt){
return txt.replace(/(\,|\;|\s)(http[^\s]*)/g,function(m,p1,p2,p3){ return p1+'<a href="'+p2+'" target="source">'+p2+'</a>'; });
}
// Select one of the people in the drop down list
function selectName(selected){
// Get the ID from the DOM element's data-id attribute
// Use that to find the index that corresponds to in the "db" hash
var data = selected.attr('data-id');
var id,n;
if(data.length==0) return;
if(data=="?"){
// First create them an ID
newid = newID(S('#search')[0].value);
id = newid.id;
// Check if this ID has been created
if(!keys[id]){
lookup[newid.name] = id;
keys[id] = newid.name;
n = JSON.parse(JSON.stringify(newid));
n.id = id;
n.gender = "self";
n.rank = 0;
n.n = "1";
n.shows = [{'key':show,'n':1}];
ids[id] = db.length;
id = ids[id];
// Add them to the DB
db.push(n);
}
}else{
id = ids[selected.attr('data-id')];
// Update show counter
console.log('Update ',db[id],show)
for(var s = 0 ; s < db[id].shows.length; s++){
if(db[id].shows[s].key == show){
db[id].shows[s].n++;
}
}
}
html = "";
// Remove events
S('.person .close').off('click');
S('.episode').append('<span class="person" data-id="'+id+'"><span class="name'+(db[id].gender ? ' '+db[id].gender : '')+'">'+db[id].name+'</span><span class="close" title="Remove">×</span></span>');
S('.searchresults').html('');
S('#search').e[0].value = "";
var p = S('.person');
for(var i = 0; i < p.e.length; i++){
el = S(p.e[i]);
el.children('.close').on('click',function(){
this.parent().remove();
updateList();
});
el.children('.name').on('click',function(){
var parent = this.parent();
var id = parent.attr('data-id');
var g = db[id].gender||"other";
if(this.hasClass('male')) g = 'male';
else if(this.hasClass('female')) g = 'female';
else if(this.hasClass('other')) g = 'other';
this.removeClass(g);
var sw = {'male':'female','female':'other','other':'male','self':'male'};
this.addClass(sw[g]);
// Update the gender
db[id].gender = sw[g];
updateList();
});
}
updateList();
}
S('#reset').on('click',function(e){
S('.episode').html('');
updateList(true);
});
S('#search').on('keyup',function(e){
if(e.originalEvent.keyCode==40 || e.originalEvent.keyCode==38){
// Down=40
// Up=38
var li = S('.searchresults li');
var s = -1;
for(var i = 0; i < li.e.length; i++){
if(S(li.e[i]).hasClass('selected')) s = i;
}
if(e.originalEvent.keyCode==40) s++;
else s--;
if(s < 0) s = li.e.length-1;
if(s >= li.e.length) s = 0;
S('.searchresults .selected').removeClass('selected');
S(li.e[s]).addClass('selected');
}else if(e.originalEvent.keyCode==13){
selectName(S('.searchresults .selected'))
}else{
var tmp = new Array();
var name = this.e[0].value;
var found = 0;
var html = "";
if(name.length < 1){
html = "";
}else{
var names = name.split(/ /);
var n = names.length;
var mx = 0;
for(var i = 0; i < db.length; i++){
if(db[i].n > mx) mx = db[i].n;
db[i].rank = 0;
}
for(var i = 0; i < db.length; i++){
rank = 0;
for(m = 0; m < n ; m++){
if(db[i].name.indexOf(names[m]) >= 0) rank++;
else rank--;
}
if(rank > 0){
datum = db[i];
datum.rank = rank*db[i].n/mx;
if(show){
scalefactor = 0.0001;
for(var s = 0; s < db[i].shows.length; s++){
if(db[i].shows[s].key==show){
scalefactor = db[i].shows[s].n/shows[show].n;
}
}
datum.rank *= scalefactor;
}
tmp.push(datum);
}
}
if(tmp.length > 0){
tmp = sortBy(tmp,'rank');
S('.searchresults li').off('click');
var n = Math.min(tmp.length,10);
for(var i = 0; i < n; i++){
found++;
html += '<li data-id="'+tmp[i].id+'" class="'+tmp[i].gender+''+(i==0 ? ' selected':'')+'"><div class="handle"></div><div class="name">'+tmp[i].name+(tmp[i].dob ? ' ('+tmp[i].dob.substr(0,4)+')' : "")+""+(tmp[i].shows && tmp[i].shows.length > 0 ? ' [<em>'+trimTo(shows[tmp[i].shows[0].key].title,(tmp[i].shows && tmp[i].shows.length > 1 ? 20 : 40))+'</em> × '+tmp[i].shows[0].n+''+(tmp[i].shows.length > 1 ? ', <em>'+trimTo(shows[tmp[i].shows[1].key].title,20)+'</em> × '+tmp[i].shows[1].n : '')+']' : '')+"</div></li>";
}
}
}
if(found < 4){
html += '<li data-id="?" class=""><div class="handle"></div><div class="name">+ Add unknown person</div></li>';
}
if(html){
S('.searchresults').html("<ol>"+html+"</ol>");
var li = S('.searchresults li');
for(var i = 0 ; i < li.e.length ; i++){
S(li.e[i]).on('click',function(){
selectName(this);
});
}
}
}
});
function trimTo(str,len){
if(str) return (str.length > len ? str.substr(0,len)+'…':str);
else return "";
}
// Sort the data
function sortBy(arr,i){
yaxis = i;
return arr.sort(function (a, b) {
return a[i] < b[i] ? 1 : -1;
});
}
S().ajax("people/shows.json",{
"dataType": "json",
"success": function(data){
shows = data;
var html = '<option value="">Select show</option>';
var showkeys = new Array();
for(s in shows){
showlookup[shows[s].i] = s;
showkeys.push(s);
}
showkeys.sort();
for(var i = 0; i < showkeys.length; i++){
html += '<option value="'+showkeys[i]+'">'+shows[showkeys[i]].title+'</option>';
}
S('#shows').html(html);
// Set the selected show
S('#shows').on('change',function(){
show = this.e[0].value;
updateList(true);
});
},
'error': function(e){
console.log(e)
}
})
S().ajax("people/ranked.csv",{
"dataType": "text",
"success": function(data){
var line,showbits,key;
if(typeof data==="string"){
data = data.replace(/\r/,'');
data = data.split(/[\n]/);
}
for(var i = 0; i < data.length; i++){
if(data[i]!=""){
line = CSVtoArray(data[i]);
if(line[0]){
//m552j442,"Jackie Brambles",Herself,female,1967,317,72:317
showbits = (line[6]) ? line[6].replace(/;$/,"").split(";") : new Array();
for(var s = 0 ; s < showbits.length; s++){
bits = showbits[s].split(/:/);
key = showlookup[bits[0]];
showbits[s] = {'key':key,'n':parseInt(bits[1])};
if(shows){
if(!shows[key]) shows[key] = {};
if(!shows[key].n) shows[key].n = 0;
shows[key].n += parseInt(bits[1]);
}
}
db.push({'id':line[0],'name':line[1],'gender':line[3],'dob':line[4],'n':line[5],'shows':showbits});
ids[line[0]] = db.length-1;
}
}
}
console.log(db)
},
'error': function(e){
console.log(e)
}
});
S().ajax("people/people.tsv",{
"dataType": "text",
"success": function(data){
lines = data.split(/[\n\r]/);
header = ['id','name','imdb','wikipedia','chortle','birthdate','ref'];
for(var i = 1; i < lines.length ; i++){
if(lines[i]!=""){
person = lines[i].split(/\t/); // id,p,imdb,w,c,g,ref
if(person[0].length == 8){
lookup[person[1]] = person[0];
keys[person[0]] = person[1];
}else{
console.warn('Bad ID for line '+i,lines[i])
}
}
}
}
});
});
// Code for creating a new ID
function newID(name){
var rtn = {'name':name};
if(name && lookup[name]){
console.warn(name+" already exists with ID "+lookup[name]);
rtn.existing = lookup[name];
}
var key = generate_random_string(name);
var str = name;
while(keys[key]){
str += 'a';
key = generate_random_string(str);
}
rtn.id = key;
return rtn;
}
// Create a seed for the random number generator based on the numbers in the input
function generate_random_string(seed,len){
length_of_randomstring = (len||8);
var n = 0;
var i,a2n,string,rand;
if(seed){
a2n = {};
string = seed.split("");
seed = 0;
n = string.length;
for(i = 0; i < string.length ; i++) seed += (string[i].charCodeAt()/256)**(n-i);
seed = (seed+"").replace(/[^0-9]/g,"").replace(/^0/g,"");
rand = srand(seed);
}
var chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'];
var random_string = "";
for(i = 1; i <= length_of_randomstring; i++) random_string += chars[Math.floor(rand(chars.length))];
return random_string;
}
/**
* srand from https://github.com/micro-js/srand MIT licence
*/
function srand(seed){
// If we're passed a string, condense it down into a number
if(typeof seed==='string'){
str = seed
seed = 0xFF
for(var i = 0; i < str.length; i++){
seed ^= str.charCodeAt(i)
}
}
return function (max, min){
max = max || 1
min = min || 0
seed = (seed * 9301 + 49297) % 233280
return min + (seed / 233280) * (max - min)
}
}
</script>
<style>
input[type=reset] {
float: right;
}
input#search, .searchresults {
width: 100%;
}
.row { height: 2em;}
.searchresults {
position: absolute;
top: 2em;
}
.searchresults ol {
margin: 0px;
padding: 0px;
}
.searchresults li {
margin-bottom: 1px;
display: block;
text-align: left;
background-color: #efefef;
color: #404040;
}
.searchresults li .name {
padding: 0 0.5em;
display: inline-block;
line-height: 2em;
}
.searchresults li .handle {
width: 1em;
height: 1em;
margin: 0.5em;
float: right;
}
li.male .handle { background-color: #0DBC37; }
li.female .handle { background-color: #722EA5; }
.searchresults li.selected, .searchresults li:hover {
background-color: black; color: #fefefe;
cursor: pointer;
}
.searchresults li div {
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.searchresults li .name {
width: auto;
}
.episode .person {
display: inline-block;
margin-right: 0.25em;
margin-bottom: 0.25em;
line-height: 1em;
}
.episode .person .name {
display: inline-block;
padding: 0.5em;
line-height: 1em;
float: left;
cursor: pointer;
}
.episode .person .close {
display: inline-block;
background-color: #cccccc;
padding: 0.5em;
cursor: pointer;
}
.episode .person .close:hover {
background-color: #bbbbbb;
}
#notes{
float: right;
line-height: 2em;
}
#textarea {
width: 100%;
margin-bottom: 0.25em;
min-height: 5em;
font-size: 1.25em;
font-family: monospace;
padding: 1em;
background-color: #efefef;
overflow-x: auto;
white-space: nowrap;
}
.episode-output { border: 0px; line-height: 1em; font: inherit; background: transparent; width: 100%; }
.extract { opacity: 0.5; }
.row {
vertical-align: top;
}
.searcher {
display: block;
position: relative;
width: 90%;
}
select, input { font-size: inherit; line-height: 1.5em; }
label { margin-right: 0.5em; }
.row { display: flex; }
</style>
</head>
<body>
<nav id="header">
<div class="padder">
<div class="title"><a href="index.html">Panel Show Db</a></div><ul id="menu"><li><a href="people/index.html">People</a></li><li><a href="https://github.com/slowe/panelshows/tree/master/data">Data</a></li></ul>
</div>
</nav>
<div class="page">
<h1>Build an episode</h1>
<div class="episode"><div id="notes"></div></div>
<div id="textarea" class="output"></div>
<div class="row">
<select id="shows"></select>
<div class="searcher">
<input type="text" id="search" value="" placeholder="e.g. Sandi Toksvig" />
<div class="searchresults"></div>
</div>
<input type="reset" id="reset" name="reset" />
</div>
</div>
</body>
</html>