-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
374 lines (300 loc) · 12.4 KB
/
index.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.png" type="image/x-icon">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="./assets/materialize/css/materialize.min.css"
media="screen,projection" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Syne:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<script src="./assets/lodash.js"></script>
<script src="./presets.js"></script>
<script type="text/javascript" src="./assets/materialize/js/materialize.min.js"></script>
<title>Responsys to DataStudio Format</title>
</head>
<body class="grey darken-3">
<nav class="grey darken-4" role="navigation">
<div class="nav-wrapper container">
<a href="#" class="brand-logo left"><i
style="font-style:normal; color:#f7ab13; font-size: unset; height: unset; margin: 0; font-weight: unset;">D</i>ataStudio
Format</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li>
<a href="https://github.com/AndersonVes/datastudio-format-converter" target="_blank">Poject</a>
</li>
</ul>
</div>
</nav>
<div class="section no-pad-bot grey darken-3" id="index-banner">
<div class="container">
<div class="row center">
<form id="myForm">
<div class="file-field input-field">
<div class="btn grey darken-4">
<span style="color: #333" class="amber-text">upload</span>
<input type="file" id="csvFile" accept=".csv" required />
</div>
<div class="file-path-wrapper">
<input class="file-path validate white-text"
onchange='document.getElementById("submit_id").style.display = "inline-block";'
type="text">
</div>
</div>
<div id="submit_id" style="display: none;" class="container">
<div class="row">
<button
class="btn-large waves-effect waves-light amber accent-4" type="submit" name="action">Converter
<i class="material-icons right">import_export</i>
</button>
</div>
<div class="row">
<span class="grey darken-2 white-text" style="padding: 5px; border-radius: 2px;">Verifique o arquivo após o download!!</span>
</div>
</div>
<br>
<div class="spacer" style="height: 220px;"></div>
</form>
</div>
</div>
</div>
<div id="modal1" class="modal modal-fixed-footer bottom-sheet grey darken-3 white-text">
<div class="modal-content grey darken-3">
<div class="row">
<div class="col s6">
<h4>Selecione os campos desejados:</h4>
</div>
<div class="col s6">
<div class="preset input-field">
<select class="browser-default" id="presetsId" onchange="onSelectChange()">
</select>
</div>
</div>
</div>
<div id="modalList"></div>
</div>
<div class="modal-footer grey darken-3">
<div class="row">
<div class="col s10" style="display: flex; margin-top: 6px; overflow-x: auto; height: 50px;"
id="tableOrder">
</div>
<div class="col s2">
<button onclick="download()" href="#!"
class="modal-close waves-effect waves-green btn amber black-text">BAIXAR
ARQUIVO</button>
</div>
</div>
</div>
</div>
<div class="fixed-action-btn">
<a class="btn grey darken-1 tooltipped" data-position="left" data-tooltip="anderson.miranda@adin.com.br"
style="border-radius: 50px; cursor: default;">
Envie seu feedback
</a>
</div>
<script>
let modalInstance;
let presetFilename = null;
//PRESETS
function onSelectChange() {
tableKeys = presets[document.getElementById('presetsId').value].fields
presetFilename = null;
if(presets[document.getElementById('presetsId').value].file_name)
presetFilename = presets[document.getElementById('presetsId').value].file_name;
download();
modalInstance.close()
}
function printPresets(allKeys) {
let containsAll;
str = '<option disabled selected>Ou selecione um preset</option>';
for (var key in presets) {
if (!presets.hasOwnProperty(key)) continue;
var obj = presets[key];
containsAll = obj.fields.every(element => {
return allKeys.includes(element);
});
if (containsAll)
str += '<option value="' + key + '">' + key + '</option>';
}
document.getElementById("presetsId").innerHTML = str;
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems, [{belowOrigin: true, coverTrigger: false}]);
}
document.addEventListener('DOMContentLoaded', function () {
//MODAL
var modal1 = document.querySelector('#modal1');
modalInstance = M.Modal.init(modal1, []);
//TOOLTIP
var tElems = document.querySelectorAll('.tooltipped');
var tooltipInstances = M.Tooltip.init(tElems, []);
});
//CONVERSOR
let finalFile;
let tableKeys;
let text;
const myForm = document.getElementById("myForm");
const csvFile = document.getElementById("csvFile");
function csvToArray(str, delimiter = ",") {
const headers = str.slice(0, str.indexOf("\n")).split(delimiter);
const rows = str.slice(str.indexOf("\n") + 1).split("\n");
const arr = rows.map(function (row) {
const values = row.split(delimiter);
const el = headers.reduce(function (object, header, index) {
object[header] = values[index];
return object;
}, {});
return el;
});
return arr;
}
function triggerList(key) {
let haveKey = false;
for (var i = 0; i < tableKeys.length; i++) {
if (!document.getElementById(key).checked && tableKeys[i] === key) {
tableKeys.splice(i, 1);
haveKey = true;
}
}
if (!haveKey) {
tableKeys.push(key);
}
printOrder();
}
function printOrder() {
let str = '';
tableKeys.forEach(e => {
str += '<span style="margin-left: 0; margin-right: 6px; min-width:max-content" class="badge amber lighten-2 black-text">' + e + '</span>';
});
document.getElementById("tableOrder").innerHTML = str;
}
function arrayToCsv(array) {
var fields = Object.keys(array[0])
var replacer = function (key, value) { return value === null || value === '' ? '0' : (!isNaN(Math.round(value)) ? Math.round(value) : value) }
var rounder = function (key, value) { return Math.round(value) !== false ? Math.round(value) : value }
var csv = array.map(function (row) {
return fields.map(function (fieldName) {
return JSON.stringify(row[fieldName], replacer)
}).join(',')
})
csv.unshift(fields.join(',')) // add header column
csv = csv.join('\r\n');
return csv
}
function printModalItens(keys) {
let htmlList = '<ul style="border: none" class="collection grey darken-1" >';
keys.forEach(key => {
htmlList += '<li class="grey darken-4 white-text collection-item" style="padding: 0; display: flex;"><label class="white-text" style="cursor: pointer; width: 100%;padding: 10px 20px;"><input onclick="triggerList(\'' + key + '\')" type="checkbox" id="' + key + '"/><span>' + key + '</span></label></li>';
});
htmlList += '</ul>';
document.getElementById('modalList').innerHTML = htmlList;
}
function formatCsv(object) {
let new_list = object.map(function (obj) { return _.pick(obj, tableKeys) });
var ret = arrayToCsv(new_list);
return ret.replaceAll('"', '');
}
myForm.addEventListener("submit", function (e) {
e.preventDefault();
var instance = M.Modal.getInstance(document.querySelector('#modal1'));
instance.open();
const input = csvFile.files[0];
const reader = new FileReader();
reader.onload = function (e) {
text = e.target.result.replaceAll(['\r'], '');
//save('datastudio', formatCsv(data));
const data = csvToArray(text);
let keys = Object.keys(data[0]);
tableKeys = [];
printModalItens(keys);
printPresets(keys);
};
reader.readAsText(input);
tableKeys = [];
printOrder();
});
String.prototype.replaceAt = function (index, replacement) {
if (index >= this.length) {
return this.valueOf();
}
return this.substring(0, index) + replacement + this.substring(index + 1);
}
function removeComma(text) {
let ret;
ret = text;
let switch_ = 0;
let commas = [];
for (let i = 0; i <= text.length - 1; i++) {
if (text.charAt(i) == '"')
switch_++;
if (switch_ % 2 != 0 && text.charAt(i) == ',')
commas.push(i);
}
commas.forEach(function (e, index) {
ret = ret.replaceAt(e, ' ');
});
return ret;
}
function download() {
let text2 = removeComma(text)
const data = csvToArray(text2);
finalFile = formatCsv(data);
save('datastudio_format', finalFile);
}
function save(filename, data) {
const blob = new Blob(["\ufeff", data], { type: 'text/csv;charset=UTF-8' });
if(presetFilename)
filename = presetFilename;
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
}
else {
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
</script>
<style>
.select {
display:flex;
flex-direction: column;
position:relative;
width:250px;
height:40px;
}
option {
padding:0 30px 0 10px;
min-height:40px;
display:flex;
align-items:center;
background:#333;
border-top:#222 solid 1px;
position:absolute;
top:0;
width: 100%;
pointer-events:none;
order:2;
z-index:1;
transition:background .4s ease-in-out;
box-sizing:border-box;
overflow:hidden;
white-space:nowrap;
color: white;
}
option:hover {
background:#666;
}
select:focus .option {
position:relative;
pointer-events:all;
}
</style>
</body>
</html>