Skip to content

Commit 17a7eb8

Browse files
Added lazy loading feature
1 parent 9cf2600 commit 17a7eb8

File tree

2 files changed

+89
-46
lines changed

2 files changed

+89
-46
lines changed

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": "1.1.3",
3+
"version": "1.1.5-alpha.2",
44
"description": "Vue adapter for csvbox.io",
55
"author": "csvbox-io",
66
"license": "MIT",

src/components/CSVBoxButton.vue

Lines changed: 88 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
<button :disabled="disableImportButton" @click="openModal">
44
<slot></slot>
55
</button>
6-
<div ref="holder" class="holder-style">
7-
<iframe ref="iframe" class="iframe" :src="iframeSrc" frameBorder="0"></iframe>
8-
</div>
6+
<div ref="holder" class="holder-style"></div>
97
</div>
108
</template>
119
<script>
@@ -66,6 +64,15 @@
6664
type: String,
6765
required: false,
6866
default: null
67+
},
68+
language: {
69+
type: String,
70+
required: false
71+
},
72+
lazy: {
73+
type: Boolean,
74+
required: false,
75+
default: false
6976
}
7077
},
7178
computed:{
@@ -81,6 +88,9 @@
8188
if(this.dataLocation) {
8289
iframeUrl += "&preventRedirect";
8390
}
91+
if(this.language) {
92+
iframeUrl += "&language=" + this.language;
93+
}
8494
return iframeUrl;
8595
}
8696
},
@@ -89,20 +99,39 @@
8999
isModalShown: false,
90100
disableImportButton: true,
91101
uuid: this._uid + '_' + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15),
92-
logger: new Logger(this.debug)
102+
logger: new Logger(this.debug),
103+
iframe: null,
104+
isIframeLoaded: false,
105+
openModalOnIframeLoad: false,
93106
}
94107
},
95108
methods: {
96109
openModal() {
110+
111+
if(this.lazy) {
112+
if(!this.iframe) {
113+
this.openModalOnIframeLoad = true;
114+
this.initImporter();
115+
return;
116+
}
117+
}
118+
97119
this.logger.info("openModal();");
120+
98121
if(!this.isModalShown) {
99-
this.logger.verbose("Opening importer modal");
100-
this.isModalShown = true;
101-
this.$refs.holder.style.display = 'block';
102-
this.$refs.iframe.contentWindow.postMessage('openModal', '*');
122+
if(this.isIframeLoaded) {
123+
this.logger.verbose("Opening importer modal");
124+
this.isModalShown = true;
125+
this.$refs.holder.style.display = 'block';
126+
console.log(this.iframe)
127+
this.iframe.contentWindow.postMessage('openModal', '*');
128+
} else {
129+
this.openModalOnIframeLoad = true;
130+
}
103131
}else{
104132
this.logger.verbose("Modal already showing or shown");
105133
}
134+
106135
},
107136
onMessageEvent(event) {
108137
if (event.data === "mainModalHidden") {
@@ -194,44 +223,56 @@
194223
}
195224
}
196225
}
197-
}
198-
},
199-
mounted() {
200-
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-
212-
window.addEventListener("message", this.onMessageEvent, false);
213-
214-
let iframe = this.$refs.iframe;
215-
216-
let self = this;
226+
},
227+
initImporter() {
228+
this.logger.info("Framework:", "Vue");
229+
this.logger.info("Library version:", version);
230+
if(this.customDomain){
231+
this.logger.info("Using domain:", this.customDomain);
232+
}
233+
if(this.dataLocation) {
234+
this.logger.info("Data location:", this.dataLocation);
235+
}
236+
this.logger.info("Importer url:", this.iframeSrc);
217237
218-
iframe.onload = function () {
238+
let iframe = document.createElement("iframe");
239+
this.iframe = iframe;
240+
iframe.setAttribute("src", this.iframeSrc);
241+
iframe.frameBorder = 0;
242+
iframe.classList.add('csvbox-iframe');
219243
220-
self.logger.info("Importer ready");
244+
window.addEventListener("message", this.onMessageEvent, false);
221245
222-
iframe.contentWindow.postMessage({
223-
"customer" : self.user ? self.user : null,
224-
"columns" : self.dynamicColumns ? self.dynamicColumns : null,
225-
"options" : self.options ? self.options : null,
226-
"unique_token": self.uuid
227-
}, "*");
246+
let self = this;
228247
229-
self.disableImportButton = false;
248+
iframe.onload = function () {
249+
self.isIframeLoaded = true;
250+
self.logger.info("Importer ready");
251+
iframe.contentWindow.postMessage({
252+
"customer" : self.user ? self.user : null,
253+
"columns" : self.dynamicColumns ? self.dynamicColumns : null,
254+
"options" : self.options ? self.options : null,
255+
"unique_token": self.uuid
256+
}, "*");
257+
self.disableImportButton = false;
258+
self.onReady();
259+
if(self.openModalOnIframeLoad) {
260+
self.openModal();
261+
}
262+
}
230263
231-
self.onReady();
264+
this.$refs.holder.appendChild(iframe);
232265
233266
}
234267
},
268+
mounted() {
269+
if(this.lazy) {
270+
this.disableImportButton = false;
271+
} else {
272+
this.initImporter();
273+
}
274+
275+
},
235276
beforeDestroy() {
236277
this.logger.verbose("Removing message event listener");
237278
window.removeEventListener("message", this.onMessageEvent);
@@ -248,11 +289,13 @@
248289
left: 0;
249290
right: 0;
250291
}
251-
.iframe {
252-
height: 100%;
253-
width: 100%;
254-
position: absolute;
255-
top: 0;
256-
left: 0;
257-
}
292+
</style>
293+
<style>
294+
.csvbox-iframe {
295+
height: 100%;
296+
width: 100%;
297+
position: absolute;
298+
top: 0;
299+
left: 0;
300+
}
258301
</style>

0 commit comments

Comments
 (0)