Skip to content

Commit f244730

Browse files
committed
整体结构更新,版本提升至1.1.0,props 增加 path,增加 click 事件
1 parent a630801 commit f244730

File tree

10 files changed

+210
-135
lines changed

10 files changed

+210
-135
lines changed

README.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
# vue-json-pretty
22

3-
> A Vue.js project
3+
A Vue.js project for json-pretty. [leezng/vue-json-pretty](https://github.com/leezng/vue-json-pretty)
44

5-
## Build Setup
5+
## Installation
66

7-
``` bash
8-
# install dependencies
9-
npm install
7+
```js
8+
npm install vue-json-pretty
9+
```
10+
11+
## Usage
12+
13+
```html
14+
<template>
15+
<div>
16+
...
17+
<vue-json-pretty
18+
:path="'res'"
19+
:data="{ key: 'value' }"
20+
@click="handleClick">
21+
</vue-json-pretty>
22+
</div>
23+
</template>
24+
```
1025

11-
# serve with hot reload at localhost:8080
12-
npm run dev
26+
```js
27+
import VueJsonPretty from 'vue-json-pretty'
1328

14-
# build for production with minification
15-
npm run build
29+
export default {
30+
components: {
31+
VueJsonPretty
32+
}
33+
}
34+
```
1635

17-
# build for production and view the bundle analyzer report
18-
npm run build --report
36+
## Props
1937

20-
# run e2e tests
21-
npm run e2e
38+
| Attribute | Description | Type | Default |
39+
|-------- |-------- |-------- | -------- |
40+
| data | json data | string, array, object | - |
41+
| path | root data path | string | root |
2242

23-
# run all tests
24-
npm test
25-
```
43+
## Events
44+
45+
| Event Name | Description | Callback Parameters |
46+
|---------- |-------- |---------- |
47+
| click | triggered when a data item is clicked | (path, data) |
2648

27-
For detailed explanation on how things work, checkout the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader).

example/App.vue

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
<template>
2-
<div id="app">
3-
<tree :data="data"></tree>
2+
<div id="app" class="example-app">
3+
<vue-json-pretty :data="data" :path="'res'" @click="handleClick"></vue-json-pretty>
4+
<div class="result">
5+
<div>path: {{itemPath}}</div>
6+
<div>data: <pre>{{itemData}}</pre></div>
7+
</div>
48
</div>
59
</template>
610

711
<script>
8-
import Tree from 'src/components/tree'
12+
import VueJsonPretty from 'src/components/tree'
913
1014
export default {
1115
name: 'app',
1216
components: {
13-
Tree
17+
VueJsonPretty
1418
},
1519
data () {
1620
return {
@@ -19,9 +23,45 @@ export default {
1923
b: [{
2024
a: 1,
2125
b: 2
22-
}]
23-
}
26+
}, {
27+
a: 'abcde',
28+
b: true
29+
}],
30+
c: {
31+
a: null,
32+
b: 2
33+
}
34+
},
35+
itemData: {},
36+
itemPath: ''
37+
}
38+
},
39+
methods: {
40+
handleClick (path, data) {
41+
this.itemPath = path
42+
this.itemData = data
2443
}
2544
}
2645
}
2746
</script>
47+
48+
<style lang="less">
49+
.example-app {
50+
padding: 30px;
51+
> div {
52+
float: left;
53+
padding: 15px;
54+
width: 50%;
55+
min-height: 300px;
56+
border: 1px solid #ccc;
57+
box-sizing: border-box;
58+
}
59+
.result {
60+
font-family: Consolas;
61+
pre {
62+
margin: 0;
63+
font-family: inherit;
64+
}
65+
}
66+
}
67+
</style>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-json-pretty",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "A Vue.js project",
55
"author": "leezng <[email protected]>",
66
"main": "vue-json-pretty.js",

src/components/brackets-left.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<div>
3+
<slot></slot>
4+
<span
5+
v-show="dataVisiable"
6+
class="vjs__tree__node"
7+
@click.stop="dataVisiable = !dataVisiable">
8+
{{ Array.isArray(data) ? '[' : '{' }}
9+
</span>
10+
<span
11+
v-show="!dataVisiable"
12+
:class="{
13+
'vjs__tree__node': true,
14+
'vjs__not__lastIndex': index !== lastIndex
15+
}"
16+
@click.stop="dataVisiable = !dataVisiable">{{ Array.isArray(data) ? '[...]' : '{...}' }}</span>
17+
</div>
18+
</template>
19+
20+
<script>
21+
import bracketsMixin from 'src/mixins/brackets-mixin'
22+
23+
export default {
24+
mixins: [bracketsMixin]
25+
}
26+
</script>

src/components/brackets-right.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div v-show="dataVisiable">
3+
<span
4+
:class="{
5+
'vjs__tree__node': true,
6+
'vjs__not__lastIndex': index !== lastIndex }"
7+
@click.stop="dataVisiable = !dataVisiable">{{ Array.isArray(data) ? ']' : '}' }}</span>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import bracketsMixin from 'src/mixins/brackets-mixin'
13+
14+
export default {
15+
mixins: [bracketsMixin]
16+
}
17+
</script>

src/components/tree-child.vue

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/components/tree.vue

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,95 @@
11
<template>
22
<div class="vjs__tree">
3-
<span v-if="!child && show" class="vjs__tree__node" @click="toggle">{{ isArray(json) ? '[' : '{' }}</span>
4-
<span v-show="!show" class="vjs__tree__node" @click.stop="toggle">
5-
{{ isArray(json) ? '[...]' : '{...}' }}
6-
</span>
3+
<brackets-left
4+
:visiable.sync="visiable"
5+
:data="data"
6+
:index="index"
7+
:last-index="lastIndex">
8+
<span v-if="child && Array.isArray(data)">{{ index }}:</span>
9+
</brackets-left>
710

11+
<!-- data 为对象时, index 表示 key, 为数组才表示索引 -->
812
<div
9-
v-for="(item, index) in json"
10-
v-show="show"
13+
v-for="(item, index) in data"
14+
v-show="visiable"
1115
class="vjs__tree__content"
1216
:style="{ 'background-color': treeContentBackground }"
1317
:key="index"
1418
@mouseover.stop="treeContentBackground = '#eee'"
1519
@mouseout.stop="treeContentBackground = 'transparent'"
16-
@click.stop="getItemData">
17-
<tree-child
18-
v-if="isArray(item) || isObject(item)"
20+
@click.stop="handleClick">
21+
<tree
22+
v-if="Array.isArray(item) || isObject(item)"
23+
:data="item"
24+
:path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)"
1925
:index="index"
20-
:data="json"
21-
:item="item">
22-
<tree
23-
:data="item"
24-
:child="true"
25-
:path="path + (isArray(json) ? `[${index}]` : `.${index}`)">
26-
</tree>
27-
</tree-child>
26+
:last-index="getLastIndex()"
27+
:child="true"
28+
@click="handleItemClick">
29+
</tree>
2830

29-
<div v-else :class="{ 'vjs__lastIndex': index !== lastIndex }" >
30-
<span v-if="isObject(json)">{{ index }}:</span>
31+
<div v-else :class="{ 'vjs__not__lastIndex': index !== getLastIndex() }" >
32+
<span v-if="isObject(data)">{{ index }}:</span>
3133
<span :class="getValueClass(item)">{{ `${item}` }}</span>
3234
</div>
3335
</div>
3436

35-
<span v-if="!child && show" class="vjs__tree__node" @click.stop="toggle">
36-
{{ isArray(json) ? ']' : '}' }}
37-
</span>
37+
<brackets-right
38+
:visiable.sync="visiable"
39+
:data="data"
40+
:index="index"
41+
:last-index="lastIndex">
42+
</brackets-right>
3843
</div>
3944
</template>
4045

4146
<script>
42-
import TreeChild from './tree-child'
43-
import mixin from 'src/mixins/mixin'
47+
import BracketsLeft from './brackets-left'
48+
import BracketsRight from './brackets-right'
4449
4550
export default {
4651
name: 'tree',
47-
mixins: [mixin],
4852
props: {
4953
data: {},
5054
child: Boolean,
5155
path: {
5256
type: String,
53-
default: 'res'
54-
}
57+
default: 'root'
58+
},
59+
index: {},
60+
lastIndex: {}
5561
},
5662
data () {
5763
return {
64+
visiable: true,
5865
treeContentBackground: 'transparent'
5966
}
6067
},
6168
components: {
62-
TreeChild
63-
},
64-
computed: {
65-
json () {
66-
return JSON.parse(JSON.stringify(this.data))
67-
}
69+
BracketsLeft,
70+
BracketsRight
6871
},
6972
methods: {
70-
getItemData () {
71-
console.log('path: ', this.path)
72-
console.log('data: ', this.json)
73+
// 触发组件的 click 事件
74+
handleClick () {
75+
this.$emit('click', this.path, this.data)
76+
},
77+
// 处理子树触发的 click 事件, 并传递到顶层
78+
handleItemClick (path, data) {
79+
this.$emit('click', path, data)
80+
},
81+
// 工具函数: 判断是否对象
82+
isObject (value) {
83+
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase() === 'object'
84+
},
85+
// 获取当前 data 中最后一项的 key 或 索引, 便于界面判断是否添加 ","
86+
getLastIndex () {
87+
if (Array.isArray(this.data)) {
88+
return this.data.length - 1
89+
} else if (this.isObject(this.data)) {
90+
let arr = Object.keys(this.data)
91+
return arr[arr.length - 1]
92+
}
7393
},
7494
getValueClass (value) {
7595
if (value === null) {
@@ -97,7 +117,7 @@
97117
color: #20a0ff;
98118
}
99119
}
100-
.vjs__lastIndex:after {
120+
.vjs__not__lastIndex:after {
101121
content: ",";
102122
}
103123
.vjs__value__null {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import VueJsonPretty from './components/tree.vue'
22

33
export default Object.assign({}, VueJsonPretty, {
4-
version: '1.0.1'
4+
version: '1.1.0'
55
})

0 commit comments

Comments
 (0)