001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.console.freemarker; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.util.*; 022 023import freemarker.core.*; 024import freemarker.template.*; 025 026/** 027 * The {@code jcHasToolkit(name)} FreeMarker method: whether the current {@code <@page toolkit=…>} lists 028 * {@code name}. Lets a chrome check which toolkits are active so it can emit toolkit-specific markup. 029 * 030 * <h5 class='section'>Example:</h5> 031 * <p class='bcode'> 032 * <#if jcHasToolkit("views")> 033 * <link rel="stylesheet" href="/app/views-extras.css"> 034 * </#if> 035 * </p> 036 * 037 * <p> 038 * Only {@code toolkit=} counts. Packs that cards require (for example {@code "datatables-glue"} for a 039 * {@code type="datatables"} card) are loaded but don't make {@code jcHasToolkit} return <jk>true</jk>. 040 * 041 * @since 10.0.0 042 */ 043public final class HasToolkitMethodModel implements TemplateMethodModelEx { 044 045 /** The shared-variable name this method registers under. */ 046 public static final String NAME = "jcHasToolkit"; 047 048 HasToolkitMethodModel() {} 049 050 @Override 051 public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { 052 if (args.size() != 1) 053 throw FtlAttrLists.reject(f("%s(name) takes exactly one argument; got '%s'.", NAME, args.size())); 054 var a = args.get(0); 055 if (! (a instanceof TemplateScalarModel s)) 056 throw FtlAttrLists.reject(f("%s(name) takes a string; got '%s'.", NAME, a)); 057 var cap = PageCapture.get(Environment.getCurrentEnvironment()); 058 return nn(cap) && cap.toolkits().contains(s.getAsString().trim()) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; 059 } 060}