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.Collections;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.context.Contextualizable;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.core.DevMode;
029import org.ametys.core.DevMode.DEVMODE;
030import org.ametys.core.ui.ClientSideElementHelper;
031import org.ametys.core.ui.SimpleMenu;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * Client side element for the dev mode button
036 */
037public class DevModeClientSideElement extends SimpleMenu implements Contextualizable
038{
039    private Context _context;
040
041    public void contextualize(Context context) throws ContextException
042    {
043        _context = context;
044    }
045    
046    @Override
047    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
048    {
049        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
050        
051        if (!scripts.isEmpty())
052        {
053            if (scripts.size() != 1)
054            {
055                throw new IllegalStateException(getId() + " cannot return more than 1 script");
056            }
057            
058            Script script = ClientSideElementHelper.cloneScript(scripts.get(0));
059
060            Request request = ContextHelper.getRequest(_context);
061            DEVMODE devMode = DevMode.getDeveloperMode(request);
062
063            boolean toggle = false;
064            I18nizableText description = null;
065            String decorator = null;
066            
067            switch (devMode)
068            {
069                case DEVELOPMENT:
070                    toggle = true;
071                    description = new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_TOOLS_DEVMODE_DEVELOPMENT_MODE_DESCRIPTION");
072                    break;
073                case SUPER_DEVELOPPMENT:
074                    toggle = true;
075                    description = new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_TOOLS_DEVMODE_SUPER_DEV_MODE_DESCRIPTION");
076                    decorator = "decorator-ametysicon-bugs3";
077                    break;
078                default:
079                case PRODUCTION:
080                    description = new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_TOOLS_DEVMODE_PRODUCTION_MODE_DESCRIPTION");
081                    break;
082            }
083            
084            Map<String, Object> parameters = script.getParameters();
085            parameters.put("description", description);
086            parameters.put("toggle-state", toggle);
087            if (decorator != null)
088            {
089                parameters.put("icon-decorator", decorator);
090            }
091            
092            return Collections.singletonList(script);
093        }
094        
095        return scripts; // Empty list
096    }
097}