cvs: pear /HTML_QuickForm_advmultiselect advmultiselect.php

From: Date: Mon, 21 Nov 2005 20:54:29 +0000
Subject: cvs: pear /HTML_QuickForm_advmultiselect advmultiselect.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-36433@lists.php.net to get a copy of this message
farell Mon Nov 21 15:54:29 2005 EDT Modified files: /pear/HTML_QuickForm_advmultiselect advmultiselect.php Log: implement new feature for release 1.1.0 extended buttons: select all, select none, toggle selection

http://cvs.php.net/diff.php/pear/HTML_QuickForm_advmultiselect/advmultiselect.php?r1=1.7&r2=1.8&ty=u Index: pear/HTML_QuickForm_advmultiselect/advmultiselect.php diff -u pear/HTML_QuickForm_advmultiselect/advmultiselect.php:1.7 pear/HTML_QuickForm_advmultiselect/advmultiselect.php:1.8 --- pear/HTML_QuickForm_advmultiselect/advmultiselect.php:1.7 Sat Aug 27 09:17:13 2005 +++ pear/HTML_QuickForm_advmultiselect/advmultiselect.php Mon Nov 21 15:54:29 2005 @@ -19,7 +19,7 @@ * @author Laurent Laville <pear@laurent-laville.org> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 -* @version CVS: $Id: advmultiselect.php,v 1.7 2005/08/27 13:17:13 farell Exp $ +* @version CVS: $Id: advmultiselect.php,v 1.8 2005/11/21 20:54:29 farell Exp $ * @link http://pear.php.net/package/HTML_QuickForm_advmultiselect */ @@ -123,6 +123,33 @@ var $_removeButtonAttributes; /** + * Associative array of the select all button attributes + * + * @var array + * @access private + * @since 1.1.0 + */ + var $_allButtonAttributes; + + /** + * Associative array of the select none button attributes + * + * @var array + * @access private + * @since 1.1.0 + */ + var $_noneButtonAttributes; + + /** + * Associative array of the toggle selection button attributes + * + * @var array + * @access private + * @since 1.1.0 + */ + var $_toggleButtonAttributes; + + /** * Associative array of the move up button attributes * * @var array @@ -271,6 +298,12 @@ $this->setButtonAttributes('add'); // set default remove button attributes $this->setButtonAttributes('remove'); + // set default selectall button attributes + $this->setButtonAttributes('all'); + // set default selectnone button attributes + $this->setButtonAttributes('none'); + // set default toggle selection button attributes + $this->setButtonAttributes('toggle'); // set default move up button attributes $this->setButtonAttributes('moveup'); // set default move up button attributes @@ -354,6 +387,42 @@ ); } break; + case 'all': + if (is_null($attributes)) { + $this->_allButtonAttributes = array('name' => 'all', + 'value' => ' Select All ', + 'type' => 'button' + ); + } else { + $this->_updateAttrArray($this->_allButtonAttributes, + $this->_parseAttributes($attributes) + ); + } + break; + case 'none': + if (is_null($attributes)) { + $this->_noneButtonAttributes = array('name' => 'none', + 'value' => ' Select None ', + 'type' => 'button' + ); + } else { + $this->_updateAttrArray($this->_noneButtonAttributes, + $this->_parseAttributes($attributes) + ); + } + break; + case 'toggle': + if (is_null($attributes)) { + $this->_toggleButtonAttributes = array('name' => 'toggle', + 'value' => ' Toggle Selection ', + 'type' => 'button' + ); + } else { + $this->_updateAttrArray($this->_toggleButtonAttributes, + $this->_parseAttributes($attributes) + ); + } + break; case 'moveup': if (is_null($attributes)) { $this->_upButtonAttributes = array('name' => 'up', @@ -504,6 +573,7 @@ . '<label' . $this->_getAttrString($labelAttributes) .'>' . '<input type="checkbox"' + . ' id="'.$this->getName().'"' . ' name="'.$selectName.'"' . $checked . $this->_getAttrString($option['attr']) @@ -516,6 +586,25 @@ $strHtmlUnselected = ''; $strHtmlAdd = ''; $strHtmlRemove = ''; + + // build the select all button with all its attributes + $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('". $this->getName() ."', 1);"); + $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes); + $attrStrAll = $this->_getAttrString($this->_allButtonAttributes); + $strHtmlAll = "<input$attrStrAll />". PHP_EOL; + + // build the select none button with all its attributes + $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('". $this->getName() ."', 0);"); + $this->_noneButtonAttributes = array_merge($this->_noneButtonAttributes, $attributes); + $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes); + $strHtmlNone = "<input$attrStrNone />". PHP_EOL; + + // build the toggle selection button with all its attributes + $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('". $this->getName() ."', 2);"); + $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes); + $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes); + $strHtmlToggle = "<input$attrStrToggle />". PHP_EOL; + $strHtmlMoveUp = ''; $strHtmlMoveDown = ''; @@ -618,6 +707,20 @@ $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes); $strHtmlAdd = "<input$attrStrAdd />". PHP_EOL; + // build the select all button with all its attributes + $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'all'); return false;"); + $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes); + $attrStrAll = $this->_getAttrString($this->_allButtonAttributes); + $strHtmlAll = "<input$attrStrAll />". PHP_EOL; + + // build the select none button with all its attributes + $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'none'); return false;"); + $this->_noneButtonAttributes = array_merge($this->_noneButtonAttributes, $attributes); + $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes); + $strHtmlNone = "<input$attrStrNone />". PHP_EOL; + + $strHtmlToggle = ''; + // build the move up button with all its attributes $attributes = array('onclick' => "{$this->_jsPrefix}moveUp(this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "']); return false;"); $this->_upButtonAttributes = array_merge($this->_upButtonAttributes, $attributes); @@ -657,12 +760,14 @@ '{stylesheet}', '{javascript}', '{class}', '{unselected}', '{selected}', '{add}', '{remove}', + '{all}', '{none}', '{toggle}', '{moveup}', '{movedown}' ); $htmlElements = array( $this->getElementCss(false), $this->getElementJs(false), $this->_tableAttributes, $strHtmlUnselected, $strHtmlSelected . $strHtmlHidden, $strHtmlAdd, $strHtmlRemove, + $strHtmlAll, $strHtmlNone, $strHtmlToggle, $strHtmlMoveUp, $strHtmlMoveDown ); @@ -689,32 +794,64 @@ // We only want to include the javascript code once per form define('HTML_QUICKFORM_ADVMULTISELECT_'.$jsfuncName.'_EXISTS', true); - $js .= " + // placeholder {unselected} existence determines if we will render + if (strpos($this->_elementTemplate, '{unselected}') === false) { + // ... a single multi-select with checkboxes + $js .= " +/* begin javascript for HTML_QuickForm_advmultiselect */ +function {$jsfuncName}(checkWhat, checkMode) { + if (checkMode !== 0 && checkMode !== 1 && checkMode !== 2) { + return; + } + + // Find all the checkboxes... + var inputs = document.getElementsByTagName('input'); + + // Loop through all form elements (input tags) + for(index = 0; index < inputs.length; index++) { + // ...if it's the type of checkbox we're looking for, change its checked status + if (inputs[index].id == checkWhat && inputs[index].type == 'checkbox') { + if (checkMode == 2) { + if (inputs[index].checked == 0) { + inputs[index].checked = 1; + } else if (inputs[index].checked == 1) { + inputs[index].checked = 0; + } + } else { + inputs[index].checked = checkMode; + } + } + } +} +/* end javascript for HTML_QuickForm_advmultiselect */ +"; + } else { + // ... or a dual multi-select + $js .= " /* begin javascript for HTML_QuickForm_advmultiselect */ function {$jsfuncName}(selectLeft, selectRight, selectHidden, action) { - if (action == 'add') { + if (action == 'add' || action == 'all') { menuFrom = selectLeft; menuTo = selectRight; - } - else { + } else { menuFrom = selectRight; menuTo = selectLeft; } // Don't do anything if nothing selected. Otherwise we throw javascript errors. - if (menuFrom.selectedIndex == -1) { + if (menuFrom.selectedIndex == -1 && (action == 'add' || action == 'remove')) { return; } // Add items to the 'TO' list. for (i=0; i < menuFrom.length; i++) { - if (menuFrom.options[i].selected == true ) { + if (action == 'all' || action == 'none' || menuFrom.options[i].selected == true ) { menuTo.options[menuTo.length]= new Option(menuFrom.options[i].text, menuFrom.options[i].value); } } // Remove items from the 'FROM' list. for (i=(menuFrom.length - 1); i>=0; i--){ - if (menuFrom.options[i].selected == true ) { + if (action == 'all' || action == 'none' || menuFrom.options[i].selected == true) { menuFrom.options[i] = null; } } @@ -811,6 +948,7 @@ /* end javascript for HTML_QuickForm_advmultiselect */ "; + } if ($raw !== true) { $js = '<script type="text/javascript">' . PHP_EOL . '//<![CDATA[' . $js . '//]]>' . PHP_EOL
« previous php.pear.cvs (#36433) next »