Skip to content

Commit

Permalink
emit a load event from iframe, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
officert committed Feb 6, 2019
1 parent 9309231 commit 7473dda
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 24 deletions.
20 changes: 12 additions & 8 deletions dist/vue-friendly-iframe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* vue-friendly-iframe v0.10.0 (https://github.com/officert/vue-friendly-iframe)
* (c) 2018 Tim Officer
* vue-friendly-iframe v0.11.0 (https://github.com/officert/vue-friendly-iframe)
* (c) 2019 Tim Officer
* Released under the MIT License.
*/
(function webpackUniversalModuleDefinition(root, factory) {
Expand Down Expand Up @@ -230,9 +230,15 @@ exports.default = {
}
},
setIframeUrl: function setIframeUrl() {
var _this = this;

var iframeDoc = this.iframeEl.contentWindow.document;
iframeDoc.open().write('<body onload="window.location.href=\'' + this.src + '\'; parent.postMessage(\'' + this.iframeLoadedMessage + '\', \'*\')"></body>');

iframeDoc.onload = function (e) {
_this.$emit('load', e);
};

iframeDoc.close();
},

Expand All @@ -252,19 +258,17 @@ exports.default = {
this.setIframeUrl();
},
listenForEvents: function listenForEvents() {
var _this = this;
var _this2 = this;

var eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
var eventer = window[eventMethod];
var messageEvent = eventMethod === 'attachEvent' ? 'onmessage' : 'message';

eventer(messageEvent, function (event) {
if (event.data === _this.iframeLoadedMessage) {
_this.$emit('load');

console.log('LOAD');
if (event.data === _this2.iframeLoadedMessage) {
_this2.$emit('load');

_this.iframeEl.setAttribute('style', 'visibility: visible;');
_this2.iframeEl.setAttribute('style', 'visibility: visible;');
}
}, false);
}
Expand Down
6 changes: 3 additions & 3 deletions dist/vue-friendly-iframe.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions docs-src/components/App/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default {

<style lang="less">
.btn-xl {
padding: 20px 30px;
font-size: 22px;
padding: 20px 30px;
font-size: 22px;
}
button,
Expand All @@ -37,7 +37,8 @@ input[type="submit"] {
}
button[disabled],
input[type="submit"][disabled], input[readonly] {
input[type="submit"][disabled],
input[readonly] {
background: #a0d4de;
border-color: #a0d4de;
cursor: default;
Expand Down Expand Up @@ -80,4 +81,15 @@ button.hide-loader .hidden {
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out,box-shadow 0.15s ease-in-out;
}
.iframe-wrapper {
border: 1px solid gray;
height: 600px;
.iframe-loading {
padding: 60px 0;
text-align: center;
color: black;
}
}
</style>
11 changes: 9 additions & 2 deletions docs-src/components/Home/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ export default {
example1Form: {
src: 'https://www.pexels.com/search',
searchTerm: 'tiger'
}
},
iframeLoading: true
}
},
methods: {}
methods: {
onLoad() {
console.log('iframe loaded');
this.iframeLoading = false;
}
}
};
</script>

Expand Down
9 changes: 6 additions & 3 deletions docs-src/components/Home/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h2>Usage</h2>
2) In your HTML add the component:
</p>
<div class="highlight">
<pre class="highlight">&lt;vue-friendly-iframe :src="example1Form.src"&gt;&lt;/vue-friendly-iframe&gt;</pre>
<pre class="highlight">&lt;vue-friendly-iframe :src="example1Form.src" @load="onLoad" &gt;&lt;/vue-friendly-iframe&gt;</pre>
</div>
</div>
<h2>Example</h2>
Expand All @@ -61,8 +61,11 @@ <h2>Example</h2>
<input class="form-control" :value="example1Form.src + '/' + example1Form.searchTerm" readonly="readonly">
</div>
</form>
<div>
<vue-friendly-iframe :src="example1Form.src + '/' + example1Form.searchTerm"></vue-friendly-iframe>
<div class="iframe-wrapper">
<div class="iframe-loading" v-if="iframeLoading">
iframe loading...
</div>
<vue-friendly-iframe :style="{ 'display' : iframeLoading ? 'none' : 'block' }" :src="example1Form.src + '/' + example1Form.searchTerm" @load="onLoad"></vue-friendly-iframe>
</div>
</div>
</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/docs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-friendly-iframe",
"version": "0.10.0",
"version": "0.11.0",
"description": "Vue component for creating dynamic async iframes",
"main": "dist/vue-friendly-iframe.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/components/FriendlyIframe/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default {
const iframeDoc = this.iframeEl.contentWindow.document;
iframeDoc.open().write(`<body onload="window.location.href='${this.src}'; parent.postMessage('${this.iframeLoadedMessage}', '*')"></body>`);
iframeDoc.onload = (e) => {
this.$emit('load', e);
};
iframeDoc.close(); //iframe onload event happens
},
reinitIframe: utils.debounce(vm => {
Expand Down Expand Up @@ -74,8 +78,6 @@ export default {
if (event.data === this.iframeLoadedMessage) {
this.$emit('load');
console.log('LOAD');
this.iframeEl.setAttribute('style', 'visibility: visible;');
}
}, false);
Expand Down

0 comments on commit 7473dda

Please sign in to comment.