Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/harmony navigator com #10664

Merged
merged 4 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.navigate {
overflow: hidden;
transition: all linear 0.2s;
}

.navigator-hover {
background-color: #dedede;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div
id="{{id}}"
class="{{clsHover}}"
@click="onClick"
@touchstart="onTouchStart"
@touchend="onTouchEnd"
>
<slot></slot>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import router from '@system.router'
import { createOption, queryToJson } from '../utils'

export default createOption({
props: {
Chen-jj marked this conversation as resolved.
Show resolved Hide resolved
id: {
default: ''
},
openType: {
default: 'navigate'
},
url: {
default: ''
},
hoverStartTime: {
default: 50
},
hoverStayTime: {
default: 600
},
hoverClass: {
default: 'navigator-hover'
}
},

data () {
return {
targetObj: {
navigate: 'push',
redirect: 'replace',
switchTab: 'push',
reLaunch: 'replace',
navigateBack: 'back'
},
hover: false,
touch: false
}
},

computed: {
clsHover () {
return this.hover ? this.hoverClass : ''
}
},

onClick () {
const { openType = 'navigate', url = '' } = this
this.getRouterFunc(`${openType}`, url)
},

getRouterFunc (method, url) {
// switchTab目前实现不了
const methodName = this.targetObj[method]
const [uri, queryString = ''] = url.split('?')
const params = queryToJson(queryString)

const uriObj = url ? { uri: uri.replace(/^\//, '') } : {}

const paramsObj = queryString ? { params } : {}

if (method === 'reLaunch') {
router.clear()
}

router[methodName]({
...uriObj,
...paramsObj
})
},

onTouchStart () {
this.touch = true
if (this.hoverClass) {
setTimeout(() => {
if (this.touch) {
this.hover = true
}
}, this.hoverStartTime)
}
},

onTouchEnd () {
this.touch = false
if (this.hoverClass) {
setTimeout(() => {
if (!this.touch) {
this.hover = false
}
}, this.hoverStayTime)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,34 @@ export function createOption (option) {
}
return option
}

export function queryToJson (str) {
const dec = decodeURIComponent
const qp = str.split('&')
const ret = {}
let name
let val
for (let i = 0, l = qp.length, item; i < l; ++i) {
item = qp[i]
if (item.length) {
const s = item.indexOf('=')
if (s < 0) {
name = dec(item)
val = ''
} else {
name = dec(item.slice(0, s))
val = dec(item.slice(s + 1))
}
if (typeof ret[name] === 'string') {
ret[name] = [ret[name]]
}

if (Array.isArray(ret[name])) {
ret[name].push(val)
} else {
ret[name] = val
}
}
}
return ret
}