HTML_QuickForm
[ class tree: HTML_QuickForm ] [ index: HTML_QuickForm ] [ all elements ]

Source for file autocomplete.php

Documentation is available at autocomplete.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author:  Matteo Di Giovinazzo <matteodg@infinito.it>                 |
  17. // |                                                                      |
  18. // | For the JavaScript code thanks to Martin Honnen and                  |
  19. // | Nicholas C. Zakas                                                    |
  20. // | See:                                                                 |
  21. // |      http://www.faqts.com/knowledge_base/view.phtml/aid/13562        |
  22. // | and                                                                  |
  23. // |      http://www.sitepoint.com/article/1220                           |
  24. // +----------------------------------------------------------------------+
  25. //
  26. // $Id: autocomplete.php,v 1.4 2004/06/15 10:51:42 mansion Exp $
  27.  
  28.  
  29. require_once("HTML/QuickForm/text.php");
  30.  
  31.  
  32. /**
  33.  * Class to dynamically create an HTML input text element that
  34.  * at every keypressed javascript event, check in an array of options
  35.  * if there's a match and autocomplete the text in case of match.
  36.  *
  37.  * Ex:
  38.  * $autocomplete =& $form->addElement('autocomplete', 'fruit', 'Favourite fruit:');
  39.  * $options = array("Apple", "Orange", "Pear", "Strawberry");
  40.  * $autocomplete->setOptions($options);
  41.  *
  42.  * @author       Matteo Di Giovinazzo <matteodg@infinito.it>
  43.  */
  44. {
  45.     // {{{ properties
  46.  
  47.     /**
  48.      * Options for the autocomplete input text element
  49.      *
  50.      * @var       array 
  51.      * @access    private
  52.      */
  53.     var $_options = array();
  54.  
  55.     // }}}
  56.     // {{{ constructor
  57.  
  58.     /**
  59.      * Class constructor
  60.      *
  61.      * @param     string    $elementName    (optional)Input field name attribute
  62.      * @param     string    $elementLabel   (optional)Input field label in form
  63.      * @param     array     $options        (optional)Autocomplete options
  64.      * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  65.      *                                       or an associative array. Date format is passed along the attributes.
  66.      * @access    public
  67.      * @return    void 
  68.      */
  69.     function HTML_QuickForm_autocomplete($elementName = null$elementLabel = null$options = null$attributes = null)
  70.     {
  71.         $this->HTML_QuickForm_text($elementName$elementLabel$attributes);
  72.         $this->_persistantFreeze = true;
  73.         $this->_type 'autocomplete';
  74.         if (isset($options)) {
  75.             $this->setOptions($options);
  76.         }
  77.     //end constructor
  78.  
  79.     // }}}
  80.     // {{{ setOptions()
  81.  
  82.     /**
  83.      * Sets the options for the autocomplete input text element
  84.      *
  85.      * @param     array    $options    Array of options for the autocomplete input text element
  86.      * @access    public
  87.      * @return    void 
  88.      */
  89.     function setOptions($options)
  90.     {
  91.         $this->_options array_values($options);
  92.     // end func setOptions
  93.  
  94.     // }}}
  95.     // {{{ toHtml()
  96.  
  97.     /**
  98.      * Returns Html for the autocomplete input text element
  99.      *
  100.      * @access      public
  101.      * @return      string 
  102.      */
  103.     function toHtml()
  104.     {
  105.         // prevent problems with grouped elements
  106.         $arrayName str_replace(array('['']')array('__''')$this->getName()) '_values';
  107.  
  108.         $this->updateAttributes(array(
  109.             'onkeypress' => 'return autocomplete(this, event, ' $arrayName ');'
  110.         ));
  111.         if ($this->_flagFrozen{
  112.             $js '';
  113.         else {
  114.             $js "<script type=\"text/javascript\">\n//<![CDATA[\n";
  115.             if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
  116.                 $js .= <<<EOS
  117.  
  118. /* begin javascript for autocomplete */
  119. function setSelectionRange(input, selectionStart, selectionEnd) {
  120.     if (input.setSelectionRange) {
  121.         input.setSelectionRange(selectionStart, selectionEnd);
  122.     }
  123.     else if (input.createTextRange) {
  124.         var range = input.createTextRange();
  125.         range.collapse(true);
  126.         range.moveEnd("character", selectionEnd);
  127.         range.moveStart("character", selectionStart);
  128.         range.select();
  129.     }
  130.     input.focus();
  131. }
  132.  
  133. function setCaretToPosition(input, position) {
  134.     setSelectionRange(input, position, position);
  135. }
  136.  
  137. function replaceSelection (input, replaceString) {
  138.     var len = replaceString.length;
  139.     if (input.setSelectionRange) {
  140.         var selectionStart = input.selectionStart;
  141.         var selectionEnd = input.selectionEnd;
  142.  
  143.         input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
  144.         input.selectionStart  = selectionStart + len;
  145.         input.selectionEnd  = selectionStart + len;
  146.     }
  147.     else if (document.selection) {
  148.         var range = document.selection.createRange();
  149.         var saved_range = range.duplicate();
  150.  
  151.         if (range.parentElement() == input) {
  152.             range.text = replaceString;
  153.             range.moveEnd("character", saved_range.selectionStart + len);
  154.             range.moveStart("character", saved_range.selectionStart + len);
  155.             range.select();
  156.         }
  157.     }
  158.     input.focus();
  159. }
  160.  
  161.  
  162. function autocompleteMatch (text, values) {
  163.     for (var i = 0; i < values.length; i++) {
  164.         if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
  165.             return values[i];
  166.         }
  167.     }
  168.  
  169.     return null;
  170. }
  171.  
  172. function autocomplete(textbox, event, values) {
  173.     if (textbox.setSelectionRange || textbox.createTextRange) {
  174.         switch (event.keyCode) {
  175.             case 38:    // up arrow
  176.             case 40:    // down arrow
  177.             case 37:    // left arrow
  178.             case 39:    // right arrow
  179.             case 33:    // page up
  180.             case 34:    // page down
  181.             case 36:    // home
  182.             case 35:    // end
  183.             case 13:    // enter
  184.             case 9:     // tab
  185.             case 27:    // esc
  186.             case 16:    // shift
  187.             case 17:    // ctrl
  188.             case 18:    // alt
  189.             case 20:    // caps lock
  190.             case 8:     // backspace
  191.             case 46:    // delete
  192.                 return true;
  193.                 break;
  194.  
  195.             default:
  196.                 var c = String.fromCharCode(
  197.                     (event.charCode == undefined) ? event.keyCode : event.charCode
  198.                 );
  199.                 replaceSelection(textbox, c);
  200.                 sMatch = autocompleteMatch(textbox.value, values);
  201.                 var len = textbox.value.length;
  202.                 
  203.                 if (sMatch != null) {
  204.                     textbox.value = sMatch;
  205.                     setSelectionRange(textbox, len, textbox.value.length);
  206.                 }
  207.                 return false;
  208.         }
  209.     }
  210.     else {
  211.         return true;
  212.     }
  213. }
  214. /* end javascript for autocomplete */
  215.  
  216. EOS;
  217.                 define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS'true);
  218.             }
  219.             $jsEscape = array(
  220.                 "\r"    => '\r',
  221.                 "\n"    => '\n',
  222.                 "\t"    => '\t',
  223.                 "'"     => "\\'",
  224.                 '"'     => '\"',
  225.                 '\\'    => '\\\\'
  226.             );
  227.             
  228.             $js .= 'var ' $arrayName " = new Array();\n";
  229.             for ($i = 0; $i count($this->_options)$i++{
  230.                 $js .= $arrayName '[' $i "] = '" strtr($this->_options[$i]$jsEscape"';\n";
  231.             }
  232.             $js .= "//]]>\n</script>\n";
  233.         }
  234.         return $js . parent::toHtml();
  235.     }// end func toHtml
  236.  
  237.     // }}}
  238. // end class HTML_QuickForm_autocomplete
  239. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.