[Rt-commit] rt branch, 4.4/ajax-admin-custom-field-values, created. rt-4.2.5-200-gb16c120

? sunnavy sunnavy at bestpractical.com
Wed Sep 17 17:19:15 EDT 2014


The branch, 4.4/ajax-admin-custom-field-values has been created
        at  b16c120e24d1f2ac58768d2a68c587ff26b5d6ec (commit)

- Log -----------------------------------------------------------------
commit b16c120e24d1f2ac58768d2a68c587ff26b5d6ec
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Sep 18 04:56:02 2014 +0800

    ajaxify add/delete actions for admin custom field values
    
    Add's backend code on Modify.html is kept(with a bit tweaking) to only make tests happy.

diff --git a/share/html/Admin/CustomFields/Modify.html b/share/html/Admin/CustomFields/Modify.html
index eea4532..1751a63 100644
--- a/share/html/Admin/CustomFields/Modify.html
+++ b/share/html/Admin/CustomFields/Modify.html
@@ -47,9 +47,6 @@
 %# END BPS TAGGED BLOCK }}}
 <& /Admin/Elements/Header,
     Title => $title,
-    Focus => (($added_cfv or $ARGS{FocusAddValue})
-        ? "input[name=CustomField-$id-Value-new-Name]"
-        : undef),
     &>
 <& /Elements/Tabs &>
 <& /Elements/ListActions, actions => \@results &>
@@ -169,15 +166,20 @@ jQuery( function() {
 
 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
 <h2><&|/l&>Values</&></h2>
-<div>
+<div class="edit_custom_field_values">
 <& /Admin/Elements/EditCustomFieldValues, CustomField => $CustomFieldObj &>
-<& /Admin/Elements/AddCustomFieldValue, CustomField => $CustomFieldObj &>
 </div>
 % }
 
 <& /Elements/Submit, Name => 'Update', Label => $id eq 'new'? loc('Create'): loc('Save Changes') &>
 
 </form>
+
+% if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
+<div class="add_custom_field_value">
+<& /Admin/Elements/AddCustomFieldValue, CustomField => $CustomFieldObj &>
+</div>
+% }
 <%INIT>
 my ($title, @results, $added_cfv);
 
@@ -267,15 +269,8 @@ if ( $ARGS{'Update'} && $id ne 'new' ) {
         push @results, $msg;
     }
 
-    my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
-    # Delete any fields that want to be deleted
-    foreach my $key ( keys %ARGS ) {
-        next unless $key =~ /^Delete-$paramtag-(\d+)$/;
-        my ($val, $msg) = $CustomFieldObj->DeleteValue( $1 );
-        push (@results, $msg);
-    }
-
     # Update any existing values
+    my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
     my $values = $CustomFieldObj->ValuesObj;
     while ( my $value = $values->Next ) {
         foreach my $attr (qw(Name Description SortOrder Category)) {
@@ -292,7 +287,12 @@ if ( $ARGS{'Update'} && $id ne 'new' ) {
         $m->callback(CallbackName => 'AfterUpdateCustomFieldValue', CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $value, ARGSRef => \%ARGS );
     }
 
+}
+
+# for old tests only, users should use ajax calls and this should never be called in real usage.
+if ( $ARGS{'AddValue'} ) {
     # Add any new values
+    my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
     if ( defined $ARGS{ $paramtag ."-new-Name" } && length $ARGS{ $paramtag ."-new-Name" } ) {
         my ($id, $msg) = $CustomFieldObj->AddValue(
             map { 
@@ -301,12 +301,6 @@ if ( $ARGS{'Update'} && $id ne 'new' ) {
                 $_ => $ARGS{ $paramtag ."-new-$_" } } qw/ Name Description SortOrder Category/
         );
         push (@results, $msg);
-        $added_cfv = 1 if $id;
-
-        my $cfv = RT::CustomFieldValue->new( $session{CurrentUser} );
-        $cfv->Load($id);
-        $m->callback(CallbackName => 'AfterCreateCustomFieldValue',
-CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $cfv, ARGSRef => \%ARGS );
     }
 }
 
@@ -330,7 +324,7 @@ $id = $CustomFieldObj->id if $CustomFieldObj->id;
 # This code does automatic redirection if any updates happen.
 MaybeRedirectForResults(
     Actions     => \@results,
-    Arguments   => { id => $id, FocusAddValue => ($added_cfv ? 1 : 0) },
+    Arguments   => { id => $id },
 ) if $CustomFieldObj->id;
 
 
diff --git a/share/html/Admin/Elements/AddCustomFieldValue b/share/html/Admin/Elements/AddCustomFieldValue
index ec1a34b..4cb2ce1 100644
--- a/share/html/Admin/Elements/AddCustomFieldValue
+++ b/share/html/Admin/Elements/AddCustomFieldValue
@@ -47,8 +47,9 @@
 %# END BPS TAGGED BLOCK }}}
 <h3><&|/l&>Add Value</&></h3>
 
+<form name="AddCustomFieldValue" action="Modify.html" method="post">
+<input type="hidden" class="hidden" name="id" value="<% $CustomField->id %>" />
 <table border="0">
-
 <tr>
 <th><&|/l&>Sort</&></th>
 <th><&|/l&>Name</&></th>
@@ -56,6 +57,7 @@
 % if ( $CustomField->Type ne 'Combobox' ) {
 <th class="categoryheader"><&|/l&>Category</&></th>
 % }
+<th></th>
 </tr>
 
 % my $paramtag = "CustomField-". $CustomField->Id ."-Value-new";
@@ -78,11 +80,39 @@
 %   }
 </td>
 % }
