001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.components;
016    
017    import org.apache.tapestry5.BindingConstants;
018    import org.apache.tapestry5.ClientElement;
019    import org.apache.tapestry5.ComponentResources;
020    import org.apache.tapestry5.MarkupWriter;
021    import org.apache.tapestry5.annotations.Environmental;
022    import org.apache.tapestry5.annotations.Import;
023    import org.apache.tapestry5.annotations.Mixin;
024    import org.apache.tapestry5.annotations.Parameter;
025    import org.apache.tapestry5.annotations.SupportsInformalParameters;
026    import org.apache.tapestry5.corelib.mixins.RenderInformals;
027    import org.apache.tapestry5.ioc.annotations.Inject;
028    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
029    
030    /**
031     * ClickSubmit allows arbitrary page (DOM) elements to submit a form.
032     *
033     * @version $Id: ClickSubmit.java 674 2010-07-29 12:47:25Z homburgs $
034     */
035    @SupportsInformalParameters
036    @Import(library = {"../Chenillekit.js", "ClickSubmit.js"})
037    public class ClickSubmit implements ClientElement
038    {
039            @Inject
040            private ComponentResources resources;
041    
042            @Environmental
043            private JavaScriptSupport javascriptSupport;
044    
045            @Mixin
046            private RenderInformals renderInformals;
047    
048            @Parameter(value = "prop:componentResources.elementName", defaultPrefix = BindingConstants.LITERAL)
049            private String element;
050    
051            /**
052             * The client-side id.
053             */
054            private String clientId;
055    
056            /**
057             * Tapestry render phase method.
058             * Initialize temporary instance variables here.
059             */
060            void setupRender()
061            {
062                    clientId = javascriptSupport.allocateClientId(resources.getId());
063            }
064    
065            /**
066             * Tapestry render phase method.
067             * Start a tag here, end it in afterRender
068             *
069             * @param writer the markup writer
070             */
071            void beginRender(MarkupWriter writer)
072            {
073                    writer.element(element, "id", getClientId());
074            }
075    
076            /**
077             * Tapestry render phase method. End a tag here.
078             *
079             * @param writer the markup writer
080             */
081            void afterRender(MarkupWriter writer)
082            {
083                    writer.end();
084                    javascriptSupport.addScript("new Ck.ClickSubmit('%s');", getClientId());
085            }
086    
087            public String getClientId()
088            {
089                    return clientId;
090            }
091    }