Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TBulletedList class file
4
 *
5
 * @author 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: TBulletedList.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * Includes TListControl class
15
 */
16
Prado::using('System.Web.UI.WebControls.TListControl');
17
 
18
/**
19
 * TBulletedList class
20
 *
21
 * TBulletedList displays items in a bullet format.
22
 * The bullet style is specified by {@link setBulletStyle BulletStyle}. When
23
 * the style is 'CustomImage', the {@link setBackImageUrl BulletImageUrl}
24
 * specifies the image used as bullets.
25
 *
26
 * TBulletedList displays the item texts in three different modes, specified
27
 * via {@link setDisplayMode DisplayMode}. When the mode is Text, the item texts
28
 * are displayed as static texts; When the mode is 'HyperLink', each item
29
 * is displayed as a hyperlink whose URL is given by the item value, and the
30
 * {@link setTarget Target} property can be used to specify the target browser window;
31
 * When the mode is 'LinkButton', each item is displayed as a link button which
32
 * posts back to the page if a user clicks on that and the event {@link onClick OnClick}
33
 * will be raised under such a circumstance.
34
 *
35
 * @author Qiang Xue <qiang.xue@gmail.com>
36
 * @version $Id: TBulletedList.php 2541 2008-10-21 15:05:13Z qiang.xue $
37
 * @package System.Web.UI.WebControls
38
 * @since 3.0
39
 */
