/* Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license */ package { /** * See the ParametersForForm.as for configurations. * @author kayoh */ import fl.controls.Button; import fl.controls.CheckBox; import fl.controls.ColorPicker; import fl.controls.ComboBox; import fl.controls.NumericStepper; import fl.controls.RadioButton; import fl.controls.RadioButtonGroup; import fl.controls.TextArea; import fl.controls.TextInput; import fl.data.DataProvider; // Add Adobe as3Validators into proper class path. // http://code.google.com/p/flash-validators/ import com.adobe.as3Validators.as3DataValidation; import com.yahoo.astra.containers.formClasses.FormLayoutStyle; import com.yahoo.astra.events.FormDataManagerEvent; import com.yahoo.astra.fl.containers.Form; import com.yahoo.astra.fl.utils.FlValueParser; import com.yahoo.astra.managers.FormDataManager; import flash.display.MovieClip; import flash.display.Sprite; import flash.text.TextFormat; public class ParametersContactForm extends Sprite { //-------------------------------------- // Constructor //-------------------------------------- public function ParametersContactForm() { super(); } //-------------------------------------- // Properties //-------------------------------------- private var myForm : Form; private var formDataManager : FormDataManager; private var dataResult : TextArea; private var submitButton : Button; //-------------------------------------- // Private Methods //-------------------------------------- internal function buildForm(config : Object = null) : Form { formDataManager = new FormDataManager(FlValueParser); myForm = initForm(config); // Attach FormDataManager to collect user input data. myForm.formDataManager = formDataManager; myForm.dataSource = initData(); return myForm; } //Init Form private function initForm(config : Object = null) : Form { var myForm : Form = new Form(); if(!config) return myForm; for (var i:String in config) { var curConfig : Object = config[i]; if(!curConfig) continue; switch (i) { case "textFormat": case "headTextFormat": case "instructionTextFormat": myForm.setStyle(i, new TextFormat(curConfig["font"].toString(), Number(curConfig["size"]), uint(curConfig["color"]))); break; case "skin": case "indicatorSkin": case "errorBoxColor": case "errorBoxAlpha": myForm.setStyle(i, curConfig); break; case "subFormHeading": if(curConfig is TextInput) { var txtInput : TextInput = curConfig as TextInput; var asterisk : MovieClip = new indicatorSkin(); var asteriskMC : Sprite = new Sprite(); asterisk.x = asterisk.width / 2; asterisk.y = asterisk.height / 2; asteriskMC.addChild(asterisk); myForm.subFormHeading(asteriskMC, txtInput.text); } break; case "errorString": if(formDataManager) formDataManager.errorString = curConfig.toString(); break; default: myForm[i] = config[i]; break; } } return myForm; } private function initData() : Array { // Init validator you want to use. var validator : as3DataValidation = new as3DataValidation(); // init UI components. var firstNameInput : TextInput = new TextInput(); firstNameInput.width = 200; var lastNameInput : TextInput = new TextInput(); lastNameInput.width = 200; var emailInput : TextInput = new TextInput(); emailInput.width = 200; var telInput : TextInput = new TextInput(); telInput.width = 200; var myRadioGroup : RadioButtonGroup = new RadioButtonGroup("options"); var emailFormatInput_text : RadioButton = new RadioButton(); emailFormatInput_text.label = "Plain Text"; emailFormatInput_text.value = "text"; emailFormatInput_text.group = myRadioGroup; var emailFormatInput_html : RadioButton = new RadioButton(); emailFormatInput_html.label = "HTML"; emailFormatInput_html.value = "html"; emailFormatInput_html.selected = true; emailFormatInput_html.group = myRadioGroup; var address_line_1 : TextInput = new TextInput(); address_line_1.width = 200; var address_line_2 : TextInput = new TextInput(); address_line_2.width = 200; var statesComboBox : ComboBox = statesComboBox(); statesComboBox.width = 90; var zipCodeInput : TextInput = new TextInput(); zipCodeInput.width = 40; var subscription_mail : CheckBox = new CheckBox(); subscription_mail.label = "Join a mailing list."; subscription_mail.width = 200; var subscription_call : CheckBox = new CheckBox(); subscription_call.label = "Join a calling list."; subscription_call.width = 200; var productColorInput : ColorPicker = new ColorPicker(); var products : MovieClip = new productList(); var productQuantity : NumericStepper = new NumericStepper(); productQuantity.stepSize = 1; productQuantity.minimum = 0; productQuantity.maximum = 12; productQuantity.width = 60; var commentInput : TextArea = new TextArea(); commentInput.width = 200; commentInput.height = 100; submitButton = new Button(); submitButton.label = "SUBMIT"; // Attach a trigger on a button(or any Display object) to start collect and validate user inputs. formDataManager.addTrigger(submitButton, handlerDataCollectionSuccess, handlerDataCollectionFail); // Init data Array to set dataSource. var myFormDataArr : Array = [{label:"First Name", items:firstNameInput, id:"firstName", source:firstNameInput}, {label:"Last Name", items:lastNameInput, required:true, id:"lastname", validator:validator.isNotEmpty, source:lastNameInput}, {label:"Address", items:[address_line_1,address_line_2], itemAlign : FormLayoutStyle.VERTICAL, id:["address_line_1","address_line_2"], source:[address_line_1,address_line_2]}, {label:"State", items:[statesComboBox,"Zipcode",zipCodeInput], required:[false,true], id:["state","zipcode"], source:[statesComboBox,zipCodeInput], property:["abbreviation", null], validator:[null, validator.isZip]}, {label:"Email", items:emailInput, instructionText:"We will not spam you.", id:"email", required:true, source:emailInput, validator:validator.isEmail}, {label:"Email Format", items:[emailFormatInput_text,emailFormatInput_html], id:"emailFormat", source:myRadioGroup}, {label:"Tel", items:telInput, required:true, id:"tel", validator:validator.isUSPhoneNumber, source:telInput}, {label:"Subsciption", items:[subscription_mail,subscription_call], itemAlign : FormLayoutStyle.VERTICAL, id:["subscription_mail","subscription_call"], source:[subscription_mail,subscription_call]}, {label:"Product ", items:products, id:"product", source:products, property:"myWishCar"}, {label:"Product Color", items:[productColorInput,"Qty", productQuantity], id:["color","qualtity"], source:[productColorInput,productQuantity]}, {label:"Message", items:commentInput, id:"message", source:commentInput, validator:validator.isNotEmpty}, {label:"", items:submitButton}]; return myFormDataArr; } private function handlerDataCollectionSuccess(e : FormDataManagerEvent) : void { if(!dataResult) { att_data_result(); dataResult.x = myForm.x + myForm.width + 50; } submitButton.enabled = false; var resultTxt : String = "*** SUCCESS ***\n\n"; for (var i:String in FormDataManager.collectedData) { resultTxt += i + " : " + FormDataManager.collectedData[i] + "\n\n"; } dataResult.text = resultTxt; } private function handlerDataCollectionFail(e : FormDataManagerEvent) : void { if(!dataResult) { att_data_result(); dataResult.x = myForm.x + myForm.width + 50; } var resultTxt : String = "### ERROR ###\n"; for (var i:String in FormDataManager.failedData) { resultTxt += i + " : " + FormDataManager.failedData[i] + "\n\n"; } dataResult.text = resultTxt; } private function att_data_result() : void { // Attach text filed to show the result of data collection. dataResult = new TextArea(); dataResult.width = 200; dataResult.height = 400; this.addChild(dataResult); } private function statesComboBox() : ComboBox { var items : XML = ; var dp : DataProvider = new DataProvider(items); var comboBox : ComboBox = new ComboBox(); comboBox.dataProvider = dp; return comboBox; } } }