'From Pharo2.0 of 7 March 2013 [Latest update: #20628] on 1 March 2015 at 2:48:18 pm'!
WebFormElement subclass: #WebMenu
	instanceVariableNames: 'multiple selected adaptorToStore sort textForEmpty'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Aida-Elements'!

!WebMenu commentStamp: '<historical>' prior: 0!
WebMenu is a dropdown menu, which can select single or multiple options. Options are read from a collection, selected options are put in selected. You can set aspect of each option if options are not plain text. 

Text to display for nil element in the collection is set with the #textForEmpty: message. This also removes unammed option from the menu.

Example:

	WebMenu aspect: #name collection: self persons selected: self selectedPersons

will show dropdown menu of all persons, shown by name. Selected person will be stored in selectedPersons collection. Above menu is  single selection, it can be multiple selection too if you do:

	aWebMenu setMultiple.!


!WebMenu methodsFor: 'model adapting' stamp: 'HilaireFernandes 3/1/2015 12:40'!
acceptInputFromValues: aCollection
	| option result values |
	self selected isNil ifTrue: [self selected: OrderedCollection new]. "we need a collection!! "
	self selected copy do: [:each | self selected remove: each].
	values := aCollection collect: [:each | AIDASite convert: each fromCodepage: #'UTF-8'].
	self selected addAll: 
		(self collection select:  [:each |
			option := self textFor: each.
			option := option trimBlanks. "it should also eliminate more than one spaces together!!"
			values contains: [:value | value match: option] ] ).
	self aspectToStore notNil ifTrue: 
		[result := self selected notEmpty ifTrue: [self selected asOrderedCollection first] ifFalse: [nil].
		self adaptorToStore value: result]! !

!WebMenu methodsFor: 'accessing' stamp: 'HilaireFernandes 3/1/2015 14:43'!
textFor: anObject 
"Give a text representation for anObject"
	^ anObject 
		ifNil: [ textForEmpty]
	 	ifNotNil: [ 
			self aspect 
				ifNil:  [ anObject ]
				ifNotNil: [anObject perform: self aspect]]! !

!WebMenu methodsFor: 'accessing' stamp: 'HilaireFernandes 3/1/2015 14:24'!
textForEmpty
	^ textForEmpty! !

!WebMenu methodsFor: 'accessing' stamp: 'HilaireFernandes 3/1/2015 14:24'!
textForEmpty: aString
	textForEmpty := aString! !

!WebMenu methodsFor: 'testing' stamp: 'HilaireFernandes 3/1/2015 14:29'!
isTextForEmpty
	^ textForEmpty isNil not! !

!WebMenu methodsFor: 'private-streaming' stamp: 'HilaireFernandes 3/1/2015 14:25'!
printOptionsOn: aStream
	| option preselected session |
	self collection isEmpty ifTrue: [^nil].
	self prepareSelected.
	preselected := self selected notNil ifTrue: [self selected asSet] ifFalse: [#()].
	self isTextForEmpty ifFalse: [aStream nextPutAll: '<option></option>']. "empty choice"
	session := self firstSessionFromStack.
	self collection do: [:each |
		option := self textFor: each. 
		option := AIDASite convertToWeb: option on: session.
		aStream nextPutAll: '<option'.
		aStream nextPutAll: ((preselected includes: each) ifTrue: [' selected>'] ifFalse: ['>']).
 		aStream nextPutAll: option; nextPutAll: '</option> ' ]! !

!WebMenu methodsFor: 'private-streaming' stamp: 'HilaireFernandes 3/1/2015 14:28'!
streamOptionsTo: aStream
	| option preselected session |
	self collection isEmpty ifTrue: [^nil].
	self prepareSelected.
	preselected := self selected notNil ifTrue: [self selected asSet] ifFalse: [#()].
	self isTextForEmpty ifFalse: [aStream nextPutAll: '<option></option>']. "empty choice"
	session := self firstSessionFromStack.
	self collection do: [:each |
		option := self textFor: each.
		option := AIDASite convertToWeb: option on: session.
		aStream nextPutAll: '<option'.
		aStream nextPutAll: ((preselected includes: each) ifTrue: [' selected>'] ifFalse: ['>']).
 		aStream nextPutAll: option; nextPutAll: '</option> ' ]! !

