Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TKeyboard class file.
4
 *
5
 * @author Sergey Morkovkin <sergeymorkovkin@mail.ru> and Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TKeyboard.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 * @since 3.1.1
12
 */
13
 
14
/**
15
 * Class TKeyboard.
16
 *
17
 * TKeyboard displays a virtual keyboard that users can click on to enter input in
18
 * an associated text box. It helps to reduce the keyboard recording hacking.
19
 *
20
 * To use TKeyboard, write a template like following:
21
 * <code>
22
 * <com:TTextBox ID="PasswordInput" />
23
 * <com:TKeyboard ForControl="PasswordInput" />
24
 * </code>
25
 *
26
 * A TKeyboard control is associated with a {@link TTextBox} control by specifying {@link setForControl ForControl}
27
 * to be the ID of that control. When the textbox is in focus, a virtual keyboard will pop up; and when
28
 * the text box is losing focus, the keyboard will hide automatically. Set {@link setAutoHide AutoHide} to
29
 * false to keep the keyboard showing all the time.
30
 *
31
 * The appearance of the keyboard can also be changed by specifying a customized CSS file via
32
 * {@link setCssUrl CssUrl}. By default, the CSS class name for the keyboard is 'Keyboard'. This may
33
 * also be changed by specifying {@link setKeyboardCssClass KeyboardCssClass}.
34
 *
35
 * @author Sergey Morkovkin <sergeymorkovkin@mail.ru> and Qiang Xue <qiang.xue@gmail.com>
36
 * @version $Id: TKeyboard.php 2541 2008-10-21 15:05:13Z qiang.xue $
37
 * @since 3.1.1
38
 */
39
class TKeyboard extends TWebControl
40
{
41
	/**
42
	 * @return string the ID path of the {@link TTextBox} control
43
	 */
44
	public function getForControl()
45
	{
46
		return $this->getViewState('ForControl','');
47
	}
48
 
49
	/**
50
	 * Sets the ID path of the {@link TTextBox} control.
51
	 * The ID path is the dot-connected IDs of the controls reaching from
52
	 * the keyboard's naming container to the target control.
53
	 * @param string the ID path
54
	 */
55
	public function setForControl($value)
56
	{
57
		$this->setViewState('ForControl', TPropertyValue::ensureString($value));
58
	}
59
 
60
	/**
61
	 * @return boolean whether the keyboard should be hidden when the textbox is not in focus. Defaults to true.
62
	 */
63
	public function getAutoHide()
64
	{
65
		return $this->getViewState('AutoHide', true);
66
	}
67
 
68
	/**
69
	 * @param boolean whether the keyboard should be hidden when the textbox is not in focus.
70
	 */
71
	public function setAutoHide($value)
72
	{
73
		$this->setViewState('AutoHide', TPropertyValue::ensureBoolean($value), true);
74
	}
75
 
76
	/**
77
	 * @return string the CSS class name for the keyboard <div> element. Defaults to 'Keyboard'.
78
	 */
79
	public function getKeyboardCssClass()
80
	{
81
		return $this->getViewState('KeyboardCssClass', 'Keyboard');
82
	}
83
 
84
	/**
85
	 * Sets a value indicating the CSS class name for the keyboard <div> element.
86
	 * Note, if you change this property, make sure you also supply a customized CSS file
87
	 * by specifying {@link setCssUrl CssUrl} which uses the new CSS class name for styling.
88
	 * @param string the CSS class name for the keyboard <div> element.
89
	 */
90
	public function setKeyboardCssClass($value)
91
	{
92
		$this->setViewState('KeyboardCssClass', $value, 'Keyboard');
93
	}
94
 
95
	/**
96
	 * @return string the URL for the CSS file to customize the appearance of the keyboard.
97
	 */
98
	public function getCssUrl()
99
	{
100
		return $this->getViewState('CssUrl', '');
101
	}
102
 
103
	/**
104
	 * @param string the URL for the CSS file to customize the appearance of the keyboard.
105
	 */
106
	public function setCssUrl($value)
107
	{
108
		$this->setViewState('CssUrl', $value, '');
109
	}
110
 
111
	/**
112
	 * Registers CSS and JS.
113
	 * This method is invoked right before the control rendering, if the control is visible.
114
	 * @param mixed event parameter
115
	 */
116
	public function onPreRender($param)
117
	{
118
		parent::onPreRender($param);
119
		if($this->getPage()->getClientSupportsJavaScript())
120
		{
121
			$this->registerStyleSheet();
122
			$this->registerClientScript();
123
		}
124
	}
125
 
126
	/**
127
	 * Adds attribute name-value pairs to renderer.
128
	 * This method overrides the parent implementation with additional TKeyboard specific attributes.
129
	 * @param THtmlWriter the writer used for the rendering purpose
130
	 */
131
	protected function addAttributesToRender($writer)
132
	{
133
		parent::addAttributesToRender($writer);
134
		if($this->getPage()->getClientSupportsJavaScript())
135
			$writer->addAttribute('id',$this->getClientID());
136
	}
137
 
138
	/**
139
	 * Registers the CSS relevant to the TKeyboard.
140
	 * It will register the CSS file specified by {@link getCssUrl CssUrl}.
141
	 * If that is not set, it will use the default CSS.
142
	 */
143
	protected function registerStyleSheet()
144
	{
145
		if(($url=$this->getCssUrl())==='')
146
			$url=$this->getApplication()->getAssetManager()->publishFilePath(dirname(__FILE__).DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'keyboard.css');
147
		$this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
148
	}
149
 
150
	/**
151
	 * Registers the relevant JavaScript.
152
	 */
153
	protected function registerClientScript()
154
	{
155
		$options=TJavaScript::encode($this->getClientOptions());
156
		$className=$this->getClientClassName();
157
		$cs=$this->getPage()->getClientScript();
158
		$cs->registerPradoScript('keyboard');
159
		$cs->registerEndScript('prado:'.$this->getClientID(), "new $className($options);");
160
	}
161
 
162
	/**
163
	 * @return string the Javascript class name for this control
164
	 */
165
	protected function getClientClassName()
166
	{
167
		return 'Prado.WebUI.TKeyboard';
168
	}
169
 
170
	/**
171
	 * @return array the JavaScript options for this control
172
	 */
173
	protected function getClientOptions()
174
	{
175
		if(($forControl=$this->getForControl())==='')
176
			throw new TConfigurationException('keyboard_forcontrol_required');
177
	    if(($target=$this->findControl($forControl))===null)
178
	        throw new TConfigurationException('keyboard_forcontrol_invalid',$forControl);
179
 
180
	    $options['ID'] = $this->getClientID();
181
	    $options['ForControl'] = $target->getClientID();
182
	    $options['AutoHide'] = $this->getAutoHide();
183
	    $options['CssClass'] = $this->getKeyboardCssClass();
184
 
185
	    return $options;
186
	}
187
}
188