Skip to content

Commit

Permalink
fix web component.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRaindrop committed Jan 31, 2018
1 parent 93b9ee1 commit 2185fbd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
5 changes: 4 additions & 1 deletion examples/components/web.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
@click.native="refresh"></button>
</div>
<web class="content" ref="webview" :src="src"
@pagestart="startload" @pagefinish="finishload" @error="failload"></web>
@pagestart="startload"
@pagefinish="finishload"
@error="failload"></web>
</div>
</template>

<script>
var webview = weex.requireModule('webview');
const src = 'http://invalid.src/for/test'
const newSrc = 'http://m.taobao.com'
// const newSrc = window.location.href.replace(/\?[^?]*$/, '') // for test.
module.exports = {
data () {
return {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "weex-vue-render",
"description": "Web renderer for weex project written in Vue DSL.",
"version": "1.0.15",
"version": "1.0.16",
"license": "Apache-2.0",
"main": "dist/index.common.js",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion packages/weex-vue-render/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weex-vue-render",
"version": "1.0.15",
"version": "1.0.16",
"description": "Web renderer for weex project written in Vue DSL.",
"license": "Apache-2.0",
"main": "dist/index.common.js",
Expand Down
78 changes: 64 additions & 14 deletions src/components/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,103 @@ function getWeb (weex) {
const { dispatchNativeEvent } = weex.utils

return {
data () {
return {
currentSrc: ''
}
},
name: 'weex-web',
props: {
src: String
},
watch: {
src (newVal) {
this.currentSrc = newVal
}
},
methods: {
// TODO: check cross-origin
goBack () {
if (this.$el) {
this.$el.contentWindow.history.back()
const el = this.$el
if (el) {
const win = el.contentWindow
try {
win.history.back()
this.currentSrc = win.location.href
}
catch (err) {
dispatchNativeEvent(el, 'error', err)
}
}
},
goForward () {
if (this.$el) {
this.$el.contentWindow.history.forward()
const el = this.$el
if (el) {
const win = el.contentWindow
try {
win.history.forward()
this.currentSrc = win.location.href
}
catch (err) {
dispatchNativeEvent(el, 'error', err)
}
}
},
reload () {
if (this.$el) {
this.$el.contentWindow.history.reload()
const el = this.$el
if (el) {
try {
el.contentWindow.location.reload()
dispatchNativeEvent(el, 'pagestart', { url: this.currentSrc })
}
catch (err) {
dispatchNativeEvent(el, 'error', err)
}
}
}
},

created () {
this.currentSrc = this.src
},

mounted () {
const el = this.$el
this._prevSrc = this.src
this._prevSrc = this.currentSrc
if (el) {
dispatchNativeEvent(el, 'pagestart', { url: this.src })
dispatchNativeEvent(el, 'pagestart', { url: this.currentSrc })
}
},

updated () {
if (this.src !== this._prevSrc) {
this._prevSrc = this.src
dispatchNativeEvent(this.$el, 'pagestart', { url: this.src })
if (this.currentSrc !== this._prevSrc) {
this._prevSrc = this.currentSrc
dispatchNativeEvent(this.$el, 'pagestart', { url: this.currentSrc })
}
},

render (createElement) {
return createElement('iframe', {
attrs: {
'weex-type': 'web',
src: this.src
src: this.currentSrc
},
on: {
load: event => {
dispatchNativeEvent(event.target, 'pagefinish', { url: this.src })
this.$nextTick(function () {
const el = this.$el
try {
const html = el.contentWindow.document.documentElement
if (html) {
dispatchNativeEvent(el, 'pagefinish', { url: this.currentSrc })
}
else {
dispatchNativeEvent(el, 'error', new Error('[vue-render]:found no page content.'))
}
}
catch (err) {
dispatchNativeEvent(el, 'error', err)
}
})
}
},
staticClass: 'weex-web weex-el',
Expand Down

0 comments on commit 2185fbd

Please sign in to comment.