<commit_before>/**
 * Copyright (C) 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.alpharogroup.wicket.behaviors;

import java.util.UUID;

import lombok.Builder;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.util.lang.Args;

/**
 * The Class JavascriptAppenderBehavior simply adds the given javascript code as String with an id
 * in the html page as script block.
 */
@Builder
public class JavascriptAppenderBehavior extends Behavior
{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/**
	 * The unique id for the javascript element. This can be null, however in that case the ajax
	 * header contribution can't detect duplicate script fragments.
	 */
	private final String id;

	/** The javascript code to be rendered. */
	private final CharSequence javascript;

	/**
	 * Instantiates a new {@link JavascriptAppenderBehavior}. The id will be generated.
	 *
	 * @param javascript
	 *            javascript content to be add.
	 */
	public JavascriptAppenderBehavior(final CharSequence javascript)
	{
		this(String.valueOf(UUID.randomUUID()), javascript);
	}

	/**
	 * Instantiates a new {@link JavascriptAppenderBehavior}.
	 *
	 * @param javascript
	 *            javascript content to be add.
	 * @param id
	 *            unique id for the javascript element.
	 */
	public JavascriptAppenderBehavior(final String id, final CharSequence javascript)
	{
		this.javascript = Args.notNull(javascript, "javascript");
		if (id == null)
		{
			this.id = String.valueOf(UUID.randomUUID());
		}
		else
		{
			this.id = id;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void renderHead(final Component component, final IHeaderResponse response)
	{
		super.renderHead(component, response);
		response.render(JavaScriptHeaderItem.forScript(this.javascript, this.id));
	}

}<commit_msg>BindEvent for binding js created<commit_after>/**
 * Copyright (C) 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.alpharogroup.wicket.behaviors;

import java.util.UUID;

import lombok.Setter;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.head.OnEventHeaderItem;
import org.apache.wicket.markup.head.OnLoadHeaderItem;
import org.apache.wicket.util.lang.Args;

/**
 * The Class JavascriptAppenderBehavior simply adds the given javascript code as String with an id
 * in the html page as script block.
 */
public class JavascriptAppenderBehavior extends Behavior
{
	public enum JavascriptBindEvent { ONDOMREADY, ONEVENT, ONLOAD};
	JavascriptBindEvent DEFAULT_BIND_EVENT = JavascriptBindEvent.ONDOMREADY;

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The target of the event handler. default window. */
	@Setter
	private String target="window";
	
	/** The event. default click. */
	@Setter
	private String event = "click";
	
	/** The bind event for the javascript. */
	@Setter
	private JavascriptBindEvent bindEvent = DEFAULT_BIND_EVENT;
	

	/**
	 * The unique id for the javascript element. This can be null, however in that case the ajax
	 * header contribution can't detect duplicate script fragments.
	 */
	private final String id;

	/** The javascript code to be rendered. */
	private final CharSequence javascript;

	/**
	 * Instantiates a new {@link JavascriptAppenderBehavior}. The id will be generated.
	 *
	 * @param javascript
	 *            javascript content to be add.
	 */
	public JavascriptAppenderBehavior(final CharSequence javascript)
	{
		this(String.valueOf(UUID.randomUUID()), javascript);
	}

	/**
	 * Instantiates a new {@link JavascriptAppenderBehavior}.
	 *
	 * @param javascript
	 *            javascript content to be add.
	 * @param id
	 *            unique id for the javascript element.
	 */
	public JavascriptAppenderBehavior(final String id, final CharSequence javascript)
	{
		this.javascript = Args.notNull(javascript, "javascript");
		if (id == null)
		{
			this.id = String.valueOf(UUID.randomUUID());
		}
		else
		{
			this.id = id;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void renderHead(final Component component, final IHeaderResponse response)
	{
		super.renderHead(component, response);
		switch (bindEvent) {
		case ONDOMREADY:
			response.render(OnDomReadyHeaderItem.forScript(this.javascript));			
			break;
		case ONEVENT:
			response.render(OnEventHeaderItem.forScript(target, event, this.javascript));		
			break;
		case ONLOAD:
			response.render(OnLoadHeaderItem.forScript(this.javascript));	
			break;
		default:
			response.render(JavaScriptHeaderItem.forScript(this.javascript, this.id));
			break;
		}
	}

}
{'fraction_non_alphanumeric': 0.09394641085014886, 'fraction_numerical': 0.0029771749917300696, 'mean_word_length': 3.8531300160513644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3962338', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1662, 'n_words': 564}