3 * TKeyboard class file.
5 * @author Sergey Morkovkin <sergeymorkovkin@mail.ru> and Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls
16 * TKeyboard displays a virtual keyboard that users can click on to enter input in
17 * an associated text box. It helps to reduce the keyboard recording hacking.
19 * To use TKeyboard, write a template like following:
21 * <com:TTextBox ID="PasswordInput" />
22 * <com:TKeyboard ForControl="PasswordInput" />
25 * A TKeyboard control is associated with a {@link TTextBox} control by specifying {@link setForControl ForControl}
26 * to be the ID of that control. When the textbox is in focus, a virtual keyboard will pop up; and when
27 * the text box is losing focus, the keyboard will hide automatically. Set {@link setAutoHide AutoHide} to
28 * false to keep the keyboard showing all the time.
30 * The appearance of the keyboard can also be changed by specifying a customized CSS file via
31 * {@link setCssUrl CssUrl}. By default, the CSS class name for the keyboard is 'Keyboard'. This may
32 * also be changed by specifying {@link setKeyboardCssClass KeyboardCssClass}.
34 * @author Sergey Morkovkin <sergeymorkovkin@mail.ru> and Qiang Xue <qiang.xue@gmail.com>
35 * @package System.Web.UI.WebControls
38 class TKeyboard extends TWebControl
41 * @return string the ID path of the {@link TTextBox} control
43 public function getForControl()
45 return $this->getViewState('ForControl','');
49 * Sets the ID path of the {@link TTextBox} control.
50 * The ID path is the dot-connected IDs of the controls reaching from
51 * the keyboard's naming container to the target control.
52 * @param string the ID path
54 public function setForControl($value)
56 $this->setViewState('ForControl', TPropertyValue::ensureString($value));
60 * @return boolean whether the keyboard should be hidden when the textbox is not in focus. Defaults to true.
62 public function getAutoHide()
64 return $this->getViewState('AutoHide', true);
68 * @param boolean whether the keyboard should be hidden when the textbox is not in focus.
70 public function setAutoHide($value)
72 $this->setViewState('AutoHide', TPropertyValue::ensureBoolean($value), true);
76 * @return string the CSS class name for the keyboard <div> element. Defaults to 'Keyboard'.
78 public function getKeyboardCssClass()
80 return $this->getViewState('KeyboardCssClass', 'Keyboard');
84 * Sets a value indicating the CSS class name for the keyboard <div> element.
85 * Note, if you change this property, make sure you also supply a customized CSS file
86 * by specifying {@link setCssUrl CssUrl} which uses the new CSS class name for styling.
87 * @param string the CSS class name for the keyboard <div> element.
89 public function setKeyboardCssClass($value)
91 $this->setViewState('KeyboardCssClass', $value, 'Keyboard');
95 * @return string the URL for the CSS file to customize the appearance of the keyboard.
97 public function getCssUrl()
99 return $this->getViewState('CssUrl', '');
103 * @param string the URL for the CSS file to customize the appearance of the keyboard.
105 public function setCssUrl($value)
107 $this->setViewState('CssUrl', $value, '');
111 * Registers CSS and JS.
112 * This method is invoked right before the control rendering, if the control is visible.
113 * @param mixed event parameter
115 public function onPreRender($param)
117 parent::onPreRender($param);
118 if($this->getPage()->getClientSupportsJavaScript())
120 $this->registerStyleSheet();
121 $this->registerClientScript();
126 * Adds attribute name-value pairs to renderer.
127 * This method overrides the parent implementation with additional TKeyboard specific attributes.
128 * @param THtmlWriter the writer used for the rendering purpose
130 protected function addAttributesToRender($writer)
132 parent::addAttributesToRender($writer);
133 if($this->getPage()->getClientSupportsJavaScript())
134 $writer->addAttribute('id',$this->getClientID());
138 * Registers the CSS relevant to the TKeyboard.
139 * It will register the CSS file specified by {@link getCssUrl CssUrl}.
140 * If that is not set, it will use the default CSS.
142 protected function registerStyleSheet()
144 if(($url=$this->getCssUrl())==='')
145 $url=$this->getApplication()->getAssetManager()->publishFilePath(dirname(__FILE__).DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'keyboard.css');
146 $this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
150 * Registers the relevant JavaScript.
152 protected function registerClientScript()
154 $options=TJavaScript::encode($this->getClientOptions());
155 $className=$this->getClientClassName();
156 $cs=$this->getPage()->getClientScript();
157 $cs->registerPradoScript('keyboard');
158 $cs->registerEndScript('prado:'.$this->getClientID(), "new $className($options);");
162 * @return string the Javascript class name for this control
164 protected function getClientClassName()
166 return 'Prado.WebUI.TKeyboard';
170 * @return array the JavaScript options for this control
172 protected function getClientOptions()
174 if(($forControl=$this->getForControl())==='')
175 throw new TConfigurationException('keyboard_forcontrol_required');
176 if(($target=$this->findControl($forControl))===null)
177 throw new TConfigurationException('keyboard_forcontrol_invalid',$forControl);
179 $options['ID'] = $this->getClientID();
180 $options['ForControl'] = $target->getClientID();
181 $options['AutoHide'] = $this->getAutoHide();
182 $options['CssClass'] = $this->getKeyboardCssClass();