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

[#57904] meeting reload button will take you to your previous spot on the page #16809

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
7 changes: 3 additions & 4 deletions app/views/layouts/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ See COPYRIGHT and LICENSE files for more details.
</div>
<% end %>
<div class="content-overlay"></div>
<main id="content-wrapper" class="<%= initial_classes %>">
<%= content_tag :main, id: "content-wrapper", class: initial_classes, data: stimulus_content_data do %>
<%# Primerized flash messages are being rendered separately %>
<div id="primerized-flash-messages" class="op-primer-flash">
<%= render_primerized_flash %>
Expand All @@ -134,8 +134,7 @@ See COPYRIGHT and LICENSE files for more details.
<% end %>
<%= content_tag :div,
id: 'content',
class: "#{initial_classes} #{'content--split' if content_for?(:content_body_right)}",
data: stimulus_content_data do %>
class: "#{initial_classes} #{'content--split' if content_for?(:content_body_right)}" do %>
<h1 class="hidden-for-sighted accessibility-helper"><%= t(:label_content) %></h1>
<% if content_for?(:content_header) %>
<div id="content-header">
Expand All @@ -162,7 +161,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= content_for :content_body_right %>
<% end %>
<% end %>
</main>
<% end %>
</div>
<div id="ajax-indicator" style="display:none;"><span><%= t(:label_loading) %></span></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/development/concepts/stimulus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ You need to take care to prefix all actions, values etc. with the exact same pat

### Requiring a page controller

If you have a single controller used in a partial, we have added a helper to use in a partial in order to append a controller to the `#content`tag. This is useful if your template doesn't have a single DOM root. For example, to load the dynamic `project-storage-form` controller and provide a custom value to it:
If you have a single controller used in a partial, we have added a helper to use in a partial in order to append a controller to the `#content-wrapper` tag. This is useful if your template doesn't have a single DOM root. For example, to load the dynamic `project-storage-form` controller and provide a custom value to it:

```erb
<% content_controller 'project-storage-form',
Expand Down
39 changes: 39 additions & 0 deletions frontend/src/stimulus/controllers/poll-for-changes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ export default class PollForChangesController extends ApplicationController {
url: String,
interval: Number,
reference: String,
autoscrollEnabled: Boolean,
};

static targets = ['reloadButton'];

declare reloadButtonTarget:HTMLLinkElement;

declare referenceValue:string;
declare urlValue:string;
declare intervalValue:number;
declare autoscrollEnabledValue:boolean;

private interval:number;

Expand All @@ -52,13 +58,21 @@ export default class PollForChangesController extends ApplicationController {
void this.triggerTurboStream();
}, this.intervalValue || 10_000);
}

if (this.autoscrollEnabledValue) {
window.addEventListener('DOMContentLoaded', this.autoscrollToLastKnownPosition.bind(this));
}
}

disconnect() {
super.disconnect();
clearInterval(this.interval);
}

reloadButtonTargetConnected() {
this.reloadButtonTarget.addEventListener('click', this.rememberCurrentScrollPosition.bind(this));
}

triggerTurboStream() {
void fetch(`${this.urlValue}?reference=${this.referenceValue}`, {
headers: {
Expand All @@ -73,4 +87,29 @@ export default class PollForChangesController extends ApplicationController {
}
});
}

rememberCurrentScrollPosition() {
const currentPosition = document.getElementById('content-body')?.scrollTop;

if (currentPosition !== undefined) {
sessionStorage.setItem(this.scrollPositionKey(), currentPosition.toString());
}
}

autoscrollToLastKnownPosition() {
const lastKnownPos = sessionStorage.getItem(this.scrollPositionKey());
if (lastKnownPos) {
const content = document.getElementById('content-body');

if (content) {
content.scrollTop = parseInt(lastKnownPos, 10);
}
}

sessionStorage.removeItem(this.scrollPositionKey());
}

private scrollPositionKey():string {
return `${this.urlValue}/scrollPosition`;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<%=
params = {
controller: "poll-for-changes",
poll_for_changes_reference_value: @meeting.changed_hash,
poll_for_changes_url_value: check_for_updates_meeting_path(@meeting),
poll_for_changes_interval_value: check_for_updates_interval
}
helpers.content_controller "poll-for-changes",
poll_for_changes_reference_value: @meeting.changed_hash,
poll_for_changes_url_value: check_for_updates_meeting_path(@meeting),
poll_for_changes_interval_value: check_for_updates_interval,
poll_for_changes_autoscroll_enabled_value: true

component_wrapper do
render(Primer::OpenProject::PageHeader.new(
test_selector: "meeting-page-header",
state: @state,
data: params
state: @state
)) do |header|
header.with_title do |title|
title.with_editable_form(model: @meeting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def call
banner.with_action_button(
tag: :a,
href: helpers.meeting_path(meeting),
data: { turbo: false },
data: { turbo: false, poll_for_changes_target: "reloadButton" },
size: :medium
) { I18n.t("label_meeting_reload") }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def trigger_dropdown_menu_item(name)

def trigger_change_poll
script = <<~JS
var target = document.querySelector('[data-test-selector="meeting-page-header"]');
var target = document.querySelector('#content-wrapper');
var controller = window.Stimulus.getControllerForElementAndIdentifier(target, 'poll-for-changes')
controller.triggerTurboStream();
JS
Expand Down