[Bps-public-commit] rt-extension-formtools branch, enabled-by, created. 0.07_02-14-g9a22f0e

Ruslan Zakirov ruz at bestpractical.com
Thu Apr 21 06:11:03 EDT 2011


The branch, enabled-by has been created
        at  9a22f0e9cfeeba34cfeee045974bbd93d20f0b2b (commit)

- Log -----------------------------------------------------------------
commit 45938a407df7a3d4a4239361dc2e9cc861b6ab99
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Apr 21 12:40:44 2011 +0400

    introduce internal variable input_type for further dev

diff --git a/html/FormTools/Field b/html/FormTools/Field
index 5abaa5f..1e9c95d 100644
--- a/html/FormTools/Field
+++ b/html/FormTools/Field
@@ -28,12 +28,14 @@ my $NamePrefix = "Object-RT::Ticket-"
                . ($ticket ? $ticket->id : '')
                . "-CustomField-";
 
+my $input_name = '';
 if ( RT::Extension::FormTools::is_core_field($name) ) {
     $field_type = 'core';
     my $res = $m->notes(
         core_fields_on_page => [ @{$m->notes('core_fields_on_page')||[]}, $name ] );
     $field_label ||= $name;
     $default ||= $ticket->$name if $ticket && $ticket->can($name);
+    $input_name = $name;
 } else {
 
     $cf = RT::CustomField->new( $session{'CurrentUser'} );
@@ -51,6 +53,8 @@ if ( RT::Extension::FormTools::is_core_field($name) ) {
         @values = map { $_->Content } @{ $cf->ValuesForObject($ticket)->ItemsArrayRef };
         $default ||= $values[0];
     }
+
+    $input_name = $NamePrefix . $cf->id .'-Value';
 }
 
 
@@ -59,8 +63,8 @@ unless ( $default ) {
     if ( $field_type eq 'core' && $request_args->{$name} ) {
         $default = $request_args->{$name};
     } elsif ( $field_type eq 'custom' &&  $cf->Id ) {
-        $default = ($request_args->{$NamePrefix.$cf->Id."-Value"} || 
-                    $request_args->{$NamePrefix.$cf->Id."-Values"} );
+        $default = ($request_args->{ $input_name } || 
+                    $request_args->{ $input_name .'s' } );
     }
 }
 
