Search in sources :

Example 1 with JavaScriptReferenceHeaderItem

use of org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem in project wicket by apache.

the class ResourceBundles method addJavaScriptBundle.

/**
 * Adds a javascript bundle that is automatically generated by concatenating the given package
 * resources. If the given resources depend on each other, you should make sure that the
 * resources are provided in the order they need to be concatenated. If the resources depend on
 * other resources, that are not part of the bundle, the bundle will inherit these dependencies.
 *
 * This method is equivalent to {@link #addBundle(HeaderItem)} with a
 * {@link JavaScriptHeaderItem} for a {@link ConcatResourceBundleReference}.
 *
 * @param scope
 *            The {@linkplain ResourceReference#getScope() scope} of the bundle
 * @param defer
 *            specifies that the execution of a script should be deferred (delayed) until after
 *            the page has been loaded.
 * @param name
 *            The name of the resource. This will show up as the filename in the markup.
 * @param references
 *            The resources this bundle will consist of.
 * @return the newly created bundle
 */
public JavaScriptReferenceHeaderItem addJavaScriptBundle(Class<?> scope, String name, boolean defer, JavaScriptResourceReference... references) {
    List<JavaScriptReferenceHeaderItem> items = new ArrayList<>();
    for (JavaScriptResourceReference curReference : references) {
        items.add(JavaScriptHeaderItem.forReference(curReference));
    }
    ConcatResourceBundleReference<JavaScriptReferenceHeaderItem> bundleReference = newBundleResourceReference(scope, name, items);
    if (Application.exists()) {
        IJavaScriptCompressor javaScriptCompressor = Application.get().getResourceSettings().getJavaScriptCompressor();
        bundleReference.setCompressor(javaScriptCompressor);
    }
    if (defer) {
        return addBundle(JavaScriptHeaderItem.forReference(bundleReference, defer));
    } else {
        return addBundle(JavaScriptHeaderItem.forReference(bundleReference));
    }
}
Also used : JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) JavaScriptResourceReference(org.apache.wicket.request.resource.JavaScriptResourceReference) IJavaScriptCompressor(org.apache.wicket.javascript.IJavaScriptCompressor) ArrayList(java.util.ArrayList)

Example 2 with JavaScriptReferenceHeaderItem

use of org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem in project webanno by webanno.

the class BootstrapAwareJQueryUIJavaScriptResourceReference method getDependencies.

@Override
public List<HeaderItem> getDependencies() {
    IBootstrapSettings settings = Bootstrap.getSettings();
    final JavaScriptReferenceHeaderItem jsReference = JavaScriptHeaderItem.forReference(settings.getJsResourceReference(), new PageParameters(), "bootstrap-js", settings.deferJavascript());
    return Dependencies.combine(super.getDependencies(), jsReference);
}
Also used : IBootstrapSettings(de.agilecoders.wicket.core.settings.IBootstrapSettings) JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters)

Example 3 with JavaScriptReferenceHeaderItem

use of org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem in project wicket by apache.

the class DecoratingHeaderResponseTest method decoratedStringPrepend.

/**
 * Basic IHeaderResponseDecorator, just prepending the DECORATED string to resource name.
 *
 * @throws IOException
 * @throws ResourceStreamNotFoundException
 * @throws ParseException
 */
@Test
public void decoratedStringPrepend() throws IOException, ResourceStreamNotFoundException, ParseException {
    tester.getApplication().setHeaderResponseDecorator(new IHeaderResponseDecorator() {

        @Override
        public IHeaderResponse decorate(IHeaderResponse response) {
            return new ResourceAggregator(new DecoratingHeaderResponse(response) {

                @Override
                public void render(HeaderItem item) {
                    if (item instanceof JavaScriptReferenceHeaderItem) {
                        JavaScriptReferenceHeaderItem original = (JavaScriptReferenceHeaderItem) item;
                        item = JavaScriptHeaderItem.forReference(new PackageResourceReference("DECORATED-" + original.getReference().getName()), original.getId());
                    }
                    super.render(item);
                }
            });
        }
    });
    tester.startPage(TestPage.class);
    XmlPullParser parser = new XmlPullParser();
    parser.parse(tester.getLastResponseAsString());
    XmlTag tag = parser.nextTag();
    boolean isDecorated = false;
    do {
        if (tag.isOpen() && "script".equals(tag.getName())) {
            isDecorated = tag.getAttribute("src").toString().contains("DECORATED");
            if (!isDecorated) {
                fail();
            }
            break;
        }
    } while ((tag = parser.nextTag()) != null);
    assertTrue(isDecorated);
}
Also used : JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) XmlPullParser(org.apache.wicket.markup.parser.XmlPullParser) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) ResourceAggregator(org.apache.wicket.markup.head.ResourceAggregator) HeaderItem(org.apache.wicket.markup.head.HeaderItem) JavaScriptHeaderItem(org.apache.wicket.markup.head.JavaScriptHeaderItem) JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Aggregations

JavaScriptReferenceHeaderItem (org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem)3 IBootstrapSettings (de.agilecoders.wicket.core.settings.IBootstrapSettings)1 ArrayList (java.util.ArrayList)1 IJavaScriptCompressor (org.apache.wicket.javascript.IJavaScriptCompressor)1 HeaderItem (org.apache.wicket.markup.head.HeaderItem)1 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)1 JavaScriptHeaderItem (org.apache.wicket.markup.head.JavaScriptHeaderItem)1 ResourceAggregator (org.apache.wicket.markup.head.ResourceAggregator)1 XmlPullParser (org.apache.wicket.markup.parser.XmlPullParser)1 XmlTag (org.apache.wicket.markup.parser.XmlTag)1 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)1 JavaScriptResourceReference (org.apache.wicket.request.resource.JavaScriptResourceReference)1 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)1 Test (org.junit.Test)1