Skip to content

task/4.x/form leave notification #442

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

Open
wants to merge 2 commits into
base: dgtkit-4.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import java.util.List;
import java.util.Locale;

// TODO rename to FormConstants
public final class WebConstants {
public final class FormsConstants {

private WebConstants() {
private FormsConstants() {

}

Expand All @@ -34,6 +33,10 @@ private WebConstants() {

public static final String DISABLE_FORM_LEAVING_JS
= "if(typeof disableFormLeavingConfirmation === 'function') disableFormLeavingConfirmation();";
public static final String ENABLE_FORM_LEAVING_JS
= "if(typeof enableFormLeavingConfirmation === 'function') enableFormLeavingConfirmation();";
public static final String BIND_FORM_LEAVING_CHECK = "if(typeof checkAnyChange === 'function') checkAnyChange();";
public static final String MARK_FORM_AS_CHANGED = "if(typeof checkAnyChange === 'function') markFormAsChanged();";

public static final String PARAM_PRINT = "print";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package org.devgateway.toolkit.forms.models;

import org.apache.wicket.model.IModel;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.wicket.components.form.GenericBootstrapFormComponent;

import java.text.SimpleDateFormat;
Expand All @@ -22,7 +22,7 @@
/**
* @author mpostelnicu Converter for {@link GenericBootstrapFormComponent}
* viewModeField This will be used when
* {@link WebConstants#PARAM_VIEW_MODE} is true in the browser and will
* {@link FormsConstants#PARAM_VIEW_MODE} is true in the browser and will
* convert the model object to something printable (string-like)
*/
public class ViewModeConverterModel<T> implements IModel<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.request.cycle.RequestCycle;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.wicket.components.form.GenericBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.events.EditingDisabledEvent;
import org.devgateway.toolkit.forms.wicket.events.EditingEnabledEvent;
Expand Down Expand Up @@ -53,13 +53,13 @@ public static MarkupContainer addRequiredFlagBootstrapFormComponent(final boolea
}

/**
* Returns true if the {@link WebConstants#PARAM_VIEW_MODE} is used as a
* Returns true if the {@link FormsConstants#PARAM_VIEW_MODE} is used as a
* parameter
*
* @return
*/
public static boolean isViewMode() {
return RequestCycle.get().getRequest().getRequestParameters().getParameterValue(WebConstants.PARAM_VIEW_MODE)
return RequestCycle.get().getRequest().getRequestParameters().getParameterValue(FormsConstants.PARAM_VIEW_MODE)
.toBoolean(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.util.JQueryUtil;
import org.devgateway.toolkit.forms.wicket.components.form.BootstrapAddButton;
import org.devgateway.toolkit.forms.wicket.components.form.BootstrapDeleteButton;
Expand Down Expand Up @@ -253,6 +254,14 @@ private BootstrapDeleteButton getRemoveChildButton(final T item) {
@Serial
private static final long serialVersionUID = 6350277641786762027L;

@Override
protected String getOnClickScript() {
if (item.isNew()) {
return super.getOnClickScript();
}
return FormsConstants.MARK_FORM_AS_CHANGED;
}

@Override
protected void onSubmit(final AjaxRequestTarget target) {
ListViewSectionPanel.this.getModelObject().remove(item);
Expand All @@ -274,6 +283,11 @@ final BootstrapAddButton getAddNewChildButton() {
@Serial
private static final long serialVersionUID = 5195431828773253867L;

@Override
protected String getOnClickScript() {
return FormsConstants.MARK_FORM_AS_CHANGED;
}

@Override
protected void onSubmit(final AjaxRequestTarget target) {
final T newChild = createNewChild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.wicket.components.table.AjaxFallbackBootstrapDataTable;
import org.devgateway.toolkit.forms.wicket.providers.ListDataProvider;

Expand All @@ -33,7 +33,7 @@ public class PageableTablePanel<T extends Serializable, PARENT extends Serializa

protected ISortableDataProvider<T, String> dataProvider;

protected int rowsPerPage = WebConstants.PAGE_SIZE;
protected int rowsPerPage = FormsConstants.PAGE_SIZE;

protected List<IColumn<T, String>> columns = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
import org.apache.wicket.request.IRequestHandler;
import org.devgateway.toolkit.forms.FormsConstants;

/**
* AJAX update and file download.
Expand Down Expand Up @@ -33,7 +34,11 @@ public void initiate(final AjaxRequestTarget target) {
}

// the timeout is needed to let Wicket release the channel
target.appendJavaScript("setTimeout(\"window.location.href='" + url + "'\", 100);");
target.appendJavaScript("setTimeout(function() {"
+ FormsConstants.DISABLE_FORM_LEAVING_JS
+ "window.location.href='" + url + "';"
+ FormsConstants.ENABLE_FORM_LEAVING_JS
+ "}, 100);");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.validation.IValidator;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.wicket.components.PageableTablePanel;
import org.devgateway.toolkit.forms.wicket.components.table.AbstractBootstrapPagingNavigationWithError;
import org.devgateway.toolkit.forms.wicket.components.table.AjaxBootstrapNavigationToolbar;
Expand Down Expand Up @@ -68,7 +68,7 @@ public EditableTablePanel(final String id, final IModel<PARENT> parentModel) {

protected AjaxFallbackBootstrapDataTable buildDataTable() {
AjaxFallbackBootstrapDataTable dataTable = super.buildDataTable();
if (this.rowsPerPage != WebConstants.PAGE_SIZE_NO_LIMIT && usePagingWithErrors) {
if (this.rowsPerPage != FormsConstants.PAGE_SIZE_NO_LIMIT && usePagingWithErrors) {
AjaxBootstrapNavigationToolbar navToolbar = dataTable.getNavigationToolbar();
navToolbar.withPagingNavFactory(new PagingNavigationFactory(EditableTablePagination.class, this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.IModel;
import org.apache.wicket.validation.validator.StringValidator;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;

/**
* @author mpostelnicu
Expand All @@ -26,7 +26,7 @@
*
*/
public abstract class OptionallyRequiredTextAreaFieldComponent<TYPE> extends TextAreaFieldBootstrapFormComponent<TYPE> {
private StringValidator validator = WebConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXTAREA;
private StringValidator validator = FormsConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXTAREA;

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.apache.wicket.validation.validator.StringValidator;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.wicket.components.ComponentUtil;

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ protected InputBehavior getInputBehavior() {

private ToolkitSummernoteEditor summernoteEditor;

private StringValidator validator = WebConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXTAREA;
private StringValidator validator = FormsConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXTAREA;

private ToolkitSummernoteEditor.ToolkitSummernoteConfig config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.IModel;
import org.apache.wicket.validation.validator.StringValidator;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;

/**
* @author mpostelnicu
*
*/
public class TextAreaFieldBootstrapFormComponent<TYPE> extends GenericBootstrapFormComponent<TYPE, TextArea<TYPE>> {
private StringValidator validator = WebConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXTAREA;
private StringValidator validator = FormsConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXTAREA;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.validation.validator.StringValidator;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;

import java.math.BigDecimal;

Expand All @@ -28,7 +28,7 @@
public class TextFieldBootstrapFormComponent<TYPE> extends GenericBootstrapFormComponent<TYPE, TextField<TYPE>> {
private static final long serialVersionUID = 8062663141536130313L;

private StringValidator validator = WebConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXT;
private StringValidator validator = FormsConstants.StringValidators.MAXIMUM_LENGTH_VALIDATOR_ONE_LINE_TEXT;

public TextFieldBootstrapFormComponent(final String id, final IModel<String> labelModel, final IModel<TYPE> model) {
super(id, labelModel, model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.IModel;
import org.devgateway.toolkit.forms.FormsConstants;

/**
* @author idobre
Expand Down Expand Up @@ -53,6 +54,7 @@ protected PagingNavigator newPagingNavigator(final String navigatorId, final Dat
@Override
protected void onAjaxEvent(final AjaxRequestTarget target) {
target.add(table);
target.appendJavaScript(FormsConstants.BIND_FORM_LEAVING_CHECK);
}
}.withPagingNavFactory(navigationFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
package org.devgateway.toolkit.forms.wicket.components.table;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.navigation.paging.IPageable;
import org.apache.wicket.markup.html.navigation.paging.IPagingLabelProvider;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigation;
import org.devgateway.toolkit.forms.WebConstants;

/**
* @author idobre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.devgateway.toolkit.forms.WebConstants;
import org.devgateway.toolkit.forms.FormsConstants;
import org.devgateway.toolkit.forms.wicket.components.form.CheckBoxBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.CheckBoxToggleBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.CheckBoxYesNoToggleBootstrapFormComponent;
Expand Down Expand Up @@ -42,23 +42,23 @@ private ComponentUtil() {
}

/**
* Returns true if the {@link WebConstants#PARAM_VIEW_MODE} is used as a
* Returns true if the {@link FormsConstants#PARAM_VIEW_MODE} is used as a
* parameter
*
* @return
*/
public static boolean isViewMode() {
return RequestCycle.get().getRequest().getRequestParameters().getParameterValue(WebConstants.PARAM_VIEW_MODE)
return RequestCycle.get().getRequest().getRequestParameters().getParameterValue(FormsConstants.PARAM_VIEW_MODE)
.toBoolean(false);
}

/**
* Returns true if the {@link WebConstants#PARAM_PRINT} is used as a parameter
* Returns true if the {@link FormsConstants#PARAM_PRINT} is used as a parameter
*
* @return
*/
public static boolean isPrintMode() {
return RequestCycle.get().getRequest().getRequestParameters().getParameterValue(WebConstants.PARAM_PRINT)
return RequestCycle.get().getRequest().getRequestParameters().getParameterValue(FormsConstants.PARAM_PRINT)
.toBoolean(false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
/*******************************************************************************
* Copyright (c) 2015 Development Gateway, Inc and others.
*
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the MIT License (MIT) which accompanies this
* distribution, and is available at https://opensource.org/licenses/MIT
*
*
* Contributors: Development Gateway - initial API and implementation
******************************************************************************/
// if nothing has changed that the user can leave the page without any
// confirmation
var shouldConfirmFormLeaving = false;
var shouldConfirmFormLeaving = true;
var formChanged = false;

$(document).ready(function () {
function checkAnyChange() {
$(':input').each(function () {
$(this).on('change', function () {
shouldConfirmFormLeaving = true;
formChanged = true;
});
});

// set shouldConfirmFormLeaving when input has focus
// set formChanged when input has focus
$(':input').each(function () {
$(this).on('keyup', function () {
shouldConfirmFormLeaving = true;
formChanged = true;
});
});

Expand All @@ -38,7 +39,7 @@ $(document).ready(function () {
that.trigger('change');
}, 50);

shouldConfirmFormLeaving = true;
formChanged = true;
});

$(document).on('drop', ':input', function (e) {
Expand All @@ -53,14 +54,23 @@ $(document).ready(function () {
that.trigger('change');
}, 50);

shouldConfirmFormLeaving = true;
formChanged = true;
});
});
}

$(document).ready(function() { checkAnyChange(); });
/*
* Below works, but we only need to rebind on dynamic elements rendered based on some conditions.
* Therefore better to append BIND_FORM_LEAVING_CHECK when dynamic content is loaded.
$(document).ajaxComplete(function() { checkAnyChange(); });
*/

// confirmation modal before window unload
$(window).on('beforeunload', function () {
if (shouldConfirmFormLeaving) {
return "${formLeavingWarning}";
$(window).on('beforeunload', function (e) {
if (shouldConfirmFormLeaving && formChanged) {
e.preventDefault();
// heads up: the latest browsers use their own generic message, not customizable here.
return e.returnValue = "${formLeavingWarning}";
}
});

Expand All @@ -71,3 +81,7 @@ function enableFormLeavingConfirmation() {
function disableFormLeavingConfirmation() {
shouldConfirmFormLeaving = false;
}

function markFormAsChanged() {
formChanged = true;
}
Loading