代码优化

master
toesbieya 6 years ago
parent 7c405a5683
commit d491804416
  1. 11
      vue/src/component/AbstractForm/FormAnchor.vue
  2. 9
      vue/src/component/FormDialog/index.vue
  3. 60
      vue/src/component/RegionSelector/Tab/index.vue
  4. 30
      vue/src/layout/component/TagsView/ScrollPane.vue
  5. 4
      vue/src/layout/component/TagsView/index.vue
  6. 10
      vue/src/layout/component/TagsView/style.scss

@ -3,11 +3,12 @@
<div <div
v-for="i in data" v-for="i in data"
:key="i.ref" :key="i.ref"
:class="{'active':i.ref === cur}" :class="{'form-anchor-item': true,'active': i.ref === cur}"
@click="() => click(i.ref)" @click="() => click(i.ref)"
> >
{{ i.label }} {{ i.label }}
</div> </div>
<div class="form-anchor-item" @click="close">关闭导航</div>
</div> </div>
</template> </template>
@ -32,6 +33,11 @@ export default {
data: () => ({cur: null}), data: () => ({cur: null}),
methods: { methods: {
close() {
this.$el.remove()
this.$destroy()
},
getCur(offsetTop, bounds) { getCur(offsetTop, bounds) {
const sections = [] const sections = []
const container = this.getContainer() const container = this.getContainer()
@ -98,13 +104,12 @@ export default {
right: 0; right: 0;
top: 200px; top: 200px;
min-width: 72px; min-width: 72px;
overflow: hidden;
border-radius: 4px 0 0 4px; border-radius: 4px 0 0 4px;
background: #fff; background: #fff;
box-shadow: 0 2px 12px 0 rgba(166, 167, 173, 0.5); box-shadow: 0 2px 12px 0 rgba(166, 167, 173, 0.5);
z-index: 100; z-index: 100;
> div { &-item {
width: inherit; width: inherit;
height: 28px; height: 28px;
line-height: 28px; line-height: 28px;

@ -15,11 +15,10 @@ export default {
}, },
render(h, context) { render(h, context) {
const {value, title, loading, top, width} = context.props const {data, children, listeners, props: {value, title, loading, top, width}} = context
const l = context.listeners
function onClose() { function onClose() {
l.input && l.input(false) listeners.input && listeners.input(false)
} }
return ( return (
@ -30,7 +29,7 @@ export default {
top={top} top={top}
width={width} width={width}
on-close={onClose} on-close={onClose}
{...context.data} {...data}
> >
<slot slot="title" name="title">{title}</slot> <slot slot="title" name="title">{title}</slot>
@ -38,7 +37,7 @@ export default {
<LoadingMask show={loading}/> <LoadingMask show={loading}/>
<el-scrollbar> <el-scrollbar>
<div class="form-dialog-body"> <div class="form-dialog-body">
{context.children} {children}
</div> </div>
<div style="margin-bottom: 17px"/> <div style="margin-bottom: 17px"/>
</el-scrollbar> </el-scrollbar>

@ -64,9 +64,46 @@ import {createLimitTree, getNodeId} from "@/util/tree"
import {store, init} from '../store' import {store, init} from '../store'
import common from '../mixin' import common from '../mixin'
const PROVINCE_CITIES = ['北京市', '天津市', '上海市', '重庆市'] const PROVINCE_CITIES = [
{id: '11', name: '北京市'},
{id: '12', name: '天津市'},
{id: '31', name: '上海市'},
{id: '50', name: '重庆市'}
]
const DEFAULT_TABS = [{name: '省/直辖市'}, {name: '市'}, {name: '区/县'}, {name: '乡/镇/街道'}] const DEFAULT_TABS = [{name: '省/直辖市'}, {name: '市'}, {name: '区/县'}, {name: '乡/镇/街道'}]
//node{id:'10',...}{name:'',...}'10'''
function predicate(node) {
if (typeof node === 'object') {
const key = node.hasOwnProperty('id') ? 'id' : 'name'
return item => item[key] === node[key]
}
else {
const firstCharCode = node.charCodeAt(0)
const key = 48 <= firstCharCode && firstCharCode <= 57 ? 'id' : 'name'
return item => item[key] === node
}
}
//id
function topDownById(tree, id) {
//=id.length/2
const depth = Math.floor(id.length / 2)
if (depth < 1) return []
const result = []
for (let i = 1; i <= depth; i++) {
const parentId = id.substring(0, i * 2)
const parent = tree.find(node => node.id === parentId)
if (!parent) return result
result.push(parent)
if (i === 1 && PROVINCE_CITIES.some(i => i.id === parentId)) {
i++
}
tree = parent.children || []
}
return result
}
export default { export default {
name: "RegionTabSelector", name: "RegionTabSelector",
@ -131,7 +168,6 @@ export default {
clickItem(item) { clickItem(item) {
this.setTabs(item, this.currentLevel) this.setTabs(item, this.currentLevel)
this.nextTab() this.nextTab()
}, },
@ -142,7 +178,7 @@ export default {
if (level === 1) { if (level === 1) {
//-1''tab //-1''tab
if (PROVINCE_CITIES.includes(item.name)) { if (PROVINCE_CITIES.some(i => i.id === item.id)) {
next.splice(1, 1) next.splice(1, 1)
this.realMaxLevel = this.maxLevel - 1 this.realMaxLevel = this.maxLevel - 1
} }
@ -189,10 +225,9 @@ export default {
: this.value.split(this.separation).filter(Boolean) : this.value.split(this.separation).filter(Boolean)
for (let i = 0; i < loopArray.length; i++) { for (let i = 0; i < loopArray.length; i++) {
const str = loopArray[i], const str = loopArray[i], parent = result[i - 1] || {children: this.treeData}
parent = result[i - 1] || {children: this.treeData}
const node = parent.children.find(predicate(str)) const node = parent.children.find(predicate(str))
//退 //退
if (!node) break if (!node) break
@ -256,19 +291,6 @@ export default {
this.init() this.init()
} }
} }
//node{id:'10',...}{name:'',...}'10'''
function predicate(node) {
if (typeof node === 'object') {
const key = node.hasOwnProperty('id') ? 'id' : 'name'
return item => item[key] === node[key]
}
else {
const firstCharCode = node.charCodeAt(0)
const key = 48 <= firstCharCode && firstCharCode <= 57 ? 'id' : 'name'
return item => item[key] === node
}
}
</script> </script>
<style lang="scss" src="./style.scss"></style> <style lang="scss" src="./style.scss"></style>

@ -1,5 +1,5 @@
<template> <template>
<el-scrollbar :vertical="false" @wheel.native.prevent="handleScroll" ref="scrollContainer"> <el-scrollbar ref="scrollContainer" native @wheel.native.prevent="handleScroll">
<slot/> <slot/>
</el-scrollbar> </el-scrollbar>
</template> </template>
@ -12,24 +12,24 @@ export default {
data: () => ({rAF: null}), data: () => ({rAF: null}),
computed: { methods: {
scrollWrapper() { getWrapper() {
return this.$refs.scrollContainer.$refs.wrap return this.$refs.scrollContainer.$refs.wrap
}, },
},
methods: {
handleScroll(e) { handleScroll(e) {
const eventDelta = e.wheelDelta || -(e.detail || 0) * 40 const eventDelta = e.wheelDelta || -(e.detail || 0) * 40
if (eventDelta > 0 && this.scrollWrapper.scrollLeft === 0) return const {scrollLeft, scrollWidth, clientWidth} = this.getWrapper()
if (eventDelta < 0 && this.scrollWrapper.scrollWidth <= this.scrollWrapper.scrollLeft + this.scrollWrapper.clientWidth) return if (eventDelta > 0 && scrollLeft === 0
|| eventDelta < 0 && scrollWidth <= scrollLeft + clientWidth) {
return
}
this.smoothScroll(-eventDelta * 2) this.smoothScroll(-eventDelta * 2)
}, },
moveToTarget(currentTag) { moveToTarget(currentTag) {
const $container = this.$refs.scrollContainer.$el const $containerWidth = this.$refs.scrollContainer.$el.offsetWidth
const $containerWidth = $container.offsetWidth const {scrollWidth, scrollLeft} = this.getWrapper()
const $scrollWrapper = this.scrollWrapper
const tagList = this.$parent.$refs.tag const tagList = this.$parent.$refs.tag
let firstTag = null let firstTag = null
@ -45,7 +45,7 @@ export default {
this.scrollLeft(0) this.scrollLeft(0)
} }
else if (lastTag === currentTag) { else if (lastTag === currentTag) {
this.scrollLeft($scrollWrapper.scrollWidth - $containerWidth) this.scrollLeft(scrollWidth - $containerWidth)
} }
else { else {
// find preTag and nextTag // find preTag and nextTag
@ -59,17 +59,17 @@ export default {
// the tag's offsetLeft before of prevTag // the tag's offsetLeft before of prevTag
const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing
if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) { if (afterNextTagOffsetLeft > scrollLeft + $containerWidth) {
this.scrollLeft(afterNextTagOffsetLeft - $containerWidth) this.scrollLeft(afterNextTagOffsetLeft - $containerWidth)
} }
else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) { else if (beforePrevTagOffsetLeft < scrollLeft) {
this.scrollLeft(beforePrevTagOffsetLeft) this.scrollLeft(beforePrevTagOffsetLeft)
} }
} }
}, },
scrollLeft(val) { scrollLeft(val) {
this.scrollWrapper.scrollTo({ this.getWrapper().scrollTo({
left: val, left: val,
behavior: 'smooth' behavior: 'smooth'
}) })
@ -80,7 +80,7 @@ export default {
let cost = 300, times = cost / 16, gap = val / times + 1 let cost = 300, times = cost / 16, gap = val / times + 1
const frameFunc = () => { const frameFunc = () => {
if (times > 0) { if (times > 0) {
this.scrollWrapper.scrollLeft += gap this.getWrapper().scrollLeft += gap
this.rAF = window.requestAnimationFrame(frameFunc) this.rAF = window.requestAnimationFrame(frameFunc)
times-- times--
} }

@ -1,6 +1,6 @@
<template> <template>
<nav class="tags-view-container"> <nav class="tags-view-container">
<scroll-pane class="tags-view-wrapper" ref="scrollPane"> <scroll-pane ref="scrollPane" class="tags-view-wrapper">
<router-link <router-link
ref="tag" ref="tag"
v-for="tag in visitedViews" v-for="tag in visitedViews"
@ -18,7 +18,7 @@
<context-menu v-model="contextmenu.show" :left="contextmenu.left" :top="contextmenu.top"> <context-menu v-model="contextmenu.show" :left="contextmenu.left" :top="contextmenu.top">
<context-menu-item @click="() => refreshSelectedTag(selectedTag)">刷新</context-menu-item> <context-menu-item @click="() => refreshSelectedTag(selectedTag)">刷新</context-menu-item>
<context-menu-item v-show="!isAffix(selectedTag)" @click="() => closeSelectedTag(selectedTag)"> <context-menu-item v-if="!isAffix(selectedTag)" @click="() => closeSelectedTag(selectedTag)">
关闭 关闭
</context-menu-item> </context-menu-item>
<context-menu-item @click="closeOthersTags">关闭其他</context-menu-item> <context-menu-item @click="closeOthersTags">关闭其他</context-menu-item>

@ -69,18 +69,8 @@
} }
} }
.el-scrollbar__bar {
display: none;
}
.el-scrollbar__wrap { .el-scrollbar__wrap {
height: 57px; height: 57px;
} }
} }
.flip-list-move {
transition: transform 0.3s;
}
} }

Loading…
Cancel
Save