Skip to content

Commit 8f8267b

Browse files
drgrice1Alex-Jordan
authored andcommitted
Suggestions for improvement.
Inline javascript is bad form. There is already a javascript file dedicated to the ProblemSetDetail.pm page, so put the javascript there instead. The return value of the Mojolicious::Plugin::TagHelpers `tag` method is already a Mojo::ByteStream object. The issue that causes the display to not work is that the contents of the `div` are not, since they are just bare strings concatentated together, and so they are html escaped. So use a Mojo::Collection that is joined by the empty string. That also returns a Mojo::ByteStream object, and so it is not html escaped. This is also consistent with how I have done this sort of thing elsewhere in the code. Also remove what appears to be an errant `warn` statement.
1 parent 4375271 commit 8f8267b

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

htdocs/js/ProblemSetDetail/problemsetdetail.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,10 @@
414414
comboBoxSelect.addEventListener('change',
415415
() => comboBoxText.value = comboBoxSelect.options[comboBoxSelect.selectedIndex].value);
416416
});
417+
418+
// Set up seed randomization buttons.
419+
for (const btn of document.querySelectorAll('.randomize-seed-btn')) {
420+
const input = document.getElementById(btn.dataset.seedInput);
421+
if (input) btn.addEventListener('click', () => (input.value = Math.floor(Math.random() * 10000)));
422+
}
417423
})();

lib/WeBWorK/ContentGenerator/Instructor/ProblemSetDetail.pm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -947,22 +947,22 @@ sub fieldHTML ($c, $userID, $setID, $problemID, $globalRecord, $userRecord, $fie
947947
);
948948
if ($field eq 'problem_seed') {
949949
# Insert a randomization button
950-
$inputType = Mojo::ByteStream->new($c->tag(
950+
$inputType = $c->tag(
951951
'div',
952952
class => 'input-group input-group-sm',
953953
style => 'min-width: 7rem',
954-
$c->number_field(@field_args, min => 0)->to_string
955-
. $c->tag(
954+
$c->c(
955+
$c->number_field(@field_args, min => 0),
956+
$c->tag(
956957
'button',
957-
type => 'button',
958-
class => 'btn btn-sm btn-secondary',
959-
title => 'randomize',
960-
onclick =>
961-
"(function() {document.getElementById('$recordType.$recordID.${field}_id').value = Math.floor(Math.random() * 10000);})();",
958+
type => 'button',
959+
class => 'randomize-seed-btn btn btn-sm btn-secondary',
960+
title => 'randomize',
961+
data => { seed_input => "$recordType.$recordID.${field}_id" },
962962
$c->tag('i', class => 'fa-solid fa-shuffle')
963963
)
964-
))->html_unescape;
965-
warn(ref($inputType));
964+
)->join('')
965+
);
966966
} else {
967967
$inputType = $c->text_field(@field_args);
968968
}

0 commit comments

Comments
 (0)