This repository has been archived by the owner on May 31, 2024. It is now read-only.
forked from zdy1988/vue-jstree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.vue
566 lines (554 loc) · 21.2 KB
/
App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<template>
<div id="app" style="padding-bottom: 300px;">
<img src="./assets/logo.png">
<h1>{{ msg }}</h1>
<h2>Tree View</h2>
<div>
<div style="width:840px; margin: 0 auto;">
<div style="width:49%; display:inline-block; vertical-align: top;">
<p style="text-align:left">Search Text <input type="text" @keyup="inputKeyUp" v-model="searchText" /></p>
<br>
<v-jstree :data="data"
:item-events="itemEvents"
show-checkbox
multiple
allow-batch
whole-row
draggable
@item-click="itemClick"
@item-drag-start="itemDragStart"
@item-drag-end="itemDragEnd"
@item-drop-before = "itemDropBefore"
@item-drop="itemDrop"
ref="tree"></v-jstree>
<br>
<span style="float: left; background-color: red; color: #fff; padding: 6px" draggable="true">
drag me to add new child !
</span>
</div>
<div style="width:50%; display:inline-block;">
<textarea style="height:300px; width:100%;">
{{data}}
</textarea>
</div>
</div>
</div>
<h2>Edit Tree Item</h2>
<p>click the node for edit</p>
<div>
<div style="width:840px; height:300px; margin: 0 auto;">
<table>
<tr>
<td>
text
</td>
<td>
<input v-model="editingItem.text" />
</td>
</tr>
<tr>
<td>
value
</td>
<td>
<input v-model="editingItem.value" />
</td>
</tr>
<tr>
<td>
icon
</td>
<td>
<input v-model="editingItem.icon" />
</td>
</tr>
<tr>
<td>
opened
</td>
<td>
<input type="checkbox" v-model="editingItem.opened"/>
</td>
</tr>
<tr>
<td>
selected
</td>
<td>
<input type="checkbox" v-model="editingItem.selected" />
</td>
</tr>
<tr>
<td>
disabled
</td>
<td>
<input type="checkbox" v-model="editingItem.disabled" />
</td>
</tr>
<tr>
<td colspan="2">
<button @click="addChildNode">add child node</button>
<button @click="removeNode">remove this node</button>
<button @click="addBeforeNode">add child before node</button>
<button @click="addAfterNode">add child after node</button>
<button @click="openChildren">open child node</button>
<button @click="closeChildren">close child node</button>
</td>
</tr>
</table>
</div>
</div>
<h2>Async Loading</h2>
<div>
<div style="width:840px; margin: 0 auto;">
<div style="width:49%; display:inline-block; vertical-align: top;">
<v-jstree :data="asyncData" :async="loadData" show-checkbox multiple allow-batch whole-row @item-click="itemClick" ref="tree2"></v-jstree>
</div>
<div style="width:50%; display:inline-block; vertical-align: top;">
<textarea style="height:300px; width:100%;">
{{asyncData}}
</textarea>
</div>
</div>
</div>
<h2>Async Operation</h2>
<div>
<div style="width:840px; margin: 0 auto; height: 300px;">
<button @click="refreshNode" >
refreshAsyncNode
</button>
</div>
</div>
<h2>Custom Tree Item</h2>
<div>
<div style="width:840px; margin: 0 auto;">
<div style="width:49%; display:inline-block; vertical-align: top;">
<v-jstree :data="data"
:item-events="itemEvents"
show-checkbox
multiple
allow-batch
whole-row
draggable
@item-click="itemClick"
@item-drag-start="itemDragStart"
@item-drag-end="itemDragEnd"
@item-drop-before = "itemDropBefore"
@item-drop="itemDrop">
<template slot-scope="_">
<div style="display: inherit; width: 200px" @click.ctrl="customItemClickWithCtrl">
<i :class="_.vm.themeIconClasses" role="presentation" v-if="!_.model.loading"></i>
{{_.model.text}}
<button style="border: 0px; background-color: transparent; cursor: pointer;" @click="customItemClick(_.vm, _.model, $event)"><i class="fa fa-remove"></i></button>
</div>
</template>
</v-jstree>
</div>
<div style="width:50%; display:inline-block;">
<textarea style="height:300px; width:100%;">
{{data}}
</textarea>
</div>
</div>
</div>
<h3>Performace Test</h3>
<div>
<div style="width:840px; margin: 0 auto;">
<div style="width:49%; display:inline-block; vertical-align: top;">
<p style="text-align:left">
<button @click="fillData">Add files to folder</button>
<input type="text" v-model="filesToAdd"/>
</p>
<v-jstree :data="data3"
show-checkbox
multiple
allow-batch
:allow-transition="false"
whole-row
@item-click="itemClick"
ref="tree3"></v-jstree>
</div>
<div style="width:50%; display:inline-block;">
<textarea style="height:300px; width:100%;">
{{data3}}
</textarea>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
msg: 'A Tree Plugin For Vue2',
searchText: '',
editingItem: {},
editingNode: null,
itemEvents: {
mouseover: function () {
console.log('mouseover')
},
contextmenu: function () {
console.log(arguments[2])
arguments[2].preventDefault()
console.log('contextmenu')
}
},
data: [
{
"text": "Same but with checkboxes",
"children": [
{
"text": "initially selected",
"selected": true
},
{
"text": "custom icon",
"icon": "fa fa-warning icon-state-danger"
},
{
"text": "initially open",
"icon": "fa fa-folder icon-state-default",
"opened": true,
"children": [
{
"text": "Another node"
}
]
},
{
"text": "custom icon",
"icon": "fa fa-warning icon-state-warning"
},
{
"text": "disabled node",
"icon": "fa fa-check icon-state-success",
"disabled": true
}
]
},
{
"text": "Same but with checkboxes",
"opened": true,
"children": [
{
"text": "initially selected",
"selected": true
},
{
"text": "custom icon",
"icon": "fa fa-warning icon-state-danger"
},
{
"text": "initially open",
"icon": "fa fa-folder icon-state-default",
"opened": true,
"children": [
{
"text": "Another node"
}
]
},
{
"text": "custom icon",
"icon": "fa fa-warning icon-state-warning"
},
{
"text": "disabled node",
"icon": "fa fa-check icon-state-success",
"disabled": true
}
]
},
{
"text": "And wholerow selection"
},
{
"text": "drag disabled",
"icon": "fa fa-warning icon-state-danger",
"dragDisabled": true
},
{
"text": "drop disabled",
"icon": "fa fa-warning icon-state-danger",
"dropDisabled": true
}
],
data2: [
{
"text2": "Same but with checkboxes",
"children2": [
{
"text2": "initially selected",
"selected": true
},
{
"text2": "custom icon",
"icon": "fa fa-warning icon-state-danger"
},
{
"text2": "initially open",
"icon": "fa fa-folder icon-state-default",
"opened": true,
"children2": [
{
"text2": "Another node"
}
]
},
{
"text2": "custom icon",
"icon": "fa fa-warning icon-state-warning"
},
{
"text2": "disabled node",
"icon": "fa fa-check icon-state-success",
"disabled": true
}
]
},
{
"text2": "Same but with checkboxes",
"opened": true,
"children2": [
{
"text2": "initially selected",
"selected": true
},
{
"text2": "custom icon",
"icon": "fa fa-warning icon-state-danger"
},
{
"text2": "initially open",
"icon": "fa fa-folder icon-state-default",
"opened": true,
"children2": [
{
"text2": "Another node"
}
]
},
{
"text2": "custom icon",
"icon": "fa fa-warning icon-state-warning"
},
{
"text2": "disabled node",
"icon": "fa fa-check icon-state-success",
"disabled": true
}
]
},
{
"text2": "And wholerow selection"
}
],
asyncData: [],
loadData: function (oriNode, resolve) {
var id = oriNode.data.id ? oriNode.data.id : 0
setTimeout(() => {
let data = []
if (id > 200) {
data = []
}
else {
data = [
{
"text": "New Item 1..." + id, "isLeaf": id > 100
},
{
"text": "New Item 2..." + id, "isLeaf": id > 100
}
]
}
resolve(data)
}, 500)
},
data3: [
{"text": "root"}
],
filesToAdd: 1,
filesToAddIndex: 0
}
},
methods: {
itemClick (node) {
this.editingNode = node
this.editingItem = node.model
console.log(node.model.text + ' clicked !')
},
itemDragStart (node) {
console.log(node.model.text + ' drag start !')
},
itemDragEnd (node) {
console.log(node.model.text + ' drag end !')
},
itemDropBefore (node, item, draggedItem , e) {
if (!draggedItem) {
item.addChild({
text: "newNode",
value: "newNode"
})
}
},
itemDrop (node, item) {
var sortBy = function(attr,rev) {
if (rev == undefined) {
rev = 1;
} else {
rev = (rev) ? 1 : -1;
}
return function (a, b) {
a = a[attr];
b = b[attr];
if (a < b) {
return rev * -1;
}
if (a > b) {
return rev * 1;
}
return 0;
}
}
item.children.sort(sortBy('text', true))
console.log(node.model.text + ' drop !')
},
inputKeyUp: function () {
var text = this.searchText
const patt = new RegExp(text);
this.$refs.tree.handleRecursionNodeChilds(this.$refs.tree, function (node) {
if (text !== '' && node.model !== undefined) {
const str = node.model.text
if (patt.test(str)) {
node.$el.querySelector('.tree-anchor').style.color = 'red'
} else {
node.$el.querySelector('.tree-anchor').style.color = '#000'
} // or other operations
} else {
node.$el.querySelector('.tree-anchor').style.color = '#000'
}
})
},
addChildNode: function () {
if (this.editingItem.id !== undefined) {
this.editingItem.addChild({
text: "newNode"
})
}
},
removeNode: function () {
if (this.editingItem.id !== undefined) {
var index = this.editingNode.parentItem.indexOf(this.editingItem)
this.editingNode.parentItem.splice(index, 1)
}
},
addBeforeNode: function () {
if (this.editingItem.id !== undefined) {
this.editingItem.addBefore({
text: this.editingItem.text + " before"
}, this.editingNode)
}
},
addAfterNode: function () {
if (this.editingItem.id !== undefined) {
this.editingItem.addAfter({
text: this.editingItem.text + " after"
}, this.editingNode)
}
},
openChildren: function () {
if (this.editingItem.id !== undefined) {
this.editingItem.openChildren()
}
},
closeChildren: function () {
if (this.editingItem.id !== undefined) {
this.editingItem.closeChildren()
}
},
refreshNode: function () {
this.asyncData = [
this.$refs.tree2.initializeLoading()
]
this.$refs.tree2.handleAsyncLoad(this.asyncData, this.$refs.tree2)
},
customItemClick: function (node ,item, e) {
e.stopPropagation()
var index = node.parentItem.indexOf(item)
node.parentItem.splice(index, 1)
},
customItemClickWithCtrl: function () {
console.log('click + ctrl')
},
fillData: function () {
if (this.editingItem.id !== undefined) {
for (var i = 0; i < this.filesToAdd; i++) {
this.filesToAddIndex++
this.editingItem.addChild({
text: "File" + this.filesToAddIndex,
icon: "fa fa-file icon-state-danger"
})
}
}
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
div{
display: block;
}
table {
width: 100%;
border-collapse: collapse;
border: 1px solid #EEE;
font-size: 14px;
}
table th {
background: #EEE;
border-bottom: 1px solid #CCC;
padding: 4px;
}
table td {
border: 1px solid #EEE;
padding: 4px;
}
.icon-state-default {
color: gray;
}
.icon-state-danger {
color: red;
}
.icon-state-warning {
color: yellow;
}
.icon-state-success {
color: green;
}
</style>