    001// Licensed under the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005// http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.corelib.components;
    014
    015import org.apache.tapestry5.BindingConstants;
    016import org.apache.tapestry5.ComponentResources;
    017import org.apache.tapestry5.alerts.AlertManager;
    018import org.apache.tapestry5.annotations.Component;
    019import org.apache.tapestry5.annotations.Environmental;
    020import org.apache.tapestry5.annotations.Parameter;
    021import org.apache.tapestry5.annotations.Property;
    022import org.apache.tapestry5.http.TapestryHttpSymbolConstants;
    023import org.apache.tapestry5.http.services.Request;
    024import org.apache.tapestry5.http.services.Session;
    025import org.apache.tapestry5.internal.services.ReloadHelper;
    026import org.apache.tapestry5.ioc.annotations.Inject;
    027import org.apache.tapestry5.ioc.annotations.Symbol;
    028import org.apache.tapestry5.services.javascript.JavaScriptSupport;
    029
    030/**
    031 * Renders a dropdown menu of useful options when developing. By default, the DevTool is disabled (invisible)
    032 * during production.
    033 *
    034 * The DevTool provides the following options:
    035 * <ul>
    036 * <li>Reset the page state, discarding any persistent page properties</li>
    037 * <li>Kill the HttpSession (discarding any server-side state)</li>
    038 * <li>Re-render the page (useful after changing the page or template)</li>
    039 * <li>Re-render the page with rendering comments</li>
    040 * <li>Reload all pages and components: classes, messages, templates</li>
    041 * <li>Open the T5 Dashboard page in a new window</li>
    042 * </ul>
    043 *
    044 * Note that due to conflicts between Prototype and jQuery, the dev tool is hidden after selecting an item from the menu.
    045 *
    046 * @tapestrydoc
    047 * @since 5.4
    048 */
    049public class DevTool
    050{
    051    /**
    052     * If false, then the component does not render. Defaults to true unless production mode is enabled.
    053     */
    054    @Parameter
    055    private boolean enabled;
    056
    057    /**
    058     * If true, then the DevTool modifies its markup so as to work within a Bootstrap 3 NavBar. This renders
    059     * the component as a {@code <li>} (instead of a {@code <div>}), and removes the "btn" CSS classes.
    060     */
    061    @Parameter
    062    private boolean navbar;
    063
    064    /**
    065     * Additional CSS selectors, e.g., "pull-right" or "dropup".
    066     */
    067    @Parameter(name = "class", defaultPrefix = BindingConstants.LITERAL)
    068    private String className;
    069
    070
    071    @Property
    072    @Inject
    073    @Symbol(TapestryHttpSymbolConstants.PRODUCTION_MODE)
    074    private boolean productionMode;
    075
    076    @Component(inheritInformalParameters = true, parameters = {
    077            "class=zoneClass",
    078            "elementName=${zoneElement}"
    079    })
    080    private Zone devmodezone;
    081
    082
    083    @Inject
    084    private AlertManager alertManager;
    085
    086    @Inject
    087    private Request request;
    088
    089    @Environmental
    090    private JavaScriptSupport javaScriptSupport;
    091
    092    @Inject
    093    private ComponentResources resources;
    094
    095    @Inject
    096    private ReloadHelper reloadHelper;
    097
    098    public String getZoneElement()
    099    {
    100        return navbar ? "li" : "div";
    101    }
    102
    103    public String getZoneClass()
    104    {
    105        return "dropdown" + (className == null ? "" : " " + className);
    106    }
    107
    108    public String getTriggerClass()
    109    {
    110        return "dropdown-toggle" + (navbar ? "" : " btn btn-default btn-xs");
    111    }
    112
    113    boolean defaultEnabled()
    114    {
    115        return !productionMode;
    116    }
    117
    118    /**
    119     * When disabled, this prevents any part of the tool from rendering.
    120     */
    121    boolean beginRender()
    122    {
    123        if (enabled)
    124        {
    125            javaScriptSupport.importStack("core").require("bootstrap/dropdown");
    126        }
    127
    128        return enabled;
    129    }
    130
    131    Object onActionFromReset()
    132    {
    133        if (!productionMode)
    134        {
    135            resources.discardPersistentFieldChanges();
    136
    137            alertManager.info("Page state discarded.");
    138        }
    139
    140        return devmodezone.getBody();
    141    }
    142
    143    Object onActionFromKill()
    144    {
    145        if (!productionMode)
    146        {
    147            Session session = request.getSession(false);
    148
    149            if (session == null)
    150            {
    151                alertManager.info("No server-side session currently exist.");
    152            } else
    153            {
    154                session.invalidate();
    155                alertManager.info("Server-side session invalidated.");
    156            }
    157        }
    158
    159        return devmodezone.getBody();
    160    }
    161
    162    Object onActionFromReload()
    163    {
    164        reloadHelper.forceReload();
    165
    166        return devmodezone.getBody();
    167    }
    168}