-
Notifications
You must be signed in to change notification settings - Fork 0
/
papaparse.html
186 lines (164 loc) · 5.83 KB
/
papaparse.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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Test jExcel for Islandora Workbench</title>
<script src="https://bossanova.uk/jexcel/v3/jexcel.js"></script>
<script src="https://bossanova.uk/jsuites/v2/jsuites.js"></script>
<script src="papaparse.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jsuites/v2/jsuites.css" type="text/css" />
<link rel="stylesheet" href="https://bossanova.uk/jexcel/v3/jexcel.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Material+Icons" />
<link rel='stylesheet' type="text/css" href="filedrag.css" />
</head>
<body>
<h1>Testing <a href="https://bossanova.uk/jexcel/v3/">jExcel</a> for <a href="https://github.com/mjordan/islandora_workbench">Islandora Workbench</a></h1>
<form id='submit_spreadsheet'>
<fieldset>
<legend>Load Spreadsheet</legend>
<div>
<label for='fileselect'>Select local CSV:</label>
<input type='file' id='fileselect' name='fileselect' />
<div id='filedrag'>or drop file here.</div>
</div>
<div id='submitbutton'>
<button type='submit'>Load</button>
</div>
</fieldset>
</form>
<h2>The Spreadsheet</h2>
<div id="spreadsheet"></div>
<script src="initialize.js"></script>
<h2>Notes</h2>
<p>Uses <a href="https://www.papaparse.com/">PapaParse</a>. There is <a href="https://github.com/mholt/PapaParse/issues/685#issuecomment-508881879">a known bug</a> where a provided spreadsheet will fail to parse if the first value of the first row is empty.</p>
<p>
Currently assumes you have an Islandora instance accessible at <a href="http://localhost:8000/">localhost:8000</a> that has JSON:API and CORS (see below) enabled with some existing <a href="https://github.com/Islandora-CLAW/controlled_access_terms">Controlled Access Terms</a> populated.
</p>
<p>
Initially my browser refused to load the dropdown boxes from Drupal due to
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">Cross-Origin Resource Sharing (CORS)</a>.
A bit of searching lead me to <a href="https://drupal.stackexchange.com/questions/245903/how-do-i-set-up-cors">a forum post</a>
indicating that I needed to configure my site's services.yml to enable CORS.
There may be a way to do it without this, but it is what I have right now.
</p>
</body>
<script>
var file;
// getElementById
function $id(id) {
return document.getElementById(id);
}
// file selection
function FileSelectHandler(e) {
// cancel event and hover styling
FileDragHover(e);
// fetch FileList object
var files = e.target.files || e.dataTransfer.files;
// process first File object
// ParseFile(files[0]);
file = files[0];
// https://www.papaparse.com/
Papa.parse(file, {
delimiter: ",",
complete: function(results, file) {
console.log("Parsing complete:", results, file);
loadData(results.data, [
{
type: 'image',
title: 'Thumbnail',
width: 120
},
{
type: 'text',
title: 'File',
width: 120
},
{
type: 'text',
title: 'Title',
width: 120,
wordWrap: true
},
{
type: 'text',
title: 'Date',
width: 120
},
{
type: 'text',
title: 'Description',
width: 120,
wordWrap: true
},
{
type: 'text',
title: 'Rights',
width: 120,
wordWrap: true
},
{
type: 'text',
title: 'Extent',
width: 100,
wordWrap: true
},
{
type: 'autocomplete',
title: 'Subjects',
width: 200,
multiple: true,
source: subjectsDropdown
},
{
type: 'dropdown',
title: 'Access Terms',
width: 200,
multiple: true,
source: accessDropdown
},
{
type: 'dropdown',
title: 'Member Of',
width: 120,
source: []
},
{
type: 'hidden',
title: 'Node ID'
}
]);
}
});
}
// file drag hover
function FileDragHover(e) {
e.stopPropagation();
e.preventDefault();
e.target.className = (e.type == "dragover" ? "hover" : "");
}
// call initialization file
if (window.File && window.FileList && window.FileReader) {
Init();
}
//
// initialize
function Init() {
var fileselect = $id("fileselect"),
filedrag = $id("filedrag"),
submitbutton = $id("submitbutton");
// file select
fileselect.addEventListener("change", FileSelectHandler, false);
// is XHR2 available?
var xhr = new XMLHttpRequest();
if (xhr.upload) {
// file drop
filedrag.addEventListener("dragover", FileDragHover, false);
filedrag.addEventListener("dragleave", FileDragHover, false);
filedrag.addEventListener("drop", FileSelectHandler, false);
filedrag.style.display = "block";
// remove submit button
submitbutton.style.display = "none";
}
}
</script>
</html>