11<template >
2- <li
3- v-if =" itemVisible"
4- :class =' liClass'
5- >
2+ <li v-if =" itemVisible" :class =" liClass" >
63 <div class =" tree-node-el" :draggable =" draggable" @dragstart =" drag(item, $event)" >
7- <span @click =" expandNode(item)" v-if =" showExpand" class =' tree-expand'
4+ <span
5+ @click =" expandNode(item)"
6+ v-if =" showExpand"
7+ class =" tree-expand"
88 :class =" item.expanded ? 'tree-open' : 'tree-close'"
9+ ></span >
10+ <span
11+ v-if =" multiple && !item.nocheck"
12+ :class =" [item.checked ? (item.halfcheck ? 'box-halfchecked' : 'box-checked') : 'box-unchecked', 'inputCheck']"
913 >
10- </span >
11- <span v-if =' multiple && !item.nocheck' :class =" [item.checked ? (item.halfcheck ? 'box-halfchecked' : 'box-checked') : 'box-unchecked', 'inputCheck']" >
12- <input :disabled =" item.chkDisabled"
14+ <input
15+ :disabled =" item.chkDisabled"
1316 :class =" ['check', item.chkDisabled ? 'chkDisabled' : '']"
14- v-if =' multiple' type =" checkbox" @change =" changeNodeCheckStatus(item, $event)" :checked =" item.checked" />
17+ v-if =" multiple"
18+ type =" checkbox"
19+ @change =" changeNodeCheckStatus(item, $event)"
20+ :checked =" item.checked"
21+ >
1522 </span >
1623 <loading v-if =" item.loading && item.expanded" />
17- <Render :node =" item" :parent =' parent' :index =' index' :tpl =' tpl' :nodeMouseOver =' nodeMouseOver' :level =' level' />
24+ <Render
25+ :node =" item"
26+ :parent =" parent"
27+ :index =" index"
28+ :tpl =" tpl"
29+ :nodeMouseOver =" nodeMouseOver"
30+ :level =" level"
31+ />
1832 </div >
1933 <template v-if =" showNextUl " >
2034 <collapse-transition >
2135 <TreeUl
2236 v-show =" item.expanded"
2337 :dragAfterExpanded =" dragAfterExpanded"
2438 :draggable =" draggable"
25- :tpl =" tpl"
39+ :tpl =" tpl"
2640 :data =" item.children"
27- :halfcheck =' halfcheck'
28- :scoped = ' scoped'
29- :parent = ' item'
30- :canDeleteRoot = ' canDeleteRoot'
41+ :halfcheck =" halfcheck"
42+ :scoped = " scoped"
43+ :parent = " item"
44+ :canDeleteRoot = " canDeleteRoot"
3145 :multiple =" multiple"
32- :level =' level'
33- :maxLevel =' maxLevel'
34- :topMustExpand =' topMustExpand'
35- :allowGetParentNode =' allowGetParentNode'
46+ :level =" level"
47+ :maxLevel =" maxLevel"
48+ :topMustExpand =" topMustExpand"
49+ :allowGetParentNode =" allowGetParentNode"
3650 />
3751 </collapse-transition >
3852 </template >
3953 </li >
4054</template >
4155<script >
42- import mixins from ' ./mixins'
43- import Render from ' ./render'
44- import Loading from ' ./loading'
45- import CollapseTransition from ' ./collapse-transition'
56+ import mixins from " ./mixins" ;
57+ import Render from " ./render" ;
58+ import Loading from " ./loading" ;
59+ import CollapseTransition from " ./collapse-transition" ;
4660export default {
47- name: ' TreeLi' ,
61+ name: " TreeLi" ,
4862 mixins: [mixins],
49- components: {Render, Loading, CollapseTransition,
63+ components: {
64+ Render,
65+ Loading,
66+ CollapseTransition
5067 // TreeUl: () => import('./treeUl.vue') // 解决循环引用的问题
5168 },
5269 beforeCreate () {
53- this .$options .components .TreeUl = require (' ./treeUl.vue' ).default
70+ this .$options .components .TreeUl = require (" ./treeUl.vue" ).default ;
5471 },
5572 props: {
5673 item: {
@@ -94,60 +111,73 @@ export default {
94111 maxLevel: Number ,
95112 level: Number ,
96113 topMustExpand: Boolean ,
97- allowGetParentNode: Boolean ,
114+ allowGetParentNode: Boolean
98115 },
99- inject: [' isLeaf' , ' childChecked' , ' parentChecked' , ' nodeSelected' , ' emitEventToTree' , ' setAttr' ],
116+ inject: [
117+ " isLeaf" ,
118+ " childChecked" ,
119+ " parentChecked" ,
120+ " nodeSelected" ,
121+ " emitEventToTree" ,
122+ " setAttr"
123+ ],
100124 computed: {
101125 itemVisible () {
102- const {visible = true } = this .item
103- return visible
126+ const { visible = true } = this .item ;
127+ return visible;
104128 },
105- hasExpended () { // 已经展开过
106- let {hasExpended = false , expanded = false } = this .item
107- return this .itemVisible && (expanded || hasExpended)
129+ hasExpended () {
130+ // 已经展开过
131+ let { hasExpended = false , expanded = false } = this .item ;
132+ return this .itemVisible && (expanded || hasExpended);
108133 },
109- liClass () {
110- const index = this .index
111- let res
134+ liClass () {
135+ const index = this .index ;
136+ let res;
112137 if (this .parent ) {
113138 res = {
114- leaf: this .isLeaf (this .item ),
115- }
116- } else { // top node
139+ leaf: this .isLeaf (this .item )
140+ };
141+ } else {
142+ // top node
117143 res = {
118- ' first-node' : index === 0 ,
119- ' only-node' : this .dataLength === 1 ,
120- ' second-node' : index === 1
121- }
144+ " first-node" : index === 0 ,
145+ " only-node" : this .dataLength === 1 ,
146+ " second-node" : index === 1
147+ };
122148 }
123- return res
124- },
125- hasChildren () {
126- const item = this .item
127- return item .children && item .children .length > 0
128- },
129- showExpand () {
130- const item = this .item
131- const isShow = ! this .parent ? this .topMustExpand : false
132- return isShow || this .hasChildren || item .async
133- },
134- showNextUl () {
135- return ! this .isLeaf (this .item ) && this .maxLevel > this .level && this .hasExpended
136- },
137- position () {
138- return {level: this .level , index: this .index }
149+ return res;
150+ },
151+ hasChildren () {
152+ const item = this .item ;
153+ return item .children && item .children .length > 0 ;
154+ },
155+ showExpand () {
156+ const item = this .item ;
157+ const isShow = ! this .parent ? this .topMustExpand : false ;
158+ return isShow || this .hasChildren || item .async ;
159+ },
160+ showNextUl () {
161+ return (
162+ ! this .isLeaf (this .item ) &&
163+ this .maxLevel > this .level &&
164+ this .hasExpended
165+ );
166+ },
167+ position () {
168+ return { level: this .level , index: this .index };
139169 }
140170 },
141171 watch: {
142- ' item.checked' : {
143- handler () {
144- this .checkedChange ()
172+ " item.checked" : {
173+ handler () {
174+ this .checkedChange ();
145175 },
146176 immediate: true
147177 },
148- ' item.halfcheck' : {
149- handler () {
150- this .checkedChange ()
178+ " item.halfcheck" : {
179+ handler () {
180+ this .checkedChange ();
151181 },
152182 immediate: true
153183 }
@@ -156,66 +186,65 @@ export default {
156186 /* @method drag node
157187 * @param node draged node
158188 * @param ev $event
159- */
160- drag (node , ev ) {
161- let guid = this .guid ()
162- this .setDragNode (guid, node, this .parent )
163- ev .dataTransfer .setData (' guid' , guid)
189+ */
190+ drag (node , ev ) {
191+ let guid = this .guid ();
192+ this .setDragNode (guid, node, this .parent );
193+ ev .dataTransfer .setData (" guid" , guid);
164194 },
165195 /* @method expand or close node
166196 * @param node current node
167- */
168- expandNode (node ) {
169- const expended = ! node .expanded
170- this .setAttr (node, ' expanded' , expended)
171- this .setAttr (node, ' hasExpended' , true )
197+ */
198+ expandNode (node ) {
199+ const expended = ! node .expanded ;
200+ this .setAttr (node, " expanded" , expended);
201+ this .setAttr (node, " hasExpended" , true );
172202 if (node .async && ! node .children ) {
173- this .emitEventToTree (' async-load-nodes' , node)
203+ this .emitEventToTree (" async-load-nodes" , node);
174204 }
175- this .emitEventToTree (' node-expand' , node, expended, this .position )
205+ this .emitEventToTree (" node-expand" , node, expended, this .position );
176206 },
177207 /* @event passing the node-check event to the parent component
178208 * @param node clicked node
179209 */
180- nodeCheck (node , checked ) {
181- this .$set (node, ' checked' , checked)
182- const halfcheck = this .halfcheck
183- if (halfcheck) {
184- this .$set (node, ' halfcheck' , false )
185- }
210+ nodeCheck (node , checked ) {
211+ this .$set (node, " checked" , checked);
186212 if (! this .scoped ) {
187- this .childChecked (node, checked, halfcheck)
188- // this.theParentChecked(checked, halfcheck)
213+ const halfcheck = this .halfcheck ;
214+ if (halfcheck) {
215+ this .$set (node, " halfcheck" , false );
216+ }
217+ this .childChecked (node, checked, halfcheck);
189218 }
190219 },
191220 /* @event passing the node-mouse-over event to the parent component
192221 * @param node overed node
193222 */
194- nodeMouseOver (node , index , parent ) {
195- this .emitEventToTree (' node-mouse-over' , node, index, parent)
223+ nodeMouseOver (node , index , parent ) {
224+ this .emitEventToTree (" node-mouse-over" , node, index, parent);
196225 // this.$emit('node-mouse-over', node)
197226 },
198227 /*
199228 *@method change the check box status method
200229 *@param node current node
201230 *@param $event event object
202231 */
203- changeNodeCheckStatus (node , $event ) {
204- const checked = $event .target .checked
205- this .nodeCheck (node, checked)
206- this .emitEventToTree (' node-check' , node, checked, this .position )
207- },
208- theParentChecked (checked , halfcheck ){
209- const parentNode = this .parent
210- this .parentChecked (parentNode, checked, halfcheck)
211- },
212- checkedChange () {
213- const {checked = false } = this .item
214- this .theParentChecked (checked, this .halfcheck )
232+ changeNodeCheckStatus (node , $event ) {
233+ const checked = $event .target .checked ;
234+ this .nodeCheck (node, checked);
235+ this .emitEventToTree (" node-check" , node, checked, this .position );
236+ },
237+ theParentChecked (checked , halfcheck ) {
238+ const parentNode = this .parent ;
239+ this .parentChecked (parentNode, checked, halfcheck);
240+ },
241+ checkedChange () {
242+ const { checked = false } = this .item ;
243+ this .theParentChecked (checked, this .halfcheck );
215244 // if (!checked) {
216245 // this.$set(this.item, 'selected', checked)
217246 // }
218- },
247+ }
219248 }
220- }
249+ };
221250 </script >
0 commit comments