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