Last active
December 17, 2015 20:28
-
-
Save TrentBrown/5667504 to your computer and use it in GitHub Desktop.
ComplexQuestionMediator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.filethis.main.view | |
{ | |
import com.filethis.main.view.components.ComplexQuestionView; | |
import com.filethis.model.vo.SourceConnectionVO; | |
import com.filethis.model.vo.QuestionVO; | |
import com.adobe.serialization.json.JSONDecoder; | |
import com.filethis.utils.StringUtil; | |
import com.filethis.view.components.TextInputFileThis; | |
import flash.utils.ByteArray; | |
import flash.utils.getDefinitionByName; | |
import mx.controls.Spacer; | |
import mx.utils.Base64Decoder; | |
import net.expantra.smartypants.SmartyPants; | |
import org.puremvc.as3.multicore.interfaces.IMediator; | |
import org.puremvc.as3.multicore.patterns.mediator.Mediator; | |
import spark.components.Image; | |
import spark.components.Label; | |
import spark.components.RadioButton; | |
import spark.components.RadioButtonGroup; | |
import spark.components.TextInput; | |
public class ComplexQuestionMediator extends Mediator implements IMediator, QuestionInterface | |
{ | |
public static const ID:String = 'ComplexQuestionMediator'; | |
protected var connection:SourceConnectionVO = null; | |
protected var question:QuestionVO = null; | |
protected var questionSpec:Object = null; | |
protected var firstTextInput:TextInput = null; | |
public function ComplexQuestionMediator | |
( | |
inConnection:SourceConnectionVO, | |
inQuestion:QuestionVO, | |
view:ComplexQuestionView, | |
id:String = ID | |
) | |
{ | |
super(id, view); | |
connection = inConnection; | |
question = inQuestion; | |
decodeQuestionData(); | |
SmartyPants.injectInto(this); | |
generateGui(); | |
} | |
public function get id() : String { return getMediatorName(); } | |
public function validateControls() : Boolean | |
{ | |
return true; | |
} | |
public function setInitialFocus() : void | |
{ | |
if (firstTextInput) | |
firstTextInput.setFocus(); | |
} | |
public function get answer() : String | |
{ | |
var answer:String = '['; | |
var isFirstPart:Boolean = true; | |
for each (var questionPartSpec:Object in questionSpec.questions) | |
{ | |
if (isFirstPart) | |
isFirstPart = false; | |
else | |
answer += ', '; | |
var key:String = questionPartSpec.key; | |
var value:String = ""; | |
switch (questionPartSpec.type) | |
{ | |
case 'choice': | |
value = questionPartSpec.choiceGroup.selectedValue.value; | |
break; | |
case 'text': | |
case 'password': | |
value = questionPartSpec.textInput.text; | |
break; | |
case 'number': | |
value = questionPartSpec.numberInput.text; | |
break; | |
} | |
key = StringUtil.escapeDoubleQuotes(key); | |
value = StringUtil.escapeDoubleQuotes(value); | |
answer += '{"key": "' + key + '", "value": "' + value + '"}'; | |
} | |
answer += ']'; | |
return answer; | |
} | |
protected function generateGui() : void | |
{ | |
if (questionSpec.title) | |
{ | |
addLabel(questionSpec.title); | |
addSpacer(20); | |
} | |
if (questionSpec.header) | |
{ | |
addLabel(questionSpec.header); | |
addSpacer(20); | |
} | |
if (questionSpec.questions) | |
{ | |
var isFirstQuestion:Boolean = true; | |
for each (var questionPartSpec:Object in questionSpec.questions) | |
{ | |
if (isFirstQuestion) | |
isFirstQuestion = false; | |
else | |
addSpacer(20); | |
switch (questionPartSpec.type) | |
{ | |
case 'choice': | |
if (!questionPartSpec.choiceGroup) | |
questionPartSpec.choiceGroup = new RadioButtonGroup; | |
for each (var choiceSpec:Object in questionPartSpec.choices) | |
{ | |
addChoice(choiceSpec.label, choiceSpec, questionPartSpec.choiceGroup); | |
addSpacer(7); | |
} | |
break; | |
case 'text': | |
case 'password': // We get this type even for "Mother's maiden name", so we don't treat it specially. | |
addLabel(questionPartSpec.label); | |
addSpacer(7); | |
questionPartSpec.textInput = addTextInput(); | |
break; | |
case 'number': | |
addLabel(questionPartSpec.label); | |
addSpacer(7); | |
questionPartSpec.numberInput = addTextInput(); // TODO: Make a special number field | |
break; | |
case 'image/png': | |
addLabel(questionPartSpec.label); | |
addSpacer(7); | |
addImage(questionPartSpec.image); | |
addSpacer(7); | |
questionPartSpec.textInput = addTextInput(); | |
break; | |
} | |
} | |
} | |
} | |
protected function decodeQuestionData() : void | |
{ | |
IF::debugging | |
{ | |
var decoder:JSONDecoder = new JSONDecoder(question.data); | |
} | |
IF::release | |
{ | |
var decoder:JSONDecoder = new JSONDecoder(question.data, false /*strict*/); | |
} | |
questionSpec = decoder.getValue(); | |
} | |
protected function addSpacer(height:Number) : Spacer | |
{ | |
var spacer:Spacer = new Spacer; | |
spacer.height = height; | |
view.addElement(spacer); | |
return spacer; | |
} | |
protected function addChoice(text:String, value:Object, group:RadioButtonGroup) : RadioButton | |
{ | |
var choice:RadioButton = new RadioButton; | |
choice.group = group; | |
choice.label = text; | |
choice.value = value; | |
choice.setStyle('color', 0x666666); | |
choice.setStyle('fontSize', 14); | |
view.addElement(choice); | |
return choice; | |
} | |
protected function addLabel(text:String) : Label | |
{ | |
var label:Label = new Label; | |
label.text = text; | |
label.percentWidth = 100; | |
label.setStyle('color', 0x666666); | |
label.setStyle('fontSize', 16); | |
view.addElement(label); | |
return label; | |
} | |
protected function addTextInput() : TextInput | |
{ | |
var textInput:TextInputFileThis = new TextInputFileThis; | |
textInput.percentWidth = 100; | |
textInput.setStyle('fontSize', 16); | |
view.addElement(textInput); | |
if (!firstTextInput) | |
firstTextInput = textInput; | |
return textInput; | |
} | |
protected function addImage(encodedData:String) : Image | |
{ | |
var decoder:Base64Decoder = new Base64Decoder; | |
decoder.decode(encodedData); | |
var data:ByteArray = decoder.toByteArray() | |
var image:Image = new Image; | |
image.source = data; | |
view.addElement(image); | |
return image; | |
} | |
protected function get view() : ComplexQuestionView | |
{ | |
return viewComponent as ComplexQuestionView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment