001/*
002 *  Copyright 2015 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.web.skin;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.core.ui.Callable;
028import org.ametys.core.ui.ClientSideElement;
029import org.ametys.core.ui.widgets.StaticClientSideWidget;
030
031/**
032 * This implementation looks at existing {@link CreateSkinActionExtensionPoint} to create a widget for selecting skin and creating a new skin.
033 */
034public class SkinClientSideWidget extends StaticClientSideWidget
035{
036    private CreateSkinActionExtensionPoint _createSkinActionEP;
037
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042        _createSkinActionEP = (CreateSkinActionExtensionPoint) smanager.lookup(CreateSkinActionExtensionPoint.ROLE);
043    }
044    
045    /**
046     * Get the actions menu for creating a new skin
047     * @return The actions configuration.
048     */
049    @Callable
050    public List<Map<String, Object>> getCreateSkinActions ()
051    {
052        List<Map<String, Object>> actions = new ArrayList<>();
053        
054        for (String id : _createSkinActionEP.getExtensionsIds())
055        {
056            ClientSideElement elmt = _createSkinActionEP.getExtension(id);
057            for (Script script : elmt.getScripts(new HashMap<String, Object>()))
058            {
059                actions.add(script.getParameters());
060            }
061        }
062        
063        return actions;
064    }
065    
066    @Override
067    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
068    {
069        List<Script> scripts = new ArrayList<>();
070
071        List<ScriptFile> scriptFiles = new ArrayList<>();
072        List<ScriptFile> cssFiles = new ArrayList<>();
073        Map<String, Object> parameters = new HashMap<>();
074        
075        for (Script script : super.getScripts(ignoreRights, contextParameters))
076        {
077            scriptFiles.addAll(script.getScriptFiles());        
078            cssFiles.addAll(script.getCSSFiles());
079            parameters.putAll(script.getParameters());
080        }
081        
082        Set<String> createActionIds = _createSkinActionEP.getExtensionsIds();
083        
084        for (String id : createActionIds)
085        {
086            ClientSideElement createAction = _createSkinActionEP.getExtension(id);
087            for (Script script : createAction.getScripts(ignoreRights, contextParameters))
088            {
089                scriptFiles.addAll(script.getScriptFiles());
090                cssFiles.addAll(script.getCSSFiles());
091                parameters.putAll(script.getParameters());
092            }
093        }
094        
095        scripts.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
096        return scripts;
097    }
098}