Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TInPlaceTextBox class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TInPlaceTextBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.ActiveControls
11
 */
12
 
13
Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
14
 
15
/**
16
 * TInPlaceTextBox Class
17
 * *
18
 * TInPlaceTextBox is a component rendered as a label and allows its
19
 * contents to be edited by changing the label to a textbox when
20
 * the label is clicked or when another control or html element with
21
 * ID given by {@link setEditTriggerControlID EditTriggerControlID} is clicked.
22
 *
23
 * If the {@link OnLoadingText} event is handled, a callback request is
24
 * made when the label is clicked, while the request is being made the
25
 * textbox is disabled from editing. The {@link OnLoadingText} event allows
26
 * you to update the content of the textbox before the client is allowed
27
 * to edit the content. After the callback request returns successfully,
28
 * the textbox is enabled and the contents is then allowed to be edited.
29
 *
30
 * Once the textbox loses focus, if {@link setAutoPostBack AutoPostBack}
31
 * is true and the textbox content has changed, a callback request is made and
32
 * the {@link OnTextChanged} event is raised like that of the TActiveTextBox.
33
 * During the request, the textbox is disabled.
34
 *
35
 * After the callback request returns sucessfully, the textbox is enabled.
36
 * If the {@link setAutoHideTextBox AutoHideTextBox} property is true, then
37
 * the textbox will be hidden and the label is then shown.
38
 *
39
 * Since 3.1.2, you can set the {@link setReadOnly ReadOnly} property to make
40
 * the control not editable. This property can be also changed on callback
41
 *
42
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
43
 * @version $Id: TInPlaceTextBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
44
 * @package System.Web.UI.ActiveControls
45
 * @since 3.1
46
 */
