Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/pear/HTML/QuickForm/autocomplete.php
Go to the documentation of this file.
00001 <?php
00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */
00003 // +----------------------------------------------------------------------+
00004 // | PHP Version 4                                                        |
00005 // +----------------------------------------------------------------------+
00006 // | Copyright (c) 1997-2003 The PHP Group                                |
00007 // +----------------------------------------------------------------------+
00008 // | This source file is subject to version 2.0 of the PHP license,       |
00009 // | that is bundled with this package in the file LICENSE, and is        |
00010 // | available at through the world-wide-web at                           |
00011 // | http://www.php.net/license/2_02.txt.                                 |
00012 // | If you did not receive a copy of the PHP license and are unable to   |
00013 // | obtain it through the world-wide-web, please send a note to          |
00014 // | license@php.net so we can mail you a copy immediately.               |
00015 // +----------------------------------------------------------------------+
00016 // | Author:  Matteo Di Giovinazzo <matteodg@infinito.it>                 |
00017 // |                                                                      |
00018 // | For the JavaScript code thanks to Martin Honnen and                  |
00019 // | Nicholas C. Zakas                                                    |
00020 // | See:                                                                 |
00021 // |      http://www.faqts.com/knowledge_base/view.phtml/aid/13562        |
00022 // | and                                                                  |
00023 // |      http://www.sitepoint.com/article/1220                           |
00024 // +----------------------------------------------------------------------+
00025 //
00026 // $Id: autocomplete.php,v 1.2 2010/12/14 17:35:24 moodlerobot Exp $
00027 
00028 
00029 require_once("HTML/QuickForm/text.php");
00030 
00031 
00044 class HTML_QuickForm_autocomplete extends HTML_QuickForm_text
00045 {
00046     // {{{ properties
00047 
00054     var $_options = array();
00055 
00062     var $_js = '';
00063 
00064     // }}}
00065     // {{{ constructor
00066 
00078     function HTML_QuickForm_autocomplete($elementName = null, $elementLabel = null, $options = null, $attributes = null)
00079     {
00080         $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
00081         $this->_persistantFreeze = true;
00082         $this->_type = 'autocomplete';
00083         if (isset($options)) {
00084             $this->setOptions($options);
00085         }
00086     } //end constructor
00087 
00088     // }}}
00089     // {{{ setOptions()
00090 
00098     function setOptions($options)
00099     {
00100         $this->_options = array_values($options);
00101     } // end func setOptions
00102 
00103     // }}}
00104     // {{{ toHtml()
00105 
00112     function toHtml()
00113     {
00114         // prevent problems with grouped elements
00115         $arrayName = str_replace(array('[', ']'), array('__', ''), $this->getName()) . '_values';
00116 
00117         $this->updateAttributes(array(
00118             'onkeypress' => 'return autocomplete(this, event, ' . $arrayName . ');'
00119         ));
00120         if ($this->_flagFrozen) {
00121             $js = '';
00122         } else {
00123             $js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
00124             if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
00125                 $this->_js .= <<<EOS
00126 
00127 /* begin javascript for autocomplete */
00128 function setSelectionRange(input, selectionStart, selectionEnd) {
00129     if (input.setSelectionRange) {
00130         input.setSelectionRange(selectionStart, selectionEnd);
00131     }
00132     else if (input.createTextRange) {
00133         var range = input.createTextRange();
00134         range.collapse(true);
00135         range.moveEnd("character", selectionEnd);
00136         range.moveStart("character", selectionStart);
00137         range.select();
00138     }
00139     input.focus();
00140 }
00141 
00142 function setCaretToPosition(input, position) {
00143     setSelectionRange(input, position, position);
00144 }
00145 
00146 function replaceSelection (input, replaceString) {
00147         var len = replaceString.length;
00148     if (input.setSelectionRange) {
00149         var selectionStart = input.selectionStart;
00150         var selectionEnd = input.selectionEnd;
00151 
00152         input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
00153                 input.selectionStart  = selectionStart + len;
00154                 input.selectionEnd  = selectionStart + len;
00155     }
00156     else if (document.selection) {
00157         var range = document.selection.createRange();
00158                 var saved_range = range.duplicate();
00159 
00160         if (range.parentElement() == input) {
00161             range.text = replaceString;
00162                         range.moveEnd("character", saved_range.selectionStart + len);
00163                         range.moveStart("character", saved_range.selectionStart + len);
00164                         range.select();
00165         }
00166     }
00167     input.focus();
00168 }
00169 
00170 
00171 function autocompleteMatch (text, values) {
00172     for (var i = 0; i < values.length; i++) {
00173         if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
00174             return values[i];
00175         }
00176     }
00177 
00178     return null;
00179 }
00180 
00181 function autocomplete(textbox, event, values) {
00182     if (textbox.setSelectionRange || textbox.createTextRange) {
00183         switch (event.keyCode) {
00184             case 38:    // up arrow
00185             case 40:    // down arrow
00186             case 37:    // left arrow
00187             case 39:    // right arrow
00188             case 33:    // page up
00189             case 34:    // page down
00190             case 36:    // home
00191             case 35:    // end
00192             case 13:    // enter
00193             case 9:     // tab
00194             case 27:    // esc
00195             case 16:    // shift
00196             case 17:    // ctrl
00197             case 18:    // alt
00198             case 20:    // caps lock
00199             case 8:     // backspace
00200             case 46:    // delete
00201                 return true;
00202                 break;
00203 
00204             default:
00205                 var c = String.fromCharCode(
00206                     (event.charCode == undefined) ? event.keyCode : event.charCode
00207                 );
00208                 replaceSelection(textbox, c);
00209                 sMatch = autocompleteMatch(textbox.value, values);
00210                 var len = textbox.value.length;
00211                                 
00212                 if (sMatch != null) {
00213                     textbox.value = sMatch;
00214                     setSelectionRange(textbox, len, textbox.value.length);
00215                 }
00216                 return false;
00217         }
00218     }
00219     else {
00220         return true;
00221     }
00222 }
00223 /* end javascript for autocomplete */
00224 
00225 EOS;
00226                 define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS', true);
00227             }
00228             $jsEscape = array(
00229                 "\r"    => '\r',
00230                 "\n"    => '\n',
00231                 "\t"    => '\t',
00232                 "'"     => "\\'",
00233                 '"'     => '\"',
00234                 '\\'    => '\\\\'
00235             );
00236 
00237             $js .= $this->_js;
00238             $js .= 'var ' . $arrayName . " = new Array();\n";
00239             for ($i = 0; $i < count($this->_options); $i++) {
00240                 $js .= $arrayName . '[' . $i . "] = '" . strtr($this->_options[$i], $jsEscape) . "';\n";
00241             }
00242             $js .= "//]]>\n</script>";
00243         }
00244         return $js . parent::toHtml();
00245     }// end func toHtml
00246 
00247     // }}}
00248 } // end class HTML_QuickForm_autocomplete
00249 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations