<commit_before>/*
 * Copyright (c) 2009 WiQuery team
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.odlabs.wiquery.core.commons;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.IRequestTarget;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.Page;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.odlabs.wiquery.core.commons.listener.JQueryCoreRenderingListener;
import org.odlabs.wiquery.core.commons.listener.JQueryUICoreRenderingListener;
import org.odlabs.wiquery.core.commons.listener.WiQueryPluginRenderingListener;
import org.odlabs.wiquery.core.commons.merge.WiQueryHeaderResponse;
import org.odlabs.wiquery.core.commons.merge.WiQueryMergedJavaScriptResourceReference;
import org.odlabs.wiquery.core.commons.merge.WiQueryMergedStyleSheetResourceReference;
import org.odlabs.wiquery.core.javascript.JsQuery;
import org.odlabs.wiquery.core.javascript.JsStatement;

/**
 * $Id$
 * <p>
 * Handles core JavaScript generation process of WiQuery.
 * </p>
 * <p>
 * If the request in a non ajax request, the generated JavaScript is wrapped by
 * a "dom ready" statement. Otherwise (in Ajax contexts, the generated
 * JavaScript is directly append to the given {@link AjaxRequestTarget}.
 * </p>
 *
 * @author <NAME>
 * @author <NAME>
 */
