-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-scripts.js
97 lines (86 loc) · 2.96 KB
/
example-scripts.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
/// *************
/// * Variables *
/// *************
var currentDatabase = undefined;
// Sets everything up
function onDocumentReady(){
GVZ.setLogging(true); // off by default, useful for debugging
GVZ.setFlair('GVZ'); // you should probably use a flair to limit search
GVZ.setAuthListener(authChanged); // set up the listener
// Initialize the GVZ library with our key and client id, keep the user signed in if they have previously
GVZ.initialize('AIzaSyCEyNTPq0R6ALudyMMsICNEQLGfD0BnE1M',
'227233657145-chcnqd16a57odfb1e2hqrecnj4ns95g4.apps.googleusercontent.com',
true
).then(function(){
GVZ.reloadDatabases().then(function(databases){
refreshDatabaseDropdown();
});
});
}
/// **********************
/// * Frontend Functions *
/// **********************
// Modifies UI whenever the auth status changes
function authChanged(newStatus){
if (newStatus){ // now signed in
document.getElementById('status-auth').innerHTML = 'Welcome, '+GVZ.getUserInfo().firstName+'! You are currently signed in.';
document.getElementById('toggle-auth').innerHTML = 'Sign Out';
document.getElementById('div-choose').classList.remove('hidden');
}
else { // now signed out
document.getElementById('status-auth').innerHTML = 'You are currently signed out. Please sign in!';
document.getElementById('toggle-auth').innerHTML = 'Sign In';
document.getElementById('div-choose').classList.add('hidden');
}
}
// Reloads database dropdown from GVZ databases
function refreshDatabaseDropdown(){
let dropdown = document.getElementById('db-selection');
let master = GVZ.getDatabases();
if (master.length > 0){
dropdown.innerHTML = ''; // remove all children
for (let i = 0; i < master.length; i++){
let el = document.createElement('option');
el.innerText = master[i].name;
el.setAttribute('value', master[i].id);
dropdown.appendChild(el);
}
}
else {
dropdown.innerHTML = '';
let el = document.createElement('option');
el.innerText = 'No databases found';
el.setAttribute('value','undefined');
dropdown.appendChild(el);
}
}
// Selects a database if possible else makes a new one
function selectDatabase(){
let id = document.getElementById.value;
if (id == 'undefined'){
createDatabase();
}
else {
currentDatabase = id;
document.getElementById('div-interact').classList.remove('hidden');
}
}
/// *********************
/// * Backend Functions *
/// *********************
// Creates database using user input
function createDatabase(){
let name = window.prompt("Type a name for the new database","My Database");
if (name !== '' && name !== null) {
// Write your database structure here
let db = new GVZ.Database(name);
let tbl = new GVZ.Table('Attendance');
tbl.columns.push(new GVZ.Column('id','unumber',0));
tbl.columns.push(new GVZ.Column('timestamp','datetime'));
tbl.columns.push(new GVZ.Column('tardy','boolean'));
db.tables.push(tbl);
GVZ.createDatabase(db).then(function(newDatabase){
refreshDatabaseDropdown();
});;
}
}