11import DataEngine from './data-engine'
22
33class NestedSort {
4-
54 /**
65 * @constructor
76 * @param {object } [actions={}]
@@ -28,7 +27,7 @@ class NestedSort {
2827 this . data = data
2928 this . selector = el
3029 this . sortableList = null
31- this . placeholderUl = null
30+ this . placeholderList = null
3231 this . placeholderInUse = null
3332 this . draggedNode = null
3433 this . targetedNode = null
@@ -44,26 +43,22 @@ class NestedSort {
4443 X : null ,
4544 Y : null ,
4645 }
47-
4846 this . distances = {
4947 droppingEdge,
5048 droppingEdgeNegative : droppingEdge * - 1 ,
5149 mouseTo : {
5250 targetedElTop : undefined ,
5351 } ,
5452 }
55-
5653 this . dimensions = {
5754 targetedEl : {
5855 H : undefined ,
5956 } ,
6057 }
61-
6258 this . cursor = {
6359 X : null ,
6460 Y : null ,
6561 }
66-
6762 this . classNames = {
6863 dragged : 'ns-dragged' ,
6964 placeholder : 'ns-placeholder' ,
@@ -76,15 +71,25 @@ class NestedSort {
7671 dragend : this . onDragEnd . bind ( this ) ,
7772 drop : this . onDrop . bind ( this ) ,
7873 }
79-
8074 const intNestingLevels = parseInt ( nestingLevels )
8175 this . nestingLevels = isNaN ( intNestingLevels ) ? - 1 : intNestingLevels // values less than 0 mean infinite levels of nesting
76+ this . listInterface = this . getListInterface ( )
8277
8378 this . maybeInitDataDom ( )
8479 this . addListAttributes ( )
8580 if ( init ) this . initDragAndDrop ( )
8681 }
8782
83+ getListInterface ( ) {
84+ if ( Array . isArray ( this . data ) && this . data . length ) return HTMLOListElement
85+
86+ const el = this . selector instanceof HTMLElement
87+ ? this . selector
88+ : document . querySelector ( this . selector )
89+
90+ return el instanceof HTMLOListElement ? HTMLOListElement : HTMLUListElement
91+ }
92+
8893 getDataEngine ( ) {
8994 if ( this . dataEngine instanceof DataEngine ) {
9095 return this . dataEngine
@@ -107,14 +112,20 @@ class NestedSort {
107112 wrapper . appendChild ( list )
108113 }
109114
115+ getListTagName ( ) {
116+ return this . listInterface === HTMLOListElement ? 'ol' : 'ul'
117+ }
118+
110119 getSortableList ( ) {
111- if ( this . sortableList instanceof HTMLUListElement ) return this . sortableList
120+ if ( this . sortableList instanceof this . listInterface ) return this . sortableList
112121
113- if ( this . selector instanceof HTMLUListElement ) {
122+ if ( this . selector instanceof this . listInterface ) {
114123 this . sortableList = this . selector
115124 } else {
116125 const list = document . querySelector ( this . selector )
117- this . sortableList = list . nodeName === 'UL' ? list : list . querySelector ( 'ul' )
126+ this . sortableList = list instanceof this . listInterface
127+ ? list
128+ : list . querySelector ( this . getListTagName ( ) )
118129 }
119130
120131 return this . sortableList
@@ -124,8 +135,8 @@ class NestedSort {
124135 const list = this . getSortableList ( )
125136
126137 list . classList . add ( ...this . listClassNames . concat ( this . mainListClassName ) )
127- list . querySelectorAll ( 'ul' ) . forEach ( ul => {
128- ul . classList . add ( ...this . listClassNames )
138+ list . querySelectorAll ( this . getListTagName ( ) ) . forEach ( l => {
139+ l . classList . add ( ...this . listClassNames )
129140 } )
130141
131142 list . querySelectorAll ( 'li' ) . forEach ( li => {
@@ -187,7 +198,7 @@ class NestedSort {
187198
188199 canBeTargeted ( el ) {
189200 if ( ! this . draggedNode || this . draggedNode === el ) return false
190- return el . nodeName === 'LI' || ( el . nodeName === 'UL' && el . classList . contains ( this . classNames . placeholder ) )
201+ return el . nodeName === 'LI' || ( el instanceof this . listInterface && el . classList . contains ( this . classNames . placeholder ) )
191202 }
192203
193204 onDragStart ( e ) {
@@ -237,7 +248,7 @@ class NestedSort {
237248 getDropLocation ( ) {
238249 if ( this . canBeDropped ( ) ) {
239250 if ( this . targetedNode . nodeName === 'LI' && ! this . cursorIsIndentedEnough ( ) ) return 'before'
240- else if ( this . targetedNode . nodeName === 'UL' ) return 'inside'
251+ else if ( this . targetedNode instanceof this . listInterface ) return 'inside'
241252 }
242253 }
243254
@@ -309,7 +320,8 @@ class NestedSort {
309320 }
310321
311322 targetedNodeIsPlaceholder ( ) {
312- return this . targetedNode . nodeName === 'UL' && this . targetedNode . classList . contains ( this . classNames . placeholder )
323+ return this . targetedNode instanceof this . listInterface
324+ && this . targetedNode . classList . contains ( this . classNames . placeholder )
313325 }
314326
315327 getTargetedNodeDepth ( ) {
@@ -318,7 +330,7 @@ class NestedSort {
318330 const list = this . getSortableList ( )
319331
320332 while ( list !== el . parentElement ) {
321- if ( el . parentElement . nodeName === 'UL' ) depth ++
333+ if ( el . parentElement instanceof this . listInterface ) depth ++
322334 el = el . parentElement
323335 }
324336
@@ -345,7 +357,7 @@ class NestedSort {
345357 }
346358 } else if ( this . targetedNode !== this . draggedNode
347359 && this . targetedNode . nodeName === 'LI'
348- && ! this . targetedNode . querySelectorAll ( 'ul' ) . length
360+ && ! this . targetedNode . querySelectorAll ( this . getListTagName ( ) ) . length
349361 && ! this . nestingThresholdReached ( ) ) {
350362 actions . push ( 'add' )
351363 }
@@ -376,7 +388,7 @@ class NestedSort {
376388
377389 targetNodeIsListWithItems ( ) {
378390 return this . targetNodeIsIdentified ( )
379- && this . targetedNode . nodeName === 'UL'
391+ && this . targetedNode instanceof this . listInterface
380392 && this . targetedNode . querySelectorAll ( 'li' ) . length
381393 }
382394
@@ -388,7 +400,7 @@ class NestedSort {
388400 }
389401
390402 cleanupPlaceholderLists ( ) {
391- this . getSortableList ( ) . querySelectorAll ( 'ul' ) . forEach ( ul => {
403+ this . getSortableList ( ) . querySelectorAll ( this . getListTagName ( ) ) . forEach ( ul => {
392404 if ( ! ul . querySelectorAll ( 'li' ) . length ) {
393405 ul . remove ( )
394406 } else if ( ul . classList . contains ( this . classNames . placeholder ) ) {
@@ -400,12 +412,12 @@ class NestedSort {
400412 }
401413
402414 initPlaceholderList ( ) {
403- this . placeholderUl = document . createElement ( 'ul' )
404- this . placeholderUl . classList . add ( this . classNames . placeholder , ...this . listClassNames )
415+ this . placeholderList = document . createElement ( this . getListTagName ( ) )
416+ this . placeholderList . classList . add ( this . classNames . placeholder , ...this . listClassNames )
405417 }
406418
407419 getPlaceholderList ( ) {
408- this . placeholderInUse = this . placeholderUl . cloneNode ( true )
420+ this . placeholderInUse = this . placeholderList . cloneNode ( true )
409421 return this . placeholderInUse
410422 }
411423}
0 commit comments