#!/usr/bin/env python # coding: utf8 from gluon.html import * from gluon.http import * from gluon.validators import * from gluon.sqlhtml import * from gluon.utils import * # request, response, session, cache, T, db(s) # must be passed and cannot be imported! class Layout(DIV): def __init__(self, **attributes ): DIV.__init__(self, **attributes) self.components = [ ] self.cols = 0 def _postprocessing(self): if '_id' not in self.attributes: self.attributes['_id'] = 'dynamic_layout_' + web2py_uuid() if '_class' in self.attributes: self.attributes['_class'] += ' dynamic-layout' else: self.attributes['_class'] = 'dynamic-layout' def addField(self, label, component): labelId = self.attributes['_id'] + '_row_' + str( len( self )) + '_label' if '_id' in component.attributes: labelId = component.attributes['_id'] + '_label' self.addRow( LABEL(label, _id =labelId ), component ) def addRow(self, *components): self.cols = max( self.cols, len(components)) componentsList = [] if len(components) == 1 and isinstance(components[0], (list, tuple)): componentsList = list(components[0]) else: componentsList = list(components) childId = self.attributes['_id'] + '_row_' + str( len( self )) row = DIV( _id = childId, _class = 'dynamic-layout-row' ) for index, component in enumerate(componentsList): if '_class' in component.attributes: component['_class'] += ' dynamic-layout-col-' + str( index ) else: component['_class'] = 'dynamic-layout-col-' + str( index ) if len( componentsList ) == 1: component['_class'] += ' dynamic-layout-span' else: component['_class'] += ' dynamic-layout-fixed' row.append( component ) self.append( row ) def hookScript(self): script = 'YAHOO.util.Event.onContentReady( "%s", function() {\n var mylayout = new YAHOO.Ext.LayoutBase( this, {cols : %d} );\n mylayout.layout();\n});\n' % ( self.attributes['_id'] , self.cols) return SCRIPT(script, _language='javascript') def xml(self): (fa, co) = self._xml() return self.hookScript().xml() + '<%s%s>%s' % (self.tag, fa, co, self.tag) def elements(self, *args, **kargs): check = True if args and 'script' in args: check = False if kargs.has_key("first_only") and check: return self.hookScript() elif check: return self.hookScript() + DIV.elements(self, *args, **kargs) else: return DIV.elements(self, *args, **kargs) class AjaxForm(object): def __init__(self, translator = None, **attributes): layoutId = attributes.get('_id', 'empty') + '_layout' self.form = FORM( **attributes ) self.layout = Layout( _id = layoutId ) self.T = translator self.VALID = 0 self.ERRORS = 1 self.DEFAULT = 3 self.form.append(self.layout) def content(self): scripts = '' for script in self.form.elements('script'): scripts += '\n'.join( script.components ) return { 'script' : scripts, 'form' : self.form } def execute(self, vars, session = None, formname = 'default', keepvalues = False, onvalidation = None): if self.form.accepts(vars, session, formname, keepvalues, onvalidation): return self.VALID elif self.form.errors: return self.ERRORS else: return self.DEFAULT