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.resource;
018
019import org.apache.wicket.Application;
020import org.apache.wicket.markup.head.IHeaderResponse;
021import org.apache.wicket.markup.head.JavaScriptHeaderItem;
022import org.apache.wicket.request.resource.ResourceReference;
023import org.apache.wicket.settings.DebugSettings;
024import org.apache.wicket.settings.JavaScriptLibrarySettings;
025
026/**
027 * A helper class that contributes all required JavaScript resources needed for Wicket Ajax
028 * functionality
029 *
030 * @since 6.0
031 */
032public class CoreLibrariesContributor
033{
034        /**
035         * Contributes the backing library plus the implementation of Wicket.Event.
036         *
037         * @param application
038         *            the application instance
039         * @param response
040         *            the current header response
041         */
042        public static void contribute(final Application application, final IHeaderResponse response)
043        {
044                JavaScriptLibrarySettings jsLibrarySettings = application.getJavaScriptLibrarySettings();
045                ResourceReference wicketAjaxReference = jsLibrarySettings.getWicketAjaxReference();
046                response.render(JavaScriptHeaderItem.forReference(wicketAjaxReference));
047        }
048
049        /**
050         * Contributes the Ajax backing library plus wicket-event.js and wicket-ajax.js implementations.
051         * Additionally if Ajax debug is enabled then wicket-ajax-debug.js implementation is also added.
052         *
053         * @param application
054         *            the application instance
055         * @param response
056         *            the current header response
057         */
058        public static void contributeAjax(final Application application, final IHeaderResponse response)
059        {
060                JavaScriptLibrarySettings jsLibrarySettings = application.getJavaScriptLibrarySettings();
061
062                ResourceReference wicketAjaxReference = jsLibrarySettings.getWicketAjaxReference();
063                response.render(JavaScriptHeaderItem.forReference(wicketAjaxReference));
064                
065                final DebugSettings debugSettings = application.getDebugSettings();
066                if (debugSettings.isAjaxDebugModeEnabled())
067                {
068                        response.render(JavaScriptHeaderItem.forScript("Wicket.Log.enabled=true;", "wicket-ajax-debug-enable"));
069                }
070        }
071}