]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TKeyboard.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TKeyboard.php
1 <?php
2 /**
3  * TKeyboard class file.
4  *
5  * @author Sergey Morkovkin <sergeymorkovkin@mail.ru> and Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.WebControls
10  * @since 3.1.1
11  */
12
13 /**
14  * Class TKeyboard.
15  *
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.
18  *
19  * To use TKeyboard, write a template like following:
20  * <code>
21  * <com:TTextBox ID="PasswordInput" />
22  * <com:TKeyboard ForControl="PasswordInput" />
23  * </code>
24  *
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.
29  *
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}.
33  *
34  * @author Sergey Morkovkin <sergeymorkovkin@mail.ru> and Qiang Xue <qiang.xue@gmail.com>
35  * @package System.Web.UI.WebControls
36  * @since 3.1.1
37  */
38 class TKeyboard extends TWebControl
39 {
40         /**
41          * @return string the ID path of the {@link TTextBox} control
42          */
43         public function getForControl()
44         {
45                 return $this->getViewState('ForControl','');
46         }
47
48         /**
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
53          */
54         public function setForControl($value)
55         {
56                 $this->setViewState('ForControl', TPropertyValue::ensureString($value));
57         }
58
59         /**
60          * @return boolean whether the keyboard should be hidden when the textbox is not in focus. Defaults to true.
61          */
62         public function getAutoHide()
63         {
64                 return $this->getViewState('AutoHide', true);
65         }
66
67         /**
68          * @param boolean whether the keyboard should be hidden when the textbox is not in focus.
69          */
70         public function setAutoHide($value)
71         {
72                 $this->setViewState('AutoHide', TPropertyValue::ensureBoolean($value), true);
73         }
74
75         /**
76          * @return string the CSS class name for the keyboard <div> element. Defaults to 'Keyboard'.
77          */
78         public function getKeyboardCssClass()
79         {
80                 return $this->getViewState('KeyboardCssClass', 'Keyboard');
81         }
82
83         /**
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.
88          */
89         public function setKeyboardCssClass($value)
90         {
91                 $this->setViewState('KeyboardCssClass', $value, 'Keyboard');
92         }
93
94         /**
95          * @return string the URL for the CSS file to customize the appearance of the keyboard.
96          */
97         public function getCssUrl()
98         {
99                 return $this->getViewState('CssUrl', '');
100         }
101
102         /**
103          * @param string the URL for the CSS file to customize the appearance of the keyboard.
104          */
105         public function setCssUrl($value)
106         {
107                 $this->setViewState('CssUrl', $value, '');
108         }
109
110         /**
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
114          */
115         public function onPreRender($param)
116         {
117                 parent::onPreRender($param);
118                 if($this->getPage()->getClientSupportsJavaScript())
119                 {
120                         $this->registerStyleSheet();
121                         $this->registerClientScript();
122                 }
123         }
124
125         /**
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
129          */
130         protected function addAttributesToRender($writer)
131         {
132                 parent::addAttributesToRender($writer);
133                 if($this->getPage()->getClientSupportsJavaScript())
134                         $writer->addAttribute('id',$this->getClientID());
135         }
136
137         /**
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.
141          */
142         protected function registerStyleSheet()
143         {
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);
147         }
148
149         /**
150          * Registers the relevant JavaScript.
151          */
152         protected function registerClientScript()
153         {
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);");
159         }
160
161         /**
162          * @return string the Javascript class name for this control
163          */
164         protected function getClientClassName()
165         {
166                 return 'Prado.WebUI.TKeyboard';
167         }
168
169         /**
170          * @return array the JavaScript options for this control
171          */
172         protected function getClientOptions()
173         {
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);
178
179             $options['ID'] = $this->getClientID();
180             $options['ForControl'] = $target->getClientID();
181             $options['AutoHide'] = $this->getAutoHide();
182             $options['CssClass'] = $this->getKeyboardCssClass();
183
184             return $options;
185         }
186 }
187