Search in sources :

Example 1 with IdAllocator

use of org.apache.tapestry5.ioc.util.IdAllocator in project tapestry-5 by apache.

the class ActivationRequestParameterWorker method preallocateName.

private static void preallocateName(TransformationSupport support, final String parameterName) {
    ComponentEventHandler handler = new ComponentEventHandler() {

        public void handleEvent(Component instance, ComponentEvent event) {
            IdAllocator idAllocator = event.getEventContext().get(IdAllocator.class, 0);
            idAllocator.allocateId(parameterName);
        }
    };
    support.addEventHandler(EventConstants.PREALLOCATE_FORM_CONTROL_NAMES, 1, "ActivationRequestParameterWorker preallocate form control name '" + parameterName + "' event handler", handler);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component)

Example 2 with IdAllocator

use of org.apache.tapestry5.ioc.util.IdAllocator in project tapestry-5 by apache.

the class AjaxFormUpdateControllerImpl method createInternalFormSupport.

private InternalFormSupport createInternalFormSupport(String formClientId, String formComponentId, ComponentActionSink actionSink) {
    // Kind of ugly, but the only way to ensure we don't have name collisions on the
    // client side is to force a unique id into each name (as well as each id, but that's
    // JavaScriptSupport's job). It would be nice if we could agree on the uid, but
    // not essential.
    String uid = Long.toHexString(System.nanoTime());
    IdAllocator idAllocator = new IdAllocator("_" + uid);
    Component formComponent = componentSource.getComponent(formComponentId);
    CaptureResultCallback<InternalFormSupport> callback = CaptureResultCallback.create();
    // This is a bit of a back-door to access a non-public method!
    formComponent.getComponentResources().triggerEvent("internalCreateRenderTimeFormSupport", new Object[] { formClientId, actionSink, idAllocator }, callback);
    return callback.getResult();
}
Also used : IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) InternalFormSupport(org.apache.tapestry5.corelib.internal.InternalFormSupport) Component(org.apache.tapestry5.runtime.Component)

Example 3 with IdAllocator

use of org.apache.tapestry5.ioc.util.IdAllocator in project tapestry-5 by apache.

the class Form method beginRender.

