2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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> |
18 // | For the JavaScript code thanks to Martin Honnen and |
19 // | Nicholas C. Zakas |
21 // | http://www.faqts.com/knowledge_base/view.phtml/aid/13562 |
23 // | http://www.sitepoint.com/article/1220 |
24 // +----------------------------------------------------------------------+
29 require_once("HTML/QuickForm/text.php");
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.
38 * $autocomplete =& $form->addElement('autocomplete', 'fruit', 'Favourite fruit:');
39 * $options = array("Apple", "Orange", "Pear", "Strawberry");
40 * $autocomplete->setOptions($options);
42 * @author Matteo Di Giovinazzo <matteodg@infinito.it>
44 class HTML_QuickForm_autocomplete
extends HTML_QuickForm_text
49 * Options for the autocomplete input text element
54 var $_options = array();
57 * "One-time" javascript (containing functions), see bug #4611
70 * @param string $elementName (optional)Input field name attribute
71 * @param string $elementLabel (optional)Input field label in form
72 * @param array $options (optional)Autocomplete options
73 * @param mixed $attributes (optional)Either a typical HTML attribute string
74 * or an associative array. Date format is passed along the attributes.
78 function HTML_QuickForm_autocomplete($elementName = null, $elementLabel = null, $options = null, $attributes = null)
80 $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
81 $this->_persistantFreeze
= true;
82 $this->_type
= 'autocomplete';
83 if (isset($options)) {
84 $this->setOptions($options);
92 * Sets the options for the autocomplete input text element
94 * @param array $options Array of options for the autocomplete input text element
98 function setOptions($options)
100 $this->_options
= array_values($options);
101 } // end func setOptions
107 * Returns Html for the autocomplete input text element
114 // prevent problems with grouped elements
115 $arrayName = str_replace(array('[', ']'), array('__', ''), $this->getName()) . '_values';
117 $this->updateAttributes(array(
118 'onkeypress' => 'return autocomplete(this, event, ' . $arrayName . ');'
120 if ($this->_flagFrozen
) {
123 $js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
124 if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
127 /* begin javascript for autocomplete */
128 function setSelectionRange(input, selectionStart, selectionEnd) {
129 if (input.setSelectionRange) {
130 input.setSelectionRange(selectionStart, selectionEnd);
132 else if (input.createTextRange) {
133 var range = input.createTextRange();
134 range.collapse(true);
135 range.moveEnd("character", selectionEnd);
136 range.moveStart("character", selectionStart);
142 function setCaretToPosition(input, position) {
143 setSelectionRange(input, position, position);
146 function replaceSelection (input, replaceString) {
147 var len = replaceString.length;
148 if (input.setSelectionRange) {
149 var selectionStart = input.selectionStart;
150 var selectionEnd = input.selectionEnd;
152 input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
153 input.selectionStart = selectionStart + len;
154 input.selectionEnd = selectionStart + len;
156 else if (document.selection) {
157 var range = document.selection.createRange();
158 var saved_range = range.duplicate();
160 if (range.parentElement() == input) {
161 range.text = replaceString;
162 range.moveEnd("character", saved_range.selectionStart + len);
163 range.moveStart("character", saved_range.selectionStart + len);
171 function autocompleteMatch (text, values) {
172 for (var i = 0; i < values.length; i++) {
173 if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
181 function autocomplete(textbox, event, values) {
182 if (textbox.setSelectionRange || textbox.createTextRange) {
183 switch (event.keyCode) {
185 case 40: // down arrow
186 case 37: // left arrow
187 case 39: // right arrow
189 case 34: // page down
198 case 20: // caps lock
205 var c = String.fromCharCode(
206 (event.charCode == undefined) ? event.keyCode : event.charCode
208 replaceSelection(textbox, c);
209 sMatch = autocompleteMatch(textbox.value, values);
210 var len = textbox.value.length;
212 if (sMatch != null) {
213 textbox.value = sMatch;
214 setSelectionRange(textbox, len, textbox.value.length);
223 /* end javascript for autocomplete */
226 define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS', true);
238 $js .= 'var ' . $arrayName . " = new Array();\n";
239 for ($i = 0; $i < count($this->_options
); $i++
) {
240 $js .= $arrayName . '[' . $i . "] = '" . strtr($this->_options
[$i], $jsEscape) . "';\n";
242 $js .= "//]]>\n</script>";
244 return $js . parent
::toHtml();
248 } // end class HTML_QuickForm_autocomplete