[Rt-commit] rt branch, 4.2/scrip-custom-code-textareas-visible-only-when-needed, created. rt-4.2.13-53-g12caab7

Dustin Collins strega at bestpractical.com
Sat Sep 3 21:02:49 EDT 2016


The branch, 4.2/scrip-custom-code-textareas-visible-only-when-needed has been created
        at  12caab7c2b7d042b1ce98add3c5568c7fcaeac6a (commit)

- Log -----------------------------------------------------------------
commit 12caab7c2b7d042b1ce98add3c5568c7fcaeac6a
Author: Dustin Collins <strega at bestpractical.com>
Date:   Mon Aug 29 11:39:25 2016 -0400

    Hide scrip custom code fields when appropriate
    
    The textareas for condition, action prepare, and action commit code
    only make sense for scrips that use "User Defined" condition or
    action. This commit shows or hides each textarea (using JavaScript)
    based on whether the scrip's current configuration will use its
    contents. This is meant to reduce user confusion about when these
    textareas take effect, and significantly reduce the length of the page
    in the common case of no "User Defined" code.
    
    To reinforce to users the relationship between the dropdown and its
    custom code textarea(s), we've moved each textarea up into the form,
    right below to its dropdown. If the fields were to remain in two
    separate sections, then users wouldn't notice the show/hide action at
    a distance and would wonder why the textareas appear only some of the
    time. This also reduces the perceived complexity of the scrip
    create/modify page down to one consistent form, rather than two
    confusingly-related sections, each with its own Save Changes button.
    
    In order to ease the transition for custom deployments, two new methods
    (UserEditableIDs and IsUserEditable) have been added to ScripCondition
    and ScripAction to allow users to easily inform RT of conditions or
    actions they have made user editable.
    
    Now that we've moved the textareas to be inline with the rest of the
    form, there arises a new problem where specifying lots of code would
    cause the rest of the page (e.g. Template selection) to scroll
    offscreen. So adapting the size of the textbox to how many lines are
    in the provided code (+3 lines for buffer) has been replaced with a
    constant of 6 rows. Users will still be able to use their browser's
    textarea resize tool to make the code entry fields longer.
    
    There is no animation for the show/hide actions because jQuery's
    slideUp and slideDown animations cannot handle table tags.
    
    Fixes: I#32260

diff --git a/lib/RT/ScripAction.pm b/lib/RT/ScripAction.pm
index 712b109..7b97733 100644
--- a/lib/RT/ScripAction.pm
+++ b/lib/RT/ScripAction.pm
@@ -244,6 +244,48 @@ sub Action {
     return $self->{'Action'};
 }
 
+=head2 IsUserEditable
+
+Returns 1 if the action can be edited by a user, otherwise 0.
+
+=cut
+
+sub IsUserEditable {
+    my $self = shift;
+
+    my @userEditableIDs = $self->UserEditableIDs;
+    my %hashedIDs = map { $_ => 1 } @userEditableIDs;
+    my $id = ($self->id || 0);
+
+    return exists($hashedIDs{$id}) ? 1 : 0;
+}
+
+
+=head2 UserEditableIds
+
+Returns an array of ScripCondition 'id's that should be user editable.
+
+=cut
+
+sub UserEditableIDs {
+    my $self = shift;
+
+    my @ids = ();
+
+    #Adds UserDefined ScripAction id
+    my $userDefinedAction = $self->new($self->CurrentUser);
+    $userDefinedAction->LoadByCols(ExecModule => 'UserDefined');
+    my $userDefinedId = $userDefinedAction->id||0;
+    if ( $userDefinedId > 0 ) {
+        push @ids, $userDefinedId;
+    }
+
+    #push additional values here onto @ids to allow them to be user editable
+    
+    return @ids;
+}
+
+
 =head2 id
 
 Returns the current value of id.
diff --git a/lib/RT/ScripCondition.pm b/lib/RT/ScripCondition.pm
index c93b606..a1b8286 100644
--- a/lib/RT/ScripCondition.pm
+++ b/lib/RT/ScripCondition.pm
@@ -203,6 +203,49 @@ sub IsApplicable  {
 }
 
 
