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 HTMLEditorStyle
031 */
032public class ParaStyleClientSideElement extends StaticClientSideElement
033{
034    /** The HTMLEditorStyle instance */
035    protected HTMLEditorStyle _htmlEditorStyle;
036    
037    @Override
038    public void service(ServiceManager smanager) throws ServiceException
039    {
040        super.service(smanager);
041        _htmlEditorStyle = (HTMLEditorStyle) smanager.lookup(HTMLEditorStyle.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        parameters.put("data", new ArrayList<>());
065        
066        @SuppressWarnings("unchecked")
067        List<Map<String, Object>> styles = (List<Map<String, Object>>) parameters.get("data");
068        
069        for (String category : _htmlEditorStyle.getCategories())
070        {
071            StyleCategory paraStyleCategory = _htmlEditorStyle.getPara(category, contextParameters);
072            if (paraStyleCategory != null)
073            {
074                for (String cssFile : paraStyleCategory.getBackOfficeCSSFiles())
075                {
076                    if (cssFile != null)
077                    {
078                        cssFiles.add(new ScriptFile(cssFile));
079                    }
080                }
081                
082                for (Style para : paraStyleCategory.getStyles())
083                {
084                    styles.add(para.getAsParameters());
085                }
086                
087            }
088            
089            cssFiles.addAll(_htmlEditorStyle.getBackOfficeCSSFiles(category, contextParameters));
090        }
091        
092        List<Script> scriptList = new ArrayList<>();
093        scriptList.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
094        return scriptList;
095    }
096}