X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/128f310047ce6d3dc7a42fa2384a1fe9bf88a36d..4180aaea33e9ff3bb35f3fa6cf91651a6225f7ad:/SL/ClientJS.pm diff --git a/SL/ClientJS.pm b/SL/ClientJS.pm index 99598afc6..51f663fa5 100644 --- a/SL/ClientJS.pm +++ b/SL/ClientJS.pm @@ -9,10 +9,13 @@ use SL::JSON (); use Rose::Object::MakeMethods::Generic ( - 'scalar --get_set_init' => [ qw(_actions) ], + 'scalar --get_set_init' => [ qw(_actions _flash _error) ], ); my %supported_methods = ( + # ## Non-jQuery methods ## + flash => 2, # display_flash(, ) + # ## jQuery basics ## # Basic effects @@ -78,6 +81,7 @@ my %supported_methods = ( 'jstree:reopen' => 1, # Modifying nodes + 'jstree:create_node' => 4, 'jstree:rename_node' => 3, 'jstree:delete_node' => 2, 'jstree:move_node' => 5, @@ -123,8 +127,18 @@ sub init__actions { return []; } +sub init__flash { + return {}; +} + +sub init__error { + return ''; +} + sub to_json { my ($self) = @_; + + return SL::JSON::to_json({ error => $self->_error }) if $self->_error; return SL::JSON::to_json({ eval_actions => $self->_actions }); } @@ -144,6 +158,29 @@ sub jstree { return $self; } +sub flash { + my ($self, $type, @messages) = @_; + + my $message = join ' ', grep { $_ } @messages; + + if (!$self->_flash->{$type}) { + $self->_flash->{$type} = [ 'flash', $type, $message ]; + push @{ $self->_actions }, $self->_flash->{$type}; + } else { + $self->_flash->{$type}->[-1] .= ' ' . $message; + } + + return $self; +} + +sub error { + my ($self, @messages) = @_; + + $self->_error(join ' ', grep { $_ } ($self->_error, @messages)); + + return $self; +} + 1; __END__ @@ -301,6 +338,29 @@ Instead of: The first variation is obviously better suited for chaining. +Additional functions: + +=over 4 + +=item C + +Display a C<$message> in the flash of type C<$type>. Multiple calls of +C on the same C<$self> will be merged by type. + +On the client side the flash of this type will be cleared before the +message is shown. + +=item C + +Causes L (and therefore L) to output a JSON object +that only contains an C field set to this C<$message>. The +client will then show the message in the 'error' flash. + +The messages of multiple calls of C on the same C<$self> will +be merged. + +=back + =head2 JQUERY FUNCTIONS The following jQuery functions are supported: