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.htmlexpert;
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.ClientSideElement;
027import org.ametys.core.ui.StaticClientSideElement;
028import org.ametys.plugins.core.ui.util.ConfigurationHelper;
029
030/**
031 * This client side element completes its configuration by loading HTMLExpertToolbarExtensionPoint buttons 
032 */
033public class HTMLExpertEditClientSideElement extends StaticClientSideElement
034{
035    /** The HTMLExpertToolbarExtensionPoint instance */
036    protected HTMLExpertToolbarExtensionPoint _htmlExpertToolbarExtensionPoint;
037    
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042        _htmlExpertToolbarExtensionPoint = (HTMLExpertToolbarExtensionPoint) smanager.lookup(HTMLExpertToolbarExtensionPoint.ROLE);
043    }
044    
045    @SuppressWarnings("unchecked")
046    @Override
047    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
048    {
049        List<ScriptFile> cssFiles = new ArrayList<>();
050        List<ScriptFile> scriptFiles = new ArrayList<>();
051        Map<String, Object> parameters = ConfigurationHelper.clonePluginParameters(_script.getParameters());
052        
053        parameters.put("toolbar-buttons", new ArrayList<Map<String, Object>>());
054        
055        for (String extensionId : _htmlExpertToolbarExtensionPoint.getExtensionsIds())
056        {
057            ClientSideElement button = _htmlExpertToolbarExtensionPoint.getExtension(extensionId);
058
059            Map<String, Object> btnParams = new HashMap<>();
060            
061            btnParams.put("id", extensionId);
062            btnParams.put("pluginName", button.getPluginName());
063            
064            Map<String, Object> btnConfig = new HashMap<>();
065            for (Script script : button.getScripts(ignoreRights, contextParameters))
066            {
067                if (script != null)
068                {
069                    btnParams.put("classname", script.getScriptClassname());
070                    for (String parameterKey : script.getParameters().keySet())
071                    {
072                        Object parameter = script.getParameters().get(parameterKey);
073                        btnConfig.put(parameterKey, parameter);
074                    }
075                }
076            }
077            
078            btnParams.put("config", btnConfig);
079            
080            ((ArrayList<Map<String, Object>>) parameters.get("toolbar-buttons")).add(btnParams);
081        }
082        
083        
084        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
085        
086        if (scripts.size() == 1)
087        {
088            Script script = scripts.get(0);
089            
090            cssFiles.addAll(script.getCSSFiles());
091            scriptFiles.addAll(script.getScriptFiles());
092            ConfigurationHelper.clonePluginParameters(script.getParameters());
093            
094            for (String extensionId : _htmlExpertToolbarExtensionPoint.getExtensionsIds())
095            {
096                ClientSideElement button = _htmlExpertToolbarExtensionPoint.getExtension(extensionId);
097                for (Script btnScript : button.getScripts(ignoreRights, contextParameters))
098                {
099                    if (btnScript != null)
100                    {
101                        cssFiles.addAll(btnScript.getCSSFiles());
102                        scriptFiles.addAll(btnScript.getScriptFiles());
103                    }
104                }
105            }
106            
107            List<Script> scriptList = new ArrayList<>();
108            scriptList.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
109            return scriptList;
110        }
111        
112        return new ArrayList<>();
113    }
114}