X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FClientJS.pm;h=204e3203b4339d0fd346fc3b1bb2289578ac4764;hb=c00f1e29f10d6b371dffc401f6e2c5d11eeb16c5;hp=51f663fa52c50b7db1941c0d94037c0cdbdcdff2;hpb=001c83b188e6a6394761c217d28984553f351c00;p=kivitendo-erp.git diff --git a/SL/ClientJS.pm b/SL/ClientJS.pm index 51f663fa5..204e3203b 100644 --- a/SL/ClientJS.pm +++ b/SL/ClientJS.pm @@ -14,7 +14,7 @@ use Rose::Object::MakeMethods::Generic my %supported_methods = ( # ## Non-jQuery methods ## - flash => 2, # display_flash(, ) + flash => 2, # kivi.display_flash(, ) # ## jQuery basics ## @@ -58,6 +58,11 @@ my %supported_methods = ( removeProp => 2, val => 2, + # Class attribute + addClass => 2, + removeClass => 2, + toggleClass => 2, + # Data storage data => 3, removeData => 2, @@ -65,6 +70,19 @@ my %supported_methods = ( # Form Events focus => 1, + # Generic Event Handling ## pattern: $().(, kivi.get_function_by_name()) + on => 3, + off => 3, + one => 3, + + # ## jQuery UI dialog plugin ## pattern: $().dialog('') + + # Closing and removing the popup + 'dialog:close' => 1, + + # ## jQuery Form plugin ## + 'ajaxForm' => 1, # pattern: $().ajaxForm({ success: eval_json_result }) + # ## jstree plugin ## pattern: $.jstree._reference($()).() # Operations on the whole tree @@ -90,6 +108,11 @@ 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 = + + reinit_widgets => 0, # kivi.reinit_widgets() ); sub AUTOLOAD { @@ -112,10 +135,10 @@ sub action { croak "Unsupported jQuery action: $method" unless defined $num_args; croak "Parameter count mismatch for $method(actual: " . scalar(@args) . " wanted: $num_args)" if scalar(@args) != $num_args; - if ($num_args) { - # Force flattening from SL::Presenter::EscapedText: "" . $... - $args[0] = "" . $args[0]; - $args[0] =~ s/^\s+//; + foreach my $idx (0..$num_args - 1) { + # Force flattening from SL::Presenter::EscapedText and trim leading whitespace for scalars + $args[$idx] = "" . $args[$idx] if ref($args[$idx]) eq 'SL::Presenter::EscapedText'; + $args[$idx] =~ s/^\s+// if !ref($args[$idx]); } push @{ $self->_actions }, [ $method, @args ]; @@ -123,6 +146,12 @@ sub action { return $self; } +sub action_if { + my ($self, $condition, @args) = @_; + + return $condition ? $self->action(@args) : $self; +} + sub init__actions { return []; } @@ -149,6 +178,7 @@ sub to_array { sub render { my ($self, $controller) = @_; + $self->reinit_widgets if $::request->presenter->need_reinit_widgets; return $controller->render(\$self->to_json, { type => 'json' }); } @@ -158,6 +188,12 @@ sub jstree { return $self; } +sub dialog { + my ($self) = @_; + $self->{_prefix} = 'dialog:'; + return $self; +} + sub flash { my ($self, $type, @messages) = @_; @@ -198,12 +234,12 @@ with jQuery First some JavaScript code: // In the client generate an AJAX request whose 'success' handler - // calls "eval_json_response(data)": + // calls "eval_json_result(data)": var data = { action: "SomeController/the_action", id: $('#some_input_field').val() }; - $.post("controller.pl", data, eval_json_response); + $.post("controller.pl", data, eval_json_result); Now some Perl code: @@ -235,6 +271,9 @@ Now some Perl code: $js->jstree->rename_node('#tb-' . $text_block->id, $text_block->title) ->jstree->select_node('#tb-' . $text_block->id); + # Close a popup opened by kivi.popup_dialog(): + $js->dialog->close('#jqueryui_popup_dialog'); + # Finally render the JSON response: $self->render($js); @@ -259,7 +298,7 @@ There are three things that need to be done for this to work: =item 1. The "client_js.js" has to be loaded before the AJAX request is started. -=item 2. The client code needs to call C with the result returned from the server. +=item 2. The client code needs to call C with the result returned from the server. =item 3. The server must use this module. @@ -297,6 +336,14 @@ to the following: $controller->render(\$self->to_json, { type => 'json' }); +=item C + +Tells C<$self> that the next action is to be called on a jQuery UI +dialog instance, e.g. one opened by C. For +example: + + $js->dialog->close('#jqueryui_popup_dialog'); + =item C Tells C<$self> that the next action is to be called on a jstree @@ -338,7 +385,38 @@ Instead of: The first variation is obviously better suited for chaining. -Additional functions: +=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 @@ -347,8 +425,8 @@ Additional functions: 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. +On the client side the flashes of all types will be cleared after each +successful ClientJS call that did not end with C<$js-Eerror(...)>. =item C @@ -359,6 +437,14 @@ 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 @@ -403,6 +489,15 @@ C, C C +=item Generic Event Handlers + +C, C, C + +These attach/detach event listeners to specific selectors. The first +argument is the selector, the second the name of the events and the +third argument is the name of the handler function. That function must +already exist when the handler is added. + =back =head2 JSTREE JQUERY PLUGIN