<commit_before>//
//  Form.as
//
//  Created by <NAME> on 2008-06-19.
//  Copyright (c) 2008 Fork Unstable Media GmbH. All rights reserved.
//

package reprise.controls.html
{

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import reprise.commands.CompositeCommand;
	import reprise.data.IValidator;
	import reprise.events.CommandEvent;
	import reprise.events.DisplayEvent;
	import reprise.events.FormEvent;
	import reprise.ui.UIComponent;
	
	
	public class Form extends UIComponent
	{
		
		/***************************************************************************
		*                           protected properties                           *
		***************************************************************************/
		protected var m_fields:Array;
		protected var m_customValidators:Array;
		protected var m_submitButtons:Array;
		protected var m_backButtons:Array;
		protected var m_data : Object;
		protected var m_validationCommand:CompositeCommand;
		protected var m_validationDisabled:Boolean = false;
		
		
		/***************************************************************************
		*                              Public methods                              *
		***************************************************************************/
		public function Form() 
		{
			m_fields = [];
			m_submitButtons = [];
			m_backButtons = [];
			m_customValidators = [];
			addEventListener(DisplayEvent.ADDED_TO_DOCUMENT, self_displayObjectAdded);
			addEventListener(DisplayEvent.REMOVED_FROM_DOCUMENT, self_displayObjectRemoved);
		}
		
		public function data():Object
		{
			var i:int = 0;
			var data:Object = {};
			for (; i < m_fields.length; i++)
			{
				var input:IInput = m_fields[i] as IInput;
				if (!input.fieldName() || data[input.fieldName()])
				{
					continue;
				}
				data[input.fieldName()] = input.value();
			}
			return data;
		}
		
		public function activate() : void
		{
			addCSSPseudoClass('active');
		}
		
		public function deactivate() : void
		{
			removeCSSPseudoClass('active');
		}
		
		public function addCustomValidator(validator:IValidator):void
		{
			m_customValidators.push(validator);
		}
		
		public function failedValidators() : Array
		{
			var validators : Array = [];
			var i:int = 0;
			for (; i < m_fields.length; i++)
			{
				var input:IInput = m_fields[i] as IInput;
				if (input.didSucceed() || (!input.fieldName() && !input.required()))
				{
					continue;
				}
				validators.push(input);
			}
			for (i = 0; i < m_customValidators.length; i++)
			{
				var validator:IValidator = IValidator(m_customValidators[i]);
				if (!validator.didSucceed())
				{
					validators.push(validator);
				}
			}
			
			return validators;
		}
		
		public function fields():Array
		{
			// don't let anyone modify our fields
			return m_fields.concat();
		}
		
		public function fieldWithName(name:String):IInput
		{
			var i:int = m_fields.length;
			while (i--)
			{
				var field:IInput = IInput(m_fields[i]);
				if (field.fieldName() == name)
				{
					return field;
				}
			}
			return null;
		}
		
		public function setValidationDisabled(bFlag:Boolean):void
		{
			m_validationDisabled = bFlag;
		}
		
		
		
		/***************************************************************************
		*                             protected methods                            *
		***************************************************************************/
		protected override function initialize():void
		{
			super.initialize();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, self_keyDown);
		}
		
		protected function validate():void
		{
			if (m_validationDisabled)
			{
				dispatchSubmitEvent();
				return;
			}
			
			m_validationCommand = new CompositeCommand();
			m_validationCommand.setAbortOnFailure(false);
			var i:int = 0;
			var count:int = 0;
			for (; i < m_fields.length; i++)
			{
				var input:IInput = m_fields[i] as IInput;
				if (!input.fieldName() && !input.required())
				{
					continue;
				}
				m_validationCommand.addCommand(input);
				count++;
			}
			for (i = 0; i < m_customValidators.length; i++)
			{
				var validator:IValidator = IValidator(m_customValidators[i]);
				m_validationCommand.addCommand(validator);
				count++;
			}
			if (!count)
			{
				dispatchSubmitEvent();
				return;
			}
			dispatchEvent(new FormEvent(FormEvent.WILL_VALIDATE));
			m_validationCommand.addEventListener(Event.COMPLETE, validation_complete);
			m_validationCommand.execute();

			if (!m_validationCommand.isExecuting())
			{
				m_validationCommand.didSucceed() 
					? dispatchSubmitEvent()
					: dispatchEvent(new FormEvent(FormEvent.VALIDATION_FAILURE));
			}
		}
		
		protected function validation_complete(e:CommandEvent):void
		{
			log('validation complete, success ' + e.success);
			if (!e.success)
			{
				dispatchEvent(new FormEvent(FormEvent.VALIDATION_FAILURE));
				return;
			}
			dispatchEvent(new FormEvent(FormEvent.SUBMIT));
		}
		
		protected function dispatchSubmitEvent():void
		{
			dispatchEvent(new FormEvent(FormEvent.SUBMIT));
		}
		
		protected function self_displayObjectAdded(e:Event) : void
		{
			if (e.target is IInput)
			{
				m_fields.push(e.target);
			}
			
			if (!(e.target is UIComponent))
			{
				return;
			}
			
			if (e.target is SubmitButton)
			{<commit_msg>- change: Forms get the pseudo class ':error' if their validation fails. This allows to e.g. show error messages on validation failure<commit_after>//
//  Form.as
//
//  Created by <NAME> on 2008-06-19.
//  Copyright (c) 2008 Fork Unstable Media GmbH. All rights reserved.
//

package reprise.controls.html
{

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import reprise.commands.CompositeCommand;
	import reprise.data.IValidator;
	import reprise.events.CommandEvent;
	import reprise.events.DisplayEvent;
	import reprise.events.FormEvent;
	import reprise.ui.UIComponent;
	
	
	public class Form extends UIComponent
	{
		
		/***************************************************************************
		*                           protected properties                           *
		***************************************************************************/
		protected var m_fields:Array;
		protected var m_customValidators:Array;
		protected var m_submitButtons:Array;
		protected var m_backButtons:Array;
		protected var m_data : Object;
		protected var m_validationCommand:CompositeCommand;
		protected var m_validationDisabled:Boolean = false;
		
		
		/***************************************************************************
		*                              Public methods                              *
		***************************************************************************/
		public function Form() 
		{
			m_fields = [];
			m_submitButtons = [];
			m_backButtons = [];
			m_customValidators = [];
			addEventListener(DisplayEvent.ADDED_TO_DOCUMENT, self_displayObjectAdded);
			addEventListener(DisplayEvent.REMOVED_FROM_DOCUMENT, self_displayObjectRemoved);
		}
		
		public function data():Object
		{
			var i:int = 0;
			var data:Object = {};
			for (; i < m_fields.length; i++)
			{
				var input:IInput = m_fields[i] as IInput;
				if (!input.fieldName() || data[input.fieldName()])
				{
					continue;
				}
				data[input.fieldName()] = input.value();
			}
			return data;
		}
		
		public function activate() : void
		{
			addCSSPseudoClass('active');
		}
		
		public function deactivate() : void
		{
			removeCSSPseudoClass('active');
		}
		
		public function addCustomValidator(validator:IValidator):void
		{
			m_customValidators.push(validator);
		}
		
		public function failedValidators() : Array
		{
			var validators : Array = [];
			var i:int = 0;
			for (; i < m_fields.length; i++)
			{
				var input:IInput = m_fields[i] as IInput;
				if (input.didSucceed() || (!input.fieldName() && !input.required()))
				{
					continue;
				}
				validators.push(input);
			}
			for (i = 0; i < m_customValidators.length; i++)
			{
				var validator:IValidator = IValidator(m_customValidators[i]);
				if (!validator.didSucceed())
				{
					validators.push(validator);
				}
			}
			
			return validators;
		}
		
		public function fields():Array
		{
			// don't let anyone modify our fields
			return m_fields.concat();
		}
		
		public function fieldWithName(name:String):IInput
		{
			var i:int = m_fields.length;
			while (i--)
			{
				var field:IInput = IInput(m_fields[i]);
				if (field.fieldName() == name)
				{
					return field;
				}
			}
			return null;
		}
		
		public function setValidationDisabled(bFlag:Boolean):void
		{
			m_validationDisabled = bFlag;
		}
		
		
		
		/***************************************************************************
		*                             protected methods                            *
		***************************************************************************/
		protected override function initialize():void
		{
			super.initialize();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, self_keyDown);
		}
		
		protected function validate():void
		{
			if (m_validationDisabled)
			{
				dispatchSubmitEvent();
				return;
			}
			
			m_validationCommand = new CompositeCommand();
			m_validationCommand.setAbortOnFailure(false);
			var i:int = 0;
			var count:int = 0;
			for (; i < m_fields.length; i++)
			{
				var input:IInput = m_fields[i] as IInput;
				if (!input.fieldName() && !input.required())
				{
					continue;
				}
				m_validationCommand.addCommand(input);
				count++;
			}
			for (i = 0; i < m_customValidators.length; i++)
			{
				var validator:IValidator = IValidator(m_customValidators[i]);
				m_validationCommand.addCommand(validator);
				count++;
			}
			if (!count)
			{
				dispatchSubmitEvent();
				return;
			}
			dispatchEvent(new FormEvent(FormEvent.WILL_VALIDATE));
			m_validationCommand.addEventListener(Event.COMPLETE, validation_complete);
			m_validationCommand.execute();

			if (!m_validationCommand.isExecuting())
			{
				validation_complete(
					new CommandEvent(Event.COMPLETE, m_validationCommand.didSucceed()));
			}
		}
		
		protected function validation_complete(e:CommandEvent):void
		{
			if (!e.success)
			{
				addCSSPseudoClass('error');
				dispatchEvent(new FormEvent(FormEvent.VALIDATION_FAILURE));
				return;
			}
			removeCSSPseudoClass('error');
			dispatchEvent(new FormEvent(FormEvent.SUBMIT));
		}
		
		protected function dispatchSubmitEvent():void
		{
			dispatchEvent(new FormEvent(FormEvent.SUBMIT));
		}
		
		protected function self_displayObjectAdded(e:Event) : void
		{
			if (e.target is IInput)
			{
				m_fields.push(e.target);
			}
			
			if (!(e.target is UIComponent))
			{
				return;
			}
			
			if (e.target is SubmitButton)
			{
{'fraction_non_alphanumeric': 0.1874479311302416, 'fraction_numerical': 0.0033324076645376284, 'mean_word_length': 3.17303978370027, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '3202746', 'n_tokens_mistral': 3997, 'n_tokens_neox': 3132, 'n_words': 611}