40
class TBulletedList extends TListControl implements IPostBackEventHandler
41
{
42
	/**
43
	 * @var boolean cached property value of Enabled
44
	 */
45
	private $_isEnabled;
46
	/**
47
	 * @var TPostBackOptions postback options
48
	 */
49
	private $_postBackOptions;
50
 
51
	private $_currentRenderItemIndex;
52
 
53
	/**
54
	 * Raises the postback event.
55
	 * This method is required by {@link IPostBackEventHandler} interface.
56
	 * If {@link getCausesValidation CausesValidation} is true, it will
57
	 * invoke the page's {@link TPage::validate validate} method first.
58
	 * It will raise {@link onClick OnClick} events.
59
	 * This method is mainly used by framework and control developers.
60
	 * @param TEventParameter the event parameter
61
	 */
62
	public function raisePostBackEvent($param)
63
	{
64
		if($this->getCausesValidation())
65
			$this->getPage()->validate($this->getValidationGroup());
66
		$this->onClick(new TBulletedListEventParameter((int)$param));
67
	}
68
 
69
	/**
70
	 * @return string tag name of the bulleted list
71
	 */
72
	protected function getTagName()
73
	{
74
		switch($this->getBulletStyle())
75
		{
76
			case TBulletStyle::Numbered:
77
			case TBulletStyle::LowerAlpha:
78
			case TBulletStyle::UpperAlpha:
79
			case TBulletStyle::LowerRoman:
80
			case TBulletStyle::UpperRoman:
81
				return 'ol';
82
		}
83
		return 'ul';
84
	}
85
 
86
	/**
87
	 * Gets the name of the javascript class responsible for performing postback for this control.
88
	 * This method overrides the parent implementation.
89
	 * @return string the javascript class name
90
	 */
91
	protected function getClientClassName()
92
	{
93
		return 'Prado.WebUI.TBulletedList';
94
	}
95
 
96
	/**
97
	 * Adds attribute name-value pairs to renderer.
98
	 * This overrides the parent implementation with additional bulleted list specific attributes.
99
	 * @param THtmlWriter the writer used for the rendering purpose
100
	 */
101
	protected function addAttributesToRender($writer)
102
	{
103
		$needStart=false;
104
		switch($this->getBulletStyle())
105
		{
106
			case TBulletStyle::Numbered:
107
				$writer->addStyleAttribute('list-style-type','decimal');
108
				$needStart=true;
109
				break;
110
			case TBulletStyle::LowerAlpha:
111
				$writer->addStyleAttribute('list-style-type','lower-alpha');
112
				$needStart=true;
113
				break;
114
			case TBulletStyle::UpperAlpha:
115
				$writer->addStyleAttribute('list-style-type','upper-alpha');
116
				$needStart=true;
117
				break;
118
			case TBulletStyle::LowerRoman:
119
				$writer->addStyleAttribute('list-style-type','lower-roman');
120
				$needStart=true;
121
				break;
122
			case TBulletStyle::UpperRoman:
123
				$writer->addStyleAttribute('list-style-type','upper-roman');
124
				$needStart=true;
125
				break;
126
			case TBulletStyle::Disc:
127
				$writer->addStyleAttribute('list-style-type','disc');
128
				break;
129
			case TBulletStyle::Circle:
130
				$writer->addStyleAttribute('list-style-type','circle');
131
				break;
132
			case TBulletStyle::Square:
133
				$writer->addStyleAttribute('list-style-type','square');
134
				break;
135
			case TBulletStyle::CustomImage:
136
				$url=$this->getBulletImageUrl();
137
				$writer->addStyleAttribute('list-style-image',"url($url)");
138
				break;
139
		}
140
		if($needStart && ($start=$this->getFirstBulletNumber())!=1)
141
			$writer->addAttribute('start',"$start");
142
		parent::addAttributesToRender($writer);
143
	}
144
 
145
	/**
146
	 * @return string image URL used for bullets when {@link getBulletStyle BulletStyle} is 'CustomImage'.
147
	 */
148
	public function getBulletImageUrl()
149
	{
150
		return $this->getViewState('BulletImageUrl','');
151
	}
152
 
153
	/**
154
	 * @param string image URL used for bullets when {@link getBulletStyle BulletStyle} is 'CustomImage'.
155
	 */
156
	public function setBulletImageUrl($value)
157
	{
158
		$this->setViewState('BulletImageUrl',$value,'');
159
	}
160
 
161
	/**
162
	 * @return TBulletStyle style of bullets. Defaults to TBulletStyle::NotSet.
163
	 */
164
	public function getBulletStyle()
165
	{
166
		return $this->getViewState('BulletStyle',TBulletStyle::NotSet);
167
	}
168
 
169
	/**
170
	 * @param TBulletStyle style of bullets.
171
	 */
172
	public function setBulletStyle($value)
173
	{
174
		$this->setViewState('BulletStyle',TPropertyValue::ensureEnum($value,'TBulletStyle'),TBulletStyle::NotSet);
175
	}
176
 
177
	/**
178
	 * @return TBulletedListDisplayMode display mode of the list. Defaults to TBulletedListDisplayMode::Text.
179
	 */
180
	public function getDisplayMode()
181
	{
182
		return $this->getViewState('DisplayMode',TBulletedListDisplayMode::Text);
183
	}
184
 
185
	/**
186
	 * @return TBulletedListDisplayMode display mode of the list.
187
	 */
188
	public function setDisplayMode($value)
189
	{
190
		$this->setViewState('DisplayMode',TPropertyValue::ensureEnum($value,'TBulletedListDisplayMode'),TBulletedListDisplayMode::Text);
191
	}
192
 
193
	/**
194
	 * @return integer starting index when {@link getBulletStyle BulletStyle} is one of
195
	 * the following: 'Numbered', 'LowerAlpha', 'UpperAlpha', 'LowerRoman', 'UpperRoman'.
196
	 * Defaults to 1.
197
	 */
198
	public function getFirstBulletNumber()
199
	{
200
		return $this->getViewState('FirstBulletNumber',1);
201
	}
202
 
203
	/**
204
	 * @param integer starting index when {@link getBulletStyle BulletStyle} is one of
205
	 * the following: 'Numbered', 'LowerAlpha', 'UpperAlpha', 'LowerRoman', 'UpperRoman'.
206
	 */
207
	public function setFirstBulletNumber($value)
208
	{
209
		$this->setViewState('FirstBulletNumber',TPropertyValue::ensureInteger($value),1);
210
	}
211
 
212
	/**
213
	 * Raises 'OnClick' event.
214
	 * This method is invoked when the {@link getDisplayMode DisplayMode} is 'LinkButton'
215
	 * and end-users click on one of the buttons.
216
	 * @param TBulletedListEventParameter event parameter.
217
	 */
218
	public function onClick($param)
219
	{
220
		$this->raiseEvent('OnClick',$this,$param);
221
	}
222
 
223
	/**
224
	 * @return string the target window or frame to display the Web page content
225
	 * linked to when {@link getDisplayMode DisplayMode} is 'HyperLink' and one of
226
	 * the hyperlinks is clicked.
227
	 */
228
	public function getTarget()
229
	{
230
		return $this->getViewState('Target','');
231
	}
232
 
233
	/**
234
	 * @param string the target window or frame to display the Web page content
235
	 * linked to when {@link getDisplayMode DisplayMode} is 'HyperLink' and one of
236
	 * the hyperlinks is clicked.
237
	 */
238
	public function setTarget($value)
239
	{
240
		$this->setViewState('Target',$value,'');
241
	}
242
 
243
	/**
244
	 * Renders the control.
245
	 * @param THtmlWriter the writer for the rendering purpose.
246
	 */
247
	public function render($writer)
248
	{
249
		if($this->getHasItems())
250
			parent::render($writer);
251
	}
252
 
253
	/**
254
	 * Renders the body contents.
255
	 * @param THtmlWriter the writer for the rendering purpose.
256
	 */
257
	public function renderContents($writer)
258
	{
259
		$this->_isEnabled=$this->getEnabled(true);
260
		$this->_postBackOptions=$this->getPostBackOptions();
261
		$writer->writeLine();
262
		foreach($this->getItems() as $index=>$item)
263
		{
264
			if($item->getHasAttributes())
265
				$writer->addAttributes($item->getAttributes());
266
			$writer->renderBeginTag('li');
267
			$this->renderBulletText($writer,$item,$index);
268
			$writer->renderEndTag();
269
			$writer->writeLine();
270
		}
271
	}
272
 
273
	/**
274
	 * Renders each item
275
	 * @param THtmlWriter writer for the rendering purpose
276
	 * @param TListItem item to be rendered
277
	 * @param integer index of the item being rendered
278
	 */
279
	protected function renderBulletText($writer,$item,$index)
280
	{
281
		switch($this->getDisplayMode())
282
		{
283
			case TBulletedListDisplayMode::Text:
284
				$this->renderTextItem($writer, $item, $index);
285
				break;
286
			case TBulletedListDisplayMode::HyperLink:
287
				$this->renderHyperLinkItem($writer, $item, $index);
288
				break;
289
			case TBulletedListDisplayMode::LinkButton:
290
				$this->renderLinkButtonItem($writer, $item, $index);
291
				break;
292
		}
293
	}
294
 
295
	protected function renderTextItem($writer, $item, $index)
296
	{
297
		if($item->getEnabled())
298
			$writer->write(THttpUtility::htmlEncode($item->getText()));
299
		else
300
		{
301
			$writer->addAttribute('disabled','disabled');
302
			$writer->renderBeginTag('span');
303
			$writer->write(THttpUtility::htmlEncode($item->getText()));
304
			$writer->renderEndTag();
305
		}
306
	}
307
 
308
	protected function renderHyperLinkItem($writer, $item, $index)
309
	{
310
		if(!$this->_isEnabled || !$item->getEnabled())
311
			$writer->addAttribute('disabled','disabled');
312
		else
313
		{
314
			$writer->addAttribute('href',$item->getValue());
315
			if(($target=$this->getTarget())!=='')
316
				$writer->addAttribute('target',$target);
317
		}
318
		if(($accesskey=$this->getAccessKey())!=='')
319
			$writer->addAttribute('accesskey',$accesskey);
320
		$writer->renderBeginTag('a');
321
		$writer->write(THttpUtility::htmlEncode($item->getText()));
322
		$writer->renderEndTag();
323
	}
324
 
325
	protected function renderLinkButtonItem($writer, $item, $index)
326
	{
327
		if(!$this->_isEnabled || !$item->getEnabled())
328
			$writer->addAttribute('disabled','disabled');
329
		else
330
		{
331
			$this->_currentRenderItemIndex = $index;
332
			$writer->addAttribute('id', $this->getClientID().$index);
333
			$writer->addAttribute('href', "javascript:;//".$this->getClientID().$index);
334
			$cs = $this->getPage()->getClientScript();
335
			$cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
336
		}
337
		if(($accesskey=$this->getAccessKey())!=='')
338
			$writer->addAttribute('accesskey',$accesskey);
339
		$writer->renderBeginTag('a');
340
		$writer->write(THttpUtility::htmlEncode($item->getText()));
341
		$writer->renderEndTag();
342
	}
343
 
344
	/**
345
	 * @return array postback options used for linkbuttons.
346
	 */
347
	protected function getPostBackOptions()
348
	{
349
		$options['ValidationGroup'] = $this->getValidationGroup();
350
		$options['CausesValidation'] = $this->getCausesValidation();
351
		$options['EventTarget'] = $this->getUniqueID();
352
		$options['EventParameter'] = $this->_currentRenderItemIndex;
353
		$options['ID'] = $this->getClientID().$this->_currentRenderItemIndex;
354
		$options['StopEvent'] = true;
355
		return $options;
356
	}
357
 
358
	protected function canCauseValidation()
359
	{
360
		$group = $this->getValidationGroup();
361
		$hasValidators = $this->getPage()->getValidators($group)->getCount()>0;
362
		return $this->getCausesValidation() && $hasValidators;
363
	}
364
 
365
	/**
366
	 * @throws TNotSupportedException if this method is invoked
367
	 */
368
	public function setAutoPostBack($value)
369
	{
370
		throw new TNotSupportedException('bulletedlist_autopostback_unsupported');
371
	}
372
 
373
	/**
374
	 * @throws TNotSupportedException if this method is invoked
375
	 */
376
	public function setSelectedIndex($index)
377
	{
378
		throw new TNotSupportedException('bulletedlist_selectedindex_unsupported');
379
	}
380
 
381
	/**
382
	 * @throws TNotSupportedException if this method is invoked
383
	 */
384
	public function setSelectedIndices($indices)
385
	{
386
		throw new TNotSupportedException('bulletedlist_selectedindices_unsupported');
387
	}
388
 
389
	/**
390
	 * @throws TNotSupportedException if this method is invoked
391
	 */
392
	public function setSelectedValue($value)
393
	{
394
		throw new TNotSupportedException('bulletedlist_selectedvalue_unsupported');
395
	}
396
 
397
	/**
398
	 * @throws TNotSupportedException if this method is invoked
399
	 */
400
	public function setSelectedValues($values)
401
	{
402
		throw new TNotSupportedException('bulletedlist_selectedvalue_unsupported');
403
	}
404
}
405
 
