Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4086: move the method AddJSOnDocumentCompleteIfNotExists() #4087

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Kernel/Output/HTML/Layout/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,62 @@ sub AddJSOnDocumentComplete {
return;
}

=head2 AddJSOnDocumentCompleteIfNotExists()

this functions adds JavaScript by the function C<AddJSOnDocumentComplete()> only if it does not exist yet.

my $Success = $LayoutObject->AddJSOnDocumentCompleteIfNotExists(
Key => 'identifier_key_of_your_js',
Code => $JSBlock,
);

Returns:

my $Success = 1;

=cut

sub AddJSOnDocumentCompleteIfNotExists {
my ( $Self, %Param ) = @_;

my $LogObject = $Kernel::OM->Get('Kernel::System::Log');

# check needed stuff
NEEDED:
for my $Needed (qw(Key Code)) {

next NEEDED if defined $Param{$Needed};

$LogObject->Log(
Priority => 'error',
Message => "Parameter '$Needed' is needed!",
);

return;
}

my $Exists = 0;
CODEJS:
for my $CodeJS ( @{ $Self->{_JSOnDocumentComplete} || [] } ) {

next CODEJS if $CodeJS !~ m{ Key: \s $Param{Key}}xms;

$Exists = 1;

last CODEJS;
}

return 1 if $Exists;

my $AddCode = "// Key: $Param{Key}\n" . $Param{Code};

$Self->AddJSOnDocumentComplete(
Code => $AddCode,
);

return 1;
}

=head2 AddJSData()

dynamically add JavaScript data that should be handed over to
Expand Down
53 changes: 0 additions & 53 deletions Kernel/Output/HTML/Layout/Znuny4OTOBORepo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -332,57 +332,4 @@ Example html for a hook:
return 1;
}

=head2 AddJSOnDocumentCompleteIfNotExists()

this functions adds JavaScript by the function AddJSOnDocumentComplete only if it not exists.

my $Success = $LayoutObject->AddJSOnDocumentCompleteIfNotExists(
Key => 'identifier_key_of_your_js',
Code => $JSBlock,
);

Returns:

my $Success = 1;

=cut

sub AddJSOnDocumentCompleteIfNotExists {
my ( $Self, %Param ) = @_;

my $LogObject = $Kernel::OM->Get('Kernel::System::Log');

# check needed stuff
NEEDED:
for my $Needed (qw(Key Code)) {

next NEEDED if defined $Param{$Needed};

$LogObject->Log(
Priority => 'error',
Message => "Parameter '$Needed' is needed!",
);
return;
}

my $Exists = 0;
CODEJS:
for my $CodeJS ( @{ $Self->{_JSOnDocumentComplete} || [] } ) {

next CODEJS if $CodeJS !~ m{ Key: \s $Param{Key}}xms;
$Exists = 1;
last CODEJS;
}

return 1 if $Exists;

my $AddCode = "// Key: $Param{Key}\n" . $Param{Code};

$Self->AddJSOnDocumentComplete(
Code => $AddCode,
);

return 1;
}

1;
Loading