Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Navigation menu editor improvements
 - Updates are visible immediately upon successful save (closes #37)
 - Editor shows active menu items in the current menu order
  • Loading branch information
jberger committed Oct 14, 2014
1 parent 8c7c782 commit b0a0abb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,8 @@ Revision history for Perl module Galileo
0.0037
- Fix installation bug causing installation of version 1 rather than the most current
- Removed ability to upgrade from unversioned schemas (pre version 0.012)
- Navigation menu changes are visible immediately upon saving
- Navigation menu editor displays active items in current menu order

0.036 2014-08-31
- Fixed setup bug introduced by Mojolicious v5.30 (fix for __DATA__ handle)
Expand Down
29 changes: 19 additions & 10 deletions lib/Galileo/Menu.pm
Expand Up @@ -2,34 +2,41 @@ package Galileo::Menu;
use Mojo::Base 'Mojolicious::Controller';

use Mojo::JSON 'j';
use Mojo::ByteStream 'b';

sub edit {
my $self = shift;
my $name = 'main';
my $schema = $self->schema;

my @active = @{ j(
$schema->resultset('Menu')->single({name => $name})->list
) };
my %active =
map { $_ => 1 }
@{ j(
$schema->resultset('Menu')->single({name => $name})->list
) };

my ($active, $inactive) = ( '', '' );
map { $_ => '' }
@active;

my @inactive;

my @pages = $schema->resultset('Page')->all;
for my $page ( @pages ) {
next unless $page;
my $name = $page->name;
my $id = $page->page_id;
next if $name eq 'home';
exists $active{$id} ? $active : $inactive
.= sprintf qq{<li id="pages-%s"><span class="label label-info">%s</span></li>\n}, $id, $page->title;
my $li = sprintf qq{<li id="pages-%s"><span class="label label-info">%s</span></li>\n}, $id, $page->title;
if (exists $active{$id}) {
$active{$id} = $li;
} else {
push @inactive, $li;
}
}

$self->title( 'Setup Main Navigation Menu' );
$self->content_for( banner => 'Setup Main Navigation Menu' );
$self->render(
active => Mojo::ByteStream->new( $active ),
inactive => Mojo::ByteStream->new( $inactive ),
active => Mojo::ByteStream->new( join '', @active{@active} ),
inactive => Mojo::ByteStream->new( join '', @inactive ),
);
}

Expand All @@ -55,9 +62,11 @@ sub store {
);

$self->memorize->expire($name);
my $content = $self->include('nav_menu') || '';
$self->send({ json => {
message => 'Changes saved',
success => \1,
content => b($content)->squish,
} });
});
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Galileo/files/templates/menu/edit.html.ep
Expand Up @@ -5,12 +5,18 @@
% end

%= javascript begin
function onSuccess(reply) {
content = $(reply.content);
id = content.attr('id');
$('#'+id).replaceWith(content);
}

function saveButton() {
var data = {
name : "main",
list : $("#list-active-pages").sortable('toArray')
};
sendViaWS("<%= url_for('storemenu')->to_abs %>", data);
sendViaWS("<%= url_for('storemenu')->to_abs %>", data, onSuccess);
}

$(function() {
Expand Down
2 changes: 1 addition & 1 deletion lib/Galileo/files/templates/nav_menu.html.ep
Expand Up @@ -2,7 +2,7 @@
% my $menu = schema->resultset('Menu')->single({name => 'main'});
% my @pages = @{ Mojo::JSON->new->decode($menu->list) };
% my $rs = schema->resultset('Page');
<div class="well" style="padding: 8px 0;">
<div class="well" id="nav_menu" style="padding: 8px 0;">
<ul id="main" class="nav nav-list">
<li class="nav-header">Navigation</li>
<li><a href="/">Home</a></li>
Expand Down
19 changes: 17 additions & 2 deletions t/basic.t
Expand Up @@ -4,6 +4,8 @@ use Galileo::DB::Deploy;

use Test::More;
use Test::Mojo;
use Mojo::JSON 'j';
use Mojo::DOM;

my $t = Galileo::DB::Deploy->create_test_object({test => 1});
$t->ua->max_redirects(2);
Expand Down Expand Up @@ -157,9 +159,14 @@ subtest 'Edit Main Navigation Menu' => sub {
$t->websocket_ok('/store/menu')
->send_ok({ json => $data })
->message_ok
->json_message_is( { success => 1, message => 'Changes saved' } )
->json_message_is( '/success' => 1 )
->json_message_is( '/message' => 'Changes saved' )
->json_message_has( '/content' )
->finish_ok;

my @items = Mojo::DOM->new(j($t->message->[1])->{content})->find('#nav_menu li')->all_text->each;
is_deeply \@items, ['Navigation', 'Home'];

# check that item is removed
$t->get_ok('/admin/menu')
->status_is(200)
Expand All @@ -174,15 +181,23 @@ subtest 'Edit Main Navigation Menu' => sub {
$t->websocket_ok('/store/menu')
->send_ok({ json => $data })
->message_ok
->json_message_is( { success => 1, message => 'Changes saved' } )
->json_message_is( '/success' => 1 )
->json_message_is( '/message' => 'Changes saved' )
->json_message_has( '/content' )
->finish_ok;

@items = ();
@items = Mojo::DOM->new(j($t->message->[1])->{content})->find('#nav_menu li')->all_text->each;
is_deeply \@items, ['Navigation', 'Home', 'Syntax', $title];

# check about page is back in nav (same as first test block)
$t->get_ok('/admin/menu')
->status_is(200)
->text_is( 'ul#main > li:nth-of-type(4) > a' => $title )
->text_is( '#list-active-pages > #pages-2 > span' => $title );

my @ids = $t->tx->res->dom->find('#list-active-pages li')->attr('id')->each;
is_deeply \@ids, ['header-active', 'pages-3', 'pages-2'], 'active pages in correct order';
};

subtest 'Administrative Overview: All Users' => sub {
Expand Down

0 comments on commit b0a0abb

Please sign in to comment.