<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.ui.selectable;

import java.util.StringTokenizer;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.odlabs.wiquery.core.javascript.JsScopeContext;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.core.options.Options;
import org.odlabs.wiquery.core.util.MarkupIdVisitor;
import org.odlabs.wiquery.ui.core.JsScopeUiEvent;
import org.odlabs.wiquery.ui.selectable.SelectableBehavior.ToleranceEnum;

/**
 * $Id: SelectableAjaxBehavior 
 * <p>
 * Sets the attached component selectable behavior. When the user finished to
 * select some childs components,{@link #onSelection(Component[], AjaxRequestTarget)} 
 * is called via Ajax.
 * </p>
 * 
 * <p>
 * This behavior contains a {@link SelectableBehavior} which is used to control 
 * the options of the selectable, including all the options and event of the
 * behavior. Example:
 * <pre>
 * SelectableAjaxBehavior selectable = new SelectableAjaxBehavior() {
 * 		public void onSelection(Component[] components, AjaxRequestTarget ajaxRequestTarget) {
 * 			...
 * 		}
 * }
 * SelectableBehavior sb = selectable.getSelectableBehavior();
 * sb.setTolerance(ToleranceEnum.FIT);
 * add(selectable);
 * </pre>
 * </p>
 * 
 * @author <NAME>
 * @since 1.0
 */
public abstract class SelectableAjaxBehavior extends AbstractDefaultAjaxBehavior {
	/**
	 * We override the behavior to deny the access of critical methods 
	 * 
	 * @author <NAME>
	 * 
	 */
	private class InnerDroppableBehavior extends SelectableBehavior {
		// Constants
		/** Constant of serialization */
		private static final long serialVersionUID = 5587258236214715234L;
		
		/**
		 * {@inheritDoc}
		 * @see org.odlabs.wiquery.ui.selectable.SelectableBehavior#getOptions()
		 */
		@Override
		protected Options getOptions() {
			throw new UnsupportedOperationException(
					"You can't call this method into the SelectableAjaxBehavior");
		}

		/**
		 * For framework internal use only.
		 */
		private void setInnerStopEvent(JsScopeUiEvent stop) {
			super.setStopEvent(stop);
		}

		/**
		 * {@inheritDoc}
		 * @see org.odlabs.wiquery.ui.selectable.SelectableBehavior#setStopEvent(org.odlabs.wiquery.ui.core.JsScopeUiEvent)
		 */
		@Override
		public SelectableBehavior setStopEvent(JsScopeUiEvent stop) {
			throw new UnsupportedOperationException(
					"You can't call this method into the SelectableAjaxBehavior");
		}

		/**
		 * {@inheritDoc}
		 * @see org.odlabs.wiquery.ui.selectable.SelectableBehavior#statement()
		 */
		@Override
		public JsStatement statement() {
			selectableBehavior.setInnerStopEvent(new JsScopeUiEvent() {
				private static final long serialVersionUID = 1L;

				/**
				 * {@inheritDoc}
				 * @see org.odlabs.wiquery.core.javascript.JsScope#execute(org.odlabs.wiquery.core.javascript.JsScopeContext)
				 */
				@Override
				protected void execute(JsScopeContext scopeContext) {
					scopeContext.append("var selected = new Array();"
							+ "jQuery.each($('#" + getComponent().getMarkupId(true) +"')" +
									".children(\"*[class*='ui-selected']\"), function(){" +
									"selected.push($(this).attr('id'));});"
							+ SelectableAjaxBehavior.this.getCallbackScript(true));
				}

			});
			
			return super.statement();
		}
	}

	// Constants
	/** Constant of serialization */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Adding the standard selectable JavaScript behavior
	 */
	private InnerDroppableBehavior selectableBehavior;

	/** Selection into the request */
	private static final String SELECTED_ARRAY = "selectedArray";

