Skip to content

Commit 117ac50

Browse files
committed
Merge branch 'dev' into webpack4-dev
# Conflicts: # src/components/app.vue # src/components/simple-text.vue
2 parents c587e44 + 0c09dfa commit 117ac50

File tree

7 files changed

+101
-69
lines changed

7 files changed

+101
-69
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default {
6868
| showSelectController | higher | whether to show the select controller at left | boolean | false |
6969
| selectOnClickNode | higher | whether to change selected value when click node | boolean | true |
7070
| highlightSelectedNode | higher | highlight current node when selected | boolean | true |
71+
| customValueFormatter | higher | a function that can return different html or strings to display for values in the data. If it returns null or empty, the default formatter is used | Function | false |
7172

7273
## Events
7374

example/App.vue

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
<label>highlightMouseoverNode</label>
2525
<input type="checkbox" v-model="highlightMouseoverNode">
2626
</div>
27+
<div>
28+
<label>collapsedOnClickBrackets</label>
29+
<input type="checkbox" v-model="collapsedOnClickBrackets">
30+
</div>
31+
<div>
32+
<label>use custom link formatter</label>
33+
<input type="checkbox" v-model="useCustomLinkFormatter">
34+
</div>
2735
<div>
2836
<label>deep</label>
2937
<select v-model="deep">
@@ -43,6 +51,8 @@
4351
:show-length="showLength"
4452
:show-line="showLine"
4553
:highlight-mouseover-node="highlightMouseoverNode"
54+
:collapsed-on-click-brackets="collapsedOnClickBrackets"
55+
:custom-value-formatter="useCustomLinkFormatter ? customLinkFormatter : null"
4656
@click="handleClick">
4757
</vue-json-pretty>
4858
</div>
@@ -96,6 +106,10 @@
96106
<label>highlightSelectedNode</label>
97107
<input type="checkbox" v-model="highlightSelectedNode">
98108
</div>
109+
<div>
110+
<label>collapsedOnClickBrackets</label>
111+
<input type="checkbox" v-model="collapsedOnClickBrackets">
112+
</div>
99113
<div>
100114
<label>deep</label>
101115
<select v-model="deep">
@@ -124,6 +138,7 @@
124138
:show-length="showLength"
125139
:show-line="showLine"
126140
:select-on-click-node="selectOnClickNode"
141+
:collapsed-on-click-brackets="collapsedOnClickBrackets"
127142
v-model="value"
128143
:path-selectable="((path, data) => typeof data !== 'number')"
129144
:selectable-type="selectableType"
@@ -159,7 +174,8 @@ export default {
159174
}, {
160175
news_id: 51183,
161176
title: 'Traffic paradise: How to design streets for people and unmanned vehicles in the future?',
162-
source: 'Netease smart'
177+
source: 'Netease smart',
178+
link: 'http://netease.smart/traffic-paradise/1235'
163179
}, {
164180
news_id: 51182,
165181
title: 'Teslamask\'s American Business Relations: The government does not pay billions to build factories',
@@ -176,6 +192,8 @@ export default {
176192
highlightMouseoverNode: true,
177193
highlightSelectedNode: true,
178194
selectOnClickNode: true,
195+
collapsedOnClickBrackets: true,
196+
useCustomLinkFormatter: false,
179197
path: 'res',
180198
deep: 3,
181199
itemData: {},
@@ -217,6 +235,13 @@ export default {
217235
},
218236
handleChange (newVal, oldVal) {
219237
console.log('newVal: ', newVal, ' oldVal: ', oldVal)
238+
},
239+
customLinkFormatter(data) {
240+
if (data.startsWith('http://')) {
241+
return `<a style="color:red;" href="${data}">"${data}"</a>`;
242+
} else {
243+
return null;
244+
}
220245
}
221246
}
222247
}

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.6.3",
3+
"version": "1.6.4",
44
"description": "A JSON tree view component that is easy to use and also supports data selection.",
55
"author": "leezng <[email protected]>",
66
"main": "vue-json-pretty.js",

src/components/app.vue

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,10 @@
1111
}"
1212
@click="handleClick"
1313
@mouseover.stop="handleMouseover"
14-
@mouseout.stop="handleMouseout"
15-
>
14+
@mouseout.stop="handleMouseout">
1615
<template v-if="showSelectController && selectable">
17-
<vue-checkbox
18-
v-if="isMultiple"
19-
v-model="currentCheckboxVal"
20-
@change="handleValueChange('checkbox')"
21-
/>
22-
<vue-radio
23-
v-else-if="isSingle"
24-
v-model="model"
25-
:path="path"
26-
@change="handleValueChange('radio')"
27-
/>
16+
<vue-checkbox v-if="isMultiple" v-model="currentCheckboxVal" @change="handleValueChange('checkbox')"></vue-checkbox>
17+
<vue-radio v-else-if="isSingle" v-model="model" @change="handleValueChange('radio')" :path="path"></vue-radio>
2818
</template>
2919

