Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TCallbackClientScript 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: TCallbackClientScript.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.ActiveControls
11
 */
12
 
13
/**
14
 * TCallbackClientScript class.
15
 *
16
 * The TCallbackClientScript class provides corresponding methods that can be
17
 * executed on the client-side (i.e. the browser client that is viewing
18
 * the page) during a callback response.
19
 *
20
 * The avaiable methods includes setting/clicking input elements, changing Css
21
 * styles, hiding/showing elements, and adding visual effects to elements on the
22
 * page. The client-side methods can be access through the CallbackClient
23
 * property available in TPage.
24
 *
25
 * For example, to hide "$myTextBox" element during callback response, do
26
 * <code>
27
 * $this->getPage()->getCallbackClient()->hide($myTextBox);
28
 * </code>
29
 *
30
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
31
 * @version $Id: TCallbackClientScript.php 2541 2008-10-21 15:05:13Z qiang.xue $
32
 * @package System.Web.UI.ActiveControls
33
 * @since 3.1
34
 */
35
class TCallbackClientScript extends TApplicationComponent
36
{
37
	/**
38
	 * @var TList list of client functions to execute.
39
	 */
40
	private $_actions;
41
 
42
	/**
43
	 * Constructor.
44
	 */
45
	public function __construct()
46
	{
47
		$this->_actions = new TList;
48
	}
49
 
50
	/**
51
	 * @return array list of client function to be executed during callback
52
	 * response.
53
	 */
54
	public function getClientFunctionsToExecute()
55
	{
56
		return $this->_actions->toArray();
57
	}
58
 
59
	/**
60
	 * Executes a client-side statement.
61
	 * @param string javascript function name
62
	 * @param array list of arguments for the function
63
	 */
64
	public function callClientFunction($function, $params=null)
65
	{
66
		if(!is_array($params))
67
			$params = array($params);
68
 
69
		if(count($params) > 0)
70
		{
71
			if($params[0] instanceof TControl)
72
				$params[0] = $params[0]->getClientID();
73
		}
74
		$this->_actions->add(array($function => $params));
75
	}
76
 
77
	/**
78
	 * Client script to set the value of a particular input element.
79
	 * @param TControl control element to set the new value
80
	 * @param string new value
81
	 */
82
	public function setValue($input, $text)
83
	{
84
		$this->callClientFunction('Prado.Element.setValue', array($input, $text));
85
	}
86
 
87
	/**
88
	 * Client script to select/clear/check a drop down list, check box list,
89
	 * or radio button list.
90
	 * The second parameter determines the selection method. Valid methods are
91
	 *  - <b>Value</b>, select or check by value
92
	 *  - <b>Values</b>, select or check by a list of values
93
	 *  - <b>Index</b>, select or check by index (zero based index)
94
	 *  - <b>Indices</b>, select or check by a list of index (zero based index)
95
	 *  - <b>Clear</b>, clears or selections or checks in the list
96
	 *  - <b>All</b>, select all
97
	 *  - <b>Invert</b>, invert the selection.
98
	 * @param TControl list control
99
	 * @param string selection method
100
	 * @param string|int the value or index to select/check.
101
	 * @param string selection control type, either 'check' or 'select'
102
	 */
103
	public function select($control, $method='Value', $value=null, $type=null)
104
	{
105
		$method = TPropertyValue::ensureEnum($method,
106
				'Value', 'Index', 'Clear', 'Indices', 'Values', 'All', 'Invert');
107
		$type = is_null($type) ? $this->getSelectionControlType($control) : $type;
108
		$total = $this->getSelectionControlIsListType($control) ? $control->getItemCount() : 1;
109
		$this->callClientFunction('Prado.Element.select',
110
				array($control, $type.$method, $value, $total));
111
	}
112
 
113
	private function getSelectionControlType($control)
114
	{
115
		if(is_string($control)) return 'check';
116
		if($control instanceof TCheckBoxList)
117
			return 'check';
118
		if($control instanceof TCheckBox)
119
			return 'check';
120
		return 'select';
121
	}
122
 
123
	private function getSelectionControlIsListType($control)
124
	{
125
		return $control instanceof TListControl;
126
	}
127
 
128
	/**
129
	 * Client script to click on an element. <b>This client-side function
130
	 * is unpredictable.</b>
131
	 * @param TControl control element or element id
132
	 */
133
	public function click($control)
134
	{
135
		$this->callClientFunction('Prado.Element.click', $control);
136
	}
137
 
138
	/**
139
	 * Client script to check or uncheck a checkbox or radio button.
140
	 * @param TControl control element or element id
141
	 * @param boolean check or uncheck the checkbox or radio button.
142
	 */
143
	public function check($checkbox, $checked=true)
144
	{
145
		$this->select($checkbox, "Value", $checked);
146
	}
147
 
148
	/**
149
	 * Raise the client side event (given by $eventName) on a particular element.
150
	 * @param TControl control element or element id
151
	 * @param string Event name, e.g. "click"
152
	 */
153
	public function raiseClientEvent($control, $eventName)
154
	{
155
		$this->callClientFunction('Event.fireEvent',
156
				array($control, strtolower($eventName)));
157
	}
158
 
159
	/**
160
	 * Sets the attribute of a particular control.
161
	 * @param TControl control element or element id
162
	 * @param string attribute name
163
	 * @param string attribute value
164
	 */
165
	public function setAttribute($control, $name, $value)
166
	{
167
        if ($control instanceof ISurroundable)
168
            $control=$control->getSurroundingTagID();
169
		$this->callClientFunction('Prado.Element.setAttribute',array($control, $name, $value));
170
	}
171
 
172
	/**
173
	 * Sets the options of a select input element.
174
	 * @param TControl control element or element id
175
	 * @param TCollection a list of new options
176
	 */
177
	public function setListItems($control, $items)
178
	{
179
		$options = array();
180
		foreach($items as $item)
181
		{
182
			if($item->getHasAttributes())
183
				$options[] =  array($item->getText(),$item->getValue(), $item->getAttributes()->itemAt('Group'));
184
			else
185
				$options[] = array($item->getText(),$item->getValue());
186
		}
187
		$this->callClientFunction('Prado.Element.setOptions', array($control, $options));
188
	}
189
 
190
	/**
191
	 * Shows an element by changing its CSS display style as empty.
192
	 * @param TControl control element or element id
193
	 */
194
	public function show($element)
195
	{
196
        if ($element instanceof ISurroundable)
197
            $element=$element->getSurroundingTagID();
198
		$this->callClientFunction('Element.show', $element);
199
	}
200
 
201
	/**
202
	 * Hides an element by changing its CSS display style to "none".
203
	 * @param TControl control element or element id
204
	 */
205
	public function hide($element)
206
	{
207
        if ($element instanceof ISurroundable)
208
            $element=$element->getSurroundingTagID();
209
		$this->callClientFunction('Element.hide', $element);
210
	}
211
 
212
	/**
213
	 * Toggles the visibility of the element.
214
	 * @param TControl control element or element id
215
	 * @param string visual effect, such as, 'appear' or 'slide' or 'blind'.
216
	 * @param array additional options.
217
	 */
218
	public function toggle($element, $effect=null, $options=array())
219
	{
220
        if ($element instanceof ISurroundable)
221
            $element=$element->getSurroundingTagID();
222
		$this->callClientFunction('Element.toggle', array($element,$effect,$options));
223
	}
224
 
225
	/**
226
	 * Removes an element from the HTML page.
227
	 * @param TControl control element or element id
228
	 */
229
	public function remove($element)
230
	{
231
        if ($element instanceof ISurroundable)
232
            $element=$element->getSurroundingTagID();
233
		$this->callClientFunction('Element.remove', $element);
234
	}
235
 
236
	public function addPostDataLoader($name)
237
	{
238
		$this->callClientFunction('Prado.CallbackRequest.addPostLoaders', $name);
239
	}
240
 
241
	/**
242
	 * Update the element's innerHTML with new content.
243
	 * @param TControl control element or element id
244
	 * @param TControl new HTML content, if content is of a TControl, the
245
	 * controls render method is called.
246
	 */
247
	public function update($element, $content)
248
	{
249
        if ($element instanceof ISurroundable)
250
            $element=$element->getSurroundingTagID();
251
		$this->replace($element, $content, 'Element.update');
252
	}
253
 
254
	/**
255
	 * Add a Css class name to the element.
256
	 * @param TControl control element or element id
257
	 * @param string CssClass name to add.
258
	 */
259
	public function addCssClass($element, $cssClass)
260
	{
261
        if ($element instanceof ISurroundable)
262
            $element=$element->getSurroundingTagID();
263
		$this->callClientFunction('Element.addClassName', array($element, $cssClass));
264
	}
265
 
266
	/**
267
	 * Remove a Css class name from the element.
268
	 * @param TControl control element or element id
269
	 * @param string CssClass name to remove.
270
	 */
271
	public function removeCssClass($element, $cssClass)
272
	{
273
        if ($element instanceof ISurroundable)
274
            $element=$element->getSurroundingTagID();
275
		$this->callClientFunction('Element.removeClassName', array($element, $cssClass));
276
	}
277
 
278
	/**
279
	 * Sets the CssClass of an element.
280
	 * @param TControl control element or element id
281
	 * @param string new CssClass name for the element.
282
	 */
283
	/*public function setCssClass($element, $cssClass)
284
	{
285
		$this->callClientFunction('Prado.Element.CssClass.set', array($element, $cssClass));
286
	}*/
287
 
288
	/**
289
	 * Scroll the top of the browser viewing area to the location of the
290
	 * element.
291
	 * @param TControl control element or element id
292
	 */
293
	public function scrollTo($element)
294
	{
295
        if ($element instanceof ISurroundable)
296
            $element=$element->getSurroundingTagID();
297
		$this->callClientFunction('Element.scrollTo', $element);
298
	}
299
 
300
	/**
301
	 * Focus on a particular element.
302
	 * @param TControl control element or element id.
303
	 */
304
	public function focus($element)
305
	{
306
		$this->callClientFunction('Prado.Element.focus', $element);
307
	}
308
 
309
	/**
310
	 * Sets the style of element. The style must be a key-value array where the
311
	 * key is the style property and the value is the style value.
312
	 * @param TControl control element or element id
313
	 * @param array list of key-value pairs as style property and style value.
314
	 */
315
	public function setStyle($element, $styles)
316
	{
317
        if ($element instanceof ISurroundable)
318
            $element=$element->getSurroundingTagID();
319
		$this->callClientFunction('Prado.Element.setStyle', array($element, $styles));
320
	}
321
 
322
	/**
323
	 * Append a HTML fragement to the element.
324
	 * @param TControl control element or element id
325
	 * @param string HTML fragement or the control to be rendered
326
	 */
327
	public function appendContent($element, $content)
328
	{
329
        if ($element instanceof ISurroundable)
330
            $element=$element->getSurroundingTagID();
331
		$this->replace($element, $content, 'Prado.Element.Insert.append');
332
	}
333
 
334
	/**
335
	 * Prepend a HTML fragement to the element.
336
	 * @param TControl control element or element id
337
	 * @param string HTML fragement or the control to be rendered
338
	 */
339
	public function prependContent($element, $content)
340
	{
341
        if ($element instanceof ISurroundable)
342
            $element=$element->getSurroundingTagID();
343
		$this->replace($element, $content, 'Prado.Element.Insert.prepend');
344
	}
345
 
346
	/**
347
	 * Insert a HTML fragement after the element.
348
	 * @param TControl control element or element id
349
	 * @param string HTML fragement or the control to be rendered
350
	 */
351
	public function insertContentAfter($element, $content)
352
	{
353
        if ($element instanceof ISurroundable)
354
            $element=$element->getSurroundingTagID();
355
		$this->replace($element, $content, 'Prado.Element.Insert.after');
356
	}
357
 
358
	/**
359
	 * Insert a HTML fragement in before the element.
360
	 * @param TControl control element or element id
361
	 * @param string HTML fragement or the control to be rendered
362
	 */
363
	public function insertContentBefore($element, $content)
364
	{
365
        if ($element instanceof ISurroundable)
366
            $element=$element->getSurroundingTagID();
367
		$this->replace($element, $content, 'Prado.Element.Insert.before');
368
	}
369
 
370
	/**
371
	 * Replace the content of an element with new content. The new content can
372
	 * be a string or a TControl component. If the <tt>content</tt> parameter is
373
	 * a TControl component, its rendered method will be called and its contents
374
	 * will be used for replacement.
375
	 * @param TControl control element or HTML element id.
376
	 * @param string HTML fragement or the control to be rendered
377
	 * @param string replacement method, default is to replace the outter
378
	 * html content.
379
	 * @param string provide a custom boundary.
380
	 * @see insertAbout
381
	 * @see insertBelow
382
	 * @see insertBefore
383
	 * @see insertAfter
384
	 */
385
	protected function replace($element, $content, $method="Element.replace", $boundary=null)
386
	{
387
		if($content instanceof TControl)
388
		{
389
			$boundary = $this->getRenderedContentBoundary($content);
390
			$content = null;
391
		}
392
		else if($content instanceof THtmlWriter)
393
		{
394
			$boundary = $this->getResponseContentBoundary($content);
395
			$content = null;
396
		}
397
 
398
		$this->callClientFunction('Prado.Element.replace',
399
					array($element, $method, $content, $boundary));
400
	}
401
 
402
	/**
403
	 * Replace the content of an element with new content contained in writer.
404
	 * @param TControl control element or HTML element id.
405
	 * @param string HTML fragement or the control to be rendered
406
	 */
407
	public function replaceContent($element,$content)
408
	{
409
        if ($element instanceof ISurroundable)
410
            $element=$element->getSurroundingTagID();
411
		$this->replace($element, $content);
412
	}
413
 
414
	/**
415
	 * Evaluate a block of javascript enclosed in a boundary.
416
	 * @param THtmlWriter writer for the content.
417
	 */
418
	public function evaluateScript($writer)
419
	{
420
		$this->replace(null, $writer, 'Prado.Element.evaluateScript');
421
	}
422
 
423
	/**
424
	 * Renders the control and return the content boundary from
425
	 * TCallbackResponseWriter. This method should only be used by framework
426
	 * component developers. The render() method is defered to be called in the
427
	 * TActivePageAdapter class.
428
	 * @param TControl control to be rendered on callback response.
429
	 * @return string the boundary for which the rendered content is wrapped.
430
	 */
431
	private function getRenderedContentBoundary($control)
432
	{
433
		$writer = $this->getResponse()->createHtmlWriter();
434
		$adapter = $control->getPage()->getAdapter();
435
		$adapter->registerControlToRender($control, $writer);
436
		return $writer->getWriter()->getBoundary();
437
	}
438
 
439
	/**
440
	 * @param THtmlWriter the writer responsible for rendering html content.
441
	 * @return string content boundary.
442
	 */
443
	private function getResponseContentBoundary($html)
444
	{
445
		if($html instanceof THtmlWriter)
446
		{
447
			if($html->getWriter() instanceof TCallbackResponseWriter)
448
				return $html->getWriter()->getBoundary();
449
		}
450
		return null;
451
	}
452
 
453
	/**
454
	 * Add a visual effect the element.
455
	 * @param string visual effect function name.
456
	 * @param TControl control element or element id
457
	 * @param array visual effect key-value pair options.
458
	 */
459
	public function visualEffect($type, $element, $options=null)
460
	{
461
        if ($element instanceof ISurroundable)
462
            $element=$element->getSurroundingTagID();
463
		$this->callClientFunction($type, array($element, $options));
464
	}
465
 
466
	/**
467
	 * Visual Effect: Gradually make the element appear.
468
	 * @param TControl control element or element id
469
	 * @param array visual effect key-value pair options.
470
	 */
471
	public function appear($element, $options=null)
472
	{
473
        if ($element instanceof ISurroundable)
474
            $element=$element->getSurroundingTagID();
475
		$this->visualEffect('Effect.Appear', $element, $options);
476
	}
477
 
478
	/**
479
	 * Visual Effect: Blind down.
480
	 * @param TControl control element or element id
481
	 * @param array visual effect key-value pair options.
482
	 */
483
	public function blindDown($element, $options=null)
484
	{
485
        if ($element instanceof ISurroundable)
486
            $element=$element->getSurroundingTagID();
487
		$this->visualEffect('Effect.BlindDown', $element, $options);
488
	}
489
 
490
	/**
491
	 * Visual Effect: Blind up.
492
	 * @param TControl control element or element id
493
	 * @param array visual effect key-value pair options.
494
	 */
495
	public function blindUp($element, $options=null)
496
	{
497
        if ($element instanceof ISurroundable)
498
            $element=$element->getSurroundingTagID();
499
		$this->visualEffect('Effect.BlindUp', $element, $options);
500
 
501
	}
502
 
503
	/**
504
	 * Visual Effect: Drop out.
505
	 * @param TControl control element or element id
506
	 * @param array visual effect key-value pair options.
507
	 */
508
	public function dropOut($element, $options=null)
509
	{
510
        if ($element instanceof ISurroundable)
511
            $element=$element->getSurroundingTagID();
512
		$this->visualEffect('Effect.DropOut', $element, $options);
513
	}
514
 
515
	/**
516
	 * Visual Effect: Gradually fade the element.
517
	 * @param TControl control element or element id
518
	 * @param array visual effect key-value pair options.
519
	 */
520
	public function fade($element, $options=null)
521
	{
522
        if ($element instanceof ISurroundable)
523
            $element=$element->getSurroundingTagID();
524
		$this->visualEffect('Effect.Fade', $element, $options);
525
	}
526
 
527
	/**
528
	 * Visual Effect: Fold.
529
	 * @param TControl control element or element id
530
	 * @param array visual effect key-value pair options.
531
	 */
532
	public function fold($element, $options = null)
533
	{
534
        if ($element instanceof ISurroundable)
535
            $element=$element->getSurroundingTagID();
536
		$this->visualEffect('Effect.Fold', $element, $options);
537
	}
538
 
539
	/**
540
	 * Visual Effect: Gradually make an element grow to a predetermined size.
541
	 * @param TControl control element or element id
542
	 * @param array visual effect key-value pair options.
543
	 */
544
	public function grow($element, $options=null)
545
	{
546
        if ($element instanceof ISurroundable)
547
            $element=$element->getSurroundingTagID();
548
		$this->visualEffect('Effect.Grow', $element, $options);
549
	}
550
 
551
	/**
552
	 * Visual Effect: Gradually grow and fade the element.
553
	 * @param TControl control element or element id
554
	 * @param array visual effect key-value pair options.
555
	 */
556
	public function puff($element, $options=null)
557
	{
558
        if ($element instanceof ISurroundable)
559
            $element=$element->getSurroundingTagID();
560
		$this->visualEffect('Effect.Puff', $element, $options);
561
	}
562
 
563
	/**
564
	 * Visual Effect: Pulsate.
565
	 * @param TControl control element or element id
566
	 * @param array visual effect key-value pair options.
567
	 */
568
	public function pulsate($element, $options=null)
569
	{
570
        if ($element instanceof ISurroundable)
571
            $element=$element->getSurroundingTagID();
572
		$this->visualEffect('Effect.Pulsate', $element, $options);
573
	}
574
 
575
	/**
576
	 * Visual Effect: Shake the element.
577
	 * @param TControl control element or element id
578
	 * @param array visual effect key-value pair options.
579
	 */
580
	public function shake($element, $options=null)
581
	{
582
        if ($element instanceof ISurroundable)
583
            $element=$element->getSurroundingTagID();
584
		$this->visualEffect('Effect.Shake', $element, $options);
585
	}
586
 
587
	/**
588
	 * Visual Effect: Shrink the element.
589
	 * @param TControl control element or element id
590
	 * @param array visual effect key-value pair options.
591
	 */
592
	public function shrink($element, $options=null)
593
	{
594
        if ($element instanceof ISurroundable)
595
            $element=$element->getSurroundingTagID();
596
		$this->visualEffect('Effect.Shrink', $element, $options);
597
	}
598
 
599
	/**
600
	 * Visual Effect: Slide down.
601
	 * @param TControl control element or element id
602
	 * @param array visual effect key-value pair options.
603
	 */
604
	public function slideDown($element, $options=null)
605
	{
606
        if ($element instanceof ISurroundable)
607
            $element=$element->getSurroundingTagID();
608
		$this->visualEffect('Effect.SlideDown', $element, $options);
609
	}
610
 
611
	/**
612
	 * Visual Effect: Side up.
613
	 * @param TControl control element or element id
614
	 * @param array visual effect key-value pair options.
615
	 */
616
	public function slideUp($element, $options=null)
617
	{
618
        if ($element instanceof ISurroundable)
619
            $element=$element->getSurroundingTagID();
620
		$this->visualEffect('Effect.SlideUp', $element, $options);
621
	}
622
 
623
	/**
624
	 * Visual Effect: Squish the element.
625
	 * @param TControl control element or element id
626
	 * @param array visual effect key-value pair options.
627
	 */
628
	public function squish($element, $options=null)
629
	{
630
        if ($element instanceof ISurroundable)
631
            $element=$element->getSurroundingTagID();
632
		$this->visualEffect('Effect.Squish', $element, $options);
633
	}
634
 
635
	/**
636
	 * Visual Effect: Switch Off effect.
637
	 * @param TControl control element or element id
638
	 * @param array visual effect key-value pair options.
639
	 */
640
	public function switchOff($element, $options=null)
641
	{
642
        if ($element instanceof ISurroundable)
643
            $element=$element->getSurroundingTagID();
644
		$this->visualEffect('Effect.SwitchOff', $element, $options);
645
	}
646
 
647
	/**
648
	 * Visual Effect: High light the element for about 2 seconds.
649
	 * @param TControl control element or element id
650
	 * @param array visual effect key-value pair options.
651
	 */
652
	public function highlight($element, $options=null)
653
	{
654
        if ($element instanceof ISurroundable)
655
            $element=$element->getSurroundingTagID();
656
		$this->visualEffect('Prado.Effect.Highlight', $element, $options);
657
	}
658
 
659
	/**
660
	 * Set the opacity on a html element or control.
661
	 * @param TControl control element or element id
662
	 * @param float opacity value between 1 and 0
663
	 */
664
	public function setOpacity($element, $value)
665
	{
666
        if ($element instanceof ISurroundable)
667
            $element=$element->getSurroundingTagID();
668
		$value = TPropertyValue::ensureFloat($value);
669
		$this->callClientFunction('Element.setOpacity', array($element,$value));
670
	}
671
}
672