-------------------------------------------------------------------------------- -- Title: LWComponent.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 LUObject = require( "LUObject" ) local LUBundle = require( "LUBundle" ) local LULog = require( "LULog" ) local LUMap = require( "LUMap" ) local LWTemplate = require( "LWTemplate" ) -- define the class local super = LUObject local self = super() -- class variable(s) local _templates = LUMap() -- factory method, this is where all the methods are implemented as closures function self:init( aContext, aParent, someBindings ) super.init( self ) self._context = aContext self._parent = aParent self._bindings = someBindings return self end -- method to access this component bindings function self:bindings() if self._bindings == nil then self._bindings = LUMap() end return self._bindings end -- method to access this component context function self:context() return self._context end -- method to access this component location function self:location() return self._location end -- method to set this component location function self:setLocation( aValue ) self._location = aValue return self end -- method to access this component page function self:page() local aComponent = self local aParent = aComponent:parent() while aParent ~= nil do aComponent = aParent aParent = aComponent:parent() end return aComponent end -- method to access this component parent function self:parent() return self._parent end -- method to access this component template function self:template() if self._template == nil then self._template = self:templateWithComponent( self ) end return self._template end -- method for string representation function self:toString() return self:template():toString() end -- method to find a component class for a given object function self:componentClassWithObject( anObject, aContext, aSuffix ) if anObject ~= nil then local aName = anObject:className() local aSuffix = aSuffix or "Page" local aBundle = LUBundle:bundleWithName( aName .. aSuffix ) if aBundle ~= nil then return aBundle:entity() end end return nil end -- method to find a component for a given object function self:componentWithObject( anObject, aContext, aSuffix ) if anObject ~= nil then local aClass = self:componentClassWithObject( anObject, aContext, aSuffix ) if aClass ~= nil then local someBindings = LUMap() someBindings:put( "value", anObject ) local aComponent = aClass( aContext, nil, someBindings ) return aComponent end end return nil end -- method to access the component templates function self:templates() return _templates end -- method to find a template for a given component function self:templateWithComponent( aComponent ) if aComponent ~= nil then local aName = aComponent:className() local someTemplates = self:templates() local aTemplate = someTemplates:get( aName ) if aTemplate == nil then local aBundle = LUBundle:bundleWithName( aName ) LULog:debug( aName, aBundle ) if aBundle ~= nil then local aFile = aBundle:fileWithName( aName, "txt" ) aTemplate = LWTemplate( aFile:content() ) someTemplates:put( aName, aTemplate ) else LULog:warning( ( "couldn't find bundle named '%s'" ):format( aName ) ) return nil end end return aTemplate:copy() end return nil end return self