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 if ("".equals(category)) 141 { 142 return _htmlEditorStyleExtensionPoint.getInlineEditorCSSFiles(contextParameters); 143 } 144 145 return Collections.EMPTY_LIST; 146 } 147 148 public Set<ClientSideElement> getConvertors(String category, Map<String, Object> contextParameters) 149 { 150 return Collections.EMPTY_SET; 151 } 152 153 public Set<ClientSideElement> getValidators(String category, Map<String, Object> contextParameters) 154 { 155 return Collections.EMPTY_SET; 156 } 157 158 public Map<String, Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>>> getAvailableStyles(String category, Map<String, Object> contextualParameters) 159 { 160 Map<String, Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>>> lists = new HashMap<>(); 161 162 Map<String, StyleCategory> styleCategories = new HashMap<>(); 163 styleCategories.put("image", _htmlEditorStyleExtensionPoint.getImage(contextualParameters)); 164 styleCategories.put("link", _htmlEditorStyleExtensionPoint.getLink(contextualParameters)); 165 styleCategories.put("ol", _htmlEditorStyleExtensionPoint.getOrderedList(contextualParameters)); 166 styleCategories.put("paragraph", _htmlEditorStyleExtensionPoint.getPara(contextualParameters)); 167 styleCategories.put("table", _htmlEditorStyleExtensionPoint.getTable(contextualParameters)); 168 styleCategories.put("ul", _htmlEditorStyleExtensionPoint.getUnorderedList(contextualParameters)); 169 170 for (Entry<String, StyleCategory> entry : styleCategories.entrySet()) 171 { 172 if (entry.getValue() != null) 173 { 174 Map<RichTextConfigurationStyleGroup, List<RichTextConfigurationStyle>> rtcStylesGroup = new HashMap<>(); 175 lists.put(entry.getKey(), rtcStylesGroup); 176 177 List<RichTextConfigurationStyle> rtcStyles = new ArrayList<>(); 178 rtcStylesGroup.put(new StaticRichTextConfigurationStyleGroup(STYLE_GROUP_LABELS.get(entry.getKey()), 1000), rtcStyles); 179 180 for (Style style : entry.getValue().getStyles()) 181 { 182 rtcStyles.add(new StaticRichTextConfigurationStyle(style.getInlineEditorRender(), style.getButtonLabel(), style.getButtonDescription(), style.getButtonCSSClass(), style.getButtonSmallIcon(), style.getButtonMediumIcon(), style.getButtonLargeIcon(), !"paragraph".equals(entry.getKey()))); 183 } 184 } 185 } 186 187 return lists; 188 } 189}