RYM Band Members Timeline
1 week ago by hogasa in HTML
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Timeline | Group example</title>


<!-- note: moment.js must be loaded before vis-timeline-graph2d or the embedded version of moment.js is used -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">

<script>
function setTimeline() {
var members = Array();
const pattern_band_members = /,(?![^\(]*\))/g;
const pattern_member = / *(.+?)( \((.+?)\)|^)/;
const pattern_dates = /[0-9]{4}(\-([0-9]+|present))?/;

var rym_band_members = $('#members').val();
var formed = $('#formed').val();
var disbanded = $('#disbanded').val();
var visualization = $('#visualization')[0];

band_members = rym_band_members.split(pattern_band_members);


$.each(band_members, function(index, value) {
var member_data = value.match(pattern_member);
var name = member_data[1];
var instruments = [];
var periods = [];
var extra_data = member_data[3].split(', ');

$.each(extra_data, function(index, value) {
if (value.match(pattern_dates)) {
periods.push(new MemberPeriod(value));
} else {
instruments.push(value);
}
});

if (periods.length == 0) {
var year_from = formed ? formed : '1900';
var year_to = disbanded ? disbanded : 'present';
periods.push(new MemberPeriod(year_from + '-' + year_to));
}

var member = new Member(name, instruments, periods);
members.push(member);
});

visualization.innerHTML = '';
var band = new Band('Metallica', formed, disbanded, members);
band.orderBandMembers();
var timeline = new BandTimeline(band, visualization);
timeline.makeTimeline();
$('html, body').animate({scrollTop: visualization.offsetTop}, 500);
}

class BandTimeline {
constructor(band=null, container=null) {
this.band = band;
this.container = container;

this.options = {};
this.groups = new vis.DataSet();
this.items = new vis.DataSet();
this.timeline= new vis.Timeline(this.container);
}

joinBySize(list, size) {
var response = list.reduce(function (a, b) {if (a[a.length-1].length < size) {a[a.length-1].push(b)} else {a.push([b])}; return a;}, [[]]);
response = response.reduce(function (a, b) {a.push(b.join(', ')); return a}, []);
response = response.join(',</BR>');
return response;
};

makeTimeline() {
this.groups = new vis.DataSet();
this.items = new vis.DataSet();
var btl = this;
$.each(btl.band.members, function(member_idx, member) {
btl.groups.add({
id: 'member_' + member_idx,
content: '<div class="member_name" data-toggle="tooltip" title="' + member.aka + '">' + member.name + '</div><div class="member_instruments">' + btl.joinBySize(member.instruments, 3) + '</div>'
});
var final_member = member.isFinalMember(btl.band.disbanded);

$.each(member.periods, function (period_idx, period) {
btl.items.add({
id: 'period_' + member_idx + '_' + period_idx,
group: 'member_' + member_idx,
content: period.period,
start: period.period_from,
end: period.period_to,
title: period.period,
className: final_member ? 'final_member' : '',
});
});
});

this.items.add({
id: 'band_period',
content: '',
start: this.band.formed ? this.band.formed + '-01-01' : new Date(),
end: this.band.disbanded ? this.band.disbanded + '-01-01' : new Date(),
type: 'background'
});

this.timeline.setOptions(this.options);
this.timeline.setGroups(this.groups);
this.timeline.setItems(this.items);
};
}

class Band {
constructor(name='', formed='', disbanded='', members=[]) {
this.name = name;
this.formed = formed;
this.disbanded = disbanded;
this.members = members;
};

orderBandMembers() {
var disbanded = this.disbanded;
this.members = this.members.sort(function(a, b) {
if (a.isFinalMember(disbanded) != b.isFinalMember(disbanded)) {
return (b.isFinalMember(disbanded) - a.isFinalMember(disbanded));
} else if (b.yearsActive() != a.yearsActive()) {
return (b.yearsActive() - a.yearsActive());
} else {
return (b.lastYearActive() - a.lastYearActive());
}
});
};
}

class Member {
constructor(name='', instruments=[], periods=[]) {
this.name = name;
this.aka = '';
this.instruments = instruments;
this.periods = periods;

var pattern_name = /(.+?)( \[(aka )?(.+?)\])?$/;
var splitted_name = this.name.match(pattern_name);
if (splitted_name) {
this.name = splitted_name[1];
this.aka = splitted_name[4] ? splitted_name[4] : '';
}
};

isFinalMember(disbanded=null) {
return this.periods.reduce(function (total, value){
return total || value.present || disbanded == value.period_to.getFullYear();
}, false);
};

yearsActive() {
return this.periods.reduce(function (total, value){
return total + value.period_to.getFullYear() - value.period_from.getFullYear();
}, 0);
};

lastYearActive() {
return this.periods.reduce(function (total, value){
var response = total < value.period_to.getFullYear() ? value.period_to.getFullYear() : total;
return response;
}, 0);
}
}

class MemberPeriod {
constructor(period='') {
this.period = String(period);
this.present = false;
this.makePeriod();
};

makePeriod() {
var period = this.period.split('-');

if (period.length == 1){
this.period_from = new Date(period[0], 0, 1);
this.period_to = new Date(Number(period[0])+1, 0, 1);
} else if (period.length == 2){
this.period_from = new Date(period[0], 0, 1);
if (period[1].length == 2) {
var year_to = period[0].substring(0, 2) + period[1];
this.period_to = new Date(year_to, 0, 1);
} else if (period[1].length == 4){
this.period_to = new Date(period[1], 0, 1);
} else if (period[1] == 'present'){
this.period_to = new Date();
this.present = true;
}
} else {
this.present = true;
}
}
}

</script>

<style>
body, html {
font-family: arial, sans-serif;
font-size: 11pt;
}

#visualization {
box-sizing: border-box;
width: 100%;
height: 100%;
}

.vis-item.final_member {
background-color: greenyellow;
border-color: green;
}

.member_name {
font-weight: bold;
font-size: larger;
}

.member_instruments {
font-family: Verdana,Arial,Sans-Serif;
font-size: 12px;
}
</style>

</head>

<body>
<div class="container">
<h2>Rate Your Music<br/>Band Members Timeline</h2>
<form class="form-horizontal">
<div class="form-row">
<div class="col-2">
<label for="formed" class="mb-2 mr-sm-2">Formed:</label>
<input type="text" class="form-control mb-2 mr-sm-2" id="formed" placeholder="Enter year" name="formed">
</div>
<div class="col-2">
<label for="disbanded" class="mb-2 mr-sm-2">Disbanded:</label>
<input type="text" class="form-control mb-2 mr-sm-2" id="disbanded" placeholder="Enter year" name="disbanded">
</div>
</div>
<div class="row">
<div class="col-12">
<div class="form-group">
<label for="members">Members:</label>
<textarea class="form-control" rows="5" id="members"></textarea>
</div>
</div>
</div>
<div class="form-row">
<div class="col-12">
<button type="button" onclick="setTimeline()" class="btn btn-primary"><i class="fas fa-stream"></i> Make Timeline</button>
</div>
</div>
</form>

<hr>

<div id="visualization"></div>
</div>
</body>
</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