@@ -193,7 +193,7 @@ class ComponentLine {
193
193
/*foot*/
194
194
this . makeFoot ( foot ) ;
195
195
/*sort*/
196
- this . sortAble ( ) ;
196
+ this . sortable ( ) ;
197
197
}
198
198
199
199
makeHead ( ) {
@@ -308,7 +308,6 @@ class ComponentLine {
308
308
object . DATA . push ( insert ) ;
309
309
object . DATA_INPUT . value = JSON . stringify ( object . DATA ) ;
310
310
object . TBODY_DOM . scrollTop = object . TBODY_DOM . scrollHeight ;
311
- object . SORTABLE . resetListHeight ( ) ;
312
311
} , false ) ;
313
312
this . DOM . getElementsByClassName ( 'JsonTableInsert' ) [ 0 ] . appendChild ( i ) ;
314
313
}
@@ -371,18 +370,21 @@ class ComponentLine {
371
370
tbody . childNodes [ node ] . setAttribute ( 'data-key' , node ) ;
372
371
}
373
372
}
374
-
375
- object . SORTABLE . resetListHeight ( ) ;
376
373
} , false ) ;
377
374
td . appendChild ( D ) ;
378
375
}
379
376
td . style = 'width:50px' ;
380
377
}
381
378
382
- sortAble ( ) {
379
+ sortable ( ) {
383
380
var object = this ;
384
- setTimeout ( function ( ) {
385
- object . SORTABLE = new ComponentSortable ( object . TBODY_DOM ) ;
381
+ this . SORTABLE = new ComponentSortable ( this . TBODY_DOM , function ( sort ) {
382
+ let data = [ ] ;
383
+ sort . forEach ( function ( k ) {
384
+ data . push ( object . DATA [ k ] ) ;
385
+ } ) ;
386
+ object . DATA = data ;
387
+ object . DATA_INPUT . value = JSON . stringify ( object . DATA ) ;
386
388
} ) ;
387
389
}
388
390
}
@@ -519,41 +521,36 @@ class ComponentPlane {
519
521
}
520
522
521
523
class ComponentSortable {
522
- list_height ;
523
- constructor ( list , options ) {
524
+ constructor ( list , callback = null ) {
524
525
this . list = ( typeof list === 'string' )
525
526
? document . querySelector ( list )
526
527
: list ;
527
- this . options = Object . assign ( {
528
+ this . options = {
528
529
animationSpeed : 200 ,
529
530
animationEasing : 'ease-out' ,
530
- } , options || { } ) ;
531
-
531
+ } ;
532
+ this . callback = callback ;
532
533
this . animation = false ;
533
- this . resetListHeight ( ) ;
534
534
this . dragStart = this . dragStart . bind ( this ) ;
535
535
this . dragMove = this . dragMove . bind ( this ) ;
536
536
this . dragEnd = this . dragEnd . bind ( this ) ;
537
537
this . list . addEventListener ( 'touchstart' , this . dragStart , false ) ;
538
538
this . list . addEventListener ( 'mousedown' , this . dragStart , false ) ;
539
539
}
540
540
541
- resetListHeight ( ) {
541
+ reset ( ) {
542
542
this . items = Array . from ( this . list . children ) ;
543
- if ( this . items . length < 1 ) return ;
544
543
if ( this . items [ this . items . length - 1 ] . offsetTop > this . list . offsetHeight ) {
545
- this . list_height = this . list . scrollHeight ;
544
+ this . listHeight = this . list . scrollHeight ;
546
545
} else {
547
- this . list_height = this . items [ this . items . length - 1 ] . offsetTop ;
546
+ this . listHeight = this . items [ this . items . length - 1 ] . offsetTop ;
548
547
}
549
548
}
550
549
551
550
dragStart ( e ) {
551
+ this . reset ( ) ;
552
552
if ( this . animation ) return ;
553
553
if ( this . items . length < 2 ) return ;
554
- if ( e . type === 'mousedown' && e . which !== 1 ) return ;
555
- if ( e . type === 'touchstart' && e . touches . length > 1 ) return ;
556
-
557
554
this . handle = null ;
558
555
559
556
let el = e . target ;
@@ -567,9 +564,7 @@ class ComponentSortable {
567
564
if ( ! this . handle ) return ;
568
565
this . list . style . position = 'relative' ;
569
566
this . item . classList . add ( 'is-dragging' )
570
- if ( ! this . items [ 1 ] ) return ;
571
567
this . itemHeight = this . items [ 1 ] . offsetTop ;
572
- this . listHeight = this . list_height ;
573
568
this . startTouchY = this . getDragY ( e ) ;
574
569
this . startTop = this . item . offsetTop ;
575
570
@@ -583,13 +578,6 @@ class ComponentSortable {
583
578
item . style . zIndex = ( item == this . item ) ? 2 : 1 ;
584
579
} ) ;
585
580
586
- setTimeout ( ( ) => {
587
- this . items . forEach ( item => {
588
- if ( this . item == item ) return ;
589
- item . style . transition = `transform ${ this . options . animationSpeed } ms ${ this . options . animationEasing } ` ;
590
- } )
591
- } ) ;
592
-
593
581
this . positions = this . items . map ( ( item , index ) => index ) ;
594
582
this . position = Math . round ( ( this . startTop / this . listHeight ) * this . items . length ) ;
595
583
@@ -611,10 +599,10 @@ class ComponentSortable {
611
599
this . swapElements ( this . positions , this . position , index ) ;
612
600
this . position = index ;
613
601
} ) ;
614
-
615
602
this . items . forEach ( ( item , index ) => {
616
603
if ( item == this . item ) return ;
617
604
item . style . transform = `translateY(${ this . positions . indexOf ( index ) * this . itemHeight } px)` ;
605
+ item . style . transition = `transform ${ this . options . animationSpeed } ms ${ this . options . animationEasing } ` ;
618
606
} ) ;
619
607
620
608
e . preventDefault ( ) ;
@@ -625,8 +613,8 @@ class ComponentSortable {
625
613
if ( this . items . length < 2 ) return ;
626
614
this . item . style . transition = `all ${ this . options . animationSpeed } ms ${ this . options . animationEasing } ` ;
627
615
this . item . style . transform = `translateY(${ this . position * this . itemHeight } px)` ;
628
-
629
616
this . item . classList . remove ( 'is-dragging' ) ;
617
+ if ( typeof this . callback == 'function' ) this . callback ( this . positions ) ;
630
618
631
619
setTimeout ( ( ) => {
632
620
this . list . style . position = '' ;
@@ -641,9 +629,11 @@ class ComponentSortable {
641
629
item . style . zIndex = '' ;
642
630
} ) ;
643
631
644
- this . positions . map ( i => this . list . appendChild ( this . items [ i ] ) ) ;
645
- this . items = Array . from ( this . list . children ) ;
646
-
632
+ this . positions . map ( i => {
633
+ if ( this . items [ i ] ) {
634
+ this . list . appendChild ( this . items [ i ] ) ;
635
+ }
636
+ } ) ;
647
637
this . animation = false ;
648
638
} , this . options . animationSpeed ) ;
649
639
0 commit comments