406
/**
407
 * TBulletedListEventParameter
408
 * Event parameter for {@link TBulletedList::onClick Click} event of the
409
 * bulleted list. The {@link getIndex Index} gives the zero-based index
410
 * of the item that is currently being clicked.
411
 *
412
 * @author Qiang Xue <qiang.xue@gmail.com>
413
 * @version $Id: TBulletedList.php 2541 2008-10-21 15:05:13Z qiang.xue $
414
 * @package System.Web.UI.WebControls
415
 * @since 3.0
416
 */
417
class TBulletedListEventParameter extends TEventParameter
418
{
419
	/**
420
	 * @var integer index of the item clicked
421
	 */
422
	private $_index;
423
 
424
	/**
425
	 * Constructor.
426
	 * @param integer index of the item clicked
427
	 */
428
	public function __construct($index)
429
	{
430
		$this->_index=$index;
431
	}
432
 
433
	/**
434
	 * @return integer zero-based index of the item (rendered as a link button) that is clicked
435
	 */
436
	public function getIndex()
437
	{
438
		return $this->_index;
439
	}
440
}
441
 
442
/**
443
 * TBulletStyle class.
444
 * TBulletStyle defines the enumerable type for the possible bullet styles that may be used
445
 * for a {@link TBulletedList} control.
446
 *
447
 * @author Qiang Xue <qiang.xue@gmail.com>
448
 * @version $Id: TBulletedList.php 2541 2008-10-21 15:05:13Z qiang.xue $
449
 * @package System.Web.UI.WebControls
450
 * @since 3.0.4
451
 */
