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