Skip to content

Commit

Permalink
Prevent modal dismiss in case of form submission errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xfyre committed Apr 16, 2016
1 parent cd4e364 commit f88e50f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.xfyre</groupId>
<artifactId>tapestry5-xtensions</artifactId>
<version>0.9-SNAPSHOT</version>
<version>0.91-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.tapestry</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.xfyre.tapestry5.xtensions.components;

import org.apache.commons.lang3.StringUtils;
import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Events;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.internal.util.Holder;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.Request;
Expand Down Expand Up @@ -35,15 +35,15 @@ public class ModalDialog implements ClientElement {
@Property @Parameter(required=false,defaultPrefix=BindingConstants.LITERAL)
private String title;

@Property @Parameter(required=false,defaultPrefix=BindingConstants.LITERAL)
private String width;

@Property @Parameter(required=false,defaultPrefix=BindingConstants.LITERAL)
private String height;

@Property @Parameter(required=false,defaultPrefix=BindingConstants.LITERAL,value="false")
private Boolean enableFooter;

/**
* Zone to monitor for updates
*/
@Parameter(required=false,defaultPrefix=BindingConstants.PROP)
private String monitorZone;

/**
* Zone to update when user dismisses modal
*/
Expand Down Expand Up @@ -78,30 +78,30 @@ public String getClientId () {

@AfterRender
private void afterRender () {
JSONObject params = new JSONObject (
final JSONObject params = new JSONObject (
"id", getClientId (),
"hide", hideAfterZoneUpdate
);

if ( updateZone != null ) {
Link link = updateContext == null ?
resources.createEventLink ( "modalHidden" ) :
resources.createEventLink ( "modalHidden", updateContext );
params.put ( "zoneId", updateZone );
final Link link = updateContext == null ?
resources.createEventLink ( "modalHiddenInternal" ) :
resources.createEventLink ( "modalHiddenInternal", updateContext );
params.put ( "updateZone", updateZone );
params.put ( "monitorZone", monitorZone );
params.put ( "updateZoneLink", link.toAbsoluteURI () );
}

if ( StringUtils.isNotBlank ( width ) )
params.put ( "width", width );

if ( StringUtils.isNotBlank ( height ) )
params.put ( "height", height );

javaScriptSupport.require ( "t5xtensions/modaldialog" ).with ( params );
}

// public Integer getLeftMargin () {
// return - ( width / 2 );
// }
Object onModalHiddenInternal ( EventContext ctx ) {
final Holder<Object> holder = new Holder<> ();
final boolean eventResult = resources.triggerContextEvent ( EVENT_MODAL_HIDDEN, ctx, result -> {
holder.put ( result );
return true;
} );
return eventResult ? holder.get () : null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xfyre.tapestry5.xtensions.mixins;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.MixinAfter;
import org.apache.tapestry5.corelib.components.Form;

@MixinAfter
public class ModalIntegration {
@InjectContainer
private Form form;

void afterRender ( MarkupWriter markupWriter ) {
if ( form.getHasErrors () )
markupWriter.attributeNS ( null, "data-submission-failed", "true" );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.xfyre.tapestry5.xtensions.services;

import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.plastic.PlasticClass;
import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
import org.apache.tapestry5.services.transform.TransformationSupport;

import com.xfyre.tapestry5.xtensions.mixins.ModalIntegration;

public class ModalIntegrationWorker implements ComponentClassTransformWorker2 {
@Override
public void transform ( PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model ) {
if ( Form.class.getName ().equals ( plasticClass.getClassName () ) )
model.addMixinClassName ( ModalIntegration.class.getName () );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ScopeConstants;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
import org.apache.tapestry5.services.ComponentClassResolver;
import org.apache.tapestry5.services.LibraryMapping;
import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
import org.apache.tapestry5.upload.internal.services.MultipartDecoderImpl;
import org.apache.tapestry5.upload.services.MultipartDecoder;
import org.slf4j.Logger;
Expand All @@ -18,7 +20,7 @@ public class XtensionsModule {
public static void initT5XtensionsModule ( Logger logger ) {
logger.info ( "Initializing t5xtensions" );
}

@Contribute(ComponentClassResolver.class)
public static void contributeComponentClassResolver ( Configuration<LibraryMapping> configuration ) {
configuration.add ( new LibraryMapping ( "t5xtensions", "com.xfyre.tapestry5.xtensions" ) );
Expand All @@ -32,6 +34,7 @@ public static MultipartDecoder buildMultipartDecoder2 ( RegistryShutdownHub shut

if ( needToAddShutdownListener.getAndSet ( false ) ) {
shutdownHub.addRegistryShutdownListener ( new Runnable () {
@Override
@SuppressWarnings("deprecation")
public void run () {
org.apache.commons.io.FileCleaner.exitWhenFinished ();
Expand All @@ -49,4 +52,9 @@ public static void contributeServiceOverride (
overrides.add ( MultipartDecoder.class, multipartDecoder );
}

@Contribute(ComponentClassTransformWorker2.class)
public static void provideWorkers ( OrderedConfiguration<ComponentClassTransformWorker2> workers ) {
workers.addInstance ( "ModalIntegrationWorker", ModalIntegrationWorker.class );
}

}
34 changes: 13 additions & 21 deletions src/main/resources/META-INF/modules/t5xtensions/modaldialog.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
define(["jquery","t5/core/events", "t5/core/zone", "bootstrap/modal"], function($, events, zone) {
return function(spec) {
var $modal = $('#' + spec.id);

/*
if (spec.width) {
var realWidth = spec.width > $(window).width() ? $(window).width() : spec.width;
$('#' + spec.id + ' > .modal-dialog').width(realWidth);
}
*/

var $modal = $('#' + spec.id)

$modal.on('hidden.bs.modal', function(event) {
$modal.remove();

$modal.remove()
if (spec.updateZoneLink) {
var $zone = $('#' + spec.zoneId);
$zone.trigger(events.zone.refresh, {url: spec.updateZoneLink});
$('#' + spec.updateZone).trigger(events.zone.refresh, {url: spec.updateZoneLink})
}
});
})

if (spec.monitorZone)
$('#' + spec.monitorZone).on(events.zone.didUpdate, function(event){
if (spec.hide && !$modal.find('form[data-submission-failed="true"]').length) $modal.modal('hide')
})

if (spec.hide && spec.zoneId) {
var $zone = $('#' + spec.zoneId);
$zone.on(events.zone.update, function(){ $modal.modal('hide'); })
}
$modal.modal()
}
});

$modal.modal();
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<h3 id="myModalLabel" class="modal-title">${title}</h3>
</div>
<div class="modal-body">
<t:delegate p:to="content"/>
<t:delegate to="content"/>
</div>
<div class="modal-footer" t:type="if" p:test="enableFooter">
<div class="modal-footer" t:type="if" test="enableFooter">
<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">${message:button.close}</button>
</div>
</div>
Expand Down

0 comments on commit f88e50f

Please sign in to comment.