From: <gem...@li...> - 2012-07-24 08:57:30
|
Revision: 870 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=870&view=rev Author: mennodekker Date: 2012-07-24 08:57:06 +0000 (Tue, 24 Jul 2012) Log Message: ----------- Optimised tableform decorator fiddling to run only when rendering the form. Introduced MUtil_Form->setLazy(boolean) to make a form lazy, otherwise it will be rendered as is since most (if not all) forms are regular forms and Lazy just adds overhead to an object that is complicated enough by itself. Modified Paths: -------------- trunk/library/classes/Gems/Form/TableForm.php trunk/library/classes/Gems/TabForm.php trunk/library/classes/MUtil/Form.php trunk/library/classes/MUtil/Html/InputRenderer.php Modified: trunk/library/classes/Gems/Form/TableForm.php =================================================================== --- trunk/library/classes/Gems/Form/TableForm.php 2012-07-23 13:33:32 UTC (rev 869) +++ trunk/library/classes/Gems/Form/TableForm.php 2012-07-24 08:57:06 UTC (rev 870) @@ -43,7 +43,8 @@ * @license New BSD License * @since Class available since version 1.4 */ -class Gems_Form_TableForm extends Gems_Form { +class Gems_Form_TableForm extends Gems_Form +{ /** * Whether or not form elements are members of an array * @var bool @@ -52,19 +53,105 @@ private $_alternate = null; - public function __construct($options = null) { - //Needed for alternating rows - $this->_alternate = new MUtil_Lazy_Alternate(array('odd','even')); - parent::__construct($options); + protected function _fixDecoratorDisplayGroup(&$element) + { + // Display group + $element->setDecorators(array('FormElements', + array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), + array(array('labelCellClose' => 'HtmlTag'), array('tag' => 'td', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'closeOnly' => true)), + 'Tooltip', + array('Description', array('tag' => 'label', 'class' => 'optional', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'escape' => false)), + array(array('labelCellOpen' => 'HtmlTag'), array('tag' => 'td', 'class' => 'label', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'openOnly' => true)), + array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $this->_alternate . ' ' . $element->getName() . ' ' . $element->getAttrib('class'))) + )); + + //Now add the right decorators to the elements + $groupElements = $element->getElements(); + foreach ($groupElements as $groupElement) { + $dec1 = $this->_getImportantDecorator($groupElement); + + $decorators = array(array('Description', array('class' => 'description')), + 'Errors', + 'Tooltip', + ); + + //If we want to see the individual fields labels, do so: + if ($element->getAttrib('showLabels') === true) { + if ($groupElement instanceof Zend_Form_Element_Checkbox) { + $decorators[] = array('Label', array('escape' => false, 'placement' => Zend_Form_Decorator_Label::APPEND)); + } else { + $decorators[] = array('Label', array('escape' => false)); + } + } + + //Apply final class and id to allow for custom styling + $decorators[] = array(array('labelCell' => 'HtmlTag'), array('tag' => 'div', 'class' => 'tab-displaygroup', 'id' => $groupElement->getName() . '_cont')); + + if (!is_null($dec1)) + array_unshift($decorators, $dec1); + $groupElement->setDecorators($decorators); + } } + protected function _fixDecoratorElement(&$element) + { + $dec1 = $this->_getImportantDecorator($element); + $decorators = array( + array('Description', array('class' => 'description')), + 'Errors', + array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), + array(array('labelCellClose' => 'HtmlTag'), array('tag' => 'td', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'closeOnly' => true)), + 'Tooltip', + array('Label', array('escape' => false)), + array(array('labelCellOpen' => 'HtmlTag'), array('tag' => 'td', 'class' => 'label', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'openOnly' => true)), + array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $this->_alternate . ' ' . $element->getName())) + ); + if (!is_null($dec1)) { + array_unshift($decorators, $dec1); + } + $element->setDecorators($decorators); + } + + protected function _fixDecoratorHiddenSubmit(&$element) + { + //No label and tooltip + $decorators = array( + 'ViewHelper', + array('Description', array('class' => 'description')), + 'Errors', + array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), + array(array('labelCellClose' => 'HtmlTag'), array('tag' => 'td', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'closeOnly' => true)), + 'Tooltip', + array(array('labelCellOpen' => 'HtmlTag'), array('tag' => 'td', 'class' => 'label', 'placement' => Zend_Form_Decorator_Abstract::PREPEND, 'openOnly' => true)), + array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $element->getName(), 'style' => 'display:none;')) + ); + $element->setDecorators($decorators); + } + + protected function _fixDecoratorHtml(&$element) + { + // Display with colspan = 2 + $decorators = array( + 'ViewHelper', + array('Description', array('class' => 'description')), + 'Errors', + 'Tooltip', + array('Label', array('escape' => false)), + array(array('labelCell' => 'HtmlTag'), array('tag' => 'td', 'class' => 'label', 'colspan' => 2)), + array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $this->_alternate . ' ' . $element->getAttrib('class') . ' ' . $element->getName())) + ); + + $element->setDecorators($decorators); + } + /** * Get a ViewHelper or ZendX decorator to add in front of the decorator chain * * @param Zend_Form_Element $element * @return null|Zend_Form_Decorator_Abstract */ - private function _getImportantDecorator($element) { + private function _getImportantDecorator($element) + { $class = get_class($element); if (strpos($class, 'JQuery')) { @@ -104,111 +191,33 @@ public function addDisplayGroup(array $elements, $name, $options = null) { //Add the group as usual, but skip decorator loading as we don't need that - parent::addDisplayGroup($elements, $name, (array) $options + array('disableLoadDefaultDecorators'=>true)); - - //Retrieve it and set decorators - $group = $this->getDisplayGroup($name); - $group->setDecorators( array('FormElements', - array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), - array(array('labelCellClose' => 'HtmlTag'), array('tag' => 'td', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'closeOnly'=>true)), - 'Tooltip', - array('Description', array('tag'=>'label', 'class'=>'optional', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'escape'=>false)), - array(array('labelCellOpen' => 'HtmlTag'), array('tag' => 'td', 'class'=>'label', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'openOnly'=>true)), - array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $group->getName(). ' ' . $group->getAttrib('class'))) - )); - - //Now add the right decorators to the elements - $groupElements = $group->getElements(); - foreach ($groupElements as $element) { - $dec1 = $this->_getImportantDecorator($element); - - $decorators = array( array('Description', array('class'=>'description')), - 'Errors', - 'Tooltip', - ); - - //If we want to see the individual fields labels, do so: - if ($group->getAttrib('showLabels')===true) { - $decorators[] = array('Label', array('escape'=>false)); - } - - //Apply final class and id to allow for custom styling - $decorators[] = array(array('labelCell' => 'HtmlTag'), array('tag' => 'div', 'class'=>'tab-displaygroup', 'id'=>$element->getName().'_cont')); - - if (!is_null($dec1)) array_unshift($decorators, $dec1); - $element->setDecorators($decorators); - if ($element instanceof Zend_Form_Element_Checkbox) { - $decorator = $element->getDecorator('Label'); - if ($decorator) { - $decorator->setOption('placement', Zend_Form_Decorator_Label::APPEND); - } - } - } - return $this; + return parent::addDisplayGroup($elements, $name, (array) $options + array('disableLoadDefaultDecorators' => true)); } /** - * Add element to stack - * - * Takes care of setting the right decorators for table display - * - * @param string|Zend_Form_Element $element - * @param string $name - * @param array|Zend_Config $options - * @throws Zend_Form_Exception on invalid element - * @return Gems_Form_TableForm + * Fix the decorators so we get the table layout we want. Normally this is called + * only once when rendering the form. */ - public function addElement($element, $name = null, $options = null) + public function fixDecorators() { - parent::addElement($element, $name, $options); + //Needed for alternating rows + $this->_alternate = new MUtil_Lazy_Alternate(array('odd', 'even')); - if (null === $name) { - $name = $element->getName(); - } else { - $element = $this->getElement($name); - } + foreach ($this as $name => $element) { + if ($element instanceof MUtil_Form_Element_Html) { + $this->_fixDecoratorHtml($element); + + } elseif ($element instanceof Zend_Form_Element_Hidden || $element instanceof Zend_Form_Element_Submit) { + $this->_fixDecoratorHiddenSubmit($element); - $dec1 = $this->_getImportantDecorator($element); + } elseif ($element instanceof Zend_Form_Element) { + $this->_fixDecoratorElement($element); + + } elseif ($element instanceof Zend_Form_DisplayGroup) { + $this->_fixDecoratorDisplayGroup($element); - if ($element instanceof MUtil_Form_Element_Html) { - //Colspan 2 - $decorators = array( - array('Description', array('class'=>'description')), - 'Errors', - 'Tooltip', - array('Label', array('escape'=>false)), - array(array('labelCell' => 'HtmlTag'), array('tag' => 'td', 'class'=>'label', 'colspan'=>2)), - array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $element->getAttrib('class') . ' ' . $element->getName())) - ); - } elseif ($element instanceof Zend_Form_Element_Hidden || - $element instanceof Zend_Form_Element_Submit) { - //No label and tooltip - $decorators = array( - array('Description', array('class'=>'description')), - 'Errors', - array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), - array(array('labelCellClose' => 'HtmlTag'), array('tag' => 'td', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'closeOnly'=>true)), - 'Tooltip', - array(array('labelCellOpen' => 'HtmlTag'), array('tag' => 'td', 'class'=>'label', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'openOnly'=>true)), - array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $element->getName())) - ); - } else { - $decorators = array( - array('Description', array('class'=>'description')), - 'Errors', - array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), - array(array('labelCellClose' => 'HtmlTag'), array('tag' => 'td', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'closeOnly'=>true)), - 'Tooltip', - array('Label', array('escape'=>false)), - array(array('labelCellOpen' => 'HtmlTag'), array('tag' => 'td', 'class'=>'label', 'placement'=> Zend_Form_Decorator_Abstract::PREPEND, 'openOnly'=>true)), - array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => $element->getName())) - ); + } } - - if (!is_null($dec1)) array_unshift($decorators, $dec1); - $element->setDecorators($decorators); - - return $this; } /** @@ -230,30 +239,26 @@ $decorators = $this->getDecorators(); if (empty($decorators)) { $this->addDecorator('AutoFocus') - ->addDecorator('FormElements') - ->addDecorator(array('table' => 'HtmlTag'), array('tag' => 'table', 'class'=>$class)) - ->addDecorator(array('tab' => 'HtmlTag'), array('tag' => 'div', 'class' => 'displayGroup')) - ->addDecorator('Form'); + ->addDecorator('FormElements') + ->addDecorator(array('table' => 'HtmlTag'), array('tag' => 'table', 'class' => $class)) + ->addDecorator(array('tab' => 'HtmlTag'), array('tag' => 'div', 'class' => 'displayGroup')) + ->addDecorator('Form'); } return $this; } - public function setView(Zend_View_Interface $view = null) + /** + * Fix the decorators the first time we try to render the form + * + * @param Zend_View_Interface $view + * @return string + */ + public function render(Zend_View_Interface $view = null) { - //If we set the view, fix the alternating rows - if ($this->_view !== $view) { - foreach($this as $name => $element) { - $decorator = $element->getDecorator('row'); - if ($decorator) { - if ($element instanceof Zend_Form_Element_Hidden) { - $decorator->setOption('style', 'display:none;'); - } else { - $decorator->setOption('class', $this->_alternate . ' ' . $decorator->getOption('class')); - } - } - } + if (!$this->_getIsRendered()) { + $this->fixDecorators(); } - return parent::setView($view); + return parent::render($view); } } \ No newline at end of file Modified: trunk/library/classes/Gems/TabForm.php =================================================================== --- trunk/library/classes/Gems/TabForm.php 2012-07-23 13:33:32 UTC (rev 869) +++ trunk/library/classes/Gems/TabForm.php 2012-07-24 08:57:06 UTC (rev 870) @@ -275,16 +275,16 @@ $this->selectTab($tab); } - $form = parent::setView($view); + parent::setView($view); - if ($view) { + if ($this->_view !== $view) { $this->activateJQuery(); if (false === $view->getPluginLoader('helper')->getPaths('Gems_JQuery_View_Helper')) { $view->addHelperPath('Gems/JQuery/View/Helper', 'Gems_JQuery_View_Helper'); } } - return $form; + return $this; } /** Modified: trunk/library/classes/MUtil/Form.php =================================================================== --- trunk/library/classes/MUtil/Form.php 2012-07-23 13:33:32 UTC (rev 869) +++ trunk/library/classes/MUtil/Form.php 2012-07-24 08:57:06 UTC (rev 870) @@ -56,6 +56,8 @@ protected $_no_dojo = true; protected $_no_jquery = true; + protected $_Lazy = false; + /** * Constructor * @@ -200,6 +202,16 @@ } /** + * Return true when the form is lazy + * + * @return boolean + */ + public function isLazy() + { + return $this->_Lazy; + } + + /** * Validate the form * * As it is better for translation utilities to set the labels etc. translated, @@ -361,6 +373,16 @@ } /** + * Is the form Lazy or can it be rendered normally? + * + * @param boolean $lazy + */ + public function setLazy($lazy = false) + { + $this->_Lazy = (bool) $lazy; + } + + /** * Set view object * * @param Zend_View_Interface $view Modified: trunk/library/classes/MUtil/Html/InputRenderer.php =================================================================== --- trunk/library/classes/MUtil/Html/InputRenderer.php 2012-07-23 13:33:32 UTC (rev 869) +++ trunk/library/classes/MUtil/Html/InputRenderer.php 2012-07-24 08:57:06 UTC (rev 870) @@ -357,8 +357,12 @@ public static function renderForm(Zend_View_Abstract $view, Zend_Form $form) { - return self::renderUntil($view, $form, - array('Zend_Form_Decorator_Form', 'Zend_Dojo_Form_Decorator_DijitForm')); + if ($form instanceof MUtil_Form && $form->isLazy()) { + return self::renderUntil($view, $form, + array('Zend_Form_Decorator_Form', 'Zend_Dojo_Form_Decorator_DijitForm')); + } else { + return self::renderComplete($view, $form); + } } public static function renderOnly(Zend_View_Abstract $view, $element, array $decorators) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |