-------------------------------------------------------------------------------- -- Title: LWFormInput.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 LWComponent = require( "LWComponent" ) local LUClass = require( "LUClass" ) local LULog = require( "LULog" ) local LUMap = require( "LUMap" ) local LUString = require( "LUString" ) -- define the class local super = LWComponent local self = super() -------------------------------------------------------------------------------- -- Input methods -------------------------------------------------------------------------------- function self:defaultName() return self:name() .. "._" end function self:name() return self:prefix() .. ( self:bindings():get( "name" ) or self:type() ) end function self:prefix() return "input" end function self:type() return "input" end function self:hidden() return ( "" ):format( self:defaultName() ) end function self:isDisabled() local aValue = self:bindings():get( "isDisabled" ) if aValue == nil then aValue = false end return aValue end function self:isReadOnly() local aValue = self:bindings():get( "isReadOnly" ) if aValue == nil then aValue = false end return aValue end -------------------------------------------------------------------------------- -- Handler methods -------------------------------------------------------------------------------- function self:handlers() local anHandlerClass = self:class() local someHandlers = LUMap() local someClassNames = LUClass:classNames() for anIndex, aClassName in someClassNames:iterator() do local aClass = LUClass:classWithName( aClassName ) local anEntity = aClass:entity() if anEntity ~= anHandlerClass and anEntity:isKindOf( anHandlerClass ) == true and anEntity:respondsTo( "handleComponentKeyWithValue" ) == true then local aPrefix = anEntity:prefix() if someHandlers:hasKey( aPrefix ) == false then someHandlers:put( aPrefix, anEntity ) end end end return someHandlers end function self:componentMethodWithKey( aComponent, aKey ) local aMethod = aKey:sub( self:prefix():len() + 1 ) aMethod = "set" .. LUString:capitalize( aMethod, false ) if aComponent:respondsTo( aMethod ) == true then return aMethod end LULog:debug( "'" .. aComponent:className() .. "' does not respond to '" .. aMethod .. "'" ) return nil end function self:handleComponent( aComponent ) local someHandlers = self:handlers() local someParameters = aComponent:context():request():parameters() for aKey, aValue in someParameters:iterator() do local aPrefix = aKey:sub( 1, aKey:find( ".", 1, true ) ) local anHandler = someHandlers:get( aPrefix ) if anHandler ~= nil then anHandler:handleComponentKeyWithValue( aComponent, aKey, aValue ) end end end return self