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.Locale; 022import java.util.Map; 023 024import org.apache.avalon.framework.context.Context; 025import org.apache.avalon.framework.context.ContextException; 026import org.apache.avalon.framework.context.Contextualizable; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.cocoon.components.ContextHelper; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.core.ui.ClientSideElement; 033import org.ametys.core.ui.StaticClientSideElement; 034import org.ametys.plugins.core.ui.help.HelpManager; 035import org.ametys.plugins.core.ui.util.ConfigurationHelper; 036 037/** 038 * This client side element completes its configuration by loading HTMLExpertToolbarExtensionPoint buttons 039 */ 040public class HTMLExpertEditClientSideElement extends StaticClientSideElement implements Contextualizable 041{ 042 /** The HTMLExpertToolbarExtensionPoint instance */ 043 protected HTMLExpertToolbarExtensionPoint _htmlExpertToolbarExtensionPoint; 044 045 /** The helper manager */ 046 protected HelpManager _helpManager; 047 048 /** The context */ 049 protected Context _context; 050 051 @Override 052 public void service(ServiceManager smanager) throws ServiceException 053 { 054 super.service(smanager); 055 _htmlExpertToolbarExtensionPoint = (HTMLExpertToolbarExtensionPoint) smanager.lookup(HTMLExpertToolbarExtensionPoint.ROLE); 056 _helpManager = (HelpManager) smanager.lookup(HelpManager.ROLE); 057 } 058 059 public void contextualize(Context context) throws ContextException 060 { 061 _context = context; 062 } 063 064 @SuppressWarnings("unchecked") 065 @Override 066 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 067 { 068 List<ScriptFile> cssFiles = new ArrayList<>(); 069 List<ScriptFile> scriptFiles = new ArrayList<>(); 070 Map<String, Object> parameters = ConfigurationHelper.clonePluginParameters(_script.getParameters()); 071 072 _addHelpURL(parameters); 073 074 parameters.put("toolbar-buttons", new ArrayList<>()); 075 076 for (String extensionId : _htmlExpertToolbarExtensionPoint.getExtensionsIds()) 077 { 078 ClientSideElement button = _htmlExpertToolbarExtensionPoint.getExtension(extensionId); 079 080 Map<String, Object> btnParams = new HashMap<>(); 081 082 btnParams.put("id", extensionId); 083 btnParams.put("pluginName", button.getPluginName()); 084 085 Map<String, Object> btnConfig = new HashMap<>(); 086 for (Script script : button.getScripts(ignoreRights, contextParameters)) 087 { 088 if (script != null) 089 { 090 btnParams.put("classname", script.getScriptClassname()); 091 for (String parameterKey : script.getParameters().keySet()) 092 { 093 Object parameter = script.getParameters().get(parameterKey); 094 btnConfig.put(parameterKey, parameter); 095 } 096 } 097 } 098 099 btnParams.put("config", btnConfig); 100 101 ((ArrayList<Map<String, Object>>) parameters.get("toolbar-buttons")).add(btnParams); 102 } 103 104 105 List<Script> scripts = super.getScripts(ignoreRights, contextParameters); 106 107 if (scripts.size() == 1) 108 { 109 Script script = scripts.get(0); 110 111 cssFiles.addAll(script.getCSSFiles()); 112 scriptFiles.addAll(script.getScriptFiles()); 113 ConfigurationHelper.clonePluginParameters(script.getParameters()); 114 115 for (String extensionId : _htmlExpertToolbarExtensionPoint.getExtensionsIds()) 116 { 117 ClientSideElement button = _htmlExpertToolbarExtensionPoint.getExtension(extensionId); 118 for (Script btnScript : button.getScripts(ignoreRights, contextParameters)) 119 { 120 if (btnScript != null) 121 { 122 cssFiles.addAll(btnScript.getCSSFiles()); 123 scriptFiles.addAll(btnScript.getScriptFiles()); 124 } 125 } 126 } 127 128 List<Script> scriptList = new ArrayList<>(); 129 scriptList.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters)); 130 return scriptList; 131 } 132 133 return new ArrayList<>(); 134 } 135 136 /** 137 * Add the help url in parameters 138 * @param parameters the parameters 139 */ 140 protected void _addHelpURL(Map<String, Object> parameters) 141 { 142 Map objectModel = ContextHelper.getObjectModel(_context); 143 144 Locale locale = org.apache.cocoon.i18n.I18nUtils.findLocale(objectModel, "locale", null, Locale.getDefault(), true); 145 String langCode = locale.getLanguage(); 146 147 String familyId = "org.ametys.core.ui.RibbonControlsManager"; 148 String id = "org.ametys.cms.edition.htmlexpert.Edit"; 149 try 150 { 151 String helpURL = _helpManager.getHelp(familyId, id, langCode); 152 if (StringUtils.isNotBlank(helpURL)) 153 { 154 parameters.put("helpURL", helpURL); 155 } 156 } 157 catch (Exception e) 158 { 159 getLogger().error("An error occurred getting help url for family '{}' and id '{}'", familyId, id, e); 160 } 161 } 162}