001/*
002 *  Copyright 2017 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.Collection;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.HashSet;
023import java.util.LinkedHashSet;
024import java.util.List;
025import java.util.Map;
026import java.util.Map.Entry;
027import java.util.Set;
028
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.avalon.framework.service.Serviceable;
032
033import org.ametys.core.ui.ClientSideElement;
034import org.ametys.core.ui.ClientSideElement.ScriptFile;
035import org.ametys.core.ui.widgets.richtext.RichTextConfiguration;
036import org.ametys.core.ui.widgets.richtext.RichTextConfigurationAttribute;
037import org.ametys.core.ui.widgets.richtext.RichTextConfigurationStyle;
038import org.ametys.core.ui.widgets.richtext.RichTextConfigurationStyleGroup;
039import org.ametys.core.ui.widgets.richtext.RichTextConfigurationTag;
040import org.ametys.core.ui.widgets.richtext.RichTextConfigurationTag.EMPTY_TAG;
041import org.ametys.core.ui.widgets.richtext.StaticRichTextConfiguration.StaticRichTextConfigurationAttribute;
042import org.ametys.core.ui.widgets.richtext.StaticRichTextConfiguration.StaticRichTextConfigurationStyle;
043import org.ametys.core.ui.widgets.richtext.StaticRichTextConfiguration.StaticRichTextConfigurationStyleGroup;
044import org.ametys.core.ui.widgets.richtext.StaticRichTextConfiguration.StaticRichTextConfigurationTag;
045import org.ametys.runtime.i18n.I18nizableText;
046
047/**
048 * Declares all rich text configurations relative the {@link HTMLEditorStyleExtensionPoint}
049 */
050public class HTMLEditorStyleRichTextConfiguration implements RichTextConfiguration, Serviceable
051{
052    private static final Map<String, I18nizableText> STYLE_GROUP_LABELS = new HashMap<>();
053    private HTMLEditorStyleExtensionPoint _htmlEditorStyleExtensionPoint;
054
055    static 
056    {
057        STYLE_GROUP_LABELS.put("image", new I18nizableText("plugin.cms", "CONTENT_EDITION_IMAGE_STYLES_GROUP_STYLED"));
058        STYLE_GROUP_LABELS.put("link", new I18nizableText("plugin.cms", "CONTENT_EDITION_LINK_STYLES_GROUP_SPECIAL"));
059        STYLE_GROUP_LABELS.put("ol", new I18nizableText("plugin.cms", "CONTENT_EDITION_CHARACTER_ORDEREDLIST_STYLES_GROUP_STYLED"));
060        STYLE_GROUP_LABELS.put("paragraph", new I18nizableText("TODO HTMLEditorStyleRichTextConfiguration")); // TODO
061        STYLE_GROUP_LABELS.put("table", new I18nizableText("plugin.cms", "CONTENT_EDITION_TABLE_STYLES_GROUP_STYLED"));
062        STYLE_GROUP_LABELS.put("ul", new I18nizableText("plugin.cms", "CONTENT_EDITION_CHARACTER_UNORDEREDLIST_STYLES_GROUP_STYLED"));
063    }
064
065    public void service(ServiceManager manager) throws ServiceException
066    {
067        _htmlEditorStyleExtensionPoint = (HTMLEditorStyleExtensionPoint) manager.lookup(HTMLEditorStyleExtensionPoint.ROLE);
068    }
069    
070    public Set<String> getCategories()
071    {
072        return Collections.singleton("");
073    }
074    
075    public Collection<RichTextConfigurationTag> getHandledTags(String category, Map<String, Object> contextParameters)
076    {
077        List<RichTextConfigurationTag> tags = new ArrayList<>();
078        
079        if ("".equals(category))
080        {
081            Map<String, StyleCategory> styleCategories = new HashMap<>();
082            styleCategories.put("img", _htmlEditorStyleExtensionPoint.getImage(contextParameters));
083            styleCategories.put("a", _htmlEditorStyleExtensionPoint.getLink(contextParameters));
084            styleCategories.put("ol", _htmlEditorStyleExtensionPoint.getOrderedList(contextParameters));
085            styleCategories.put("table", _htmlEditorStyleExtensionPoint.getTable(contextParameters));
086            styleCategories.put("ul", _htmlEditorStyleExtensionPoint.getUnorderedList(contextParameters));
087            
088            for (Entry<String, StyleCategory> entry : styleCategories.entrySet())
089            {
090                if (entry.getValue() != null)
091                {
092                    Set<RichTextConfigurationAttribute> attributes = new HashSet<>();
093                    Set<String> authorizedValues = new LinkedHashSet<>();
094                    attributes.add(new StaticRichTextConfigurationAttribute("class", null, authorizedValues, null));
095                    RichTextConfigurationTag tag = new StaticRichTextConfigurationTag(entry.getKey(), null, null, attributes);
096                    tags.add(tag);
097                    
098                    for (Style style : entry.getValue().getStyles())
099                    {
100                        authorizedValues.add(style.getInlineEditorRender());
101                    }
102                }
103            }
104            
105            StyleCategory paraStyleCategory = _htmlEditorStyleExtensionPoint.getPara(contextParameters);
106            if (paraStyleCategory != null)
107            {
108                for (Style style : paraStyleCategory.getStyles())
109                {
110                    String tagName = style.getInlineEditorRender();
111                    Set<RichTextConfigurationAttribute> attributes = new HashSet<>();
112                    Set<String> authorizedValues = new HashSet<>();
113                    authorizedValues.add("text-align");
114                    attributes.add(new StaticRichTextConfigurationAttribute("style", null, authorizedValues, null));
115                    
116                    Set<String> authorizedClassValues = new HashSet<>();
117                    
118                    int dotPosition = tagName.indexOf('.');
119                    if (dotPosition >= 0)
120                    {
121                        String className = tagName.substring(dotPosition + 1);
122                        tagName = tagName.substring(0, dotPosition);
123                        
124                        authorizedClassValues.add(className);
125                    }
126                    
127                    attributes.add(new StaticRichTextConfigurationAttribute("class", null, authorizedClassValues, null));
128                    
129                    RichTextConfigurationTag tag = new StaticRichTextConfigurationTag(tagName, "p".equals(tagName) ? null : EMPTY_TAG.REMOVE_EMPTY_CONTENT, null, attributes);
130                    tags.add(tag);
131                }                
132            }
133        }
134        
135        return tags;
136    }
137    
138    public List<ScriptFile> getCSSFiles(String category, Map<String, Object> contextParameters)
139    {
140        List<ScriptFile> files = new ArrayList<>();
141        
142        if ("".equals(category))
143        {
144            List<StyleCategory> styleCategories = new ArrayList<>();
145            styleCategories.add(_htmlEditorStyleExtensionPoint.getImage(contextParameters));
146            styleCategories.add(_htmlEditorStyleExtensionPoint.getLink(contextParameters));
147            styleCategories.add(_htmlEditorStyleExtensionPoint.getOrderedList(contextParameters));
148            styleCategories.add(_htmlEditorStyleExtensionPoint.getPara(contextParameters));
149            styleCategories.add(_htmlEditorStyleExtensionPoint.getTable(contextParameters));
150            styleCategories.add(_htmlEditorStyleExtensionPoint.getUnorderedList(contextParameters));
151            
152            for (StyleCategory styleCategory : styleCategories)
153            {
154                if (styleCategory != null)
155                {
156                    String file = styleCategory.getInlineEditorCSSFile();
157                    if (file != null)
158                    {
159                        files.add(new ScriptFile(file));
160                    }
161                }
162            }
163            
164        }
165        
166        return files;
167    }
168    
169    public Set<ClientSideElement> getConvertors(String category, Map<String, Object> contextParameters)
170    {
171        return Collections.EMPTY_SET;
172    }
173    
174    public Set<ClientSideElement> getValidators(String category, Map<String, Object> contextParameters)
175    {
176        return Collections.EMPTY_SET;
177    }
178    
179    public Map<String, Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>>> getAvailableStyles(String category, Map<String, Object> contextualParameters)
180    {
181        Map<String, Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>>> lists = new HashMap<>();
182        
183        Map<String, StyleCategory> styleCategories = new HashMap<>();
184        styleCategories.put("image", _htmlEditorStyleExtensionPoint.getImage(contextualParameters));
185        styleCategories.put("link", _htmlEditorStyleExtensionPoint.getLink(contextualParameters));
186        styleCategories.put("ol", _htmlEditorStyleExtensionPoint.getOrderedList(contextualParameters));
187        styleCategories.put("paragraph", _htmlEditorStyleExtensionPoint.getPara(contextualParameters));
188        styleCategories.put("table", _htmlEditorStyleExtensionPoint.getTable(contextualParameters));
189        styleCategories.put("ul", _htmlEditorStyleExtensionPoint.getUnorderedList(contextualParameters));
190
191        for (Entry<String, StyleCategory> entry : styleCategories.entrySet())
192        {
193            if (entry.getValue() != null)
194            {
195                Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>> rtcStylesGroup = new HashMap<>();
196                lists.put(entry.getKey(), rtcStylesGroup);
197                
198                List<RichTextConfigurationStyle> rtcStyles = new ArrayList<>();
199                rtcStylesGroup.put(new StaticRichTextConfigurationStyleGroup(STYLE_GROUP_LABELS.get(entry.getKey()), 1000), rtcStyles);
200                
201                for (Style style : entry.getValue().getStyles())
202                {
203                    rtcStyles.add(new StaticRichTextConfigurationStyle(style.getInlineEditorRender(), style.getButtonLabel(), style.getButtonDescription(), style.getButtonCSSClass(), style.getButtonSmallIcon(), style.getButtonMediumIcon(), style.getButtonLargeIcon(), !"paragraph".equals(entry.getKey())));
204                }
205            }
206        }
207        
208        return lists;
209    }
210}