-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
212 lines (194 loc) · 6.94 KB
/
index.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
<!doctype html>
<html>
<head>
<title>Timeline</title>
<link rel="stylesheet" href="tufte.css"/>
<script type="text/javascript" src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>
<link href="https://unpkg.com/vis-timeline@latest/styles/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<style type="text/css">
#visualization {
width: 1200px;
height: 600px;
border: 1px solid lightgray;
}
.group-name-input {
margin-bottom: 5px;
}
</style>
<style>
.vis-item {
border-color: #AFBED1;
background-color: #AFBED1;
}
</style>
</head>
<body>
<h1>Timeline of People and Places</h1>
<h4>Click on people to move around on the timeline, place into different rooms or delete altogether. Add new people and change room
names in the area below the timeline. Save your work with the "Export Timeline" button or save it as an image.</h4>
<div id="visualization"></div>
<h4>Add a new person and their details here:</h4>
<div>
<label for="itemName">Name:</label>
<input type="text" id="itemName">
<label for="startDate">Start Date:</label>
<input type="date" id="startDate">
<label for="endDate">End Date:</label>
<input type="date" id="endDate">
<label for="groupSelect">Group:</label>
<select id="groupSelect">
<!-- Group options will be added dynamically here -->
</select>
<button onclick="addTimelineItem()">Add Item</button>
</div>
<div id="groupNames">
<h3>Group Names</h3>
<h4>Change these names to your place names and they will be updated on the timeline</h4>
<!-- Group names and input fields will be added dynamically here -->
</div>
<h4>To save your work as a JSON text file, click "Export Timeline". Or, "Import Timeline", to populate the above timeline with a saved timeline.</h4>
<button onclick="document.getElementById('importFile').click()">Import Timeline</button>
<button onclick="exportTimeline()">Export Timeline</button>
<button onclick="saveTimelineAsImage()">Save Timeline as Image</button>
<input type="file" id="importFile" accept=".json" style="display:none">
<script type="text/javascript">
var container = document.getElementById('visualization');
var items = new vis.DataSet();
var groups = [];
var timeline;
var options = {
editable: {
add: true,
remove: true,
updateGroup: false,
updateTime: true,
overrideItems: false,
}, // default for all items
};
function initializeTimeline(data) {
items.add(data.items);
groups = data.groups;
timeline = new vis.Timeline(container, items, groups, options);
populateGroupInputs();
updateGroupSelectOptions();
}
function fetchData() {
fetch('timeline.json')
.then(response => response.json())
.then(data => initializeTimeline(data))
.catch(error => console.error('Error fetching data:', error));
}
function populateGroupInputs() {
var groupNamesDiv = document.getElementById('groupNames');
groupNamesDiv.innerHTML = ''; // Clear existing input fields
groups.forEach(function(group) {
var inputField = document.createElement('input');
inputField.type = 'text';
inputField.value = group.content;
inputField.className = 'group-name-input';
inputField.addEventListener('input', function() {
updateGroupName(group.id, this.value);
});
groupNamesDiv.appendChild(inputField);
});
}
function updateGroupName(groupId, newName) {
var group = groups.find(function(group) {
return group.id === groupId;
});
if (group) {
group.content = newName;
timeline.setGroups(groups);
updateGroupSelectOptions(); // Update group select options
}
}
function updateGroupSelectOptions() {
var groupSelect = document.getElementById('groupSelect');
groupSelect.innerHTML = ''; // Clear existing options
groups.forEach(function(group) {
var option = document.createElement('option');
option.value = group.id;
option.textContent = group.content;
groupSelect.appendChild(option);
});
}
function addTimelineItem() {
var itemName = document.getElementById('itemName').value.trim();
var startDate = document.getElementById('startDate').value.trim();
var endDate = document.getElementById('endDate').value.trim();
var groupId = document.getElementById('groupSelect').value;
// Validate input
if (itemName === '' || startDate === '' || groupId === '') {
alert('Please enter item name, start date, and select a group.');
return;
}
var newItem = { id: items.length + 1, content: itemName, start: startDate, group: parseInt(groupId), style: "color: #292928; background-color: #AFBED1;" };
if (endDate !== '') {
newItem.end = endDate;
}
items.add(newItem);
// Clear input fields
document.getElementById('itemName').value = '';
document.getElementById('startDate').value = '';
document.getElementById('endDate').value = '';
}
function exportTimeline() {
var exportedData = {
items: items.get(),
groups: groups
};
var blob = new Blob([JSON.stringify(exportedData, undefined, 1)], { type: 'application/json' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'timeline.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
document.getElementById('importFile').addEventListener('change', function(event) {
var file = event.target.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
try {
var importedData = JSON.parse(e.target.result);
items.clear();
items.add(importedData.items);
groups = importedData.groups;
timeline.setGroups(groups);
timeline.fit();
updateGroupSelectOptions(); // Update group select options after importing
} catch (error) {
alert("Error importing timeline data. Please make sure the JSON format is correct.");
}
};
reader.readAsText(file);
}
});
function saveTimelineAsImage() {
html2canvas(document.getElementById('visualization')).then(function(canvas) {
var link = document.createElement('a');
link.href = canvas.toDataURL();
link.download = 'timeline.png';
link.click();
});
}
// Add double click event listener to the timeline
timeline && timeline.on('doubleClick', function (properties) {
if (properties.item) {
var itemId = properties.item;
var item = items.get(itemId);
var newText = prompt("Enter new text:", item.content);
if (newText !== null) {
items.update({ id: itemId, content: newText });
}
}
});
// Fetch data when the page loads
window.onload = fetchData;
</script>
</body>
</html>