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 HTMLEditorStyle}
049 */
050public class HTMLEditorStyleRichTextConfiguration implements RichTextConfiguration, Serviceable
051{
052    private static final Map<String, I18nizableText> STYLE_GROUP_LABELS = new HashMap<>();
053    private HTMLEditorStyle _htmlEditorStyle;
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        _htmlEditorStyle = (HTMLEditorStyle) manager.lookup(HTMLEditorStyle.ROLE);
068    }
069    
070    public Set<String> getCategories()
071    {
072        return _htmlEditorStyle.getCategories();
073    }
074    
075    public Collection<RichTextConfigurationTag> getHandledTags(String category, Map<String, Object> contextParameters)
076    {
077        List<RichTextConfigurationTag> tags = new ArrayList<>();
078        
079        if (getCategories().contains(category))
080        {
081            Map<String, StyleCategory> styleCategories = new HashMap<>();
082            styleCategories.put("img", _htmlEditorStyle.getImage(category, contextParameters));
083            styleCategories.put("a", _htmlEditorStyle.getLink(category, contextParameters));
084            styleCategories.put("ol", _htmlEditorStyle.getOrderedList(category, contextParameters));
085            styleCategories.put("table", _htmlEditorStyle.getTable(category, contextParameters));
086            styleCategories.put("ul", _htmlEditorStyle.getUnorderedList(category, 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 = _htmlEditorStyle.getPara(category, 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        return _htmlEditorStyle.getInlineEditorCSSFiles(category, contextParameters);
141    }
142    
143    public Set<ClientSideElement> getConvertors(String category, Map<String, Object> contextParameters)
144    {
145        return Collections.EMPTY_SET;
146    }
147    
148    public Set<ClientSideElement> getValidators(String category, Map<String, Object> contextParameters)
149    {
150        return Collections.EMPTY_SET;
151    }
152    
153    public Map<String, Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>>> getAvailableStyles(String category, Map<String, Object> contextualParameters)
154    {
155        Map<String, Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>>> lists = new HashMap<>();
156        
157        if (getCategories().contains(category))
158        {
159            Map<String, StyleCategory> styleCategories = new HashMap<>();
160            
161            styleCategories.put("image", _htmlEditorStyle.getImage(category, contextualParameters));
162            styleCategories.put("link", _htmlEditorStyle.getLink(category, contextualParameters));
163            styleCategories.put("ol", _htmlEditorStyle.getOrderedList(category, contextualParameters));
164            styleCategories.put("paragraph", _htmlEditorStyle.getPara(category, contextualParameters));
165            styleCategories.put("table", _htmlEditorStyle.getTable(category, contextualParameters));
166            styleCategories.put("ul", _htmlEditorStyle.getUnorderedList(category, contextualParameters));
167            
168            for (Entry<String, StyleCategory> entry : styleCategories.entrySet())
169            {
170                if (entry.getValue() != null)
171                {
172                    Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>> rtcStylesGroup = new HashMap<>();
173                    lists.put(entry.getKey(), rtcStylesGroup);
174                    
175                    List<RichTextConfigurationStyle> rtcStyles = new ArrayList<>();
176                    rtcStylesGroup.put(new StaticRichTextConfigurationStyleGroup(STYLE_GROUP_LABELS.get(entry.getKey()), 1000), rtcStyles);
177                    
178                    for (Style style : entry.getValue().getStyles())
179                    {
180                        rtcStyles.add(new StaticRichTextConfigurationStyle(style.getInlineEditorRender(), style.getButtonLabel(), style.getButtonDescription(), style.getButtonCSSClass(), style.getButtonSmallIcon(), style.getButtonMediumIcon(), style.getButtonLargeIcon(), !"paragraph".equals(entry.getKey())));
181                    }
182                }
183            }
184        }
185        
186        return lists;
187    }
188}