001/*
002 *  Copyright 2017 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.content.referencetable;
017
018import java.util.HashMap;
019import java.util.List;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.configuration.DefaultConfiguration;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.core.ui.StaticFileImportsClientSideElement;
028
029/**
030 * This implementation imports the file only if it is needed, i.e. if there is at least one hierarchy of simple content
031 */
032public class ChooseHierarchicalContentFileImportsClientSideElement extends StaticFileImportsClientSideElement
033{
034    /** The helper component for hierarchical simple contents */
035    protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper;
036    
037    @Override
038    public void service(ServiceManager smanager) throws ServiceException
039    {
040        super.service(smanager);
041        _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) smanager.lookup(HierarchicalReferenceTablesHelper.ROLE);
042    }
043    
044    @Override
045    protected Script _configureScript(Configuration configuration) throws ConfigurationException
046    {
047        DefaultConfiguration dynamicScriptConfig = new DefaultConfiguration("scripts");
048        
049        // Only import ChooseHierarchicalContent.js if needed
050        if (_hierarchicalSimpleContentsHelper.hasAtLeastOneHierarchy())
051        {
052            DefaultConfiguration fileConfig = new DefaultConfiguration("file");
053            fileConfig.setValue("js/Ametys/plugins/cms/content/tree/HierarchicalReferenceTablesTree.js");
054            dynamicScriptConfig.addChild(fileConfig);
055            
056            fileConfig = new DefaultConfiguration("file");
057            fileConfig.setValue("js/Ametys/plugins/cms/content/tree/HierarchicalReferenceTablesTree/HierarchicalReferenceTablesTreeEntry.js");
058            dynamicScriptConfig.addChild(fileConfig);
059            
060            fileConfig = new DefaultConfiguration("file");
061            fileConfig.setValue("js/Ametys/cms/uihelper/ChooseHierarchicalContent.js");
062            dynamicScriptConfig.addChild(fileConfig);
063        }
064        
065        List<ScriptFile> scriptsImports = _configureImports(dynamicScriptConfig);
066        List<ScriptFile> cssImports = _configureImports(configuration.getChild("css"));
067        
068        return new Script(this.getId(), "", scriptsImports, cssImports, new HashMap<>());
069    }
070}