void beginRender(MarkupWriter writer) {
    Link link = resources.createFormEventLink(EventConstants.ACTION, context);
    String actionURL = secure && secureEnabled ? link.toAbsoluteURI(true) : link.toURI();
    actionSink = new ComponentActionSink(logger, clientDataEncoder);
    clientId = javascriptSupport.allocateClientId(resources);
    // Pre-register some names, to prevent client-side collisions with function names
    // attached to the JS Form object.
    IdAllocator allocator = new IdAllocator();
    preallocateNames(allocator);
    formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
    environment.push(FormSupport.class, formSupport);
    environment.push(ValidationTracker.class, tracker);
    if (autofocus) {
        ValidationDecorator autofocusDecorator = new AutofocusValidationDecorator(environment.peek(ValidationDecorator.class), tracker, javascriptSupport);
        environment.push(ValidationDecorator.class, autofocusDecorator);
    }
    // Now that the environment is setup, inform the component or other
    // listeners that the form
    // is about to render.
    resources.triggerEvent(EventConstants.PREPARE_FOR_RENDER, context, null);
    resources.triggerEvent(EventConstants.PREPARE, context, null);
    // Push BeanValidationContext only after the container had a chance to prepare
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
    // Save the form element for later, in case we want to write an encoding
    // type attribute.
    form = writer.element("form", "id", clientId, "method", "post", "action", actionURL, "data-update-zone", zone, DATA_ATTRIBUTE, DATA_ATTRIBUTE_VALUE);
    if (clientValidation != ClientValidation.NONE) {
        writer.attributes("data-validate", "submit");
    }
    if (async) {
        javascriptSupport.require("t5/core/zone");
        writer.attributes("data-async-trigger", true);
    }
    resources.renderInformalParameters(writer);
    div = writer.element("div");
    for (String parameterName : link.getParameterNames()) {
        String[] values = link.getParameterValues(parameterName);
        for (String value : values) {
            // but the input value shouldn't be encoded.
            try {
                value = URLDecoder.decode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                logger.error("Enable to decode parameter value for parameter {} in form {}", parameterName, form.getName(), e);
            }
            writer.element("input", "type", "hidden", "name", parameterName, "value", value);
            writer.end();
        }
    }
    // div
    writer.end();
    environment.peek(Heartbeat.class).begin();
}
Also used : ComponentActionSink(org.apache.tapestry5.corelib.internal.ComponentActionSink) IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Heartbeat(org.apache.tapestry5.services.Heartbeat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Link(org.apache.tapestry5.http.Link) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 4 with IdAllocator

use of org.apache.tapestry5.ioc.util.IdAllocator in project tapestry-5 by apache.

the class Form method preallocateNames.

private void preallocateNames(IdAllocator idAllocator) {
    for (String name : formControlNameManager.getReservedNames()) {
        idAllocator.allocateId(name);
        // See https://issues.apache.org/jira/browse/TAP5-1632
        javascriptSupport.allocateClientId(name);
    }
    Component activePage = componentSource.getActivePage();
    if (activePage == null)
        return;
    ComponentResources activePageResources = activePage.getComponentResources();
    try {
        activePageResources.triggerEvent(EventConstants.PREALLOCATE_FORM_CONTROL_NAMES, new Object[] { idAllocator }, null);
    } catch (RuntimeException ex) {
        logger.error(String.format("Unable to obtain form control names to preallocate: %s", ExceptionUtils.toMessage(ex)), ex);
    }
}
Also used : Component(org.apache.tapestry5.runtime.Component)

Example 5 with IdAllocator

use of org.apache.tapestry5.ioc.util.IdAllocator in project tapestry-5 by apache.

the class JavaScriptModule method exposeJavaScriptSupportForPartialPageRender.

/**
 * Contributes {@link PartialMarkupRendererFilter}s used when rendering a
 * partial Ajax response.
 * <dl>
 * <dt>JavaScriptSupport
 * <dd>Provides {@link JavaScriptSupport}</dd>
 * </dl>
 */
@Contribute(PartialMarkupRenderer.class)
public void exposeJavaScriptSupportForPartialPageRender(OrderedConfiguration<PartialMarkupRendererFilter> configuration, final JavaScriptStackSource javascriptStackSource, final JavaScriptStackPathConstructor javascriptStackPathConstructor, final Request request) {
    final BooleanHook suppressCoreStylesheetsHook = createSuppressCoreStylesheetHook(request);
    PartialMarkupRendererFilter javascriptSupport = new PartialMarkupRendererFilter() {

        public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer) {
            IdAllocator idAllocator;
            if (request.getParameter(InternalConstants.SUPPRESS_NAMESPACED_IDS) == null) {
                String uid = Long.toHexString(System.nanoTime());
                String namespace = "_" + uid;
                idAllocator = new IdAllocator(namespace);
            } else {
                // When suppressed, work just like normal rendering.
                idAllocator = new IdAllocator();
            }
            DocumentLinker linker = environment.peekRequired(DocumentLinker.class);
            JavaScriptSupportImpl support = new JavaScriptSupportImpl(linker, javascriptStackSource, javascriptStackPathConstructor, idAllocator, true, suppressCoreStylesheetsHook);
            environment.push(JavaScriptSupport.class, support);
            renderer.renderMarkup(writer, reply);
            environment.pop(JavaScriptSupport.class);
            support.commit();
        }
    };
    configuration.add("JavaScriptSupport", javascriptSupport, "after:DocumentLinker");
}
Also used : IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) PartialMarkupRendererFilter(org.apache.tapestry5.services.PartialMarkupRendererFilter) JSONObject(org.apache.tapestry5.json.JSONObject) DocumentLinker(org.apache.tapestry5.internal.services.DocumentLinker) PartialMarkupRenderer(org.apache.tapestry5.services.PartialMarkupRenderer) MarkupWriter(org.apache.tapestry5.MarkupWriter) JavaScriptSupportImpl(org.apache.tapestry5.internal.services.ajax.JavaScriptSupportImpl) BooleanHook(org.apache.tapestry5.BooleanHook) Contribute(org.apache.tapestry5.ioc.annotations.Contribute)

Aggregations

IdAllocator (org.apache.tapestry5.ioc.util.IdAllocator)4 Component (org.apache.tapestry5.runtime.Component)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BooleanHook (org.apache.tapestry5.BooleanHook)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 ComponentActionSink (org.apache.tapestry5.corelib.internal.ComponentActionSink)1 InternalFormSupport (org.apache.tapestry5.corelib.internal.InternalFormSupport)1 Link (org.apache.tapestry5.http.Link)1 BeanValidationContextImpl (org.apache.tapestry5.internal.BeanValidationContextImpl)1 DocumentLinker (org.apache.tapestry5.internal.services.DocumentLinker)1 JavaScriptSupportImpl (org.apache.tapestry5.internal.services.ajax.JavaScriptSupportImpl)1 AutofocusValidationDecorator (org.apache.tapestry5.internal.util.AutofocusValidationDecorator)1 Contribute (org.apache.tapestry5.ioc.annotations.Contribute)1 JSONObject (org.apache.tapestry5.json.JSONObject)1 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)1 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)1 Heartbeat (org.apache.tapestry5.services.Heartbeat)1 PartialMarkupRenderer (org.apache.tapestry5.services.PartialMarkupRenderer)1 PartialMarkupRendererFilter (org.apache.tapestry5.services.PartialMarkupRendererFilter)1