-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathform.js
104 lines (90 loc) · 3.64 KB
/
form.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
98
99
100
101
102
103
import { getDatabase, ref, set, push, query, equalTo, orderByChild, onChildAdded, onChildChanged, onChildRemoved, onValue} from "https://www.gstatic.com/firebasejs/10.4.0/firebase-database.js";
import { initializeFirebase, printUsernamePath } from './firebaseAuth.js';
// import { printProjectFolderPath, printAssumptionInDB, printQuestionInDB } from './sketch.js';
let dataBase;
let username;
let projectFolder;
let assumptionInDB; //create assumption folder in project folder
let questionInDB; //create question folder in project folder
fetch('firebaseConfig.json')
.then(response => response.json())
.then(data => {
const firebaseData = initializeFirebase(data);
const {
db,
} = firebaseData;
dataBase = db;
printUsernamePath((updatedUsername) => {
username = updatedUsername;
console.log(username);
});
})
.catch(error => {
console.error('Error loading Firebase configuration:', error);
});
//Project Name
let projectTitleFolder = document.getElementById("projectTitleFolder");
console.log(projectTitleFolder);
let projectTitleEntry = document.createElement("input");
projectTitleEntry.placeholder = "Enter a Project Name";
let projectTitleButton = document.createElement("button");
projectTitleButton.textContent = "Load Form";
projectTitleButton.style.backgroundColor = "white";
projectTitleButton.style.color = "black";
projectTitleButton.style.borderStyle = "solid";
console.log(projectTitleEntry);
projectTitleFolder.appendChild(projectTitleEntry);
projectTitleFolder.appendChild(projectTitleButton);
projectTitleButton.addEventListener('click', () => {
projectFolder = projectTitleEntry.value
console.log(projectFolder)
console.log(username);
assumptionInDB = ref(dataBase, username + '/' + projectFolder + '/assumptions')
questionInDB = ref(dataBase, username + '/' + projectFolder + '/questions')
console.log(assumptionInDB);
onValue(assumptionInDB, (snapshot) => {
const data = snapshot.val();
console.log(data)
init();
});
});
function init() {
console.log("init");
let textContainerDiv = document.createElement('div');
textContainerDiv.id = "textContainerDiv";
document.body.append(textContainerDiv);
subscribeToForm()
}
function subscribeToForm() {
onChildAdded(assumptionInDB, (data) => {
console.log("added", data.key, data.val());
addTextfield(data.key, data.val());
});
onChildAdded(questionInDB, (data) => {
console.log("added", data.key, data.val());
addTextfield(data.key, data.val());
});
// onChildChanged(assumptionInDB, (data) => {
// const element = document.getElementById(data.key);
// element.innerHTML = data.val().username + ": " + data.val().text;
// console.log("changed", data.key, data.val(), element);
// });
// onChildRemoved(assumptionInDB, (data) => {
// console.log("removed", data.key, data.val());
// });
}
function addTextfield(key, data) {
let changedDiv = document.getElementById(key);
if (changedDiv) {
changedDiv.innerHTML = data;
} else {
let formInput = document.createElement('textarea');
formInput.id = key; //syncing the id with the key in the database
formInput.setAttribute("contenteditable", true);
formInput.value = data;
formInput.style.overflow = "auto"; // Make the textarea resizable
formInput.style.width = "75ch"; // maximum width
formInput.style.minHeight = "40px"; // minimum height
textContainerDiv.append(formInput);
}
}