X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FClientJS.pm;h=d58905e75986cb90178a56a7f3a6a30d3164dc0e;hb=e28d95b4826728e7490d708b40b2514b2fe88a34;hp=6f9ce5adbcd3311126bcd4153fed865dd2469f9c;hpb=6ca2197818029fad5253edd8f08988ddc66aa359;p=kivitendo-erp.git diff --git a/SL/ClientJS.pm b/SL/ClientJS.pm index 6f9ce5adb..d58905e75 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 @@ -55,10 +58,18 @@ my %supported_methods = ( removeProp => 2, val => 2, + # Class attribute + addClass => 2, + removeClass => 2, + toggleClass => 2, + # Data storage data => 3, removeData => 2, + # Form Events + focus => 1, + # ## jstree plugin ## pattern: $.jstree._reference($()).() # Operations on the whole tree @@ -75,6 +86,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, @@ -83,6 +95,9 @@ my %supported_methods = ( 'jstree:select_node' => 2, # $.jstree._reference($()).(, true) 'jstree:deselect_node' => 2, 'jstree:deselect_all' => 1, + + # ## other stuff ## + redirect_to => 1, # window.location.href = ); sub AUTOLOAD { @@ -93,6 +108,11 @@ sub AUTOLOAD { my $method = $AUTOLOAD; $method =~ s/.*:://; return if $method eq 'DESTROY'; + return $self->action($method, @args); +} + +sub action { + my ($self, $method, @args) = @_; $method = (delete($self->{_prefix}) || '') . $method; my $num_args = $supported_methods{$method}; @@ -111,12 +131,28 @@ sub AUTOLOAD { return $self; } +sub action_if { + my ($self, $condition, @args) = @_; + + return $condition ? $self->action(@args) : $self; +} + 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 }); } @@ -136,6 +172,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__ @@ -263,6 +322,98 @@ instance. For example: =head1 FUNCTIONS EVALUATED ON THE CLIENT SIDE +=head2 GENERIC FUNCTION + +All of the following functions can be invoked in two ways: either by +calling the function name directly on C<$self> or by calling +L with the function name as the first parameter. Therefore +the following two calls are identical: + + $js->insertAfter($html, '#some-id'); + $js->action('insertAfter', $html, '#some-id'); + +The second form, calling L, is more to type but can be useful +in situations in which you have to call one of two functions depending +on context. For example, when you want to insert new code in a +list. If the list is empty you might have to use C, if it +isn't you might have to use C. Example: + + my $html = $self->render(...); + $js->action($list_is_empty ? 'appendTo' : 'insertAfter', $html, '#text-block-' . ($list_is_empty ? 'list' : $self->text_block->id)); + +Instead of: + + my $html = $self->render(...); + if ($list_is_empty) { + $js->appendTo($html, '#text-block-list'); + } else { + $js->insertAfter($html, '#text-block-' . $self->text_block->id); + } + +The first variation is obviously better suited for chaining. + +=over 4 + +=item C + +Call the function with the name C<$method> on C<$self> with arguments +C<@args>. Returns the return value of the actual function +called. Useful for chaining (see above). + +=item C + +Call the function with the name C<$method> on C<$self> with arguments +C<@args> if C<$condition> is trueish. Does nothing otherwise. + +Returns the return value of the actual function called if +C<$condition> is trueish and C<$self> otherwise. Useful for chaining +(see above). + +This function is equivalent to the following: + + if ($condition) { + $obj->$method(@args); + } + +But it is easier to integrate into a method call chain, e.g.: + + $js->html('#content', $html) + ->action_if($item->is_flagged, 'toggleClass', '#marker', 'flagged') + ->render($self); + +=back + +=head2 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. + +=item C + +Redirects the browser window to the new URL by setting the JavaScript +property C. Note that +L is AJAX aware and uses this +function if the current request is an AJAX request as determined by +L. + +=back + =head2 JQUERY FUNCTIONS The following jQuery functions are supported: @@ -301,6 +452,10 @@ C, C, C, C, C C, C +=item Form Events + +C + =back =head2 JSTREE JQUERY PLUGIN