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.LinkedHashSet;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.ametys.core.ui.ClientSideElement.ScriptFile;
025
026/**
027 * This extension point allows to determine which styles does exist for html edition 
028 */
029public interface HTMLEditorStyle
030{
031    /** Avalon role name */
032    public static final String ROLE = HTMLEditorStyle.class.getName();
033    
034    /**
035     * Get the categories supported by this component
036     * @return The categories supported. Cannot be null, but may be empty and of course contains the "" key for the default category.
037     */
038    public Set<String> getCategories();
039    
040    /**
041     * Get the list of styles for paragraphs
042     * @param category The richtext category
043     * @param contextualParameters Contextuals parameters transmitted by the environment.
044     * @return A category. Can be null.
045     */
046    public StyleCategory getPara(String category, Map<String, Object> contextualParameters);
047    
048    /**
049     * Get the list of styles for tables
050     * @param category The richtext category
051     * @param contextualParameters Contextuals parameters transmitted by the environment.
052     * @return A category. Can be null.
053     */
054    public StyleCategory getTable(String category, Map<String, Object> contextualParameters);
055    
056    /**
057     * Get the list of styles for link
058     * @param category The richtext category
059     * @param contextualParameters Contextuals parameters transmitted by the environment.
060     * @return A category. Can be null.
061     */
062    public StyleCategory getLink(String category, Map<String, Object> contextualParameters);
063    
064    /**
065     * Get the list of styles for images
066     * @param category The richtext category
067     * @param contextualParameters Contextuals parameters transmitted by the environment.
068     * @return A category. Can be null.
069     */
070    public StyleCategory getImage(String category, Map<String, Object> contextualParameters);
071    
072    /**
073     * Get the list of styles for unordered lists
074     * @param category The richtext category
075     * @param contextualParameters Contextuals parameters transmitted by the environment.
076     * @return A category. Can be null.
077     */
078    public StyleCategory getUnorderedList(String category, Map<String, Object> contextualParameters);
079    
080    /**
081     * Get the list of styles for ordered lists
082     * @param category The richtext category
083     * @param contextualParameters Contextuals parameters transmitted by the environment.
084     * @return A category. Can be null.
085     */
086    public StyleCategory getOrderedList(String category, Map<String, Object> contextualParameters);
087    
088    /**
089     * Get the aggregation of all backoffice css files
090     * @param category The richtext category
091     * @param contextParameters The contextual parameters
092     * @return A non null list of ScriptFile
093     */
094    public default List<ScriptFile> getBackOfficeCSSFiles(String category, Map<String, Object> contextParameters)
095    {
096        List<ScriptFile> cssFiles = new ArrayList<>();
097        
098        List<StyleCategory> styleCategories = new ArrayList<>();
099        styleCategories.add(getImage(category, contextParameters));
100        styleCategories.add(getLink(category, contextParameters));
101        styleCategories.add(getOrderedList(category, contextParameters));
102        styleCategories.add(getPara(category, contextParameters));
103        styleCategories.add(getTable(category, contextParameters));
104        styleCategories.add(getUnorderedList(category, contextParameters));
105        
106        Set<String> filesAsString = new LinkedHashSet<>();
107        for (StyleCategory styleCategory : styleCategories)
108        {
109            if (styleCategory != null)
110            {
111                filesAsString.addAll(styleCategory.getBackOfficeCSSFiles());
112            }
113        }
114        filesAsString.remove(null);
115        
116        for (String fileAsString : filesAsString)
117        {
118            cssFiles.add(new ScriptFile(fileAsString));
119        }
120        
121        return cssFiles;
122    }
123    
124    /**
125     * Get the aggregation of all inline editor css files
126     * @param category The richtext category
127     * @param contextParameters The contextual parameters
128     * @return A non null list of ScriptFile
129     */
130    public default List<ScriptFile> getInlineEditorCSSFiles(String category, Map<String, Object> contextParameters)
131    {
132        List<ScriptFile> cssFiles = new ArrayList<>();
133        
134        List<StyleCategory> styleCategories = new ArrayList<>();
135        styleCategories.add(getImage(category, contextParameters));
136        styleCategories.add(getLink(category, contextParameters));
137        styleCategories.add(getOrderedList(category, contextParameters));
138        styleCategories.add(getPara(category, contextParameters));
139        styleCategories.add(getTable(category, contextParameters));
140        styleCategories.add(getUnorderedList(category, contextParameters));
141        
142        Set<String> filesAsString = new LinkedHashSet<>();
143        for (StyleCategory styleCategory : styleCategories)
144        {
145            if (styleCategory != null)
146            {
147                filesAsString.addAll(styleCategory.getInlineEditorCSSFiles());
148            }
149        }
150        filesAsString.remove(null);
151        
152        for (String fileAsString : filesAsString)
153        {
154            cssFiles.add(new ScriptFile(fileAsString));
155        }
156        
157        return cssFiles;
158    }
159}