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.ClientElement;
018    import org.apache.tapestry5.ComponentResources;
019    import org.apache.tapestry5.MarkupWriter;
020    import org.apache.tapestry5.annotations.Environmental;
021    import org.apache.tapestry5.annotations.Import;
022    import org.apache.tapestry5.annotations.Mixin;
023    import org.apache.tapestry5.annotations.SupportsInformalParameters;
024    import org.apache.tapestry5.corelib.mixins.RenderInformals;
025    import org.apache.tapestry5.ioc.annotations.Inject;
026    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
027    
028    /**
029     * LinkSubmit allows arbitrary page (DOM) elements to submit a form.
030     *
031     * @version $Id: LinkSubmit.java 674 2010-07-29 12:47:25Z homburgs $
032     * @since 0.2.0
033     */
034    @SupportsInformalParameters
035    @Import(library = {"../Chenillekit.js", "ClickSubmit.js"})
036    public class LinkSubmit implements ClientElement
037    {
038            @Mixin
039            private RenderInformals renderInformals;
040    
041            @Inject
042            private ComponentResources resources;
043    
044            @Environmental
045            private JavaScriptSupport javascriptSupport;
046    
047            /**
048             * The client-side id.
049             */
050            private String clientId;
051    
052            /**
053             * Tapestry render phase method.
054             * Initialize temporary instance variables here.
055             */
056            void setupRender()
057            {
058                    clientId = javascriptSupport.allocateClientId(resources.getId());
059            }
060    
061            /**
062             * Tapestry render phase method.
063             * Start a tag here, end it in afterRender
064             *
065             * @param writer the markup writer
066             */
067            void beginRender(final MarkupWriter writer)
068            {
069                    javascriptSupport.addScript("new Ck.ClickSubmit('%s');", getClientId());
070                    writer.element("a", "id", getClientId(), "href", "#");
071            }
072    
073            /**
074             * Tapestry render phase method. End a tag here.
075             *
076             * @param writer the markup writer
077             */
078            void afterRender(final MarkupWriter writer)
079            {
080                    writer.end();
081            }
082    
083            public String getClientId()
084            {
085                    return clientId;
086            }
087    }