47
class TInPlaceTextBox extends TActiveTextBox
48
{
49
	/**
50
	 * Sets the auto post back to true by default.
51
	 */
52
	public function __construct()
53
	{
54
		parent::__construct();
55
		$this->setAutoPostBack(true);
56
	}
57
 
58
	/**
59
	 * @param boolean true to hide the textbox after losing focus.
60
	 */
61
	public function setAutoHideTextBox($value)
62
	{
63
		$this->setViewState('AutoHide', TPropertyValue::ensureBoolean($value), true);
64
	}
65
 
66
	/**
67
	 * @return boolean true will hide the textbox after losing focus.
68
	 */
69
	public function getAutoHideTextBox()
70
	{
71
		return $this->getViewState('AutoHide', true);
72
	}
73
 
74
	/**
75
	 * @param boolean true to display the edit textbox
76
	 */
77
	public function setDisplayTextBox($value)
78
	{
79
		$value = TPropertyValue::ensureBoolean($value);
80
		$this->setViewState('DisplayTextBox', $value,false);
81
		if($this->getActiveControl()->canUpdateClientSide())
82
			$this->callClientFunction('setDisplayTextBox',$value);
83
	}
84
 
85
	/**
86
	 * @return boolean true to display the edit textbox
87
	 */
88
	public function getDisplayTextBox()
89
	{
90
		return $this->getViewState('DisplayTextBox', false);
91
	}
92
 
93
	/**
94
	 * Calls the client-side static method for this control class.
95
	 * @param string static method name
96
	 * @param mixed method parmaeter
97
	 */
98
	protected function callClientFunction($func,$value)
99
	{
100
		$client = $this->getPage()->getCallbackClient();
101
		$code = $this->getClientClassName().'.'.$func;
102
		$client->callClientFunction($code,array($this,$value));
103
	}
104
 
105
	/**
106
	 * @param string ID of the control that can trigger to edit the textbox
107
	 */
108
	public function setEditTriggerControlID($value)
109
	{
110
		$this->setViewState('EditTriggerControlID', $value);
111
	}
112
 
113
	/**
114
	 * @return string ID of the control that can trigger to edit the textbox
115
	 */
116
	public function getEditTriggerControlID()
117
	{
118
		return $this->getViewState('EditTriggerControlID');
119
	}
120
 
121
	/**
122
	 * @return string edit trigger control client ID.
123
	 */
124
	protected function getExternalControlID()
125
	{
126
		$extID = $this->getEditTriggerControlID();
127
		if(is_null($extID)) return '';
128
		if(($control = $this->findControl($extID))!==null)
129
			return $control->getClientID();
130
		return $extID;
131
	}
132
 
133
	/**
134
	 * On callback response, the inner HTMl of the label and the
135
	 * value of the textbox is updated
136
	 * @param string the text value of the label
137
	 */
138
	public function setText($value)
139
	{
140
		TTextBox::setText($value);
141
		if($this->getActiveControl()->canUpdateClientSide())
142
		{
143
			$client = $this->getPage()->getCallbackClient();
144
			$client->update($this->getLabelClientID(), $value);
145
			$client->setValue($this, $value);
146
		}
147
	}
148
 
149
	/**
150
	 * Update ClientSide Readonly property
151
	 * @param boolean value
152
	 * @since 3.1.2
153
	 */
154
	public function setReadOnly ($value)
155
	{
156
		$value=TPropertyValue::ensureBoolean($value);
157
		TTextBox::setReadOnly($value);
158
		if ($this->getActiveControl()->canUpdateClientSide())
159
		{
160
			$this->callClientFunction('setReadOnly', $value);
161
		}
162
	}
163
 
164
	/**
165
	 * @return string tag name of the label.
166
	 */
167
	protected function getTagName()
168
	{
169
		return 'span';
170
	}
171
 
172
	/**
173
	 * Renders the body content of the label.
174
	 * @param THtmlWriter the writer for rendering
175
	 */
176
	public function renderContents($writer)
177
	{
178
		if(($text=$this->getText())==='')
179
			parent::renderContents($writer);
180
		else
181
			$writer->write($text);
182
	}
183
 
184
	/**
185
	 * @return string label client ID
186
	 */
187
	protected function getLabelClientID()
188
	{
189
		return $this->getClientID().'__label';
190
	}
191
 
192
	/**
193
	 * This method is invoked when a callback is requested. The method raises
194
	 * 'OnCallback' event to fire up the event handlers. If you override this
195
	 * method, be sure to call the parent implementation so that the event
196
	 * handler can be invoked.
197
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
198
	 */
199
	public function onCallback($param)
200
	{
201
		$action = $param->getCallbackParameter();
202
		if(is_array($action) && $action[0] === '__InlineEditor_loadExternalText__')
203
		{
204
			$parameter = new TCallbackEventParameter($this->getResponse(), $action[1]);
205
			$this->onLoadingText($parameter);
206
		}
207
		$this->raiseEvent('OnCallback', $this, $param);
208
	}
209
 
210
	/**
211
	 * @return array callback options.
212
	 */
213
	protected function getPostBackOptions()
214
	{
215
		$options = parent::getPostBackOptions();
216
		$options['ID'] = $this->getLabelClientID();
217
		$options['TextBoxID'] = $this->getClientID();
218
		$options['ExternalControl'] = $this->getExternalControlID();
219
		$options['AutoHide'] = $this->getAutoHideTextBox() == false ? '' : true;
220
		$options['AutoPostBack'] = $this->getAutoPostBack() == false ? '' : true;
221
		if($this->getTextMode()==='MultiLine')
222
		{
223
			$options['Columns'] = $this->getColumns();
224
			$options['Rows'] = $this->getRows();
225
			$options['Wrap'] = $this->getWrap()== false ? '' : true;
226
		}
227
		else
228
		{
229
			$length = $this->getMaxLength();
230
			$options['MaxLength'] = $length > 0 ? $length : '';
231
		}
232
 
233
		if($this->hasEventHandler('OnLoadingText'))
234
			$options['LoadTextOnEdit'] = true;
235
 
236
		$options['ReadOnly']=$this->getReadOnly();
237
		return $options;
238
	}
239
 
240
	/**
241
	 * Raised when editing the content is requsted to be loaded from the
242
	 * server side.
243
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
244
	 */
245
	public function onLoadingText($param)
246
	{
247
		$this->raiseEvent('OnLoadingText',$this,$param);
248
	}
249
 
250
	/**
251
	 * @return string corresponding javascript class name for this TInPlaceTextBox
252
	 */
253
	protected function getClientClassName()
254
	{
255
		return 'Prado.WebUI.TInPlaceTextBox';
256
	}
257
 
258
	/**
259
	 * Ensure that the ID attribute is rendered and registers the javascript code
260
	 * for initializing the active control.
261
	 */
262
	protected function addAttributesToRender($writer)
263
	{
264
		//calls the TWebControl to avoid rendering other attribute normally render for a textbox.
265
		TWebControl::addAttributesToRender($writer);
266
		$writer->addAttribute('id',$this->getLabelClientID());
267
		$this->getActiveControl()->registerCallbackClientScript(
268
			$this->getClientClassName(), $this->getPostBackOptions());
269
	}
270
 
271
}