-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.html
More file actions
132 lines (127 loc) · 4.74 KB
/
form.html
File metadata and controls
132 lines (127 loc) · 4.74 KB
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
<html>
<head>
<title>invoke ace-shex-user</title>
<style>
h1 { text-align: center; margin: 0; color: #33f; font-size: 1.4em; }
*[name=schemaURL] { width: 33%; margin-right: .2em; }
*[name=schema] { width: 100%; height: 20%; margin-bottom: .2ex; }
.plus { font-size: .7em; padding: 0; cursor: copy; }
.hover { border: .25em dashed #f0a133; }
</style>
</head>
<body>
<h1>invoke ace-shex-user</h1>
<form method="get" action="ace-shexc">
<p>
<label for="schemaURL">schemaURL:</label> <span class="wrapper"><input type="text" name="schemaURL"/></span><br/>
<label for="schema">schema:</label><span class="wrapper"><textarea name="schema"></textarea></span>
<input type="submit" value="Submit"/>
</p>
</form>
<!-- The following javascript is not necessary for basic functionality, -->
<script type="application/javascript">
const wrappers = document.querySelectorAll(".wrapper")
wrappers.forEach(wrapper => enableDragAndDrop(wrapper.firstChild))
wrappers.forEach(addPlusses)
function addPlusses (wrapper) {
const button = document.createElement("button")
button.setAttribute("class", "plus")
button.textContent = "+"
button.onclick = (evt) => {
evt.preventDefault()
addChild(wrapper)
}
wrapper.prepend(button)
}
function addChild (parent) {
const target = parent.lastElementChild
const clone = target.cloneNode()
clone.value = ''
parent.appendChild(clone)
return clone
}
async function enableDragAndDrop (elt) {
const ons = ["ondragover", "ondragenter"]
const offs = ["ondragend", "ondragleave"]
ons.forEach(e => elt[e] = evt => elt.classList.add("hover"))
offs.forEach(e => elt[e] = evt => elt.classList.remove("hover"))
elt.ondrop = function (evt) {
elt.classList.remove("hover")
evt.preventDefault()
let xfer = evt.dataTransfer;
const prefTypes = [
{type: "files"},
{type: "text/uri-list"},
{type: "text/shex"},
{type: "text/plain"}
];
if (prefTypes.find(l => {
if (l.type.indexOf("/") === -1) {
if (xfer[l.type].length > 0) {
console.log("handling "+xfer[l.type].length+" files...");
readfiles(xfer[l.type], elt);
return true;
}
} else {
if (xfer.getData(l.type)) {
var val = xfer.getData(l.type);
console.log("handling "+l.type+"...");
if (l.type === "text/uri-list") {
val.split(/\r?\n/) // split on newline
.filter(l => l[0] !== '#') // strip out comments
.forEach(async (uri, idx) => {
if (idx > 0)
elt = addChild(elt.parentElement)
if (elt.getAttribute("name") === "schema") {
console.log(uri)
const resp = await fetch(uri, { headers: {Accept: "text/shex,text/plain"}})
const text = await resp.text()
elt.value = text
} else {
console.log(uri)
elt.value = uri
}
})
} else /* if (l.type === "text/plain") */ {
elt.value = val;
}
return true;
}
}
return false;
}) === undefined)
alert(
"drag and drop not recognized:\n" +
JSON.stringify({
dropEffect: xfer.dropEffect,
effectAllowed: xfer.effectAllowed,
files: xfer.files.length,
items: [].slice.call(xfer.items).map(i => {
return {kind: i.kind, type: i.type}
})
}, null, 2)
)
}
}
function readfiles(files, target) {
var sucecesses = 0;
for (var i = 0; i < files.length; i++) {
var file = files[i], name = file.name;
if (i > 0)
target = addChild(target.parentElement)
var formData = new FormData();
formData.append("file", file);
var reader = new FileReader();
reader.onload = (function (target) {
return function (event) {
target.value = event.target.result
}
})(target);
reader.readAsText(file);
++sucecesses;
}
console.log("loaded "+sucecesses+" files.");
}
</script>
</body>
</html>