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