-
+<td><input type="submit" name="AddValue" value="<&|/l&>Add</&>" /></td>
 % $m->callback(CallbackName => 'AfterCustomFieldValueInput', CustomFieldObj => $CustomField, ARGSRef => \%ARGS );
 </tr>
 
 </table>
+</form>
+<script type="text/javascript">
+jQuery( function() {
+    jQuery('form[name=AddCustomFieldValue]').submit(function() {
+        jQuery.post('<% RT->Config->Get('WebPath') %>/Helpers/Admin/EditCustomFieldValue', {
+            action: 'add',
+            cf_id: <% $CustomField->id %>,
+            sort_order: jQuery('input[name=CustomField-<% $CustomField->id %>-Value-new-SortOrder]').val(),
+            name: jQuery('input[name=CustomField-<% $CustomField->id %>-Value-new-Name]').val(),
+            description: jQuery('input[name=CustomField-<% $CustomField->id %>-Value-new-Description]').val(),
+            category: jQuery('[name=CustomField-<% $CustomField->id %>-Value-new-Category]').val()
+        }, function(data) {
+            if ( data.status != 0 ) {
+                jQuery('div.edit_custom_field_values').html(data.html);
+                jQuery('input[name=CustomField-<% $CustomField->id %>-Value-new-SortOrder]').val(''),
+                jQuery('input[name=CustomField-<% $CustomField->id %>-Value-new-Name]').val('').focus(),
+                jQuery('input[name=CustomField-<% $CustomField->id %>-Value-new-Description]').val(''),
+                jQuery('[name=CustomField-<% $CustomField->id %>-Value-new-Category]').val('');
+            }
+            else {
+                alert(data.message);
+            }
+        }, 'json');
+        return false;
+    });
+});
+</script>
+
 <%args>
 $CustomField => undef
 </%args>
diff --git a/share/html/Admin/Elements/EditCustomFieldValues b/share/html/Admin/Elements/EditCustomFieldValues
index 5a81ec7..90db3be 100644
--- a/share/html/Admin/Elements/EditCustomFieldValues
+++ b/share/html/Admin/Elements/EditCustomFieldValues
@@ -53,19 +53,18 @@
 <table>
 
 <tr>
-<th> </th>
 <th><&|/l&>Sort</&></th>
 <th><&|/l&>Name</&></th>
 <th><&|/l&>Description</&></th>
 % if ($CustomField->Type ne 'Combobox') {
 <th class="categoryheader"><&|/l&>Category</&></th>
 % }
+<th></th>
 </tr>
 
 % while ( my $value = $values->Next ) {
 % my $paramtag = "CustomField-". $CustomField->Id ."-Value-". $value->Id;
 <tr>
-<td><input type="checkbox" class="checkbox" name="Delete-<% $paramtag %>" /></td>
 <td><input type="text" size="3" name="<% $paramtag %>-SortOrder" value="<% $value->SortOrder %>" /></td>
 <td><input type="text" size="25" name="<% $paramtag %>-Name" value="<% $value->Name %>" /></td>
 <td><input type="text" size="45" name="<% $paramtag %>-Description" value="<% $value->Description || '' %>" /></td>
@@ -84,12 +83,29 @@
 %   }
 </td>
 % }
+<td><input type="button" class="delete_custom_field_value" data-cfv-id="<% $value->id %>" value="<&|/l&>Delete</&>" onclick="delete_custom_field_value(<% $value->id %>)" /></td>
 % $m->callback(CallbackName => 'AfterCustomFieldValueInput', CustomFieldObj => $CustomField, CustomFieldValueObj => $value, ARGSRef => \%ARGS );
 </tr>
 % }
 
 </table>
-<i><&|/l&>(Check box to delete)</&></i>
+
+<script type="text/javascript">
+function delete_custom_field_value(value_id) {
+    jQuery.post('<% RT->Config->Get('WebPath') %>/Helpers/Admin/EditCustomFieldValue', {
+        action: 'delete',
+        cf_id: <% $CustomField->id %>,
+        value_id: value_id
+    }, function(data) {
+        if ( data.status != 0 ) {
+            jQuery('div.edit_custom_field_values').html(data.html);
+        }
+        else {
+            alert(data.message);
+        }
+    }, 'json')
+}
+</script>
 <%init>
 
 my $values = $CustomField->ValuesObj();
