001/* 002 * Copyright 2024 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.odf.tree; 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.ClientSideElementHelper; 027import org.ametys.core.util.JSONUtils; 028import org.ametys.plugins.contentstree.ui.TreeToolClientSideElement; 029 030/** 031 * Client side element for ODF tree tool that supports indicators 032 * 033 */ 034public class ODFTreeToolClientSideElement extends TreeToolClientSideElement 035{ 036 /** Extension point for indicators */ 037 protected ODFTreeIndicatorExtensionPoint _odfTreeIndicatorEP; 038 /** JSON utils */ 039 protected JSONUtils _jsonUtils; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 super.service(smanager); 045 _odfTreeIndicatorEP = (ODFTreeIndicatorExtensionPoint) smanager.lookup(ODFTreeIndicatorExtensionPoint.ROLE); 046 _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE); 047 } 048 049 @Override 050 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 051 { 052 List<Script> scripts = super.getScripts(ignoreRights, contextParameters); 053 054 if (scripts.size() > 0) 055 { 056 Script script = ClientSideElementHelper.cloneScript(scripts.get(0)); 057 058 scripts = new ArrayList<>(); 059 060 List<Map<String, Object>> indicators = new ArrayList<>(); 061 062 for (String indicatorId : _odfTreeIndicatorEP.getExtensionsIds()) 063 { 064 ODFTreeIndicator indicator = _odfTreeIndicatorEP.getExtension(indicatorId); 065 066 indicators.add(_indicator2json(indicator)); 067 068 List<Script> indicatorScripts = indicator.getScripts(contextParameters); 069 if (indicatorScripts.size() > 0) 070 { 071 // Add required scripts and/or css files for indicator 072 Script indicatorScript = ClientSideElementHelper.cloneScript(indicatorScripts.get(0)); 073 script.getScriptFiles().addAll(indicatorScript.getScriptFiles()); 074 script.getCSSFiles().addAll(indicatorScript.getCSSFiles()); 075 } 076 } 077 078 script.getParameters().put("indicators", _jsonUtils.convertObjectToJson(indicators)); 079 080 scripts = new ArrayList<>(); 081 scripts.add(script); 082 } 083 084 return scripts; 085 } 086 087 private Map<String, Object> _indicator2json(ODFTreeIndicator indicator) 088 { 089 Map<String, Object> indicatorInfo = new HashMap<>(); 090 indicatorInfo.put("id", indicator.getId()); 091 indicatorInfo.put("label", indicator.getLabel()); 092 indicatorInfo.put("description", indicator.getDescription()); 093 indicatorInfo.put("iconGlyph", indicator.getIconGlyph()); 094 indicatorInfo.put("matchFn", indicator.getMatchJSFunction()); 095 indicatorInfo.put("applyFn", indicator.getApplyJSFunction()); 096 return indicatorInfo; 097 } 098}