+=head2 IsUserEditable
+
+Returns 1 if the action can be edited by a user, otherwise 0.
+
+=cut
+
+sub IsUserEditable {
+    my $self = shift;
+
+    my @userEditableIDs = $self->UserEditableIDs;
+    my %hashedIDs = map { $_ => 1 } @userEditableIDs;
+
+    my $id = ($self->id || 0);
+
+    return exists($hashedIDs{$id}) ? 1 : 0;
+}
+
+
+=head2 UserEditableIds
+
+Returns an array of ScripCondition 'id's that should be user editable.
+
+=cut
+
+sub UserEditableIDs {
+    my $self = shift;
+
+    my @ids = ();
+
+    #Adds UserDefined ScripCondition id
+    my $userDefinedCondition = $self->new($self->CurrentUser);
+    $userDefinedCondition->LoadByCols(ExecModule => 'UserDefined');
+    my $userDefinedId = $userDefinedCondition->id||0;
+    if ( $userDefinedId > 0 ) {
+        push @ids, $userDefinedId;
+    }
+
+    #push additional values here onto @ids to allow them to be user editable
+
+    return @ids;
+}
+
+
 
 =head2 id
 
diff --git a/share/html/Admin/Scrips/Create.html b/share/html/Admin/Scrips/Create.html
index 7cc3406..4af7a7f 100644
--- a/share/html/Admin/Scrips/Create.html
+++ b/share/html/Admin/Scrips/Create.html
@@ -75,14 +75,6 @@
     Name => 'Create',
 &>
 
-% if ($session{CurrentUser}->HasRight(Object => $RT::System, Right => 'ExecuteCode')) {
-<& Elements/EditCustomCode, %ARGS, Scrip => $scrip &>
-<& /Elements/Submit,
-    Label => loc('Create'),
-    Name => 'Create',
-&>
-% }
-
 </form>
 <%ARGS>
 $Queue => 0
diff --git a/share/html/Admin/Scrips/Elements/EditBasics b/share/html/Admin/Scrips/Elements/EditBasics
index aad681c..4c4a9da 100644
--- a/share/html/Admin/Scrips/Elements/EditBasics
+++ b/share/html/Admin/Scrips/Elements/EditBasics
@@ -56,19 +56,66 @@
     Default => $ARGS{"ScripCondition"} || $Scrip->ConditionObj->Id,
 &></td></tr>
 
+% if ($canExecuteCode) {
+<tr class="CustomIsApplicableCode <% $Scrip->ConditionObj->IsUserEditable ? '' : 'hidden' %>">
+<td class="label"><&|/l&>Condition code</&>:</td><td class="value">\
+% my $conditionCode = $ARGS{ CustomIsApplicableCode } || $Scrip->CustomIsApplicableCode() || '';
+<textarea cols="80" rows="6" name="CustomIsApplicableCode"><% $conditionCode %></textarea>
+</td></tr>
+% }
+
 <tr><td class="label"><&|/l&>Action</&>:</td><td class="value">\
 <& /Admin/Elements/SelectScripAction,
     Default => $ARGS{"ScripAction"} || $Scrip->ActionObj->Id,
 &></td></tr>
 
+% if ($canExecuteCode) {
+<tr class="CustomPrepareCode <% $Scrip->ActionObj->IsUserEditable ? '' : 'hidden' %>">
+<td class="label"><&|/l&>Prepare code</&>:</td><td class="value">\
+% my $prepareCode = $ARGS{ CustomPrepareCode } || $Scrip->CustomPrepareCode() || '';
+<textarea cols="80" rows="6" name="CustomPrepareCode"><% $prepareCode %></textarea>
+</td></tr>
+
+<tr class="CustomCommitCode <% $Scrip->ActionObj->IsUserEditable ? '' : 'hidden' %>">
+<td class="label"><&|/l&>Commit code</&>:</td><td class="value">\
+% my $commitCode = $ARGS{ CustomCommitCode } || $Scrip->CustomCommitCode() || '';
+<textarea cols="80" rows="6" name="CustomCommitCode"><% $commitCode %></textarea>
+</td></tr>
+% }
+
 <tr><td class="label"><&|/l&>Template</&>:</td><td class="value">\
 <& SelectTemplate,
     Default => $ARGS{"Template"}, Scrip => $Scrip, Queue => $Queue,
 &></td></tr>
 
+<script type="text/javascript">
+jQuery(function () {
+    jQuery("select[name=ScripCondition]").change(function (e) {
+        var scripConditionId = parseInt(jQuery(this).val());
+        var userEditableIDs = [<% join( ', ', RT::ScripCondition->new($session{CurrentUser})->UserEditableIDs ) %>]
+        if (userEditableIDs.indexOf(scripConditionId) > -1) {
+            jQuery(".CustomIsApplicableCode").removeClass('hidden');
+        }else{
+            jQuery(".CustomIsApplicableCode").addClass('hidden');
+        }
+    });
+
+    jQuery("select[name=ScripAction]").change(function (e) {
+        var scripConditionId = parseInt(jQuery(this).val());
+        var userEditableIDs = [<% join( ', ', RT::ScripAction->new($session{CurrentUser})->UserEditableIDs ) %>]
+        if (userEditableIDs.indexOf(scripConditionId) > -1) {
+            jQuery(".CustomPrepareCode, .CustomCommitCode").removeClass('hidden');
+        }else{
+            jQuery(".CustomPrepareCode, .CustomCommitCode").addClass('hidden');
+        }
+    });
+});
+</script>
+
 <%ARGS>
 $Scrip
 $Queue => undef
 </%ARGS>
 <%INIT>