diff --git a/share/html/Admin/Elements/AddCustomFieldValue b/share/html/Helpers/Admin/EditCustomFieldValue
similarity index 60%
copy from share/html/Admin/Elements/AddCustomFieldValue
copy to share/html/Helpers/Admin/EditCustomFieldValue
index ec1a34b..83cd86c 100644
--- a/share/html/Admin/Elements/AddCustomFieldValue
+++ b/share/html/Helpers/Admin/EditCustomFieldValue
@@ -45,44 +45,47 @@
 %# those contributions and any derivatives thereof.
 %#
 %# END BPS TAGGED BLOCK }}}
-<h3><&|/l&>Add Value</&></h3>
+% $r->content_type('application/json; charset=utf-8');
+<% JSON( $out ) |n %>
+% $m->abort;
+<%INIT>
 
-<table border="0">
+my $out = {};
 
-<tr>
-<th><&|/l&>Sort</&></th>
-<th><&|/l&>Name</&></th>
-<th><&|/l&>Description</&></th>
-% if ( $CustomField->Type ne 'Combobox' ) {
-<th class="categoryheader"><&|/l&>Category</&></th>
-% }
-</tr>
+if ( $cf_id ) {
+    my $cf = RT::CustomField->new($session{CurrentUser});
+    $cf->LoadById($cf_id);
+    if ( !$cf->Name ) {
+        $out = { status => 0, message => loc("Current User cannot see this Custom Field, terminating") };
+    }
+    else {
+        my ( $ret, $msg, $html );
+        if ( $action eq 'add' ) {
+            ( $ret, $msg ) = $cf->AddValue(
+                Name        => $ARGS{name},
+                SortOrder   => $ARGS{sort_order} || 0,
+                Description => $ARGS{description},
+                Category    => $ARGS{category},
+            );
+            $html  = $m->scomp('/Admin/Elements/EditCustomFieldValues', CustomField => $cf ) if $ret;
+        }
+        elsif ( $action eq 'delete' ) {
+            ( $ret, $msg ) = $cf->DeleteValue( $ARGS{value_id} );
+            $html  = $m->scomp('/Admin/Elements/EditCustomFieldValues', CustomField => $cf ) if $ret;
+        }
+        else {
+            ( $ret, $msg ) = ( 0, loc('Invalid action') );
+        }
 
-% my $paramtag = "CustomField-". $CustomField->Id ."-Value-new";
-<tr>
-<td><input type="text" size="3"  name="<% $paramtag %>-SortOrder"   /></td>
-<td><input type="text" size="25" name="<% $paramtag %>-Name"        /></td>
-<td><input type="text" size="45" name="<% $paramtag %>-Description" /></td>
-% if ( $CustomField->Type ne 'Combobox' ) {
-<td>
-% my $BasedOnObj = $CustomField->BasedOnObj;
-% if ( $BasedOnObj and $BasedOnObj->id ) {
-% my $Categories = $BasedOnObj->Values;
-<select class="editcategory" name="<% $paramtag %>-Category" size="1">
-<option value=""><&|/l&>(no value)</&></option>
-% while (my $Value = $Categories->Next) {
-<option value="<% $Value->Name %>"><% $Value->Name %></option>
-% }
-%   } else {
-<input type="text" size="10" class="editcategory" name="<% $paramtag %>-Category" value="" />
-%   }
-</td>
-% }
+        $out =  { status => $ret || 0, message => $msg, html => $html };
+    }
+}
+else {
+    $out = { status => 0, message => loc("No CustomField provided") };
+}
+</%INIT>
 
-% $m->callback(CallbackName => 'AfterCustomFieldValueInput', CustomFieldObj => $CustomField, ARGSRef => \%ARGS );
-</tr>
-
-</table>
-<%args>
-$CustomField => undef
-</%args>
+<%ARGS>
+$action => ''
+$cf_id => undef
+</%ARGS>
diff --git a/t/web/cf_select_one.t b/t/web/cf_select_one.t
index 4f81e2a..8a2e3ac 100644
--- a/t/web/cf_select_one.t
+++ b/t/web/cf_select_one.t
@@ -30,11 +30,11 @@ diag "add 'qwe', 'ASD', '0' and ' foo ' as values to the CF";
 {
     foreach my $value(qw(qwe ASD 0), 'foo ') {
         $m->submit_form(
-            form_name => "ModifyCustomField",
+            form_name => "AddCustomFieldValue",
             fields => {
                 "CustomField-". $cfid ."-Value-new-Name" => $value,
             },
-            button => 'Update',
+            button => 'AddValue',
         );
         $m->content_contains('Object created', 'added a value to the CF' ); # or diag $m->content;
         my $v = $value;

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


More information about the rt-commit mailing list