-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill.html
214 lines (164 loc) · 5.76 KB
/
fill.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
<input type="file" id="files" name="files[]" single /><br>
<button onclick="download('test.txt');">Save me</button><br>
<div id="inputList">
<ul id="listOfBlocks">
</ul>
</div>
<div>
<button onclick="createNewBlock();">Add block</button>
<!-- <input type="text" name="name" id="addFieldName" value="what">
<input type="text" name="value" id="addFieldValue" value="value"><br> -->
</div>
</body>
<script type="text/javascript">
function download(filename) {
var myarray = [];
var text = "";
// var text = "[";
var inputs = document.getElementById("inputList").getElementsByTagName('input');
console.log(inputs);
for (var i = 0, len = inputs.length; i < len; i++) {
var obj = new Object;
obj.name = inputs[i].name;
obj.value = inputs[i].value;
console.log(inputs[i]);
myarray.push(obj);
// text += '{' + inputs[i].name + ': ' + inputs[i].value + '}, '; // should use JSON.stringify
// text += JSON.stringify(obj)
};
// text += "]";
text += JSON.stringify(myarray)
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
}
function readFile (evt) {
// read the only file selected
var files = evt.target.files;
var file = files[0];
var reader = new FileReader();
reader.onload = function() {
var text = this.result
var jsonObject = JSON.parse(text)
each(jsonObject)
}
reader.readAsText(file)
}
document.getElementById('files').addEventListener('change', readFile, false);
function each(jsonObject){
for (var i = 0, len = jsonObject.length; i < len; i++) {
createNewBlock(jsonObject[i].name, jsonObject[i].value);
}
}
var _selectIndex = 0;
function freshID(){
_selectIndex++;
return _selectIndex;
}
function addField(id){ // id of the block
var container = document.getElementById(id).childNodes;
// get the name and the value of the futur field$
var xxx = "nameInput" + id;
console.log("ADDDD " + xxx);
console.log("ccc " + container);
var name = document.getElementById("nameInput" + id);
var value = document.getElementById("valueInput" + id);
// add new field at the block
var fields = document.getElementById("fields" + id);
createNewField(fields, name.value, value.value);
// reset
name.value = "what";
value.value = "value";
}
function createNewAddFieldSubBlock(id){ // id of the block
var container = document.getElementById(id)
// subBlock
var subBlock = document.createElement("div");
subBlock.id = "addFieldSubBlock" + id;
// name input
var nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.id = "nameInput" + id;
nameInput.value = "what";
// value input
var valueInput = document.createElement("input");
valueInput.type = "text";
valueInput.id = "valueInput" + id;
valueInput.value = "value";
// add field button
var buttonAddField = document.createElement("button");
buttonAddField.innerHTML = "Add a field";
buttonAddField.id = "buttonAddField" + id;
buttonAddField.onclick = function() { addField(id) };
container.appendChild(nameInput);
container.appendChild(valueInput);
container.appendChild(buttonAddField);
}
function createNewField(container, name, value){
var fieldSubBlock = document.createElement("div");
var thisID = "fieldSubBlock" + container.id;
fieldSubBlock.id = thisID;
var text = document.createTextNode(name + " : ");
var input = document.createElement("input");
input.type = "text";
input.id = "field" + thisID;
input.name = name;
input.value = value;
var buttonDeleteField = document.createElement("button");
buttonDeleteField.innerHTML = "Delete the field";
buttonDeleteField.id = "buttonAddField" + thisID;
buttonDeleteField.onclick = function() { deleteField(this.id) };
container.appendChild(fieldSubBlock);
fieldSubBlock.appendChild(text);
fieldSubBlock.appendChild(input);
fieldSubBlock.appendChild(buttonDeleteField);
}
function deleteField(id){
var button = document.getElementById(id);
var subBlock = button.parentNode;
subBlock.parentNode.removeChild(subBlock);
}
function createNewBlock(){
var container = document.getElementById("listOfBlocks");
// the block
var bulletPoint = document.createElement("li");
thisID = "bulletPoint" + freshID();
bulletPoint.id = thisID;
// title of the block
var titleText = document.createTextNode("Block name : ");
var titleInput = document.createElement("input");
titleInput.type = "text";
titleInput.value = "title";
// subBlock: fields in the block
var fields = document.createElement("div");
fields.id = "fields" + thisID;
// button to delete the block
var buttonDeleteBlock = document.createElement("button");
buttonDeleteBlock.innerHTML = "deleteBlock";
buttonDeleteBlock.id = "buttonDeleteBlock" + thisID;
buttonDeleteBlock.onclick = function() { deleteBlock(this.id) };
container.appendChild(bulletPoint);
bulletPoint.appendChild(buttonDeleteBlock);
bulletPoint.appendChild(fields);
fields.appendChild(titleText);
fields.appendChild(titleInput);
fields.appendChild(document.createElement("br"));
createNewAddFieldSubBlock(thisID);
fields.appendChild(document.createElement("br"));
}
function deleteBlock(id) {
console.log("Delete " + id);
var button = document.getElementById(id);
var bulletPoint = button.parentNode;
bulletPoint.parentNode.removeChild(bulletPoint);
}
</script>
</html>