/
databasing.js
90 lines (82 loc) · 2.64 KB
/
databasing.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
var rawData;
var modals = [];
var picker;
function init() {
// tabletop.js init() function returns data as an array of objects, like this:
// [{"animal": "horse",
// "color": "brown"},
// {"animal": "chick",
// "color": "yellow"}]
// alt key: '1asZC5tWkBpMZ47S05VxJj0fh3gyMjaKYpK1sDNE2iCA'
Tabletop.init( { key: '1Obseltf_Ef0peTYXzzVkZPAgs8pcE34277QnsGAfkaM',
callback: gotData,
simpleSheet: true } )
}
window.addEventListener('DOMContentLoaded', init)
function gotData(data, tabletop) {
rawData = data;
createModal();
}
function createModal() {
var modalName;
var modalText;
var parentDiv;
// if there's data
if (rawData) {
// iterate over each object in the data set
for (var i = 0; i < rawData.length; i++) {
// assign each object a modalName
modalName = 'modal' + i;
// push the modal name to the modals array
modals.push(modalName);
// get the modal text from the "text" column of the spreadsheet
modalText = rawData[i]["text"];
// set the parent element to the <p> tags
parentEl = document.getElementById(modals[i]).children[0].children[1]
parentEl.innerText = modalText;
}
}
openModal();
}
function openModal() {
// get all the <input> tags
var inputs = document.getElementsByTagName("input")
// create an array to store buttons
var btn = [];
for (var i = 0; i < inputs.length; i++) {
btn = inputs[i]
// when user clicks the button
btn.onclick = function() {
// prevent default behavior of checkboxes
preventDef(event);
// create a picker value to choose which modal text is displayed
picker = Math.floor(Math.random() * modals.length);
// display the modal indexed by the picker value
document.getElementById(modals[picker]).style.display = "block";
function closeModal(picker) {
var span = document.getElementsByClassName("close")[picker];
// when the user clicks the "close span",
span.onclick = function() {
// change style to "display: none"
document.getElementById(modals[picker]).style.display = "none";
resetPicker()
}
// if the user clicks on the window outside the modal
window.onclick = function(event) {
if (event.target == document.getElementById(modals[picker])) {
// change style to "display: none"
document.getElementById(modals[picker]).style.display = "none";
resetPicker()
}
}
}
closeModal(picker)
}
}
}
function preventDef(event) {
event.preventDefault();
}
function resetPicker() {
picker = null;
}