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        StyleCategory styleCategory = _getStyleCategory(contextParameters);
090        if (styleCategory != null)
091        {
092            for (String cssFile : styleCategory.getBackOfficeCSSFiles())
093            {
094                if (cssFile != null)
095                {
096                    cssFiles.add(new ScriptFile(cssFile));
097                }
098            }
099        }
100        
101        List<Script> scriptList = new ArrayList<>();
102        scriptList.add(new Script(this.getId(), _script.getScriptClassname(), jsFiles, cssFiles, parameters));
103        return scriptList;
104    }
105
106    private Map<String, Object> getParameters(Map<String, Object> parameters, Map<String, Object> contextualParameters)
107    {
108        if (_galleryItems.size() > 0 && _autoGroupLabel != null)
109        {
110            @SuppressWarnings("unchecked")
111            Map<String, Object> galleryItems = (Map<String, Object>) parameters.get("gallery-item");
112            @SuppressWarnings("unchecked")
113            List<Map<String, Object>> galleryGroups = (List<Map<String, Object>>) galleryItems.get("gallery-groups");
114            
115            for (Map<String, Object> groupParams : galleryGroups)
116            {
117                if (groupParams.get("label").equals(_autoGroupLabel))
118                {
119                    List<Map<String, Object>> gpItems = new ArrayList<>();
120                    
121                    StyleCategory styleCategory = _getStyleCategory(contextualParameters);
122                    if (styleCategory != null)
123                    {
124                        for (Style style : styleCategory.getStyles())
125                        {
126                            Map<String, Object> itemCfg = new HashMap<>();
127                            
128                            itemCfg.put("className", _autoItemClassName);
129                            
130                            Map<String, Object> styleConfig = new HashMap<>();
131                            styleConfig.put("label", style.getButtonLabel());
132                            styleConfig.put("description", style.getButtonDescription());
133                            
134                            String smallIcon = style.getButtonSmallIcon();
135                            if (smallIcon != null)
136                            {
137                                styleConfig.put("icon-small", smallIcon);
138                            }
139                            
140                            String mediumIcon = style.getButtonMediumIcon();
141                            if (mediumIcon != null)
142                            {
143                                styleConfig.put("icon-medium", mediumIcon);
144                            }
145                            
146                            String largeIcon = style.getButtonLargeIcon();
147                            if (largeIcon != null)
148                            {
149                                styleConfig.put("icon-large", largeIcon);
150                            }
151                            
152                            String buttonCSSClass = style.getButtonCSSClass();
153                            if (buttonCSSClass != null)
154                            {
155                                styleConfig.put("cls", buttonCSSClass);
156                            }
157                            
158                            styleConfig.put("css-class", style.getInlineEditorRender());
159                            styleConfig.putAll(_defaultValues);
160                            
161                            itemCfg.put("config", styleConfig);
162                            gpItems.add(itemCfg);
163                        }
164                    }
165                    
166                    groupParams.put("items", gpItems);
167                }
168            }
169        }
170        
171        return parameters;
172    }
173    
174    @Override
175    protected GalleryGroup _configureGroupGallery(Configuration configuration) throws ConfigurationException
176    {
177        GalleryGroup group = super._configureGroupGallery(configuration);
178        
179        if (configuration.getChild("auto", false) != null)
180        {
181            // Register default values from auto configuration
182            Configuration autoConfig = configuration.getChild("auto");
183            _defaultValues = _configureParameters (autoConfig);
184            
185            _autoGroupLabel = group.getLabel();
186            _autoItemClassName = autoConfig.getAttribute("className");
187        }
188        
189        return group;
190    }
191}