001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.devmode;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.core.DevMode;
031import org.ametys.core.DevMode.DEVMODE;
032import org.ametys.core.ui.StaticClientSideElement;
033
034/**
035 * Client side element for the dev mode menu item buttons
036 */
037public class DevModeMenuItemClientSideElement extends StaticClientSideElement implements Contextualizable
038{
039    private Context _context;
040    private String _toggleOn;
041    private Boolean _dynamicDecorator;
042
043    public void contextualize(Context context) throws ContextException
044    {
045        _context = context;
046    }
047    
048    @Override
049    public void configure(Configuration configuration) throws ConfigurationException
050    {
051        super.configure(configuration);
052        _toggleOn = configuration.getChild("toggle-on").getValue("");
053        _dynamicDecorator = Boolean.valueOf(configuration.getChild("dynamic-decorator").getValue("false"));
054    }
055
056    @Override
057    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
058    {
059        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
060        
061        if (!scripts.isEmpty())
062        {
063            Script script = scripts.get(0);
064
065            Request request = ContextHelper.getRequest(_context);
066            DEVMODE devMode = DevMode.getDeveloperMode(request);
067            String forceDevMode = request.getParameter(DevMode.REQUEST_PARAM_FORCE_DEVMODE);
068            String decorator = null;
069
070            if (_dynamicDecorator)
071            {
072                if (DEVMODE.PRODUCTION.equals(devMode))
073                {
074                    decorator = "decorator-ametysicon-sign-raw-cross";
075                }
076                else if (DEVMODE.DEVELOPMENT.equals(devMode))
077                {
078                    decorator = "decorator-ametysicon-sign-raw-check";
079                }
080                else if (DEVMODE.SUPER_DEVELOPPMENT.equals(devMode))
081                {
082                    decorator = "decorator-ametysicon-bugs3";
083                }
084            }
085            
086            Map<String, Object> parameters = script.getParameters();
087            if (decorator != null)
088            {
089                parameters.put("icon-decorator", decorator);
090            }
091            boolean toggled = forceDevMode != null ? forceDevMode.equals(_toggleOn) : StringUtils.isEmpty(_toggleOn);
092            parameters.put("toggle-state", toggled);
093            parameters.put("disabled", toggled);
094        }
095        
096        return scripts;
097    }
098}