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

WIP Evaluate viability of using HTMX #5913

Closed
Closed
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
3 changes: 3 additions & 0 deletions assets/assetpack.def
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
< javascripts/needleeditor.js
< javascripts/shapes.js

! htmx.js
< ../node_modules/htmx.org/dist/htmx.min.js

! bootstrap.js
< ../node_modules/jquery/dist/jquery.min.js
< javascripts/openqa.js
Expand Down
51 changes: 34 additions & 17 deletions assets/javascripts/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@
window.alert(context + getXhrError(jqXHR, textStatus, errorThrown));
}

function updateNumerOfComments() {
function updateNumberOfComments() {
const commentsLink = document.querySelector('a[href="#comments"]');
if (commentsLink) {
const linkText = 'Comments (' + document.getElementsByClassName('comment-row').length + ')';
commentsLink.innerHTML = linkText;
}
}

function deleteComment(deleteButton) {
const author = deleteButton.dataset.author;
if (!window.confirm('Do you really want to delete the comment written by ' + author + '?')) {
return;
}
$.ajax({
url: deleteButton.dataset.deleteUrl,
method: 'DELETE',
success: () => {
$(deleteButton).parents('.comment-row, .pinned-comment-row').remove();
updateNumerOfComments();
},
error: showXhrError.bind(undefined, "The comment couldn't be deleted: ")
});
}
//function deleteComment(deleteButton) {
// const author = deleteButton.dataset.author;
// if (!window.confirm('Do you really want to delete the comment written by ' + author + '?')) {
// return;
// }
// $.ajax({
// url: deleteButton.dataset.deleteUrl,
// method: 'DELETE',
// success: () => {
// $(deleteButton).parents('.comment-row, .pinned-comment-row').remove();
// updateNumberOfComments();
// },
// error: showXhrError.bind(undefined, "The comment couldn't be deleted: ")
// });
//}

function updateComment(form) {
const textElement = form.text;
Expand Down Expand Up @@ -135,7 +135,7 @@
nextElement.parentNode.insertBefore(commentRow, nextElement);
$('html, body').animate({scrollTop: commentRow.offsetTop}, 1000);
textElement.value = '';
updateNumerOfComments();
updateNumberOfComments();
},
error: () => location.reload()
});
Expand All @@ -148,4 +148,21 @@
const textarea = document.getElementById('text');
const template = button.dataset.template;
textarea.value += textarea.value ? '\n' + template : template;
throw new Error("DELETION ATTEMPT");

Check failure on line 151 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Replace `"DELETION·ATTEMPT"` with `'DELETION·ATTEMPT'`
}

function getCSRFToken() {
const metaTag = document.querySelector('meta[name="csrf-token"]');
return metaTag ? metaTag.getAttribute('content') : '';
}

htmx.logger = function(elt, event, data) {

Check failure on line 159 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Insert `·`
if(console) {

Check failure on line 160 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Replace `··if` with `if·`
console.log(event, elt, data);

Check failure on line 161 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Replace `········` with `····`
}

Check failure on line 162 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Replace `····` with `··`
}

Check failure on line 163 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Insert `;`

htmx.on('htmx:configRequest', function(event) {

Check failure on line 165 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Insert `·`
event.detail.headers['X-CSRF-Token'] = getCSRFToken();
});

Check failure on line 168 in assets/javascripts/comments.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Delete `⏎`
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dagre-d3": "^0.6.4",
"datatables.net-bs5": "^2.1.6",
"fork-awesome": "^1.2.0",
"htmx.org": "^2.0.2",
"jquery": "^3.7.1",
"jquery-ujs": "^1.2.3",
"timeago": "^1.6.7"
Expand Down
1 change: 1 addition & 0 deletions t/ui/15-comments.t
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ sub test_comment_editing {
};

