Skip to content

Commit

Permalink
fix csv capitalization in import (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhelton authored Oct 10, 2024
1 parent 6410d29 commit d4b27d1
Showing 1 changed file with 122 additions and 122 deletions.
244 changes: 122 additions & 122 deletions dlx_rest/static/js/modals/export.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Jmarc } from "../jmarc.mjs"
import { CSV } from "../CSV.mjs"
import { CSV } from "../csv.mjs"

export let exportmodal = {
props: {
links: {
type: Object
}
},
template: `<div v-if="showModal">
props: {
links: {
type: Object
}
},
template: `<div v-if="showModal">
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
Expand Down Expand Up @@ -57,128 +57,128 @@ export let exportmodal = {
</div>
</transition>
</div>`,
data: function() {
return {
showModal: false,
showSpinner: false,
selectedFormat: 'mrk',
selectedFields: '',
selectedExportUrl: null,
currentStatus: null
}
data: function () {
return {
showModal: false,
showSpinner: false,
selectedFormat: 'mrk',
selectedFields: '',
selectedExportUrl: null,
currentStatus: null
}
},
methods: {
show: async function () {
this.showModal = true
this.setFormat('mrk')
},
methods: {
show: async function() {
this.showModal = true
this.setFormat('mrk')
},
setFormat(format) {
this.selectedFormat = format
this.selectedExportUrl = this.links.format[format.toUpperCase()]
let url = new URL(this.selectedExportUrl)
let search = new URLSearchParams(url.search)
search.set("fields", this.selectedFields)
search.set("start", 1)
search.set("limit", 100)
//search.set("listtype", "export")
url.search = search
this.selectedExportUrl = url
},
setOutputFields(e) {
this.selectedFields = e.target.value
let url = new URL(this.selectedExportUrl)
let search = new URLSearchParams(url.search)
search.set("fields", e.target.value)
search.set("limit", 100)
search.set("listtype", "export")
url.search = search
this.selectedExportUrl = url
},
async submitExport() {
this.currentStatus = null;
this.showSpinner = true;
const url = new URL(this.selectedExportUrl);
const params = new URLSearchParams(url.search);
const format = params.get("format");
const countUrl = this.selectedExportUrl.toString().replace('/records', '/records/count');
const total = await fetch(countUrl).then(response => response.json()).then(json => json['data']);
let currentUrl = this.selectedExportUrl;
let page = 0;
let mimetype = null;
let buffer = '';
let xml = format === 'xml' ?
// XMLDocument object will be used to combine xml from each page
(new DOMParser()).parseFromString("<collection></collection>", "text/xml") :
null;
let csv = new CSV();

while (true) {
// cycle through pages synchronously until no more records are found
const response = await fetch(currentUrl);
const blob = await response.blob();
let text = await blob.text();
setFormat(format) {
this.selectedFormat = format
this.selectedExportUrl = this.links.format[format.toUpperCase()]
let url = new URL(this.selectedExportUrl)
let search = new URLSearchParams(url.search)
search.set("fields", this.selectedFields)
search.set("start", 1)
search.set("limit", 100)
//search.set("listtype", "export")
url.search = search
this.selectedExportUrl = url
},
setOutputFields(e) {
this.selectedFields = e.target.value
let url = new URL(this.selectedExportUrl)
let search = new URLSearchParams(url.search)
search.set("fields", e.target.value)
search.set("limit", 100)
search.set("listtype", "export")
url.search = search
this.selectedExportUrl = url
},
async submitExport() {
this.currentStatus = null;
this.showSpinner = true;
const url = new URL(this.selectedExportUrl);
const params = new URLSearchParams(url.search);
const format = params.get("format");
const countUrl = this.selectedExportUrl.toString().replace('/records', '/records/count');
const total = await fetch(countUrl).then(response => response.json()).then(json => json['data']);
let currentUrl = this.selectedExportUrl;
let page = 0;
let mimetype = null;
let buffer = '';
let xml = format === 'xml' ?
// XMLDocument object will be used to combine xml from each page
(new DOMParser()).parseFromString("<collection></collection>", "text/xml") :
null;
let csv = new CSV();

mimetype = response.headers.get("Content-Type");

if (text) {
if (mimetype.match('^text/xml')) {
const pageXml = (new DOMParser()).parseFromString(text, "text/xml")
const recordNodes = pageXml.getElementsByTagName("record")
while (true) {
// cycle through pages synchronously until no more records are found
const response = await fetch(currentUrl);
const blob = await response.blob();
let text = await blob.text();

for (const recordXml of [...recordNodes]) { // have to use the "..." operator on the node list to treat it as an array
xml.getElementsByTagName("collection")[0].appendChild(recordXml);
}
} else if (format === 'csv') {
csv.parseText(text);
} else {
// mrk is plain text
buffer += text
}
} else {
// end of results
break
}
mimetype = response.headers.get("Content-Type");

if (format === 'xml') {
buffer = (new XMLSerializer()).serializeToString(xml);
} else if (format === 'csv') {
buffer = csv.toString();
}
if (text) {
if (mimetype.match('^text/xml')) {
const pageXml = (new DOMParser()).parseFromString(text, "text/xml")
const recordNodes = pageXml.getElementsByTagName("record")

// next page
let newUrl = new URL(currentUrl);
let params = new URLSearchParams(newUrl.search);
params.set("start", Number(params.get("start")) + Number(params.get("limit")));
newUrl.search = params;
currentUrl = newUrl;

// status
page++;
const results = page * 100;
let percent = (results / total) * 100;
percent = percent > 100 ? 100 : percent.toFixed(2);
this.currentStatus = `${percent}% of ${total} records`;
for (const recordXml of [...recordNodes]) { // have to use the "..." operator on the node list to treat it as an array
xml.getElementsByTagName("collection")[0].appendChild(recordXml);
}
} else if (format === 'csv') {
csv.parseText(text);
} else {
// mrk is plain text
buffer += text
}
} else {
// end of results
break
}

const blob = new File([format === 'xml' ? (new XMLSerializer()).serializeToString(xml) : buffer], {"type": mimetype});
this.download(blob, `export.${this.selectedFormat}`);
this.showSpinner = false;
this.currentStatus = "Done!"
},
download(blob, filename) {
const url = window.URL.createObjectURL(blob)
const a = document.createElement("a")
a.style.display = "none"
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
},
reloadPage() {
// this doesn't work when embedded in the template for some reason
location.reload()
if (format === 'xml') {
buffer = (new XMLSerializer()).serializeToString(xml);
} else if (format === 'csv') {
buffer = csv.toString();
}

// next page
let newUrl = new URL(currentUrl);
let params = new URLSearchParams(newUrl.search);
params.set("start", Number(params.get("start")) + Number(params.get("limit")));
newUrl.search = params;
currentUrl = newUrl;

// status
page++;
const results = page * 100;
let percent = (results / total) * 100;
percent = percent > 100 ? 100 : percent.toFixed(2);
this.currentStatus = `${percent}% of ${total} records`;
}

const blob = new File([format === 'xml' ? (new XMLSerializer()).serializeToString(xml) : buffer], { "type": mimetype });
this.download(blob, `export.${this.selectedFormat}`);
this.showSpinner = false;
this.currentStatus = "Done!"
},
download(blob, filename) {
const url = window.URL.createObjectURL(blob)
const a = document.createElement("a")
a.style.display = "none"
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
},
reloadPage() {
// this doesn't work when embedded in the template for some reason
location.reload()
}
}
}

0 comments on commit d4b27d1

Please sign in to comment.