+my $canExecuteCode = ($session{CurrentUser}->HasRight(Object => $RT::System, Right => 'ExecuteCode'));
 </%INIT>
diff --git a/share/html/Admin/Scrips/Elements/EditCustomCode b/share/html/Admin/Scrips/Elements/EditCustomCode
deleted file mode 100644
index 1c6247e..0000000
--- a/share/html/Admin/Scrips/Elements/EditCustomCode
+++ /dev/null
@@ -1,77 +0,0 @@
-%# BEGIN BPS TAGGED BLOCK {{{
-%#
-%# COPYRIGHT:
-%#
-%# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
-%#                                          <sales at bestpractical.com>
-%#
-%# (Except where explicitly superseded by other copyright notices)
-%#
-%#
-%# LICENSE:
-%#
-%# This work is made available to you under the terms of Version 2 of
-%# the GNU General Public License. A copy of that license should have
-%# been provided with this software, but in any event can be snarfed
-%# from www.gnu.org.
-%#
-%# This work is distributed in the hope that it will be useful, but
-%# WITHOUT ANY WARRANTY; without even the implied warranty of
-%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-%# General Public License for more details.
-%#
-%# You should have received a copy of the GNU General Public License
-%# along with this program; if not, write to the Free Software
-%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-%# 02110-1301 or visit their web page on the internet at
-%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
-%#
-%#
-%# CONTRIBUTION SUBMISSION POLICY:
-%#
-%# (The following paragraph is not intended to limit the rights granted
-%# to you to modify and distribute this software under the terms of
-%# the GNU General Public License and is only of importance to you if
-%# you choose to contribute your changes and enhancements to the
-%# community by submitting them to Best Practical Solutions, LLC.)
-%#
-%# By intentionally submitting any modifications, corrections or
-%# derivatives to this work, or any other work intended for use with
-%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
-%# you are the copyright holder for those contributions and you grant
-%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
-%# royalty-free, perpetual, license to use, copy, create derivative
-%# works based on those contributions, and sublicense and distribute
-%# those contributions and any derivatives thereof.
-%#
-%# END BPS TAGGED BLOCK }}}
-<&| /Widgets/TitleBox, title => loc('User Defined conditions and results') &>
-
-<table>
-<tr><td colspan="2" class="comment">
-<i><&|/l&>(Use these fields when you choose 'User Defined' for a condition or action)</&></i>
-</td></tr>
-
-% while ( my ($method, $desc) = splice @list, 0, 2 ) {
-<tr><td class="labeltop"><% $desc %>:</td><td class="value">
-% my $code = $ARGS{ $method } || $Scrip->$method() || '';
-% my $lines = @{[ $code =~ /\n/gs ]} + 3;
-% $lines = $min_lines if $lines < $min_lines;
-<textarea cols="80" rows="<% $lines %>" name="<% $method %>"><% $code %></textarea>
-</td></tr>
-% }
-
-</table>
-</&>
-<%ARGS>
-$Scrip
-</%ARGS>
-<%INIT>
-my @list = (
-    CustomIsApplicableCode => loc('Custom condition'),
-    CustomPrepareCode      => loc('Custom action preparation code'),
-    CustomCommitCode       => loc('Custom action commit code'),
-);
-
-my $min_lines = 10;
-</%INIT>
diff --git a/share/html/Admin/Scrips/Modify.html b/share/html/Admin/Scrips/Modify.html
index 618c48d..d5929e3 100644
--- a/share/html/Admin/Scrips/Modify.html
+++ b/share/html/Admin/Scrips/Modify.html
@@ -88,11 +88,6 @@
 
 <& /Elements/Submit, Label => loc('Save Changes'), Name => 'Update', Reset => 1 &>
 
-% if ($session{CurrentUser}->HasRight(Object => $RT::System, Right => 'ExecuteCode')) {
-<& Elements/EditCustomCode, %ARGS, Scrip => $scrip &>
-<& /Elements/Submit, Label => loc('Save Changes'), Name => 'Update', Reset => 1 &>
-% }
-
 </form>
 <%ARGS>
 $id     => undef

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


More information about the rt-commit mailing list