Skip to content

Commit 9cf2600

Browse files
Added debug logs
1 parent 6d383c6 commit 9cf2600

File tree

4 files changed

+122
-34
lines changed

4 files changed

+122
-34
lines changed

package-lock.json

Lines changed: 18 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@csvbox/vuejs",
3-
"version": "0.0.16",
3+
"version": "1.1.3",
44
"description": "Vue adapter for csvbox.io",
55
"author": "csvbox-io",
66
"license": "MIT",

src/components/CSVBoxButton.vue

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@
99
</div>
1010
</template>
1111
<script>
12+
13+
import { version } from "../../package.json"
14+
import { Logger } from "./../utils/Logger";
15+
1216
export default{
1317
name: 'csvbox-button',
1418
props: {
1519
licenseKey: {
1620
type: String,
1721
required: true
1822
},
19-
debugMode: {
20-
type: Boolean,
21-
required: false
22-
},
23-
useStagingServer: {
24-
type: Boolean,
25-
required: false
26-
},
2723
onImport: {
2824
type: Function,
2925
default: function() {}
@@ -57,27 +53,55 @@
5753
default: function () {
5854
return { user_id: 'default123' };
5955
}
56+
},
57+
dataLocation: {
58+
type: String,
59+
required: false
60+
},
61+
customDomain: {
62+
type: String,
63+
required: false
64+
},
65+
debug: {
66+
type: String,
67+
required: false,
68+
default: null
6069
}
6170
},
6271
computed:{
6372
iframeSrc() {
64-
let BASE_URL = `https://${this.useStagingServer ? 'staging' : 'app' }.csvbox.io/embed/${this.licenseKey}`;
65-
return `${BASE_URL}?library-version=2&source=embedCode&sourceLang=vue`;
73+
let domain = this.customDomain ? this.customDomain : "app.csvbox.io";
74+
if(this.dataLocation) {
75+
domain = `${this.dataLocation}-${domain}`;
76+
}
77+
78+
let iframeUrl = `https://${domain}/embed/${this.licenseKey}`;
79+
iframeUrl += `?library-version=${version}`;
80+
iframeUrl += "&framework=vue";
81+
if(this.dataLocation) {
82+
iframeUrl += "&preventRedirect";
83+
}
84+
return iframeUrl;
6685
}
6786
},
6887
data(){
6988
return {
7089
isModalShown: false,
7190
disableImportButton: true,
72-
uuid: this._uid + '_' + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
91+
uuid: this._uid + '_' + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15),
92+
logger: new Logger(this.debug)
7393
}
7494
},
7595
methods: {
7696
openModal() {
97+
this.logger.info("openModal();");
7798
if(!this.isModalShown) {
99+
this.logger.verbose("Opening importer modal");
78100
this.isModalShown = true;
79101
this.$refs.holder.style.display = 'block';
80102
this.$refs.iframe.contentWindow.postMessage('openModal', '*');
103+
}else{
104+
this.logger.verbose("Modal already showing or shown");
81105
}
82106
},
83107
onMessageEvent(event) {
@@ -94,10 +118,12 @@
94118
}
95119
if(typeof event.data == "object") {
96120
if(event?.data?.data?.unique_token == this.uuid) {
121+
122+
this.logger.verbose("Event:", `'${event.data.type}'`, event.data.data);
123+
97124
if(event.data.type && event.data.type == "data-on-submit") {
98125
let metadata = event.data.data;
99126
metadata["column_mappings"] = event.data.column_mapping;
100-
// this.callback(true, metadata);
101127
delete metadata["unique_token"];
102128
this.onSubmit?.(metadata);
103129
}
@@ -157,22 +183,32 @@
157183
} else {
158184
this.onImport(false, event.data.data);
159185
}
186+
}else if(event.data.type && event.data.type == "csvbox-modal-hidden") {
187+
this.$refs.holder.style.display = 'none';
188+
this.isModalShown = false;
189+
this.onClose();
190+
} else if(event.data.type && event.data.type == "csvbox-upload-successful") {
191+
this.onImport(true);
192+
} else if(event.data.type && event.data.type == "csvbox-upload-failed") {
193+
this.onImport(false);
160194
}
161195
}
162196
}
163-
},
164-
randomString() {
165-
let result = '';
166-
let characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
167-
let charactersLength = characters.length;
168-
for (let i = 0; i < 20; i++) {
169-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
170-
}
171-
return result;
172197
}
173198
},
174199
mounted() {
175200
201+
this.logger.info("Framework:", "Vue");
202+
this.logger.info("Library version:", version);
203+
if(this.customDomain){
204+
this.logger.info("Using domain:", this.customDomain);
205+
}
206+
if(this.dataLocation) {
207+
this.logger.info("Data location:", this.dataLocation);
208+
}
209+
210+
this.logger.info("Importer url:", this.iframeSrc);
211+
176212
window.addEventListener("message", this.onMessageEvent, false);
177213
178214
let iframe = this.$refs.iframe;
@@ -181,6 +217,8 @@
181217
182218
iframe.onload = function () {
183219
220+
self.logger.info("Importer ready");
221+
184222
iframe.contentWindow.postMessage({
185223
"customer" : self.user ? self.user : null,
186224
"columns" : self.dynamicColumns ? self.dynamicColumns : null,
@@ -195,6 +233,7 @@
195233
}
196234
},
197235
beforeDestroy() {
236+
this.logger.verbose("Removing message event listener");
198237
window.removeEventListener("message", this.onMessageEvent);
199238
}
200239
}

src/utils/Logger.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export const TYPE = {
2+
info: {
3+
name: 'Info',
4+
priority: 2,
5+
textColor: 'green'
6+
},
7+
verbose: {
8+
name: 'Verbose',
9+
priority: 1,
10+
textColor: 'yellow'
11+
}
12+
};
13+
14+
export class Logger {
15+
16+
constructor(level) {
17+
if(sessionStorage && sessionStorage.getItem('CSVBOX_DEBUG_LEVEL')) {
18+
level = sessionStorage.getItem('CSVBOX_DEBUG_LEVEL');
19+
}
20+
if(TYPE[level]) {
21+
this.debugLevelPriority = TYPE[level].priority;
22+
}
23+
}
24+
25+
shouldLog(priority) {
26+
return priority >= this.debugLevelPriority;
27+
}
28+
29+
info(...data) {
30+
this.log(TYPE.info, ...data);
31+
}
32+
33+
verbose(...data) {
34+
this.log(TYPE.verbose, ...data);
35+
}
36+
37+
log() {
38+
let [level, ...data] = arguments;
39+
if(this.shouldLog(level.priority)) {
40+
console.log(`%c[CSVBox]%c[${level.name}]`, "color:blue;font-weight:bold;", `color:${level.textColor};font-weight:bold;`, ...data);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)