452
class TBulletStyle extends TEnumerable
453
{
454
	const NotSet='NotSet';
455
	const Numbered='Numbered';
456
	const LowerAlpha='LowerAlpha';
457
	const UpperAlpha='UpperAlpha';
458
	const LowerRoman='LowerRoman';
459
	const UpperRoman='UpperRoman';
460
	const Disc='Disc';
461
	const Circle='Circle';
462
	const Square='Square';
463
	const CustomImage='CustomImage';
464
}
465
 
466
/**
467
 * TBulletedListDisplayMode class.
468
 * TBulletedListDisplayMode defines the enumerable type for the possible display mode
469
 * of a {@link TBulletedList} control.
470
 *
471
 * The following enumerable values are defined:
472
 * - Text: the bulleted list items are displayed as plain texts
473
 * - HyperLink: the bulleted list items are displayed as hyperlinks
474
 * - LinkButton: the bulleted list items are displayed as link buttons that can cause postbacks
475
 *
476
 * @author Qiang Xue <qiang.xue@gmail.com>
477
 * @version $Id: TBulletedList.php 2541 2008-10-21 15:05:13Z qiang.xue $
478
 * @package System.Web.UI.WebControls
479
 * @since 3.0.4
480
 */
481
class TBulletedListDisplayMode extends TEnumerable
482
{
483
	const Text='Text';
484
	const HyperLink='HyperLink';
485
	const LinkButton='LinkButton';
486
}
487