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.wicket.markup.head; 018 019import java.util.Objects; 020 021import org.apache.wicket.request.mapper.parameter.PageParameters; 022import org.apache.wicket.request.resource.ResourceReference; 023 024/** 025 * Base class for all {@link HeaderItem}s that represent javascripts. This class mainly contains 026 * factory methods. 027 * 028 * @author papegaaij 029 */ 030public abstract class JavaScriptHeaderItem extends AbstractCspHeaderItem 031{ 032 private static final long serialVersionUID = 1L; 033 034 /** 035 * An optional markup id to set on the rendered <script> HTML element for 036 * this header item 037 */ 038 private String markupId; 039 040 /** 041 * @return unique id for the javascript element. 042 */ 043 public String getId() 044 { 045 return markupId; 046 } 047 048 /** 049 * Sets the markup id for this header item 050 * @param markupId 051 * the markup id 052 * @return {@code this} object, for method chaining 053 */ 054 public JavaScriptHeaderItem setId(String markupId) 055 { 056 this.markupId = markupId; 057 return this; 058 } 059 060 /** 061 * Creates a {@link JavaScriptReferenceHeaderItem} for the given reference. 062 * 063 * @param reference 064 * resource reference pointing to the javascript resource 065 * @param pageParameters 066 * the parameters for this Javascript resource reference 067 * @param id 068 * id that will be used to filter duplicate reference (it's still filtered by URL 069 * too) 070 * @return A newly created {@link JavaScriptReferenceHeaderItem} for the given reference. 071 */ 072 public static JavaScriptReferenceHeaderItem forReference(ResourceReference reference, 073 PageParameters pageParameters, String id) 074 { 075 return new JavaScriptReferenceHeaderItem(reference, pageParameters, id); 076 } 077 078 /** 079 * Creates a {@link JavaScriptReferenceHeaderItem} for the given reference. 080 * 081 * @param reference 082 * resource reference pointing to the JavaScript resource 083 * @param id 084 * id that will be used to filter duplicate reference (it's still filtered by URL 085 * too) 086 * @return A newly created {@link JavaScriptReferenceHeaderItem} for the given reference. 087 */ 088 public static JavaScriptReferenceHeaderItem forReference(ResourceReference reference, 089 String id) 090 { 091 return forReference(reference, null, id); 092 } 093 094 /** 095 * Creates a {@link JavaScriptReferenceHeaderItem} for the given reference. 096 * 097 * @param reference 098 * resource reference pointing to the JavaScript resource 099 * @return A newly created {@link JavaScriptReferenceHeaderItem} for the given reference. 100 */ 101 public static JavaScriptReferenceHeaderItem forReference(ResourceReference reference) 102 { 103 return forReference(reference, null, null); 104 } 105 106 /** 107 * Creates a {@link JavaScriptContentHeaderItem} for the given content. 108 * 109 * @param javascript 110 * javascript content to be rendered. 111 * @param id 112 * unique id for the javascript element. This can be null, however in that case the 113 * ajax header contribution can't detect duplicate script fragments. 114 * @return A newly created {@link JavaScriptContentHeaderItem} for the given content. 115 */ 116 public static JavaScriptContentHeaderItem forScript(CharSequence javascript, String id) 117 { 118 return new JavaScriptContentHeaderItem(javascript, id); 119 } 120 121 /** 122 * Creates a {@link JavaScriptUrlReferenceHeaderItem} for the given url. 123 * 124 * @param url 125 * context-relative url of the the javascript resource 126 * @return A newly created {@link JavaScriptUrlReferenceHeaderItem} for the given url. 127 */ 128 public static JavaScriptUrlReferenceHeaderItem forUrl(String url) 129 { 130 return forUrl(url, null); 131 } 132 133 /** 134 * Creates a {@link JavaScriptUrlReferenceHeaderItem} for the given url. 135 * 136 * @param url 137 * context-relative url of the the javascript resource 138 * @param id 139 * id that will be used to filter duplicate reference (it's still filtered by URL 140 * too) 141 * @return A newly created {@link JavaScriptUrlReferenceHeaderItem} for the given url. 142 */ 143 public static JavaScriptUrlReferenceHeaderItem forUrl(String url, String id) 144 { 145 return new JavaScriptUrlReferenceHeaderItem(url, id); 146 } 147 148 @Override 149 public boolean equals(Object o) 150 { 151 if (this == o) return true; 152 if (o == null || getClass() != o.getClass()) return false; 153 JavaScriptHeaderItem that = (JavaScriptHeaderItem) o; 154 return Objects.equals(markupId, that.markupId); 155 } 156 157 @Override 158 public int hashCode() 159 { 160 return Objects.hash(markupId); 161 } 162}