adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / autocomplete.php
blobc39bd8dec4cf5ace51c5b7a243a36c97a9edab65
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 // +----------------------------------------------------------------------+
26 // $Id$
29 require_once("HTML/QuickForm/text.php");
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.
37 * Ex:
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
46 // {{{ properties
48 /**
49 * Options for the autocomplete input text element
51 * @var array
52 * @access private
54 var $_options = array();
56 /**
57 * "One-time" javascript (containing functions), see bug #4611
59 * @var string
60 * @access private
62 var $_js = '';
64 // }}}
65 // {{{ constructor
67 /**
68 * Class constructor
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.
75 * @access public
76 * @return void
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);
86 } //end constructor
88 // }}}
89 // {{{ setOptions()
91 /**
92 * Sets the options for the autocomplete input text element
94 * @param array $options Array of options for the autocomplete input text element
95 * @access public
96 * @return void
98 function setOptions($options)
100 $this->_options = array_values($options);
101 } // end func setOptions
103 // }}}
104 // {{{ toHtml()
107 * Returns Html for the autocomplete input text element
109 * @access public
110 * @return string
112 function toHtml()
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) {
121 $js = '';
122 } else {
123 $js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
124 if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
125 $this->_js .= <<<EOS
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);
137 range.select();
139 input.focus();
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);
164 range.select();
167 input.focus();
171 function autocompleteMatch (text, values) {
172 for (var i = 0; i < values.length; i++) {
173 if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
174 return values[i];
178 return null;
181 function autocomplete(textbox, event, values) {
182 if (textbox.setSelectionRange || textbox.createTextRange) {
183 switch (event.keyCode) {
184 case 38: // up arrow
185 case 40: // down arrow
186 case 37: // left arrow
187 case 39: // right arrow
188 case 33: // page up
189 case 34: // page down
190 case 36: // home
191 case 35: // end
192 case 13: // enter
193 case 9: // tab
194 case 27: // esc
195 case 16: // shift
196 case 17: // ctrl
197 case 18: // alt
198 case 20: // caps lock
199 case 8: // backspace
200 case 46: // delete
201 return true;
202 break;
204 default:
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);
216 return false;
219 else {
220 return true;
223 /* end javascript for autocomplete */
225 EOS;
226 define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS', true);
228 $jsEscape = array(
229 "\r" => '\r',
230 "\n" => '\n',
231 "\t" => '\t',
232 "'" => "\\'",
233 '"' => '\"',
234 '\\' => '\\\\'
237 $js .= $this->_js;
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();
245 }// end func toHtml
247 // }}}
248 } // end class HTML_QuickForm_autocomplete