@@ -140,9 +144,8 @@ unless ( $default ) {
 
 % } elsif ($render_as =~ /^radio/)  {
 %       my $selected = 0;
-%       my $id = $NamePrefix . $cf->Id;
 % if ($empty_allowed) {
-        <input type="radio" name="<%$id%>-Values" value="" <% !$default ?  'CHECKED' : '' %>><span class="radio-label"><&|/l&>(no value)</&></span>
+        <input type="radio" name="<% $input_name %>s" value="" <% !$default ?  'CHECKED' : '' %>><span class="radio-label"><&|/l&>(no value)</&></span>
 %        if ($render_as =~ /vertical/) { 
             <br/>
 %        }
@@ -150,7 +153,7 @@ unless ( $default ) {
 % }
 % my $CFVs = $cf->Values;
 % while ($CFVs and my $value = $CFVs->Next ) {
-        <input type="radio" name="<%$id%>-Values"  value="<%$value->Name%>" 
+        <input type="radio" name="<% $input_name %>s"  value="<%$value->Name%>" 
 %  if ($default) {
 %       if (ref $default) {
             <% (grep { $_ eq $value->Name} @$default) ? 'CHECKED' : '' %>
@@ -166,10 +169,9 @@ unless ( $default ) {
 
 % }
 % } elsif ($render_as =~  /^check/i)  {
-%       my $id = $NamePrefix . $cf->Id;
 % my $CFVs = $cf->Values;
 % while ($CFVs and my $value = $CFVs->Next ) {
-        <input type="checkbox" name="<%$id%>-Values"  value="<%$value->Name%>" 
+        <input type="checkbox" name="<% $input_name %>s"  value="<%$value->Name%>" 
 %  if ($default) {
 %       if (ref $default) {
             <% (grep { $_ eq $value->Name} @$default) ? 'CHECKED' : '' %>
@@ -189,9 +191,8 @@ unless ( $default ) {
 
 % }
 % } elsif ($render_as =~  /^boolean/i)  {
-%       my $id = $NamePrefix . $cf->Id;
 %       my $value = 'Yes';
-        <input type="checkbox" name="<% $id %>-Values"  value="<% $value %>"
+        <input type="checkbox" name="<% $input_name %>s"  value="<% $value %>"
 % my $checked = 0;
 %  if ($default) {
 %       $checked = ref $default? (grep { $_ eq $value} @$default) : $default eq $value;
@@ -209,7 +210,7 @@ unless ( $default ) {
 % }
 
 % } elsif ($render_as =~  /^date/i)  {
-<& /Elements/SelectDate, Name => $NamePrefix.$cf->Id."-Values", current => 0, 
+<& /Elements/SelectDate, Name => $input_name .'s', current => 0, 
                          ($default ? (Default => $default) : ()) &>
 
 % } elsif ($render_as eq 'readonly') {

commit 66ea4523bd146917a4b0e3f8f9d4676d3a64cd6a
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Apr 21 12:41:37 2011 +0400

    form_tools.js file

diff --git a/html/NoAuth/js/form_tools.js b/html/NoAuth/js/form_tools.js
new file mode 100644
index 0000000..a150498
--- /dev/null
+++ b/html/NoAuth/js/form_tools.js
@@ -0,0 +1,25 @@
+function disable_form_field(disable, selector) {
+    if ( disable ) {
+        jQuery(selector).addClass('hidden').find('input,select').attr('disabled', 'disabled');
+    }
+    else {
+        jQuery(selector).removeClass('hidden');
+        jQuery(selector).find('input,select').filter( function() {
+            return jQuery(this).closest('.hidden').length == 0
+        } ).removeAttr('disabled');
+    }
+}
+
+function should_disable_form_field( fields, values ) {
+    for ( var i = 0; i<fields.length; i++ ) {
+        var field = fields[i];
+        var active = jQuery('input:checked:enabled[name="'+ field +'"], input:checked:enabled[name="'+ field +'s"]').filter(function() {
+            for ( var i = 0; i < values[field].length; i++ ) {
+                if ( this.value == values[field][i] ) { return 1 }
+            }
+            return 0;
+        }).length;
+        if ( active ) return 0;
+    }
+    return 1;
+}

commit 1552db34add4be0e214da0d562bc605bf8534e74
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Apr 21 12:42:31 2011 +0400

    announce JS file in the head

diff --git a/html/Callbacks/FormTools/Elements/Header/Head b/html/Callbacks/FormTools/Elements/Header/Head
new file mode 100644
index 0000000..f29a82c
--- /dev/null
+++ b/html/Callbacks/FormTools/Elements/Header/Head
@@ -0,0 +1 @@
+<script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/NoAuth/js/form_tools.js"></script>

commit 9a22f0e9cfeeba34cfeee045974bbd93d20f0b2b
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Apr 21 14:10:07 2011 +0400

    enabled_by argument for a field

diff --git a/html/FormTools/Field b/html/FormTools/Field
index 1e9c95d..d528484 100644
--- a/html/FormTools/Field
+++ b/html/FormTools/Field
@@ -10,6 +10,8 @@ $empty_allowed => 1
 $show_validation => 0
 $show_label => 1
 $disables => {}
+$enabled_by => []
+$container_selector => undef
 </%args>
 <%init>
 use RT::Extension::FormTools;
@@ -70,16 +72,30 @@ unless ( $default ) {
 
 </%init>
 
-% if ( keys %$disables ) {
+% if ( @$enabled_by ) {
+%    my $escape = sub { my $res = shift; $res =~ s/'/\\'/; "'$res'" };
     <script>
-    function disable_form_field(disable, selector) {
-        if ( disable ) {
-            jQuery(selector).addClass('hidden').find('input').attr('disabled', 'disabled');
-        }
-        else {
-            jQuery(selector).removeClass('hidden').find('input').removeAttr('disabled');
+    jQuery(function () {
+        var fields = [<% join ', ', map $escape->($_->{'field'}), @$enabled_by |n %>];
+        var values = new Array();
+% foreach my $e ( @$enabled_by ) {
+        values[<% $escape->($e->{'field'}) |n %>] = [<% join ', ', map $escape->($_), ref($e->{'value'})? (@{ $e->{'value'} }) : ($e->{'value'}) |n %>];
+% }
+        for ( var i = 0; i<fields.length; i++ ) {
+            var field = fields[i];
+            jQuery('input[name="'+ field +'"], input[name="'+ field +'s"]').change( function(e) {
+                disable_form_field( should_disable_form_field(fields, values), <% $escape->( $container_selector ) |n %> );
+            });
         }
-    }
+    });
+    jQuery(function () {
+        var fields = [<% join ', ', map $escape->($_->{'field'}), @$enabled_by |n %>];
+        var values = new Array();
+% foreach my $e ( @$enabled_by ) {
+        values[<% $escape->($e->{'field'}) |n %>] = [<% join ', ', map $escape->($_), ref($e->{'value'})? (@{ $e->{'value'} }) : ($e->{'value'}) |n %>];
+% }
+        disable_form_field( should_disable_form_field(fields, values), <% $escape->( $container_selector ) |n %> );
+    });
     </script>
 % }
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list