	/**<commit_msg>Wiquery 1.2 & 1.5<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.ui.selectable;

import java.util.StringTokenizer;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.odlabs.wiquery.core.javascript.JsScopeContext;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.core.options.Options;
import org.odlabs.wiquery.core.util.MarkupIdVisitor;
import org.odlabs.wiquery.ui.core.JsScopeUiEvent;
import org.odlabs.wiquery.ui.selectable.SelectableBehavior.ToleranceEnum;

/**
 * $Id: SelectableAjaxBehavior 
 * <p>
 * Sets the attached component selectable behavior. When the user finished to
 * select some childs components,{@link #onSelection(Component[], AjaxRequestTarget)} 
 * is called via Ajax.
 * </p>
 * 
 * <p>
 * This behavior contains a {@link SelectableBehavior} which is used to control 
 * the options of the selectable, including all the options and event of the
 * behavior. Example:
 * <pre>
 * SelectableAjaxBehavior selectable = new SelectableAjaxBehavior() {
 * 		public void onSelection(Component[] components, AjaxRequestTarget ajaxRequestTarget) {
 * 			...
 * 		}
 * }
 * SelectableBehavior sb = selectable.getSelectableBehavior();
 * sb.setTolerance(ToleranceEnum.FIT);
 * add(selectable);
 * </pre>
 * </p>
 * 
 * @author <NAME>
 * @since 1.0
 */
public abstract class SelectableAjaxBehavior extends AbstractDefaultAjaxBehavior {
	/**
	 * We override the behavior to deny the access of critical methods 
	 * 
	 * @author <NAME>
	 * 
	 */
	private class InnerDroppableBehavior extends SelectableBehavior {
		// Constants
		/** Constant of serialization */
		private static final long serialVersionUID = 5587258236214715234L;
		
		/**
		 * {@inheritDoc}
		 * @see org.odlabs.wiquery.ui.selectable.SelectableBehavior#getOptions()
		 */
		@Override
		protected Options getOptions() {
			throw new UnsupportedOperationException(
					"You can't call this method into the SelectableAjaxBehavior");
		}

		/**
		 * For framework internal use only.
		 */
		private void setInnerStopEvent(JsScopeUiEvent stop) {
			super.setStopEvent(stop);
		}

		/**
		 * {@inheritDoc}
		 * @see org.odlabs.wiquery.ui.selectable.SelectableBehavior#setStopEvent(org.odlabs.wiquery.ui.core.JsScopeUiEvent)
		 */
		@Override
		public SelectableBehavior setStopEvent(JsScopeUiEvent stop) {
			throw new UnsupportedOperationException(
					"You can't call this method into the SelectableAjaxBehavior");
		}

		/**
		 * {@inheritDoc}
		 * @see org.odlabs.wiquery.ui.selectable.SelectableBehavior#statement()
		 */
		@Override
		public JsStatement statement() {
			selectableBehavior.setInnerStopEvent(new JsScopeUiEvent() {
				private static final long serialVersionUID = 1L;

				/**
				 * {@inheritDoc}
				 * @see org.odlabs.wiquery.core.javascript.JsScope#execute(org.odlabs.wiquery.core.javascript.JsScopeContext)
				 */
				@Override
				protected void execute(JsScopeContext scopeContext) {
					scopeContext.append("var selected = new Array();"
							+ "jQuery.each($('#" + getComponent().getMarkupId(true) +"')" +
									".find(\".ui-selectee.ui-selected\"), function(){" +
									"selected.push($(this).attr('id'));});"
							+ SelectableAjaxBehavior.this.getCallbackScript(true));
				}

			});
			
			return super.statement();
		}
	}

	// Constants
	/** Constant of serialization */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Adding the standard selectable JavaScript behavior
	 */
	private InnerDroppableBehavior selectableBehavior;

	/** Selection into the request */
	private static final String SELECTED_ARRAY = "selectedArray";

	/**
{'fraction_non_alphanumeric': 0.10208744417904247, 'fraction_numerical': 0.006023470765396199, 'mean_word_length': 4.08716323296355, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1118299', 'n_tokens_mistral': 3127, 'n_tokens_neox': 2716, 'n_words': 820}