Skip to content

Commit

Permalink
replace with 50.0.0-rc03 code; closes #1047
Browse files Browse the repository at this point in the history
  • Loading branch information
jflamy committed Jul 20, 2024
1 parent 1399e02 commit 6f029d3
Show file tree
Hide file tree
Showing 23 changed files with 1,191 additions and 131 deletions.
1 change: 1 addition & 0 deletions publicresults/frontend/components/ResultsPR.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ResultsPR extends LitElement {
}

render() {
this.$server.visibilityStatus(!window.document.hidden);
return html`
<link rel="stylesheet" type="text/css" .href="${"local/" + (this.stylesDir ?? "") + "/" + (this.video ?? "") + "colors" + (this.autoversion ?? "") + ".css"}" />
<link rel="stylesheet" type="text/css" .href="${"local/" + (this.stylesDir ?? "") + "/" + (this.video ?? "") + "results" + (this.autoversion ?? "") + ".css"}" />
Expand Down
183 changes: 183 additions & 0 deletions publicresults/frontend/unload/unload-observerPR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { html, LitElement, css } from "lit";
/*******************************************************************************
* Copyright (c) 2009-2023 Jean-François Lamy
*
* Licensed under the Non-Profit Open Software License version 3.0 ("NPOSL-3.0")
* License text at https://opensource.org/licenses/NPOSL-3.0
*******************************************************************************/

/**
* A web component that listens to {@code beforeunload} events and sends them to server-side.
* This requires Flow and a corresponding server-side Java component to work properly.
* Alternatively, make sure that this.$server.visibilityChange() is available.
* Based on the code written by Kaspar Scherrer and Stuart Robinson: https://vaadin.com/forum/thread/17523194/unsaved-changes-detect-page-exit-or-reload
*
* author: miki@vaadin.com
*/
export class UnloadObserver extends LitElement {
render() {
return html`
<form id="reloadForm" action="../expired" method="post" style="display: none;">
<input type="text" id="reloadTitle" name="reloadTitle" value="${this.reloadTitle}">
<input type="text" id="reloadText" name="reloadText" value="${this.reloadText}">
<input type="text" id="reloadLabel" name="reloadLabel" value="${this.reloadLabel}">
<input type="text" id="reloadUrl" name="reloadUrl" value="${this.reloadUrl}">
</form>`;
}

static get is() {
return "unload-observer-pr";
}

static get properties() {
return {
reloadTitle: {},
reloadText: {},
reloadLabel: {},
reloadUrl: {},
}
}

updated(changedProperties) {
console.warn("changed properties {} ", changedProperties)
if (changedProperties.has('reloadUrl')) {
this.postReload();
}
}

postReload() {
let form = this.shadowRoot.querySelector('#reloadForm');
console.warn("reload page, target location "+this.reloadUrl+" ");
form.submit();
}

/**
* Initialises the observer and registers a beforeunload listener.
*/
initObserver() {
const src = this;
if (window.Vaadin.unloadObserver === undefined) {
window.Vaadin.unloadObserver = {
attemptHandler: undefined,
};
}
if (window.Vaadin.unloadObserver.attemptHandler !== undefined) {
window.removeEventListener(
"beforeunload",
window.Vaadin.unloadObserver.attemptHandler
);
window.removeEventListener(
"unload",
window.Vaadin.unloadObserver.unloadHandler
);
window.removeEventListener(
"pagehide",
window.Vaadin.unloadObserver.unloadHandler
);
document.removeEventListener(
"visibilitychange",
window.Vaadin.unloadObserver.visibilityHandler
);
window.removeEventListener(
"blur",
window.Vaadin.unloadObserver.blurHandler
);
window.removeEventListener(
"focus",
window.Vaadin.unloadObserver.focusHandler
);
}

window.Vaadin.unloadObserver.attemptHandler = (event) => {
src.visibilityChange(src, event, "beforeunload")
};

window.Vaadin.unloadObserver.unloadHandler = (event) => {
src.visibilityChange(src, event, "unload")
};

window.Vaadin.unloadObserver.hideHandler = (event) =>
src.visibilityChange(src, event, "pagehide");

window.Vaadin.unloadObserver.visibilityHandler = (event) =>
{
console.warn("visibility "+document.visibilityState);
if (document.visibilityState === 'hidden') {
src.visibilityChange(src, event, "visibilityHidden");
} else {
src.visibilityChange(src, event, "visibilityShown");
}
}

window.Vaadin.unloadObserver.blurHandler = (event) =>
{
console.warn("blur "+document.visibilityState);
src.visibilityChange(src, event, "blur");
}

window.Vaadin.unloadObserver.focusHandler = (event) =>
{
console.warn("focus "+document.visibilityState);
src.visibilityChange(src, event, "focus");
}

window.addEventListener(
"beforeunload",
window.Vaadin.unloadObserver.attemptHandler
);
window.addEventListener(
"unload",
window.Vaadin.unloadObserver.unloadHandler
);
window.addEventListener(
"pagehide",
window.Vaadin.unloadObserver.hideHandler
);
window.addEventListener(
"blur",
window.Vaadin.unloadObserver.blurHandler
);
window.addEventListener(
"focus",
window.Vaadin.unloadObserver.focusHandler
);
document.addEventListener(
"visibilitychange",
window.Vaadin.unloadObserver.visibilityHandler
);

}

/**
* Invoked in response to beforeunload browser event.
* @param source An unload observer.
* @param event Event that happened.
*/
visibilityChange(source, event, change) {
if (window.Vaadin.unloadObserver.query) {
console.warn("UO: responding to unload attempt...");
event.preventDefault();
event.returnValue = "";
if (source.$server) {
source.$server.visibilityChange(change);
}
} else {
source.$server.unloadHappened(change);
}
}

/**
* Controls whether or not prevent unload events.
* @param value When {@code truthy} (recommended String "true"), unload event will be prevented.
*/
queryOnUnload(value) {
if (value) {
window.Vaadin.unloadObserver.query = "true";
} else {
delete window.Vaadin.unloadObserver.query;
}
}

}

customElements.define(UnloadObserver.is, UnloadObserver);
32 changes: 22 additions & 10 deletions publicresults/package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public default boolean isShowInitialDialog() {

public default HashMap<String, List<String>> readParams(Location location,
Map<String, List<String>> parametersMap) {
logger.debug("location {} getLocation {}", location.getPathWithQueryParameters(),
getLocation().getPathWithQueryParameters());
// logger.debug("location {} getLocation {}", location.getPathWithQueryParameters(),
// getLocation().getPathWithQueryParameters());

HashMap<String, List<String>> newParameterMap = new HashMap<>(parametersMap);

Expand Down Expand Up @@ -154,7 +154,7 @@ public default void setParameter(BeforeEvent event, @OptionalParameter String un

// change the URL to reflect the updated parameters
Location location2 = new Location(location.getPath(), new QueryParameters(URLUtils.cleanParams(params)));
logger.debug("setParameter {}", location2);
//logger.debug("setParameter {}", location2);
event.getUI().getPage().getHistory().replaceState(null, location2);
}

Expand Down Expand Up @@ -196,7 +196,7 @@ public default void updateURLLocation(UI ui, Location location, String parameter

Location location2 = new Location(location.getPath(), new QueryParameters(parametersMap));
ui.getPage().getHistory().replaceState(null, location2);
logger.debug("location2 = {}", location2.getPathWithQueryParameters());
//logger.debug("location2 = {}", location2.getPathWithQueryParameters());
setLocation(location2);
storeReturnURL();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.vaadin.flow.component.UI;

import app.owlcms.init.OwlcmsSession;
import app.owlcms.publicresults.DecisionReceiverServlet;
import app.owlcms.publicresults.TimerReceiverServlet;
import app.owlcms.publicresults.UpdateReceiverServlet;
import app.owlcms.uievents.TimerEvent;
Expand Down Expand Up @@ -179,10 +178,7 @@ protected void init() {
protected void onAttach(AttachEvent attachEvent) {
init();

eventBusRegister(this, TimerReceiverServlet.getEventBus());
eventBusRegister(this, UpdateReceiverServlet.getEventBus());
eventBusRegister(this, DecisionReceiverServlet.getEventBus());

this.ui = UI.getCurrent();
this.setFopName(OwlcmsSession.getFopName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected void onAttach(AttachEvent attachEvent) {
this.ui = attachEvent.getUI();
init();

eventBusRegister(this, TimerReceiverServlet.getEventBus());
//eventBusRegister(this, TimerReceiverServlet.getEventBus());
eventBusRegister(this, UpdateReceiverServlet.getEventBus());

setFopName(OwlcmsSession.getFopName());
Expand All @@ -208,5 +208,5 @@ private String formatDuration(Integer milliseconds) {
return (milliseconds != null && milliseconds >= 0) ? DurationFormatUtils.formatDurationHMS(milliseconds)
: (milliseconds != null ? milliseconds.toString() : "-");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import app.owlcms.init.OwlcmsSession;
import app.owlcms.prutils.SafeEventBusRegistrationPR;
import app.owlcms.publicresults.DecisionReceiverServlet;
import app.owlcms.publicresults.TimerReceiverServlet;
import app.owlcms.publicresults.UpdateReceiverServlet;
import app.owlcms.uievents.BreakTimerEvent;
import app.owlcms.uievents.DecisionEvent;
Expand Down Expand Up @@ -106,7 +104,7 @@ public void slaveDecision(DecisionEvent de) {
} else {
switch (de.getEventType()) {
case DOWN_SIGNAL:
logger.debug("*****showing down");
logger.debug("showing down");
this.getElement().callJsFunction("showDown", false, isSilenced());
break;
case FULL_DECISION:
Expand Down Expand Up @@ -218,28 +216,18 @@ protected void onAttach(AttachEvent attachEvent) {

this.ui = attachEvent.getUI();
eventBusRegister(this, UpdateReceiverServlet.getEventBus());
eventBusRegister(this, DecisionReceiverServlet.getEventBus());
eventBusRegister(this, TimerReceiverServlet.getEventBus());
// eventBusRegister(this, DecisionReceiverServlet.getEventBus());
// eventBusRegister(this, TimerReceiverServlet.getEventBus());

setFopName(OwlcmsSession.getFopName());
}

@Override
protected void onDetach(DetachEvent detachEvent) {
super.onDetach(detachEvent);
this.ui = null;
try {
DecisionReceiverServlet.getEventBus().unregister(this);
} catch (Exception e) {
}
try {
TimerReceiverServlet.getEventBus().unregister(this);
} catch (Exception e) {
}
}

private void init() {
getElement().setProperty("publicFacing", true);
}

}
Loading

0 comments on commit 6f029d3

Please sign in to comment.