Skip to content

Commit

Permalink
Support lazy load for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnavy committed Jul 2, 2024
1 parent 53fe89c commit c9548a5
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 56 deletions.
3 changes: 3 additions & 0 deletions lib/RT/Interface/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,9 @@ if ((RequestENV('HTTP_USER_AGENT') || '') =~ /(?:hiptop|Blazer|Novarra|Vagabond|

}

sub ModernClient {
return ( RequestENV('HTTP_USER_AGENT') // '' ) =~ /WWW-Mechanize/ ? 0 : 1;
}

sub StripContent {
my %args = @_;
Expand Down
2 changes: 1 addition & 1 deletion share/html/Elements/DropdownMenu
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
% if ( $item eq 'HideUnsetFields' ) {
% my $show_label = loc("Show unset fields");
% my $hide_label = loc("Hide unset fields");
% my $initial_label = $ARGS{HideUnsetFields} ? $show_label : $hide_label;
% my $initial_label = $ARGS{HideUnsetFields} // $DECODED_ARGS->{HideUnsetFields} ? $show_label : $hide_label;
<li class="dropdown-item">
<a hx-boost="false" href="#" data-show-label="<% $show_label %>" data-hide-label="<% $hide_label %>" onclick="return toggle_hide_unset(this)"><% $initial_label %></a>
</li>
Expand Down
14 changes: 11 additions & 3 deletions share/html/Elements/ShowWidgets
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@
% else {
% $path = $widget;
% }
% $path = '/Ticket/Widgets/' . $path unless $path =~ m{^/};
% $path = $path_prefix . $path;
% if ( $m->comp_exists( $path ) ) {
<& $path, %$ARGSRef, ref $widget ? ( WidgetConfig => $widget ) : () &>
% # Disable lazy load for wrapper widgets like "Summary" or mechanize tests
% if ( ref $widget || !RT::Interface::Web->ModernClient() ) {
<& $path, ref $widget ? ( WidgetConfig => $widget ) : (), $Object->isa('RT::Ticket') ? ( Ticket => $Object ) : ( Object => $Object ) &>
% } else {
<& /Widgets/HTMXLoadStart, htmx_get => "/Views$path?id=" . $Object->Id, lazy_load => 1 &>
<& /Widgets/HTMXLoadEnd &>
% }
% } else {
% RT->Logger->warning("Could not find $path");
% }
Expand Down Expand Up @@ -93,12 +99,14 @@ for my $item ( split /\s*,\s*/, $Layout // '' ) {
RT->Logger->warning("Unsupported layout item '$item'");
}
}

my $path_prefix = $Object->isa('RT::Ticket') ? '/Ticket/Widgets/' : '';
</%INIT>

<%ARGS>
$ARGSRef => {}
$Content => undef
$Layout => undef
$InRow => undef
$ExtraColumnClass => undef
$Object => ''
</%ARGS>
12 changes: 1 addition & 11 deletions share/html/Ticket/Display.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
<& Elements/ShowDependencyStatus, Ticket => $TicketObj &>
</div>


<& /Elements/ShowWidgets, Content => $content, Layout => $layout, InRow => 0, ARGSRef => { %ARGS, Ticket => $TicketObj, Transactions => $transactions, Attachments => $attachments, InlineEdit => $InlineEdit } &>
<& /Elements/ShowWidgets, Content => $content, Layout => $layout, InRow => 0, Object => $TicketObj &>

<%ARGS>
$TicketObj => undef
Expand Down Expand Up @@ -226,15 +225,6 @@
Arguments => { id => $TicketObj->id, exists $ARGS{ShowHistory} ? ( ShowHistory => $ARGS{ShowHistory} ) : () },
);

my $transactions;
if ( $ARGS{'ReverseTxns'} ) {
$transactions = $TicketObj->SortedTransactions($ARGS{'ReverseTxns'});
}
else {
$transactions = $TicketObj->SortedTransactions;
}
my $attachments = $TicketObj->Attachments;

my %link_rel;
if (RT::Config->Get( 'ShowSearchNavigation', $session{'CurrentUser'} ) && defined $session{'collection-RT::Tickets'} and ($ARGS{'Query'} or $session{'CurrentSearchHash'}->{'Query'})) {
my $item_map = $session{'collection-RT::Tickets'}->ItemMap;
Expand Down
21 changes: 1 addition & 20 deletions share/html/Ticket/Elements/ShowSummary
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,11 @@
Layout => $Layout,
ExtraColumnClass => 'boxcontainer',
InRow => 1,
ARGSRef => {
%ARGS,
CanModify => $can_modify,
CanModifyCF => $can_modify_cf,
CanModifyOwner => $can_modify_owner,
CanModifyPeople => $can_modify_people,
},
Object => $Ticket,
&>
</div>
<%ARGS>
$Ticket => undef
$Attachments => undef
$InlineEdit => 0
$Content => undef
$Layout => 'col-md-6'
</%ARGS>
<%INIT>
my $can_modify = $Ticket->CurrentUserHasRight('ModifyTicket');
my $can_modify_cf = $Ticket->CurrentUserHasRight('ModifyCustomField');
my ($can_modify_owner) = $Ticket->CurrentUserCanSetOwner();
my $can_modify_people = $Ticket->CurrentUserHasRight('Watch')
|| $Ticket->CurrentUserHasRight('WatchAsAdminCc');

$m->callback( CallbackName => 'ModifyRights', %ARGS, TicketObj => $Ticket, ARGSRef => \%ARGS,
CanModify => \$can_modify, CanModifyCF => \$can_modify_cf,
CanModifyOwner => \$can_modify_owner, CanModifyPeople => \$can_modify_people );
</%INIT>
12 changes: 9 additions & 3 deletions share/html/Ticket/Widgets/Basics
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
% $m->callback( %ARGS, CallbackName => 'BeforeBasics' );

<&| /Widgets/TitleBox, title => loc('The Basics'),
(($ARGS{CanModify} || $ARGS{CanModifyCF}) ? (title_href => RT->Config->Get('WebPath')."/Ticket/Modify.html?id=".$Ticket->Id) : ()),
(($ARGS{CanModify} || $ARGS{CanModifyCF}) && $modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $m->scomp('/Elements/InlineEditLink')) : ()),
(($can_modify || $can_modify_cf) ? (title_href => RT->Config->Get('WebPath')."/Ticket/Modify.html?id=".$Ticket->Id) : ()),
(($can_modify || $can_modify_cf) && $modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $m->scomp('/Elements/InlineEditLink')) : ()),
class => (join " ", 'ticket-info-basics', ($modify_behavior eq 'always' ? 'editing' : ())),
data => { 'inline-edit-behavior' => $modify_behavior },
&>
Expand Down Expand Up @@ -80,7 +80,13 @@ my %inline_edit_behavior;
if ( RT->Config->Get('InlineEditPanelBehavior') ) {
%inline_edit_behavior = %{ RT->Config->Get('InlineEditPanelBehavior')->{'RT::Ticket'} || {} };
}
my $modify_behavior = $ARGS{InlineEdit} ? ($inline_edit_behavior{Basics} || $inline_edit_behavior{_default} || 'link') : 'hide';
my $modify_behavior
= RT->Config->Get( 'InlineEdit', $session{CurrentUser} )
? ( $inline_edit_behavior{Basics} || $inline_edit_behavior{_default} || 'link' )
: 'hide';

my $can_modify = $Ticket->CurrentUserHasRight('ModifyTicket');
my $can_modify_cf = $Ticket->CurrentUserHasRight('ModifyCustomField');
</%INIT>
<%ARGS>
$Ticket
Expand Down
9 changes: 7 additions & 2 deletions share/html/Ticket/Widgets/CustomFieldCustomGroupings
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@

<& /Elements/ShowCustomFieldCustomGroupings,
Object => $Ticket,
title_href => ($ARGS{CanModify} || $ARGS{CanModifyCF}) ? RT->Config->Get('WebPath')."/Ticket/Modify.html" : "",
InlineEdit => ($ARGS{CanModify} || $ARGS{CanModifyCF}) ? $ARGS{InlineEdit} : 0,
title_href => ( $can_modify || $can_modify_cf ) ? RT->Config->Get('WebPath') . "/Ticket/Modify.html" : "",
InlineEdit => ( $can_modify || $can_modify_cf ) ? RT->Config->Get( 'InlineEdit', $session{CurrentUser} ) : 0,
&>

% $m->callback( %ARGS, CallbackName => 'AfterCustomFields' );

<%INIT>
my $can_modify = $Ticket->CurrentUserHasRight('ModifyTicket');
my $can_modify_cf = $Ticket->CurrentUserHasRight('ModifyCustomField');
</%INIT>

<%ARGS>
$Ticket
</%ARGS>
10 changes: 7 additions & 3 deletions share/html/Ticket/Widgets/Dates
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
% $m->callback( %ARGS, CallbackName => 'BeforeDates' );

<&| /Widgets/TitleBox, title => loc("Dates"),
($ARGS{CanModify} ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyDates.html?id=".$Ticket->Id) : ()),
($can_modify ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyDates.html?id=".$Ticket->Id) : ()),
class => (join " ", 'ticket-info-dates', ($modify_behavior eq 'always' ? 'editing' : ())),
($ARGS{CanModify} && $modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $m->scomp('/Elements/InlineEditLink')) : ()),
($can_modify && $modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $m->scomp('/Elements/InlineEditLink')) : ()),
data => { 'inline-edit-behavior' => $modify_behavior },
&>
% unless ($modify_behavior eq 'always') {
Expand Down Expand Up @@ -80,7 +80,11 @@ if ( RT->Config->Get('InlineEditPanelBehavior') ) {
%inline_edit_behavior = %{ RT->Config->Get('InlineEditPanelBehavior')->{'RT::Ticket'} || {} };
}
my $modify_behavior
= $ARGS{InlineEdit} ? ( $inline_edit_behavior{Dates} || $inline_edit_behavior{_default} || 'link' ) : 'hide';
= RT->Config->Get( 'InlineEdit', $session{CurrentUser} )
? ( $inline_edit_behavior{Dates} || $inline_edit_behavior{_default} || 'link' )
: 'hide';

my $can_modify = $Ticket->CurrentUserHasRight('ModifyTicket');
</%INIT>

<%ARGS>
Expand Down
9 changes: 5 additions & 4 deletions share/html/Ticket/Widgets/Links
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@

% $m->callback( %ARGS, CallbackName => 'LinksExtra', extra => \@extra );
<&| /Widgets/TitleBox, title => loc('Links'),
($ARGS{CanModify} ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyLinks.html?id=".$Ticket->Id) : ()),
($can_modify ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyLinks.html?id=".$Ticket->Id) : ()),
class => (join " ", 'ticket-info-links', ($modify_behavior eq 'always' ? 'editing' : ())),
data => { 'inline-edit-behavior' => $modify_behavior },
($ARGS{CanModify} ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyLinks.html?id=".$Ticket->Id) : ()),
($can_modify ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyLinks.html?id=".$Ticket->Id) : ()),
@extra,
&>
% unless ($modify_behavior eq 'always') {
Expand Down Expand Up @@ -87,14 +87,15 @@ if ( RT->Config->Get('InlineEditPanelBehavior') ) {
%inline_edit_behavior = %{ RT->Config->Get('InlineEditPanelBehavior')->{'RT::Ticket'} || {} };
}
my $modify_behavior
= $ARGS{InlineEdit} ? ( $inline_edit_behavior{Links} || $inline_edit_behavior{_default} || 'link' ) : 'hide';
= RT->Config->Get( 'InlineEdit', $session{CurrentUser} ) ? ( $inline_edit_behavior{Links} || $inline_edit_behavior{_default} || 'link' ) : 'hide';
my $can_modify = $Ticket->CurrentUserHasRight('ModifyTicket');

my @extra;
my $alt = loc('Graph ticket links');
my $links_graph = '<a href="' . RT->Config->Get('WebPath') . '/Ticket/Graphs/index.html?id=' . $Ticket->id . '"><span class="fas fa-project-diagram icon-bordered fa-2x" alt="' . $alt . '" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="' . $alt . '"></span></a>';

my $links_titleright = join ' ',
($ARGS{CanModify} && $modify_behavior =~ /^(link|click)$/ ? ($m->scomp('/Elements/InlineEditLink')) : ()),
($can_modify && $modify_behavior =~ /^(link|click)$/ ? ($m->scomp('/Elements/InlineEditLink')) : ()),
(RT->Config->Get('DisableGraphViz') ? () : $links_graph);
push @extra, (titleright_raw => $links_titleright) if $links_titleright;
</%INIT>
Expand Down
12 changes: 9 additions & 3 deletions share/html/Ticket/Widgets/People
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
% $m->callback( %ARGS, CallbackName => 'BeforePeople' );

<&| /Widgets/TitleBox, title => loc('People'),
(($ARGS{CanModify} || $ARGS{CanModifyOwner} || $ARGS{CanModifyPeople}) ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyPeople.html?id=".$Ticket->Id) : ()),
(($can_modify || $can_modify_owner || $can_modify_people) ? (title_href => RT->Config->Get('WebPath')."/Ticket/ModifyPeople.html?id=".$Ticket->Id) : ()),
class => (join " ", 'ticket-info-people', ($modify_behavior eq 'always' ? 'editing' : ())),
(($ARGS{CanModify} || $ARGS{CanModifyOwner} || $ARGS{CanModifyPeople}) && $modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $m->scomp('/Elements/InlineEditLink')) : ()),
(($can_modify || $can_modify_owner || $can_modify_people) && $modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $m->scomp('/Elements/InlineEditLink')) : ()),
data => { 'inline-edit-behavior' => $modify_behavior },
&>
% unless ($modify_behavior eq 'always') {
Expand Down Expand Up @@ -81,7 +81,13 @@ if ( RT->Config->Get('InlineEditPanelBehavior') ) {
%inline_edit_behavior = %{ RT->Config->Get('InlineEditPanelBehavior')->{'RT::Ticket'} || {} };
}
my $modify_behavior
= $ARGS{InlineEdit} ? ( $inline_edit_behavior{People} || $inline_edit_behavior{_default} || 'link' ) : 'hide';
= RT->Config->Get( 'InlineEdit', $session{CurrentUser} )
? ( $inline_edit_behavior{People} || $inline_edit_behavior{_default} || 'link' )
: 'hide';

my $can_modify = $Ticket->CurrentUserHasRight('ModifyTicket');
my ($can_modify_owner) = $Ticket->CurrentUserCanSetOwner();
my $can_modify_people = $Ticket->CurrentUserHasRight('Watch') || $Ticket->CurrentUserHasRight('WatchAsAdminCc');
</%INIT>
<%ARGS>
$Ticket
Expand Down
2 changes: 1 addition & 1 deletion share/html/Ticket/Widgets/Summary
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
%#
%# END BPS TAGGED BLOCK }}}
% $m->callback( %ARGS, CallbackName => 'BeforeShowSummary' );
<div class="summary unset-fields-container<% $ARGS{HideUnsetFields} ? ' unset-fields-hidden' : '' %>">
<div class="summary unset-fields-container<% $DECODED_ARGS->{HideUnsetFields} ? ' unset-fields-hidden' : '' %>">
<&| /Widgets/TitleBox, title => loc('Ticket metadata'), titleright_raw => $m->scomp('/Elements/DropdownMenu', %ARGS), class => 'fullwidth' &>
<& /Ticket/Elements/ShowSummary, %ARGS, ref $WidgetConfig ? %$WidgetConfig : () &>
</&>
Expand Down
11 changes: 9 additions & 2 deletions share/html/Views/Ticket/dhandler
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ unless ( $ticket->CurrentUserCanSee ) {
Abort( loc('Permission Denied'), SuppressHeader => 1, Code => HTTP::Status::HTTP_FORBIDDEN );
}

unless ( $m->comp_exists("/Ticket/Elements/$component_name") ) {
my $path;
if ( $m->comp_exists("/Ticket/$component_name") ) {
$path = "/Ticket/$component_name";
}
elsif ( $m->comp_exists("/Ticket/Elements/$component_name") ) {
$path = "/Ticket/Elements/$component_name";
}
else {
RT->Logger->warning( "Component $component_name does not exist" );
Abort( loc('Invalid Path'), SuppressHeader => 1 );
}

my $out;
eval { $out = $m->scomp( "/Ticket/Elements/$component_name", Ticket => $ticket, TicketObj => $ticket, %ARGS ) };
eval { $out = $m->scomp( $path, Ticket => $ticket, TicketObj => $ticket, %ARGS ) };
if ($@) {
RT->Logger->warning("Error loading $component_name: $@");
Abort( loc('Error'), SuppressHeader => 1 );
Expand Down
5 changes: 2 additions & 3 deletions share/html/Widgets/TitleBox
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ $htmx_get => undef
my $content;

# If htmx_load is set to 0 explicitly, render with titlebox even for /Views/
if ( $m->request_path =~ /^\/Views/ && ( $htmx_load // 1 ) ) {
if ( $m->request_path =~ /^\/Views/ && $m->request_path !~ m{/Widgets/} && ( $htmx_load // 1 ) ) {

# HTMX load, send content with no titlebox
$m->out( $m->content );
Expand All @@ -103,8 +103,7 @@ elsif ( my $name = $m->notes('HTMXLoadComponent') ) {
$htmx_get = RT->Config->Get('WebPath') . '/Views/Component/' . $name . '?From=' . RT->Config->Get('WebPath') . $m->request_path;
}

$content = $m->content
unless $htmx_load && ( RT::Interface::Web::RequestENV('HTTP_USER_AGENT') // '' ) !~ /WWW-Mechanize/;
$content = $m->content unless $htmx_load && RT::Interface::Web->ModernClient();

$m->callback( CallbackName => "ModifyContent", ARGSRef => \%ARGS, Content => \$content,
Class => \$class, HideEmpty => \$hide_empty, HideChrome => \$hide_chrome );
Expand Down

0 comments on commit c9548a5

Please sign in to comment.