tags:

views:

153

answers:

1

The problem is like this; A webpage contains multiple form elements, which can be altered and saved by the user via a save button, or changes can be discarded.

If the user attempts to navigate away from the page without saving the changes, I need a modal window to pop us asking if the user would like to save the changes before leaving the page.

How would I go about checking to see if the page/form model had been changed by a user since it was first loaded, and how could I initiate this check when any page link is clicked?

Any response or suggestions would be much appreciated,

Thanks.

+3  A: 

I think you would be looking for a javascript-only solution, usually packaged as a wicket behavior.

implementation depends on the javascript library you use, here is some prototype code:

var windowdirty = false;
var windowdirtyinitialized = false;

function initwindowdirty(){
    if(windowdirtyinitialized)return;
    windowdirtyinitialized=true;

    Event.observe(window,"beforeunload",function(){
        return (!windowdirty || 
          confirm("You have started entering values, do you really want to leave");
    });
}

function monitor(componentId){
    $(componentId).observe("change",function(){
        windowdirty = true;
    });
}

function undirty(){
    windowdirty=false;
}

We'll put this in a File called DontLeaveBehavior.js

Here's a behavior that uses this javascript file:

public class DontLeaveBehavior extends AbstractBehavior{

    /**
     * {@inheritDoc}
     */
    @Override
    public void renderHead(final IHeaderResponse response){
        response.renderJavascriptReference(new JavascriptResourceReference(DontLeaveBehavior.class,
            "DontLeaveBehavior.js"));
        response.renderOnDomReadyJavascript("initwindowdirty();");
        super.renderHead(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void bind(final Component component){
        super.bind(component);
        component.setOutputMarkupId(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onRendered(final Component component){
        final Response response = RequestCycle.get().getResponse();
        response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
        response.write("monitor('" + component.getMarkupId() + "');");
        response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
    }

}

Now here's a page that automatically assigns this behavior to all of it's children that are text components:

public class Mypage extends WebPage{

    ...

    private boolean behaviorAssigned = false;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onBeforeRender(){
        if(!behaviorAssigned){
            behaviorAssigned=true;
            visitChildren(new IVisitor<Component>(){

                @Override
                public Object component(Component component){
                    if(component instanceof AbstractTextComponent<?>){
                        component.add(new DontLeaveBehavior());
                    }
                    return null;
                }
            });
        }
        super.onBeforeRender();
    }

}

and last but not least, your submit button has to call undirty() of course.

None of this has been tested, because I have to go home now to have dinner with my wife & kids (which is even more fun than wicket coding, of course), but it should get you started.

The prototype specific portion should be easily ported to any other javascript lib, but you probably shouldn't do it without a lib if you don't know what you're doing.


Edit:

I have created a new version of this that works with prototype and mootools and posted it on my weblog. This version is only installed to the form component and automatically attaches itself to the children via javascript.

EDIT again: there, now the link is working

seanizer
Thanks for your answer, however it appears the client wants the popup to be a modal window. So using Javascript may not be the best solution. Sorry I didn't mention that in the question.I will take a look at the rest, I'm sure there's something I can use :) Thanks again! If I solve my problem then I'll mark your answer.
Jivings
Sorry, I entered the wrong method name. It's 'confirm', not 'prompt'. And yes, it's a modal window and no, you can't create one without javascript. However, I am working on a slightly more elegant solution which I'll post shortly.
seanizer
new version available, see my update.
seanizer
Wow, thanks for your solutions. I have come up with a much less elegant solution, but one which doesn't require the use of Javascript.Firstly I created an Observable object and made the Parent page the Observer.I used the onBeforeRender function to assign the form elements onSubmit or onUpdate functions. When the elements are altered, the functions call the Observable's notifyObservers method, and thus the parent is notified of any changes to the page, causing it to open a modal window.
Jivings
what do you mean by a modal window? the only way to have a real modal window in a web page is through javascript alert() or confirm(). What are you using?
seanizer
A wicket ModalWindow.http://www.wicketframework.org/wicket-extensions/apidocs/wicket/extensions/ajax/markup/html/modal/ModalWindow.htmlAnd by doesn't require the use of Javascript, I guess I really mean external libraries.
Jivings
I see. But a) it's no problem to use external libraries with wicket and b) it should also be no problem to write a few javascript functions that provide the functionality used, but the css selector part will be tough, so you'll probably need a different way to locate the form components (I'm referring to the version in my blog post now)
seanizer
Yes, but my way I don't have to worry about external libraries and Javascript issues, as I can code it only using Java.Thanks for your help anyway :) Without it I wouldn't have considered the onBeforeRender method.
Jivings
yup. onBeforeRender() is a powerful ally :-)
seanizer