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.6 2005/08/05 16:33:56 avb 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.      * "One-time" javascript (containing functions), see bug #4611
  57.      *
  58.      * @var     string 
  59.      * @access  private
  60.      */
  61.     var $_js '';
  62.  
  63.     // }}}
  64.     // {{{ constructor
  65.  
  66.     /**
  67.      * Class constructor
  68.      *
  69.      * @param     string    $elementName    (optional)Input field name attribute
  70.      * @param     string    $elementLabel   (optional)Input field label in form
  71.      * @param     array     $options        (optional)Autocomplete options
  72.      * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  73.      *                                       or an associative array. Date format is passed along the attributes.
  74.      * @access    public
  75.      * @return    void 
  76.      */
  77.     function HTML_QuickForm_autocomplete($elementName = null$elementLabel = null$options = null$attributes = null)
  78.     {
  79.         $this->HTML_QuickForm_text($elementName$elementLabel$attributes);
  80.         $this->_persistantFreeze = true;
  81.         $this->_type 'autocomplete';
  82.         if (isset($options)) {
  83.             $this->setOptions($options);
  84.         }
  85.     //end constructor
  86.  
  87.     // }}}
  88.     // {{{ setOptions()
  89.  
  90.     /**
  91.      * Sets the options for the autocomplete input text element
  92.      *
  93.      * @param     array    $options    Array of options for the autocomplete input text element
  94.      * @access    public
  95.      * @return    void 
  96.      */
  97.     function setOptions($options)
  98.     {
  99.         $this->_options array_values($options);
  100.     // end func setOptions
  101.  
  102.     // }}}
  103.     // {{{ toHtml()
  104.  
  105.     /**
  106.      * Returns Html for the autocomplete input text element
  107.      *
  108.      * @access      public
  109.      * @return      string 
  110.      */
  111.     function toHtml()
  112.     {
  113.         // prevent problems with grouped elements
  114.         $arrayName str_replace(array('['']')array('__''')$this->getName()) '_values';
  115.  
  116.         $this->updateAttributes(array(
  117.             'onkeypress' => 'return autocomplete(this, event, ' $arrayName ');'
  118.         ));
  119.         if ($this->_flagFrozen{
  120.             $js '';
  121.         else {
  122.             $js "<script type=\"text/javascript\">\n//<![CDATA[\n";
  123.             if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
  124.                 $this->_js .= <<<EOS
  125.  
  126. /* begin javascript for autocomplete */
  127. function setSelectionRange(input, selectionStart, selectionEnd) {
  128.     if (input.setSelectionRange) {
  129.         input.setSelectionRange(selectionStart, selectionEnd);
  130.     }
  131.     else if (input.createTextRange) {
  132.         var range = input.createTextRange();
  133.         range.collapse(true);
  134.         range.moveEnd("character", selectionEnd);
  135.         range.moveStart("character", selectionStart);
  136.         range.select();
  137.     }
  138.     input.focus();
  139. }
  140.  
  141. function setCaretToPosition(input, position) {
  142.     setSelectionRange(input, position, position);
  143. }
  144.  
  145. function replaceSelection (input, replaceString) {
  146.     var len = replaceString.length;
  147.     if (input.setSelectionRange) {
  148.         var selectionStart = input.selectionStart;
  149.         var selectionEnd = input.selectionEnd;
  150.  
  151.         input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
  152.         input.selectionStart  = selectionStart + len;
  153.         input.selectionEnd  = selectionStart + len;
  154.     }
  155.     else if (document.selection) {
  156.         var range = document.selection.createRange();
  157.         var saved_range = range.duplicate();
  158.  
  159.         if (range.parentElement() == input) {
  160.             range.text = replaceString;
  161.             range.moveEnd("character", saved_range.selectionStart + len);
  162.             range.moveStart("character", saved_range.selectionStart + len);
  163.             range.select();
  164.         }
  165.     }
  166.     input.focus();
  167. }
  168.  
  169.  
  170. function autocompleteMatch (text, values) {
  171.     for (var i = 0; i < values.length; i++) {
  172.         if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
  173.             return values[i];
  174.         }
  175.     }
  176.  
  177.     return null;
  178. }
  179.  
  180. function autocomplete(textbox, event, values) {
  181.     if (textbox.setSelectionRange || textbox.createTextRange) {
  182.         switch (event.keyCode) {
  183.             case 38:    // up arrow
  184.             case 40:    // down arrow
  185.             case 37:    // left arrow
  186.             case 39:    // right arrow
  187.             case 33:    // page up
  188.             case 34:    // page down
  189.             case 36:    // home
  190.             case 35:    // end
  191.             case 13:    // enter
  192.             case 9:     // tab
  193.             case 27:    // esc
  194.             case 16:    // shift
  195.             case 17:    // ctrl
  196.             case 18:    // alt
  197.             case 20:    // caps lock
  198.             case 8:     // backspace
  199.             case 46:    // delete
  200.                 return true;
  201.                 break;
  202.  
  203.             default:
  204.                 var c = String.fromCharCode(
  205.                     (event.charCode == undefined) ? event.keyCode : event.charCode
  206.                 );
  207.                 replaceSelection(textbox, c);
  208.                 sMatch = autocompleteMatch(textbox.value, values);
  209.                 var len = textbox.value.length;
  210.                 
  211.                 if (sMatch != null) {
  212.                     textbox.value = sMatch;
  213.                     setSelectionRange(textbox, len, textbox.value.length);
  214.                 }
  215.                 return false;
  216.         }
  217.     }
  218.     else {
  219.         return true;
  220.     }
  221. }
  222. /* end javascript for autocomplete */
  223.  
  224. EOS;
  225.                 define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS'true);
  226.             }
  227.             $jsEscape = array(
  228.                 "\r"    => '\r',
  229.                 "\n"    => '\n',
  230.                 "\t"    => '\t',
  231.                 "'"     => "\\'",
  232.                 '"'     => '\"',
  233.                 '\\'    => '\\\\'
  234.             );
  235.  
  236.             $js .= $this->_js;
  237.             $js .= 'var ' $arrayName " = new Array();\n";
  238.             for ($i = 0; $i count($this->_options)$i++{
  239.                 $js .= $arrayName '[' $i "] = '" strtr($this->_options[$i]$jsEscape"';\n";
  240.             }
  241.             $js .= "//]]>\n</script>";
  242.         }
  243.         return $js . parent::toHtml();
  244.     }// end func toHtml
  245.  
  246.     // }}}
  247. // end class HTML_QuickForm_autocomplete
  248. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.