@csvbox/csvboxjs
Advanced tools
Comparing version
113
index.js
class CSVBoxImporter { | ||
constructor(slug, data = {}, callback = null) { | ||
constructor(slug, data = {}, callback = null, configuration = {}) { | ||
this.isIframeLoaded = false; | ||
@@ -9,12 +9,18 @@ this.shouldOpenModalonIframeLoad = false; | ||
this.data = data; | ||
this.columns = []; | ||
this.options = []; | ||
this.configuration = configuration; | ||
if (callback && (typeof callback == "function")) { | ||
this.callback = callback; | ||
} | ||
let self= this; | ||
let self = this; | ||
if(document.readyState === "complete") { | ||
self.setUpImporter(); | ||
} | ||
document.addEventListener('DOMContentLoaded', function() { | ||
@@ -48,4 +54,2 @@ self.setUpImporter(); | ||
"position: absolute;" + | ||
"top: 0px;" + | ||
"left: 0px;" + | ||
"}"; | ||
@@ -68,4 +72,11 @@ | ||
this.iframe = iframe; | ||
iframe.setAttribute("src", "https://app.csvbox.io/embed/" + this.slug); | ||
let BASE_URL = "https://app.csvbox.io/embed/"; | ||
let url = BASE_URL + this.slug; | ||
url += `?debug=${!!this.configuration?.debug}`; | ||
url += `&source=embedCode`; | ||
iframe.setAttribute("src", url); | ||
iframe.frameBorder = 0; | ||
@@ -79,3 +90,5 @@ this.holder.id = this.id; | ||
iframe.onload = function () { | ||
self.isIframeLoaded = true; | ||
if(self.shouldOpenModalonIframeLoad) { | ||
@@ -85,23 +98,17 @@ self.shouldOpenModalonIframeLoad = false; | ||
} | ||
iframe.contentWindow.postMessage({ | ||
"customer" : self.data | ||
}, "*"); | ||
iframe.contentWindow.postMessage({ | ||
"columns" : self.columns | ||
}, "*"); | ||
iframe.contentWindow.postMessage({ | ||
"options" : self.options | ||
}, "*"); | ||
} | ||
if(document.querySelector("[data-csvbox]") != null){ | ||
document.onreadystatechange = () => { | ||
if (document.readyState === 'complete') { | ||
document.querySelector("[data-csvbox]").disabled = false; | ||
}else{ | ||
document.querySelector("[data-csvbox]").disabled = true; | ||
} | ||
}; | ||
self.onReady?.(); | ||
} | ||
} | ||
@@ -118,7 +125,22 @@ | ||
listen(type, callback = null) { | ||
if(typeof callback == "function") { | ||
switch(type){ | ||
case "onReady": | ||
this.onReady = callback; | ||
break; | ||
case "onClose": | ||
this.onClose = callback; | ||
break; | ||
} | ||
} | ||
} | ||
setupMessageListener() { | ||
window.addEventListener("message", (event) => { | ||
if (event.data === "mainModalHidden") { | ||
this.holder.style.display = 'none'; | ||
this.isModalShown = false; | ||
this.onClose?.(); | ||
} | ||
@@ -137,8 +159,44 @@ if(event.data === "uploadSuccessful") { | ||
if(event.data.type && event.data.type == "data-push-status") { | ||
if(event.data.data.import_status == "success"){ | ||
this.callback(true, event.data.data); | ||
if(event.data.data.import_status == "success") { | ||
// this.callback(true, event.data.data); | ||
if(event?.data?.row_data) { | ||
let primary_row_data = event.data.row_data; | ||
let headers = event.data.headers; | ||
let rows = []; | ||
let dynamic_columns_indexes = event.data.dynamicColumnsIndexes; | ||
primary_row_data.forEach((row_data) => { | ||
let x = {}; | ||
let dynamic_columns = {}; | ||
row_data.data.forEach((col, i)=>{ | ||
if(col == undefined){ col = ""}; | ||
if(dynamic_columns_indexes.includes(i)) { | ||
dynamic_columns[headers[i]] = col; | ||
}else{ | ||
x[headers[i]] = col; | ||
} | ||
}); | ||
if(row_data?.unmapped_data) { | ||
x["_unmapped_data"] = row_data.unmapped_data; | ||
} | ||
if(dynamic_columns && Object.keys(dynamic_columns).length > 0) { | ||
x["_dynamic_data"] = dynamic_columns; | ||
} | ||
rows.push(x); | ||
}); | ||
let metadata = event.data.data; | ||
metadata["rows"] = rows; | ||
delete metadata["unique_token"]; | ||
this.callback(true, metadata); | ||
}else{ | ||
let metadata = event.data.data; | ||
delete metadata["unique_token"]; | ||
this.callback(true, metadata); | ||
} | ||
}else { | ||
this.callback(false, event.data.data); | ||
let metadata = event.data.data; | ||
delete metadata["unique_token"]; | ||
this.callback(false, metadata); | ||
} | ||
} | ||
@@ -159,2 +217,3 @@ } | ||
} | ||
} | ||
@@ -192,2 +251,12 @@ | ||
if(document.querySelector("[data-csvbox]") != null){ | ||
document.onreadystatechange = () => { | ||
if (document.readyState === 'complete') { | ||
document.querySelector("[data-csvbox]").disabled = false; | ||
}else{ | ||
document.querySelector("[data-csvbox]").disabled = true; | ||
} | ||
}; | ||
} | ||
export default CSVBoxImporter; |
{ | ||
"name": "@csvbox/csvboxjs", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "JS adapter for csvbox.io", | ||
@@ -5,0 +5,0 @@ "author": "csvbox-io", |
9724
34.01%210
32.91%