Skip to content

Commit

Permalink
Support to autocomplete users/groups for Autocomplete custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnavy committed Oct 13, 2023
1 parent 7e3a1ba commit 0efb81e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/RT/CustomField.pm
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ sub Values {
my $self = shift;
my $class = $self->ValuesClass;
# For back compatibility, autocomplete user/group cfs are not supposed to use Values
$class = 'RT::CustomFieldValues' if $class =~ /^RT::(?:Users|Groups)::/;

if ( $class ne 'RT::CustomFieldValues') {
RT::StaticUtil::RequireModule($class) or die "Can't load $class: $@";
}
Expand Down Expand Up @@ -870,6 +874,7 @@ sub ValidateValuesClass {

return 1 if !$class || $class eq 'RT::CustomFieldValues';
return 1 if grep $class eq $_, RT->Config->Get('CustomFieldValuesSources');
return 1 if $self->Type eq 'Autocomplete' && $class =~ /^RT::(?:Users|Groups)::/;
return undef;
}

Expand Down
2 changes: 1 addition & 1 deletion share/html/Admin/CustomFields/Modify.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
</&>
% }

% if ( $CustomFieldObj->Id and $CustomFieldObj->IsSelectionType and RT->Config->Get('CustomFieldValuesSources') and ( scalar(@{RT->Config->Get('CustomFieldValuesSources')}) > 0 ) ) {
% if ( $CustomFieldObj->Id and $CustomFieldObj->IsSelectionType and ( $CustomFieldObj->Type eq 'Autocomplete' or ( RT->Config->Get('CustomFieldValuesSources') and ( scalar(@{RT->Config->Get('CustomFieldValuesSources')}) > 0 ) ) ) ) {

<&| /Elements/LabeledValue, Label => loc("Field values source") &>
<& /Admin/Elements/EditCustomFieldValuesSource, CustomField => $CustomFieldObj &>
Expand Down
5 changes: 5 additions & 0 deletions share/html/Admin/Elements/EditCustomFieldValuesSource
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ foreach my $class( 'RT::CustomFieldValues', RT->Config->Get('CustomFieldValuesSo
push @sources, \%res;
}

if ( $CustomField->Type eq 'Autocomplete' ) {
push @sources, { Class => "RT::Users::$_", Description => loc('RT user [_1]', $_) } for qw/id Name EmailAddress/;
push @sources, { Class => "RT::Groups::$_", Description => loc('RT group [_1]', $_) } for qw/id Name/;
}

return unless grep $_->{'Class'} ne 'RT::CustomFieldValues', @sources;

</%INIT>
Expand Down
21 changes: 19 additions & 2 deletions share/html/Elements/EditCustomFieldAutocomplete
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ cols="<% $Cols %>" \
% if ( defined $Rows ) {
rows="<% $Rows %>" \
% }
name="<% $name %>" id="<% $name %>" class="CF-<%$CustomField->id%>-Edit form-control"><% $Default || '' %></textarea>
name="<% $name %>" id="<% $name %>" class="CF-<%$CustomField->id%>-Edit form-control no-selectize"
% if ( $CustomField->ValuesClass =~ /^RT::(Users|Groups)::(\w+)/ ) {
data-autocomplete="<% $1 %>" data-autocomplete-return="<% $2 %>" data-autocomplete-multiple="1"
% }

><% $Default || '' %></textarea>

% if ( $CustomField->ValuesClass !~ /^RT::(?:Users|Groups)::/ ) {
<script type="text/javascript">
var id = <% "$name" |n,j%>;
id = id.replace(/:/g,'\\:');
Expand All @@ -77,15 +83,26 @@ jQuery('#'+id).autocomplete( {
}
}
);
% }

% } else {
<input type="text" id="<% $name %>" name="<% $name %>" class="CF-<%$CustomField->id%>-Edit form-control" value="<% $Default || '' %>"/>
<input type="text" id="<% $name %>" name="<% $name %>" class="CF-<%$CustomField->id%>-Edit form-control" value="<% $Default || '' %>"
% if ( $CustomField->ValuesClass =~ /^RT::(Users|Groups)::(\w+)/ ) {
data-autocomplete="<% $1 %>" data-autocomplete-return="<% $2 %>"
% }
/>

% if ( $CustomField->ValuesClass !~ /^RT::(?:Users|Groups)::/ ) {

<script type="text/javascript">
var id = <% $name |n,j%>;
id = id.replace(/:/g,'\\:');
jQuery('#'+id).autocomplete( {
source: RT.Config.WebHomePath + "/Helpers/Autocomplete/CustomFieldValues?"+<% $Context |n,j %>+<% $name |n,u,j%>
}
);
% }

% }
</script>
<%INIT>
Expand Down
15 changes: 10 additions & 5 deletions share/static/js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ Selectize.define('rt_drag_drop', function(options) {

window.RT.Autocomplete.bind = function(from) {

jQuery("input[data-autocomplete]", from).each(function(){
jQuery(":input[data-autocomplete]", from).each(function(){
var input = jQuery(this);
var what = input.attr("data-autocomplete");
var wants = input.attr("data-autocomplete-return");

if (!what || !window.RT.Autocomplete.Classes[what])
return;

if ( (what === 'Users' || what === 'Principals') && input.is('[data-autocomplete-multiple]')) {
if ( (what === 'Users' || what === 'Principals') && input.is('[data-autocomplete-multiple]') && !input.hasClass('no-selectize') ) {
var options = input.attr('data-options');
var items = input.attr('data-items');
input.selectize({
Expand Down Expand Up @@ -151,7 +151,12 @@ window.RT.Autocomplete.bind = function(from) {

if (input.is('[data-autocomplete-multiple]')) {
if ( what != 'Tickets' ) {
queryargs.push("delim=,");
if ( input.is('textarea') ) {
queryargs.push("delim=%0A");
}
else {
queryargs.push("delim=,");
}
}

options.focus = function () {
Expand All @@ -160,7 +165,7 @@ window.RT.Autocomplete.bind = function(from) {
}

options.select = function(event, ui) {
var terms = this.value.split(what == 'Tickets' ? /\s+/ : /,\s*/);
var terms = this.value.split(what == 'Tickets' ? /\s+/ : input.is('textarea') ? /\n+/ : /,\s*/);
terms.pop(); // remove current input
if ( what == 'Tickets' ) {
// remove non-integers in case subject search with spaces in (like "foo bar")
Expand All @@ -175,7 +180,7 @@ window.RT.Autocomplete.bind = function(from) {
}
terms.push( ui.item.value ); // add selected item
terms.push(''); // add trailing delimeter so user can input another value directly
this.value = terms.join(what == 'Tickets' ? ' ' : ", ");
this.value = terms.join(what == 'Tickets' ? ' ' : input.is('textarea') ? "\n" : ',');
jQuery(this).change();

return false;
Expand Down

0 comments on commit 0efb81e

Please sign in to comment.