001/*
002 *  Copyright 2010 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.cms.clientsideelement.styles;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027
028import org.ametys.core.ui.SimpleMenu;
029import org.ametys.core.util.I18nUtils;
030import org.ametys.plugins.core.ui.util.ConfigurationHelper;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * This StaticMenu also search for special tags to transform them into parameters thanks to HTMLEditorStyleExtensionPoint
035 * Special tags are
036 * <ul>
037 *  <li>table</li>
038 * </ul> 
039 */
040public abstract class AbstractEditorStyleMenu extends SimpleMenu
041{
042    /** The HTMLEditorStyleExtensionPoint instance */
043    protected HTMLEditorStyleExtensionPoint _htmlEditorStyleExtensionPoint;
044    /** I18n helper */
045    protected I18nUtils _i18nUtils;
046    
047    /** The default value */
048    protected Map<String, Object> _defaultValues = new HashMap<>();
049    /** The label of auto group */
050    protected I18nizableText _autoGroupLabel;
051    /** JS class name for auto items */
052    protected String _autoItemClassName;
053    
054    /**
055     * Return the {@link StyleCategory} to use
056     * @param contextParameters Contextual parameters transmitted by the environment.
057     * @return A non null category
058     */
059    protected abstract StyleCategory _getStyleCategory(Map<String, Object> contextParameters);
060    
061    @Override
062    public void service(ServiceManager smanager) throws ServiceException
063    {
064        super.service(smanager);
065        _htmlEditorStyleExtensionPoint = (HTMLEditorStyleExtensionPoint) smanager.lookup(HTMLEditorStyleExtensionPoint.ROLE);
066        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
067    }
068
069    @Override
070    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
071    {
072        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
073        
074        if (scripts.size() == 0)
075        {
076            return scripts;
077        }
078        
079        List<ScriptFile> cssFiles = new ArrayList<>();
080        List<ScriptFile> jsFiles = new ArrayList<>();
081        Map<String, Object> parameters = new HashMap<>();
082        for (Script script : scripts)
083        {
084            cssFiles.addAll(script.getCSSFiles());
085            jsFiles.addAll(script.getScriptFiles());
086            parameters.putAll(getParameters(ConfigurationHelper.clonePluginParameters(script.getParameters()), contextParameters));
087        }
088        
089        String cssFile = _getStyleCategory(contextParameters).getBackOfficeCSSFile();
090        if (cssFile != null)
091        {
092            cssFiles.add(new ScriptFile(cssFile));
093        }
094        
095        List<Script> scriptList = new ArrayList<>();
096        scriptList.add(new Script(this.getId(), _script.getScriptClassname(), jsFiles, cssFiles, parameters));
097        return scriptList;
098    }
099
100    private Map<String, Object> getParameters(Map<String, Object> parameters, Map<String, Object> contextualParameters)
101    {
102        if (_galleryItems.size() > 0 && _autoGroupLabel != null)
103        {
104            @SuppressWarnings("unchecked")
105            Map<String, Object> galleryItems = (Map<String, Object>) parameters.get("gallery-item");
106            @SuppressWarnings("unchecked")
107            List<Map<String, Object>> galleryGroups = (List<Map<String, Object>>) galleryItems.get("gallery-groups");
108            
109            for (Map<String, Object> groupParams : galleryGroups)
110            {
111                if (groupParams.get("label").equals(_autoGroupLabel))
112                {
113                    List<Map<String, Object>> gpItems = new ArrayList<>();
114                    
115                    for (Style style : _getStyleCategory(contextualParameters).getStyles())
116                    {
117                        Map<String, Object> itemCfg = new HashMap<>();
118                        
119                        itemCfg.put("className", _autoItemClassName);
120                        
121                        Map<String, Object> styleConfig = new HashMap<>();
122                        styleConfig.put("label", style.getButtonLabel());
123                        styleConfig.put("description", style.getButtonDescription());
124                        
125                        String smallIcon = style.getButtonSmallIcon();
126                        if (smallIcon != null)
127                        {
128                            styleConfig.put("icon-small", smallIcon);
129                        }
130                        
131                        String mediumIcon = style.getButtonMediumIcon();
132                        if (mediumIcon != null)
133                        {
134                            styleConfig.put("icon-medium", mediumIcon);
135                        }
136                        
137                        String largeIcon = style.getButtonLargeIcon();
138                        if (largeIcon != null)
139                        {
140                            styleConfig.put("icon-large", largeIcon);
141                        }
142                        
143                        String buttonCSSClass = style.getButtonCSSClass();
144                        if (buttonCSSClass != null)
145                        {
146                            styleConfig.put("cls", buttonCSSClass);
147                        }
148                        
149                        styleConfig.put("css-class", style.getInlineEditorRender());
150                        styleConfig.putAll(_defaultValues);
151                        
152                        itemCfg.put("config", styleConfig);
153                        gpItems.add(itemCfg);
154                    }
155                    
156                    groupParams.put("items", gpItems);
157                }
158            }
159        }
160        
161        return parameters;
162    }
163    
164    @Override
165    protected GalleryGroup _configureGroupGallery(Configuration configuration) throws ConfigurationException
166    {
167        GalleryGroup group = super._configureGroupGallery(configuration);
168        
169        if (configuration.getChild("auto", false) != null)
170        {
171            // Register default values from auto configuration
172            Configuration autoConfig = configuration.getChild("auto");
173            _defaultValues = _configureParameters (autoConfig);
174            
175            _autoGroupLabel = group.getLabel();
176            _autoItemClassName = autoConfig.getAttribute("className");
177        }
178        
179        return group;
180    }
181}