SCSeasideForm


SeasideTesting-Core

Comment:



Hierarchy:

ProtoObject
Object
SCXMLElementWrapper
SCSeasideForm

Summary:

instance variables:

buttons inputs radioGroups

methods:

instance class
buttons initialize inputs posting private-inputs no messages

Detail:

instance variables:

buttons
inferredType:
UndefinedObject
inputs
inferredType:
UndefinedObject
radioGroups
InitialValue:
a Dictionary()
inferredType:
Dictionary

instance methods:

buttons
buttonWithValue: aString
 
	^ self buttons
		detect: [:each | each value = aString] 
		ifNone: []
buttons

	^buttons

initialize
componentForXMLElement: element
 
	| type |
	element name = #input
		ifTrue: [type _ (element
						attributeAt: 'type'
						ifAbsent: ['']) asLowercase.
			type = 'text' | (type = 'password')
				ifTrue: [^ SCTextHtmlInput forElement: element ofForm: self].
			type = 'checkbox'
				ifTrue: [^ SCCheckboxHtmlInput forElement: element ofForm: self].
			type = 'hidden'
				ifTrue: [^ SCHiddenHtmlInput forElement: element ofForm: self].
			type = 'submit'
				ifTrue: [^ SCSubmitButtonHtmlInput forElement: element ofForm: self].
			type = 'radio'
				ifTrue: [^ self radioComponentForElement: element].
			type = 'reset'
				ifTrue: [^nil].
			self error: 'unknown input type'].
	element name = #select
		ifTrue: [^ SCSelectHtmlInput forElement: element ofForm: self].
	element name = #textarea
		ifTrue: [^ SCTextAreaHtmlInput forElement: element ofForm: self].
	^ nil
initializeFromXMLElement: anXMLElement
 
	| possibleInputs |
	super initializeFromXMLElement: anXMLElement.
	possibleInputs _ OrderedCollection new.
	self allElements
		do: [:element | (self componentForXMLElement: element)
				ifNotNilDo: [:v | possibleInputs add: v]].
	buttons _ possibleInputs
				select: [:each | each isButton].
	inputs _ possibleInputs reject: [:each | each isButton]
radioComponentForElement: anXMLElement
 
	| group button |
	group _ self
				getOrCreateRadioGroup: (anXMLElement attributeAt: 'name').
	button _ SCRadioButtonHtmlInput forGroup: group form: self fromXMLElement: anXMLElement.
	group addButton: button.
	^ button

inputs
activateCheckboxWithId: stringId
 
	self checkboxWithId: stringId value: true
activateRadioButtonWithId: stringId
 
	(self radioButtonWithId: stringId) makeSelected
checkboxWithId: stringId

	| input |
	input _ self
				inputWithId: stringId
				ifAbsent: [self error: 'No such input component.'].
	input isCheckbox
		ifFalse: [self error: 'Not a checkbox component.'].
	^input
checkboxWithId: stringId value: aBoolean
 
	(self checkboxWithId: stringId) value: aBoolean
deactivateCheckboxWithId: stringId
 
	self checkboxWithId: stringId value: false
inputs

	^inputs
radioButtonWithId: stringId
 
	| input |
	input _ self
				inputWithId: stringId
				ifAbsent: [self error: 'No such input component.'].
	input isRadioButton
		ifFalse: [self error: 'Not a radio button component.'].
	^input
radioGroups

	^radioGroups ifNil: [radioGroups _ Dictionary new]
selectListWithId: aStringId

	| elem |
	elem _ self
				inputWithId: aStringId
				ifAbsent: [self error: 'No such input field.'].
	elem isSelectInput
		ifFalse: [self error: 'Not a select'].
	^ elem
selectListWithId: aStringId selectOptionWithText: text
 
	(self selectListWithId: aStringId) selectOptionWithText: text ifNone: [self error: 'No such option.']
textInputWithId: stringId

	| input |
	input _ self
				inputWithId: stringId
				ifAbsent: [self error: 'No such input component.'].
	input isTextInput
		ifFalse: [self error: 'Not a text component.'].
	^input
textInputWithId: stringId value: stringValue
 
	(self textInputWithId: stringId) value: stringValue

posting
actionUrl

	^ xmlElement attributeAt: 'action'
httpDataWithButton: button
 
	| resultStream |
	resultStream := WriteStream on: String new.
	((button addToRequestStream: resultStream)
			and: [inputs notEmpty])
		ifTrue: [resultStream nextPut: $&].
	inputs allButLast
		do: [:each | (each addToRequestStream: resultStream)
				ifTrue: [resultStream nextPut: $&]].
	inputs last addToRequestStream: resultStream.
	^ resultStream contents
writeHttpDataFor: key value: value on: stream
 
	| name |
	name _ key.
	value
		ifNil: [^ stream nextPutAll: name].
	value isString
		ifTrue: [stream nextPutAll: name;
				 nextPut: $=;
				 nextPutAll: value]
		ifFalse: [value
				do: [:each | self
						writeHttpDataFor: key
						value: each
						on: stream]
				separatedBy: [stream nextPut: $&]]

private-inputs
getOrCreateRadioGroup: groupName

	^self radioGroups at: groupName ifAbsentPut: [SCRadioGroup named: groupName].
inputWithId: anId ifAbsent: aBlock

	^inputs detect: [:each | each id = anId] ifNone: aBlock
inputWithName: aName ifAbsent: aBlock
 
	^ inputs
		detect: [:each | each name = aName]
		ifNone: aBlock
optionValuesByTextForSelect: aSelectElement
 
	| optionElements result |
	optionElements := aSelectElement elements
				select: [:each | each name = 'option'].
	result := Dictionary new.
	optionElements
		do: [:each | result at: (each contents first string) put: (each attributeAt: 'value') ].
	^result

class methods:

^top


- made by Dandelion -