3020
<template v-if="Array.isArray(data) || isObject(data)">
@@ -33,24 +23,20 @@
3323
:visible.sync="visible"
3424
:data="data"
3525
:show-length="showLength"
36-
:show-comma="notLastKey"
37-
>
38-
<span
39-
v-if="currentDeep > 1 && !Array.isArray(parentData)"
40-
class="vjs-key"
41-
>{{ keyFormatter(currentKey) }}:</span>
26+
:collapsed-on-click-brackets="collapsedOnClickBrackets"
27+
:show-comma="notLastKey">
28+
<span v-if="currentDeep > 1 && !Array.isArray(parentData)" class="vjs-key">{{ keyFormatter(currentKey) }}:</span>
4229
</brackets-left>
4330

4431
<!-- 数据内容, data 为对象时, key 表示键名, 为数组时表示索引 -->
4532
<div
4633
v-for="(item, key) in data"
4734
v-show="visible"
48-
:key="key"
4935
:class="{
5036
'vjs-tree__content': true,
5137
'has-line': showLine
5238
}"
53-
>
39+
:key="key">
5440
<vue-json-pretty
5541
v-model="model"
5642
:parent-data="data"
@@ -66,29 +52,32 @@
6652
:selectable-type="selectableType"
6753
:show-select-controller="showSelectController"
6854
:select-on-click-node="selectOnClickNode"
55+
:collapsed-on-click-brackets="collapsedOnClickBrackets"
6956
:current-key="key"
7057
:current-deep="currentDeep + 1"
58+
:custom-value-formatter="customValueFormatter"
7159
@click="handleItemClick"
72-
@change="handleItemChange"
73-
/>
60+
@change="handleItemChange">
61+
</vue-json-pretty>
7462
</div>
7563

7664
<!-- 右闭合 -->
7765
<brackets-right
7866
:visible.sync="visible"
7967
:data="data"
80-
:show-comma="notLastKey"
81-
/>
68+
:collapsed-on-click-brackets="collapsedOnClickBrackets"
69+
:show-comma="notLastKey">
70+
</brackets-right>
8271
</template>
8372

