-------------------------------------------------------------------------------- -- Title: LWFormInputSelect.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LWFormInput = require( "LWFormInput" ) local LWString = require( "LWString" ) local LWStyle = require( "LWStyle" ) local LUList = require( "LUList" ) local LUObject = require( "LUObject" ) local math = require( "math" ) -- define the class local super = LWFormInput local self = super() local function NormalizeValue( aValue ) if aValue ~= nil then aValue = tonumber( aValue ) if aValue ~= nil then aValue = math.floor( math.abs( aValue ) ) if aValue == 0 then aValue = nil end end end return aValue end function self:handleComponentKeyWithValue( aComponent, aKey, aValue ) local aDefaultKey = aKey:gsub( "%.%_$", "" ) local aMethod = self:componentMethodWithKey( aComponent, aDefaultKey ) if aMethod ~= nil then local someParameters = aComponent:context():request():parameters() if aKey == aDefaultKey and someParameters:hasKey( aDefaultKey ) == true or aKey ~= aDefaultKey and someParameters:hasKey( aDefaultKey ) == false then if aKey ~= aDefaultKey then aValue = nil end if LUObject:isObject( aValue ) == true and aValue:isKindOf( LUList ) == true then local aList = LUList() for anIndex, aValue in aValue:iterator() do aList:add( NormalizeValue( aValue ) ) end aValue = aList:sort() else aValue = LUList():add( NormalizeValue( aValue ) ) end aComponent:invoke( aMethod, aValue or LUList() ) end end end function self:prefix() return "select." end function self:type() return "select" end function self:hasMultiple() local aSelection = self:selection() if LUObject:isObject( aSelection ) == true and aSelection:isKindOf( LUList ) == true then return true end return false end function self:selection() return self:bindings():get( "selection" ) end function self:isSelected( aValue ) local aSelection = self:selection() if LUObject:isObject( aSelection ) == true and aSelection:isKindOf( LUList ) == true then return aSelection:hasValue( aValue ) end return aValue == aSelection end function self:size() local aValue = self:bindings():get( "size" ) if aValue == nil then if self:hasMultiple() == true then aValue = 5 end end if aValue == nil then aValue = 1 end return aValue end function self:values() return self:bindings():get( "values" ) end -- method for string representation function self:toString() local aBuffer = LUList() local aStyle = LWStyle:styleWithComponent( self ) local someValues = self:values() aBuffer:add( "" ) aBuffer:add( self:hidden() ) return aBuffer:join( "" ) end return self