[Rt-commit] rt branch, 4.0/menu-convenience-methods, updated. rt-4.0.4-116-g52f78e5
Thomas Sibley
trs at bestpractical.com
Mon Aug 27 20:54:39 EDT 2012
The branch, 4.0/menu-convenience-methods has been updated
via 52f78e5fe9de91a53eb403a5c869a5296000a164 (commit)
via 0377588e0b08be82903374573990fd489d6f2b4d (commit)
from 366d8f4b26595edecebac281f8e2f10df09118fd (commit)
Summary of changes:
lib/RT/Interface/Web/Menu.pm | 38 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)
- Log -----------------------------------------------------------------
commit 0377588e0b08be82903374573990fd489d6f2b4d
Author: Thomas Sibley <trs at bestpractical.com>
Date: Mon Aug 27 17:37:50 2012 -0700
The new child effectively replaces the current child in the sort_order
Adding +1 meant the new child and the current child ended up with the
same sort_order instead of the new child taking the current child's
place.
diff --git a/lib/RT/Interface/Web/Menu.pm b/lib/RT/Interface/Web/Menu.pm
index 4d8507e..5d0f710 100644
--- a/lib/RT/Interface/Web/Menu.pm
+++ b/lib/RT/Interface/Web/Menu.pm
@@ -340,8 +340,8 @@ sub add_after {
=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.
+Called on a child, inserts a new menu item at the child's location and shifts
+the child and 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.
@@ -356,7 +356,7 @@ sub add_before {
my $sort_order;
for my $contemporary ($parent->children) {
if ( $contemporary->key eq $self->key ) {
- $sort_order = $contemporary->sort_order + 1;
+ $sort_order = $contemporary->sort_order;
}
if ( $sort_order ) {
$contemporary->sort_order( $contemporary->sort_order + 1 );
commit 52f78e5fe9de91a53eb403a5c869a5296000a164
Author: Thomas Sibley <trs at bestpractical.com>
Date: Mon Aug 27 17:53:26 2012 -0700
Refactor the guts of add_after and add_before into a single private method
diff --git a/lib/RT/Interface/Web/Menu.pm b/lib/RT/Interface/Web/Menu.pm
index 5d0f710..d4b285b 100644
--- a/lib/RT/Interface/Web/Menu.pm
+++ b/lib/RT/Interface/Web/Menu.pm
@@ -322,21 +322,7 @@ 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 );
-}
+sub add_after { shift->_insert_sibling("after", @_) }
=head2 add_before
@@ -350,13 +336,27 @@ Takes all the regular arguments to L<child>.
=cut
-sub add_before {
+sub add_before { shift->_insert_sibling("before", @_) }
+
+sub _insert_sibling {
my $self = shift;
+ my $where = shift;
my $parent = $self->parent;
my $sort_order;
for my $contemporary ($parent->children) {
if ( $contemporary->key eq $self->key ) {
- $sort_order = $contemporary->sort_order;
+ if ($where eq "before") {
+ # Bump the current child and the following
+ $sort_order = $contemporary->sort_order;
+ }
+ elsif ($where eq "after") {
+ # Leave the current child along, bump the rest
+ $sort_order = $contemporary->sort_order + 1;
+ next;
+ }
+ else {
+ # never set $sort_order, act no differently than ->child()
+ }
}
if ( $sort_order ) {
$contemporary->sort_order( $contemporary->sort_order + 1 );
-----------------------------------------------------------------------
More information about the Rt-commit
mailing list