ClientJS: AJAX-Form-Submit mit jQuery-Form-Plugin
[kivitendo-erp.git] / SL / ClientJS.pm
index 96132cf..204e320 100644 (file)
@@ -70,10 +70,18 @@ my %supported_methods = (
   # Form Events
   focus        => 1,
 
-  # ## jqModal plugin ##
+  # Generic Event Handling ## pattern: $(<TARGET>).<FUNCTION>(<ARG1>, kivi.get_function_by_name(<ARG2>))
+  on           => 3,
+  off          => 3,
+  one          => 3,
+
+  # ## jQuery UI dialog plugin ## pattern: $(<TARGET>).dialog('<FUNCTION>')
 
   # Closing and removing the popup
-  jqmClose               => 1,
+  'dialog:close'         => 1,
+
+  # ## jQuery Form plugin ##
+  'ajaxForm'             => 1, # pattern: $(<TARGET>).ajaxForm({ success: eval_json_result })
 
   # ## jstree plugin ## pattern: $.jstree._reference($(<TARGET>)).<FUNCTION>(<ARGS>)
 
@@ -103,6 +111,8 @@ my %supported_methods = (
 
   # ## other stuff ##
   redirect_to            => 1,  # window.location.href = <TARGET>
+
+  reinit_widgets         => 0,  # kivi.reinit_widgets()
 );
 
 sub AUTOLOAD {
@@ -125,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 ];
@@ -168,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' });
 }
 
@@ -177,6 +188,12 @@ sub jstree {
   return $self;
 }
 
+sub dialog {
+  my ($self) = @_;
+  $self->{_prefix} = 'dialog:';
+  return $self;
+}
+
 sub flash {
   my ($self, $type, @messages) = @_;
 
@@ -254,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);
 
@@ -316,6 +336,14 @@ to the following:
 
   $controller->render(\$self->to_json, { type => 'json' });
 
+=item C<dialog>
+
+Tells C<$self> that the next action is to be called on a jQuery UI
+dialog instance, e.g. one opened by C<kivi.popup_dialog()>. For
+example:
+
+  $js->dialog->close('#jqueryui_popup_dialog');
+
 =item C<jstree>
 
 Tells C<$self> that the next action is to be called on a jstree
@@ -461,6 +489,15 @@ C<data>, C<removeData>
 
 C<focus>
 
+=item Generic Event Handlers
+
+C<on>, C<off>, C<one>
+
+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