[Rt-commit] rt branch, 4.0/menu-convenience-methods, created. rt-4.0.4-114-g366d8f4

Kevin Falcone falcone at bestpractical.com
Thu Dec 8 13:43:21 EST 2011


The branch, 4.0/menu-convenience-methods has been created
        at  366d8f4b26595edecebac281f8e2f10df09118fd (commit)

- Log -----------------------------------------------------------------
commit c3d8654da94a8aca36ce62b72cdae220c1d5c38d
Author: Kevin Falcone <falcone at bestpractical.com>
Date:   Thu Dec 8 13:29:45 2011 -0500

    Add an add_after method
    
    This allows you to call PageMenu->child('foo') and insert
    a new menu item after it from an extension without doing all the
    sort_order math yourself.

diff --git a/lib/RT/Interface/Web/Menu.pm b/lib/RT/Interface/Web/Menu.pm
index df86400..3edbe97 100644
--- a/lib/RT/Interface/Web/Menu.pm
+++ b/lib/RT/Interface/Web/Menu.pm
@@ -310,4 +310,32 @@ sub children {
     return wantarray ? @kids : \@kids;
 }
 
+=head2 add_after
+
+Called on a child, inserts a new menu item after it and shifts any other
+menu items at this level to the right.
+
+L<child> by default would insert at the end of the list of children, unless you
+did manual sort_order calculations.
+
+Takes all the regular arguments to L<child>.
+
+=cut
+
+sub add_after {
+    my $self = shift;
+    my $parent = $self->parent;
+    my $sort_order;
+    for my $contemporary ($parent->children) {
+        if ( $contemporary->key eq $self->key ) {
+            $sort_order = $contemporary->sort_order + 1;
+            next;
+        }
+        if ( $sort_order ) {
+            $contemporary->sort_order( $contemporary->sort_order + 1 );
+        }
+    }
+    $parent->child( @_, sort_order => $sort_order );
+}
+
 1;

commit 366d8f4b26595edecebac281f8e2f10df09118fd
Author: Kevin Falcone <falcone at bestpractical.com>
Date:   Thu Dec 8 13:31:30 2011 -0500

    Add an add_before method
    
    This is basically a clone of add_after except it doesn't skip to the
    next item before beginning to shift sort order.
    These want to be refactored together somehow.

diff --git a/lib/RT/Interface/Web/Menu.pm b/lib/RT/Interface/Web/Menu.pm
index 3edbe97..4d8507e 100644
--- a/lib/RT/Interface/Web/Menu.pm
+++ b/lib/RT/Interface/Web/Menu.pm
@@ -338,4 +338,31 @@ sub add_after {
     $parent->child( @_, sort_order => $sort_order );
 }
 
+=head2 add_before
+
+Called on a child, inserts a new menu item before it and shifts the other
+menu items at this level to the right.
+
+L<child> by default would insert at the end of the list of children, unless you
+did manual sort_order calculations.
+
+Takes all the regular arguments to L<child>.
+
+=cut
+
+sub add_before {
+    my $self = shift;
+    my $parent = $self->parent;
+    my $sort_order;
+    for my $contemporary ($parent->children) {
+        if ( $contemporary->key eq $self->key ) {
+            $sort_order = $contemporary->sort_order + 1;
+        }
+        if ( $sort_order ) {
+            $contemporary->sort_order( $contemporary->sort_order + 1 );
+        }
+    }
+    $parent->child( @_, sort_order => $sort_order );
+}
+
 1;

-----------------------------------------------------------------------


More information about the Rt-commit mailing list