public class WiQueryCoreHeaderContributor implements Serializable,
IHeaderContributor {
	// Constants
	/** Constant of serialization */
	private static final long serialVersionUID = -347081993448442637L;

	/**
	 * meta data for WiQueryCoreHeaderContributor.
	 */
	private static final MetaDataKey<Boolean> WIQUERY_KEY = new MetaDataKey<Boolean>() {
		private static final long serialVersionUID = 1L;
	};

	/**
	 * Default constructor. Declares a standard configuration for listeners,
	 * e.g. uses jQuery and jQuery UI listeners by default.
	 */
	public WiQueryCoreHeaderContributor() {
		super();
	}

	/**
	 * Renders WiQuery's JavaScript code.
	 */
	public void renderHead(final IHeaderResponse response) {

		Boolean rendered = RequestCycle.get().getMetaData(WIQUERY_KEY);
		if(rendered == null || !rendered){

			WiQuerySettings instanciation = WiQuerySettings.get();

			final List<WiQueryPluginRenderingListener> pluginRenderingListeners = new ArrayList<WiQueryPluginRenderingListener>();

			pluginRenderingListeners.add(new JQueryCoreRenderingListener());
			pluginRenderingListeners.add(new JQueryUICoreRenderingListener());
			// Listeners add by users
			for(Iterator<WiQueryPluginRenderingListener> iterator = instanciation.getListeners(); iterator.hasNext();){
				pluginRenderingListeners.add(iterator.next());
			}

			// Shall we merge ?
			boolean enableResourcesMerging = instanciation.isEnableResourcesMerging();

			// the response is a unique statement containing all statements
			// to call
			JsQuery jsq = new JsQuery();
			JsStatement jsStatement = new JsStatement();
			JsStatement tempStatement = null;
			IRequestTarget target = RequestCycle.get().getRequestTarget();
			IHeaderResponse headerResponse = null;
			IWiQueryPlugin plugin = null;

			final List<IWiQueryPlugin> plugins = new ArrayList<IWiQueryPlugin>();
			AjaxRequestTarget ajaxRequestTarget = AjaxRequestTarget.get();

			IVisitor<Component> visitor = new IVisitor<Component>() {

				public Object component(Component component) {
					if(component.isEnabledInHierarchy() && component.determineVisibility()){
						if(component instanceof IWiQueryPlugin){
							plugins.add((IWiQueryPlugin) component);
						}
						for(IBehavior behavior : component.getBehaviors()){
							if(behavior instanceof IWiQueryPlugin && behavior.isEnabled(component)){
								plugins.add((IWiQueryPlugin) behavior);
							}
						}
						return CONTINUE_TRAVERSAL;
					}
					else{
						return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
					}
				}
			};

			if(ajaxRequestTarget == null){
				//is a normal page render
				Page page = RequestCycle.get().getResponsePage();
				if(page != null){
					page.visitChildren(visitor);
					if(page instanceof IWiQueryPlugin){
						plugins.add((IWiQueryPlugin) page);
					}
					for(IBehavior behavior : page.getBehaviors()){
						if(behavior instanceof IWiQueryPlugin && behavior.isEnabled(page)){
							plugins.add((IWiQueryPlugin) behavior);
						}
					}
				}
			}
			else{
				//is an ajax render
				for(Component component : ajaxRequestTarget.getComponents()){
					if(component.isEnabledInHierarchy() && component.determineVisibility()){
						if(component instanceof IWiQueryPlugin){
							plugins.add((IWiQueryPlugin) component);
						}
						for(IBehavior behavior : component.getBehaviors()){
							if(behavior instanceof IWiQueryPlugin && behavior.isEnabled(component)){
								plugins.add((IWiQueryPlugin) behavior);
							}
						}<commit_msg>dont check for disabled in wiquery components Issue-60<commit_after>/*
 * Copyright (c) 2009 WiQuery team
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.odlabs.wiquery.core.commons;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.IRequestTarget;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.Page;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.odlabs.wiquery.core.commons.listener.JQueryCoreRenderingListener;
import org.odlabs.wiquery.core.commons.listener.JQueryUICoreRenderingListener;
import org.odlabs.wiquery.core.commons.listener.WiQueryPluginRenderingListener;
import org.odlabs.wiquery.core.commons.merge.WiQueryHeaderResponse;
import org.odlabs.wiquery.core.commons.merge.WiQueryMergedJavaScriptResourceReference;
import org.odlabs.wiquery.core.commons.merge.WiQueryMergedStyleSheetResourceReference;
import org.odlabs.wiquery.core.javascript.JsQuery;
import org.odlabs.wiquery.core.javascript.JsStatement;

/**
 * $Id$
 * <p>
 * Handles core JavaScript generation process of WiQuery.
 * </p>
 * <p>
 * If the request in a non ajax request, the generated JavaScript is wrapped by
 * a "dom ready" statement. Otherwise (in Ajax contexts, the generated
 * JavaScript is directly append to the given {@link AjaxRequestTarget}.
 * </p>
 *
 * @author <NAME>
 * @author <NAME>
 */
public class WiQueryCoreHeaderContributor implements Serializable,
IHeaderContributor {
	// Constants
	/** Constant of serialization */
	private static final long serialVersionUID = -347081993448442637L;

	/**
	 * meta data for WiQueryCoreHeaderContributor.
	 */
	private static final MetaDataKey<Boolean> WIQUERY_KEY = new MetaDataKey<Boolean>() {
		private static final long serialVersionUID = 1L;
	};

	/**
	 * Default constructor. Declares a standard configuration for listeners,
	 * e.g. uses jQuery and jQuery UI listeners by default.
	 */
	public WiQueryCoreHeaderContributor() {
		super();
	}

	/**
	 * Renders WiQuery's JavaScript code.
	 */
	public void renderHead(final IHeaderResponse response) {

		Boolean rendered = RequestCycle.get().getMetaData(WIQUERY_KEY);
		if(rendered == null || !rendered){

			WiQuerySettings instanciation = WiQuerySettings.get();

			final List<WiQueryPluginRenderingListener> pluginRenderingListeners = new ArrayList<WiQueryPluginRenderingListener>();

			pluginRenderingListeners.add(new JQueryCoreRenderingListener());
			pluginRenderingListeners.add(new JQueryUICoreRenderingListener());
			// Listeners add by users
			for(Iterator<WiQueryPluginRenderingListener> iterator = instanciation.getListeners(); iterator.hasNext();){
				pluginRenderingListeners.add(iterator.next());
			}

			// Shall we merge ?
			boolean enableResourcesMerging = instanciation.isEnableResourcesMerging();

			// the response is a unique statement containing all statements
			// to call
			JsQuery jsq = new JsQuery();
			JsStatement jsStatement = new JsStatement();
			JsStatement tempStatement = null;
			IRequestTarget target = RequestCycle.get().getRequestTarget();
			IHeaderResponse headerResponse = null;
			IWiQueryPlugin plugin = null;

			final List<IWiQueryPlugin> plugins = new ArrayList<IWiQueryPlugin>();
			AjaxRequestTarget ajaxRequestTarget = AjaxRequestTarget.get();

			IVisitor<Component> visitor = new IVisitor<Component>() {

				public Object component(Component component) {
					if(component.determineVisibility()){
						if(component instanceof IWiQueryPlugin){
							plugins.add((IWiQueryPlugin) component);
						}
						for(IBehavior behavior : component.getBehaviors()){
							if(behavior instanceof IWiQueryPlugin && behavior.isEnabled(component)){
								plugins.add((IWiQueryPlugin) behavior);
							}
						}
						return CONTINUE_TRAVERSAL;
					}
					else{
						return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
					}
				}
			};

			if(ajaxRequestTarget == null){
				//is a normal page render
				Page page = RequestCycle.get().getResponsePage();
				if(page != null){
					page.visitChildren(visitor);
					if(page instanceof IWiQueryPlugin){
						plugins.add((IWiQueryPlugin) page);
					}
					for(IBehavior behavior : page.getBehaviors()){
						if(behavior instanceof IWiQueryPlugin && behavior.isEnabled(page)){
							plugins.add((IWiQueryPlugin) behavior);
						}
					}
				}
			}
			else{
				//is an ajax render
				for(Component component : ajaxRequestTarget.getComponents()){
					if(component.determineVisibility()){
						if(component instanceof IWiQueryPlugin){
							plugins.add((IWiQueryPlugin) component);
						}
						for(IBehavior behavior : component.getBehaviors()){
							if(behavior instanceof IWiQueryPlugin && behavior.isEnabled(component)){
								plugins.add((IWiQueryPlugin) behavior);
							}
						}
{'fraction_non_alphanumeric': 0.09270406934745967, 'fraction_numerical': 0.003852636648206116, 'mean_word_length': 4.429193899782135, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1119383', 'n_tokens_mistral': 4078, 'n_tokens_neox': 3493, 'n_words': 967}