subtest 'remove' => sub {

# try to remove the first displayed comment (the one which has just been edited)
$driver->find_element('button.remove-edit-button')->click();

Expand Down
79 changes: 44 additions & 35 deletions templates/webapi/comments/comment_row.html.ep
Original file line number Diff line number Diff line change
@@ -1,54 +1,63 @@
% use OpenQA::Constants;

% if (!$context->{pinned}) {
<div class="row comment-row">
<div class="row comment-row comment-container">
<div class="col-sm-1">
<img class="media-object img-circle" src="<%= $user->gravatar(60) %>" alt="profile" title="<%= $user->name %>">
</div>
<div class="col-sm-11">
% }
% else {
<div class="row pinned-comment-row">
<meta name="csrf-token" content="<%= csrf_token %>">
<div class="row pinned-comment-row comment-container">
<div class="col-sm-12">
% }
<div class="media-body comment-body" id="comment-<%= $comment_id %>">
<div class="well well-lg">
<form onsubmit="updateComment(this); return false;" onreset="hideCommentEditor(this); return true;" data-put-url="<%= url_for($put_action => ($context->{type}.'_id' => $context->{id}, comment_id => $comment_id)) %>">
% if (current_user && current_user->is_admin) {
<button class="btn btn-danger btn-circle btn-sm remove-edit-button" type="button" name="removeComment" onclick="deleteComment(this);" data-author="<%= $user->name %>" data-delete-url="<%= url_for($delete_action => ($context->{type}.'_id' => $context->{id}, comment_id => $comment_id)) %>">
<button class="btn btn-danger btn-circle btn-sm remove-edit-button"
type="button"
name="removeComment"
data-author="<%= $user->name %>"
hx-delete="<%= url_for($delete_action => ($context->{type}.'_id' => $context->{id}, comment_id => $comment_id)) %>"
onclick="return confirm('Do you really want to delete the comment written by <%= $user->name %>?')"
hx-target="closest .comment-container"
hx-swap="delete"
hx-on:htmx:afterSwap="updateNumberOfComments()">
<i class="fa fa-fw fa-trash"></i>
</button>
% }
% if (current_user && (current_user->id eq $user->id)) {
<button class="btn btn-primary btn-circle btn-sm trigger-edit-button" type="button" name="editComment" onclick="showCommentEditor(this.form);">
<i class="fa fa-fw fa-pencil"></i>
</button>
% }
<h4 class="media-heading">
% if($comment) {
<%= $user->name %> wrote
<a href="#comment-<%= $comment_id %>" class="comment-anchor"><abbr class="timeago" title="<%= $comment->t_created->datetime() %>Z"><%= format_time($comment->t_created) %></abbr></a>
% if ($comment->t_updated->subtract(seconds => OpenQA::Constants::DB_TIMESTAMP_ACCURACY) >= $comment->t_created) {
(last edited <abbr class="timeago" title="<%= $comment->t_updated->datetime() %>Z"><%= format_time($comment->t_updated) %></abbr>)
% }
% }
</h4>
<div class="media-comment markdown">
% if($comment) {
%= $comment->rendered_markdown
% }
</div>
% if (current_user && ((current_user->id eq $user->id))) {
<textarea class="form-control comment-editing-control" name="text" rows="5"><%= $comment ? $comment->text : '' %></textarea>
<button class="btn btn-success btn-circle comment-editing-control" type="submit" name="applyChanges">
<i class="fa fa-check-circle"></i> Apply changes
</button>
<button class="btn btn-warning btn-circle comment-editing-control" type="reset" name="discardChanges">
<i class="fa fa-trash"></i> Discard changes
</button>
% }
</form>
</div>
</div>
</div>
% if (current_user && (current_user->id eq $user->id)) {
<button class="btn btn-primary btn-circle btn-sm trigger-edit-button" type="button" name="editComment" onclick="showCommentEditor(this.form);">
<i class="fa fa-fw fa-pencil"></i>
</button>
% }
<h4 class="media-heading">
% if($comment) {
<%= $user->name %> wrote
<a href="#comment-<%= $comment_id %>" class="comment-anchor"><abbr class="timeago" title="<%= $comment->t_created->datetime() %>Z"><%= format_time($comment->t_created) %></abbr></a>
% if ($comment->t_updated->subtract(seconds => OpenQA::Constants::DB_TIMESTAMP_ACCURACY) >= $comment->t_created) {
(last edited <abbr class="timeago" title="<%= $comment->t_updated->datetime() %>Z"><%= format_time($comment->t_updated) %></abbr>)
% }
% }
</h4>
<div class="media-comment markdown">
% if($comment) {
%= $comment->rendered_markdown
% }
</div>
% if (current_user && ((current_user->id eq $user->id))) {
<textarea class="form-control comment-editing-control" name="text" rows="5"><%= $comment ? $comment->text : '' %></textarea>
<button class="btn btn-success btn-circle comment-editing-control" type="submit" name="applyChanges">
<i class="fa fa-check-circle"></i> Apply changes
</button>
<button class="btn btn-warning btn-circle comment-editing-control" type="reset" name="discardChanges">
<i class="fa fa-trash"></i> Discard changes
</button>
% }
</form>
</div>
</div>
</div>
</div>
Loading