@@ -602,17 +602,78 @@ Craft.NestedElementManager = Garnish.Base.extend(
602602 }
603603 }
604604
605+ const destructiveGroup = actionDisclosure . getFirstDestructiveGroup ( ) ;
606+ let moveUpButton , moveDownButton , duplicateButton ;
607+
608+ const $li = $element . parent ( ) ;
609+ const getPrev = ( ) => $li . prev ( 'li' ) ;
610+ const getNext = ( ) => $li . next ( 'li' ) ;
611+
612+ if ( this . settings . sortable ) {
613+ this . elementSort . addItems ( $li ) ;
614+
615+ const ul = actionDisclosure . addGroup ( null , true , destructiveGroup ) ;
616+
617+ // Move up/forward
618+ moveUpButton = actionDisclosure . addItem (
619+ {
620+ icon : async ( ) =>
621+ await Craft . ui . icon (
622+ this . settings . showInGrid
623+ ? Craft . orientation === 'ltr'
624+ ? 'arrow-left'
625+ : 'arrow-right'
626+ : 'arrow-up'
627+ ) ,
628+ label : this . settings . showInGrid
629+ ? Craft . t ( 'app' , 'Move forward' )
630+ : Craft . t ( 'app' , 'Move up' ) ,
631+ onActivate : ( ) => {
632+ const $prev = getPrev ( ) ;
633+ if ( $prev . length ) {
634+ $li . insertBefore ( $prev ) ;
635+ this . onSortChange ( $li ) ;
636+ }
637+ } ,
638+ } ,
639+ ul
640+ ) ;
641+
642+ // Move down/backward
643+ moveDownButton = actionDisclosure . addItem (
644+ {
645+ icon : async ( ) =>
646+ await Craft . ui . icon (
647+ this . settings . showInGrid
648+ ? Craft . orientation === 'ltr'
649+ ? 'arrow-right'
650+ : 'arrow-left'
651+ : 'arrow-down'
652+ ) ,
653+ label : this . settings . showInGrid
654+ ? Craft . t ( 'app' , 'Move backward' )
655+ : Craft . t ( 'app' , 'Move down' ) ,
656+ onActivate : ( ) => {
657+ const $next = getNext ( ) ;
658+ if ( $next . length ) {
659+ $li . insertAfter ( $next ) ;
660+ this . onSortChange ( $li ) ;
661+ }
662+ } ,
663+ } ,
664+ ul
665+ ) ;
666+ }
667+
605668 const duplicatable = Garnish . hasAttr ( $element , 'data-duplicatable' ) ;
606669 const copyable = Garnish . hasAttr ( $element , 'data-copyable' ) ;
607670
608671 if ( duplicatable || copyable ) {
609- const destructiveGroup =
610- actionDisclosure . getFirstDestructiveGroup ( ) ;
611672 const ul = actionDisclosure . addGroup ( null , true , destructiveGroup ) ;
612673
613674 if ( duplicatable ) {
614675 // Duplicate
615- const duplicateButton = actionDisclosure . addItem (
676+ duplicateButton = actionDisclosure . addItem (
616677 {
617678 icon : async ( ) => await Craft . ui . icon ( 'clone' ) ,
618679 label : Craft . t ( 'app' , 'Duplicate' ) ,
@@ -622,14 +683,6 @@ Craft.NestedElementManager = Garnish.Base.extend(
622683 } ,
623684 ul
624685 ) ;
625-
626- actionDisclosure . on ( 'show' , ( ) => {
627- if ( this . canCreate ( ) ) {
628- actionDisclosure . showItem ( duplicateButton ) ;
629- } else {
630- actionDisclosure . hideItem ( duplicateButton ) ;
631- }
632- } ) ;
633686 }
634687
635688 if ( copyable ) {
@@ -652,95 +705,39 @@ Craft.NestedElementManager = Garnish.Base.extend(
652705 ) ;
653706 }
654707 }
655- }
656- } , 1 ) ;
657708
658- if ( this . settings . sortable ) {
659- this . elementSort . addItems ( $element . parent ( ) ) ;
660- }
709+ if ( Garnish . hasAttr ( $element , 'data-deletable' ) ) {
710+ const ul = actionDisclosure . addGroup ( ) ;
711+ actionDisclosure . addItem (
712+ {
713+ icon : async ( ) => await Craft . ui . icon ( 'trash' ) ,
714+ label : this . settings . deleteLabel || Craft . t ( 'app' , 'Delete' ) ,
715+ destructive : true ,
716+ onActivate : ( ) => {
717+ if ( confirm ( this . settings . deleteConfirmationMessage ) ) {
718+ this . deleteElement ( $element ) ;
719+ }
720+ } ,
721+ } ,
722+ ul
723+ ) ;
724+ }
661725
662- const $actionMenuBtn = $element . find ( '.action-btn' ) ;
663- if ( $actionMenuBtn . length > 0 ) {
664- const disclosureMenu = $actionMenuBtn
665- . disclosureMenu ( )
666- . data ( 'disclosureMenu' ) ;
667- const $actionMenu = disclosureMenu . $container ;
668-
669- if ( this . settings . sortable ) {
670- const $container = $element . parents ( '.elements' ) ;
671- const $parent = $element . parent ( ) ;
672-
673- // show/hide move up/down buttons
674- disclosureMenu . on ( 'show' , ( ) => {
675- if ( $parent . prev ( 'li' ) . length ) {
676- $actionMenu
677- . find ( 'button[data-move-action=up]:first' )
678- . parent ( )
679- . removeClass ( 'hidden' ) ;
680- } else {
681- $actionMenu
682- . find ( 'button[data-move-action=up]:first' )
683- . parent ( )
684- . addClass ( 'hidden' ) ;
685- }
686- if ( $parent . next ( 'li' ) . length ) {
687- $actionMenu
688- . find ( 'button[data-move-action=down]:first' )
689- . parent ( )
690- . removeClass ( 'hidden' ) ;
691- } else {
692- $actionMenu
693- . find ( 'button[data-move-action=down]:first' )
694- . parent ( )
695- . addClass ( 'hidden' ) ;
726+ actionDisclosure . on ( 'show' , ( ) => {
727+ if ( moveUpButton ) {
728+ actionDisclosure . toggleItem ( moveUpButton , getPrev ( ) . length ) ;
696729 }
697- } ) ;
698730
699- const $moveUpBtn = $element
700- . find ( '.action-btn' )
701- . data ( 'disclosureMenu' )
702- ?. $container . find ( '[data-move-action="up"]' ) ;
703- if ( $moveUpBtn ?. length ) {
704- this . addListener ( $moveUpBtn , 'activate' , ( ) => {
705- let $prev = $parent . prev ( 'li' ) ;
706- if ( $prev . length ) {
707- $parent . insertBefore ( $prev ) ;
708- this . onSortChange ( $parent ) ;
709- }
710- } ) ;
711- }
712- const $moveDownBtn = $element
713- . find ( '.action-btn' )
714- . data ( 'disclosureMenu' )
715- ?. $container . find ( '[data-move-action="down"]' ) ;
716- if ( $moveDownBtn ?. length ) {
717- this . addListener ( $moveDownBtn , 'activate' , ( ) => {
718- let $next = $parent . next ( 'li' ) ;
719- if ( $next . length ) {
720- $parent . insertAfter ( $next ) ;
721- this . onSortChange ( $parent ) ;
722- }
723- } ) ;
724- }
725- }
731+ if ( moveDownButton ) {
732+ actionDisclosure . toggleItem ( moveDownButton , getNext ( ) . length ) ;
733+ }
726734
727- if ( Garnish . hasAttr ( $element , 'data-deletable' ) ) {
728- const ul = disclosureMenu . addGroup ( ) ;
729- disclosureMenu . addItem (
730- {
731- icon : async ( ) => await Craft . ui . icon ( 'trash' ) ,
732- label : this . settings . deleteLabel || Craft . t ( 'app' , 'Delete' ) ,
733- destructive : true ,
734- onActivate : ( ) => {
735- if ( confirm ( this . settings . deleteConfirmationMessage ) ) {
736- this . deleteElement ( $element ) ;
737- }
738- } ,
739- } ,
740- ul
741- ) ;
735+ if ( duplicateButton ) {
736+ actionDisclosure . toggleItem ( duplicateButton , this . canCreate ( ) ) ;
737+ }
738+ } ) ;
742739 }
743- }
740+ } , 1 ) ;
744741 } ,
745742
746743 createElementEditor ( $element ) {
0 commit comments