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.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027
028import org.ametys.core.ui.StaticClientSideElement;
029import org.ametys.plugins.core.ui.util.ConfigurationHelper;
030
031/**
032 * This static client side element also add parameters taken from the HTMLEditorStyleExtensionPoint
033 */
034public class ParaStyleClientSideElement extends StaticClientSideElement
035{
036    /** The HTMLEditorStyleExtensionPoint instance */
037    protected HTMLEditorStyleExtensionPoint _htmlEditorStyleExtensionPoint;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _htmlEditorStyleExtensionPoint = (HTMLEditorStyleExtensionPoint) smanager.lookup(HTMLEditorStyleExtensionPoint.ROLE);
044    }
045
046    @Override
047    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
048    {
049        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
050        
051        if (scripts.size() == 0)
052        {
053            return scripts;
054        }
055        
056        List<ScriptFile> cssFiles = new ArrayList<>();
057        List<ScriptFile> scriptFiles = new ArrayList<>();
058        Map<String, Object> parameters = new HashMap<>();
059        for (Script script : scripts)
060        {
061            cssFiles.addAll(script.getCSSFiles());
062            scriptFiles.addAll(script.getScriptFiles());
063            parameters.putAll(ConfigurationHelper.clonePluginParameters(script.getParameters()));
064        }
065        
066        Set<String> potentialCSSFiles = new HashSet<>();
067        potentialCSSFiles.add(_htmlEditorStyleExtensionPoint.getPara(contextParameters).getBackOfficeCSSFile());
068        potentialCSSFiles.add(_htmlEditorStyleExtensionPoint.getTable(contextParameters).getBackOfficeCSSFile());
069        potentialCSSFiles.add(_htmlEditorStyleExtensionPoint.getLink(contextParameters).getBackOfficeCSSFile());
070        potentialCSSFiles.add(_htmlEditorStyleExtensionPoint.getUnorderedList(contextParameters).getBackOfficeCSSFile());
071        potentialCSSFiles.add(_htmlEditorStyleExtensionPoint.getOrderedList(contextParameters).getBackOfficeCSSFile());
072        potentialCSSFiles.remove(null);
073        for (String cssFile : potentialCSSFiles)
074        {
075            cssFiles.add(new ScriptFile(cssFile));
076        }
077        
078        parameters.put("data", new ArrayList<Map<String, Object>>());
079        
080        @SuppressWarnings("unchecked")
081        List<Map<String, Object>> styles = (List<Map<String, Object>>) parameters.get("data");
082        for (Style para : _htmlEditorStyleExtensionPoint.getPara(contextParameters).getStyles())
083        {
084            styles.add(para.getAsParameters());
085        }
086        
087        List<Script> scriptList = new ArrayList<>();
088        scriptList.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
089        return scriptList;
090    }
091}