8473
<simple-text
8574
v-else
75+
:custom-value-formatter="customValueFormatter"
8676
:show-double-quotes="showDoubleQuotes"
8777
:show-comma="notLastKey"
8878
:parent-data="parentData"
8979
:data="data"
90-
:current-key="currentKey"
91-
>
80+
:current-key="currentKey">
9281
<span
9382
v-if="parentData && currentKey && !Array.isArray(parentData)"
9483
class="vjs-key"
@@ -108,7 +97,7 @@
10897
import { getDataType } from 'src/utils'
10998
11099
export default {
111-
name: 'VueJsonPretty',
100+
name: 'vue-json-pretty',
112101
components: {
113102
SimpleText,
114103
VueCheckbox,
@@ -119,10 +108,7 @@
119108
props: {
120109
/* outer props */
121110
// 当前树的数据
122-
data: {
123-
type: [String, Number, Boolean, Array, Object],
124-
default: null
125-
},
111+
data: {},
126112
// 定义树的深度, 大于该深度的子树将不被展开
127113
deep: {
128114
type: Number,
@@ -183,24 +169,28 @@
183169
type: Boolean,
184170
default: true
185171
},
172+
// collapsed control
173+
collapsedOnClickBrackets: {
174+
type: Boolean,
175+
default: true
176+
},
177+
// custom formatter for values
178+
customValueFormatter: {
179+
type: Function,
180+
default: null
181+
},
186182
/* outer props */
187183
188184
/* inner props */
189185
// 当前树的父级数据
190-
parentData: {
191-
type: [String, Number, Boolean, Array, Object],
192-
default: null
193-
},
186+
parentData: {},
194187
// 当前树的深度, 以根节点作为0开始, 所以第一层树的深度为1, 递归逐次递增
195188
currentDeep: {
196189
type: Number,
197190
default: 1
198191
},
199192
// 当前树的数据 data 为数组时 currentKey 表示索引, 为对象时表示键名
200-
currentKey: {
201-
type: [Number, String],
202-
default: ''
203-
}
193+
currentKey: [Number, String]
204194
/* outer props */
205195
},
206196
data () {
@@ -229,7 +219,6 @@
229219
let arr = Object.keys(this.parentData)
230220
return arr[arr.length - 1]
231221
}
232-
return ''
233222
},
234223
235224
// 是否不是最后一项
@@ -267,19 +256,6 @@
267256
return error ? 'When selectableType is not null, selectOnClickNode and showSelectController cannot be false at the same time, because this will cause the selection to fail.' : ''
268257
}
269258
},
270-
watch: {
271-
deep (newVal) {
272-
this.visible = this.currentDeep <= newVal
273-
},
274-
propsError: {
275-
handler (message) {
276-
if (message) {
277-
throw new Error(`[vue-json-pretty] ${message}`)
278-
}
279-
},
280-
immediate: true
281-
}
282-
},
283259
methods: {
284260
handleValueChange (emitType) {
285261
if (this.isMultiple && (emitType === 'checkbox' || emitType === 'tree')) {
@@ -358,6 +334,19 @@
358334
// 因为是递归组件,因此错误只对外暴露一次,子组件的错误不再对外传递
359335
errorCaptured () {
360336
return false
337+
},
338+
watch: {
339+
deep (newVal) {
340+
this.visible = this.currentDeep <= newVal
341+
},
342+
propsError: {
343+
handler (message) {
344+
if (message) {
345+
throw new Error(`[vue-json-pretty] ${message}`)
346+
}
347+
},
348+
immediate: true
349+
}
361350
}
362351
}
363352
</script>

src/components/simple-text.vue

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<template>
22
<div>
3-
<slot />
4-
<span :class="`vjs-value vjs-value__${dataType}`">
5-
{{ textFormatter(data) }}
6-
</span>
3+
<slot></slot>
4+
<span v-if="customValueFormatter" :class="valueClass" v-html="customFormatter(data)"/>
5+
<span v-else :class="valueClass">{{ defaultFormatter(data) }}</span>
76
</div>
87
</template>
98

@@ -13,24 +12,39 @@
1312
export default {
1413
props: {
1514
showDoubleQuotes: Boolean,
16-
data: {
17-
type: [String, Number, Boolean],
18-
default: ''
19-
},
20-
showComma: Boolean
15+
parentData: {},
16+
data: {},
17+
showComma: Boolean,
18+
currentKey: [Number, String],
19+
customValueFormatter: Function
2120
},
2221
computed: {
22+
valueClass () {
23+
return `vjs-value vjs-value__${this.dataType}`
24+
},
25+
2326
// 当前数据类型
2427
dataType () {
2528
return getDataType(this.data)
29+
},
30+
31+
// 父级数据类型
32+
parentDataType () {
33+
return getDataType(this.parentData)
2634
}
2735
},
2836
methods: {
29-
textFormatter (data) {
37+
defaultFormatter (data) {
3038
let text = data + ''
3139
if (this.dataType === 'string') text = `"${text}"`
3240
if (this.showComma) text += ','
3341
return text
42+
},
43+
44+
customFormatter (data) {
45+
return this.customValueFormatter(
46+
data, this.currentKey, this.parentData
47+
) || this.defaultFormatter(data)
3448
}
3549
}
3650
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import App from './components/app.vue'
22
import './assets/less/index.less'
33

44
export default Object.assign({}, App, {
5-
version: '1.6.2'
5+
version: '1.6.4'
66
})

src/mixins/brackets-mixin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ export default {
77
data: {
88
required: true
99
},
10-
showComma: Boolean
10+
showComma: Boolean,
11+
collapsedOnClickBrackets: Boolean
1112
},
1213
computed: {
1314
dataVisiable: {
1415
get () {
1516
return this.visible
1617
},
1718
set (val) {
18-
this.$emit('update:visible', val)
19+
if (this.collapsedOnClickBrackets) {
20+
this.$emit('update:visible', val)
21+
}
1922
}
2023
}
2124
},

0 commit comments

Comments
 (0)