summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TTimeTriggeredCallback.php
blob: 10923d03bbc81648c03a4216020088e027ff05a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
 * TTimeTriggeredCallback class file.
 *
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Web.UI.ActiveControls
 */

/**
 * Load active callback control.
 */
Prado::using('System.Web.UI.ActiveControls.TCallback');

/**
 * TTimeTriggeredCallback class.
 *
 * TTimeTriggeredCallback sends callback request every {@link setInterval Interval} seconds.
 * Upon each callback request, the {@link onCallback OnCallback} event is raised.
 *
 * The timer can be started by calling {@link startTimer()} and stopped using
 * {@link stopTimer()}. The timer can be automatically started when
 * {@link setStartTimerOnLoad StartTimerOnLoad} is true.
 *
 * The intervals between each request can be increased when the browser is inactive
 * by changing the {@link setDecayRate DecayRate} to a positive number. The
 * default decay rate, {@link setDecayType DecayType}, is linear and can be changed to
 * 'Exponential', 'Linear', 'Quadratic' or 'Cubic'.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TTimeTriggeredCallback extends TCallback
{
	/**
	 * @return float seconds between callback requests. Default is 1 second.
	 */
	public function getInterval()
	{
		return $this->getViewState('Interval', 1);
	}

	/**
	 * @param float seconds between callback requests, must be a positive number, default is 1 second.
	 */
	public function setInterval($value)
	{
		$interval = TPropertyValue::ensureFloat($value);
		if($interval <= 0)
			throw new TConfigurationException('callback_interval_be_positive', $this->getID());
		$this->setViewState('Interval', $interval, 1);
	}

	/**
	 * Gets the decay rate between callbacks. Default is 0;
	 * @return float decay rate between callbacks.
	 */
	public function getDecayRate()
	{
		return $this->getViewState('Decay', 0);
	}

	/**
	 * Sets the decay rate between callback. Default is 0;
	 * @param float decay rate between callbacks.
	 */
	public function setDecayRate($value)
	{
		$decay = TPropertyValue::ensureFloat($value);
		if($decay < 0)
			throw new TConfigurationException('callback_decay_be_not_negative', $this->getID());
		$this->setViewState('Decay', $decay);
	}

	/**
	 * @param string Decay type, allows 'Exponential', 'Linear', 'Quadratic' and 'Cubic'. Default is 'Linear'.
	 */
	public function setDecayType($value)
	{
		$this->setViewState('DecayType', TPropertyValue::ensureEnum($value,
			'Exponential', 'Linear', 'Quadratic', 'Cubic'), 'Linear');
	}

	/**
	 * @return string decay type, default is 'Linear', valid types are 'Exponential', 'Linear', 'Quadratic' and 'Cubic'.
	 */
	public function getDecayType()
	{
		return $this->getViewState('DecayType', 'Linear');
	}

	/**
	 * Registers the javascript code to start the timer.
	 */
	public function startTimer()
	{
		$id = $this->getClientID();
		$code = "Prado.WebUI.TTimeTriggeredCallback.start('{$id}');";
		$cs = $this->getPage()->getClientScript();
		$cs->registerEndScript("{$id}:start", $code);
	}

	/**
	 * Registers the javascript code to stop the timer.
	 */
	public function stopTimer()
	{
		$id = $this->getClientID();
		$code = "Prado.WebUI.TTimeTriggeredCallback.stop('{$id}');";
		$cs = $this->getPage()->getClientScript();
		$cs->registerEndScript("{$id}:stop", $code);
	}

	/**
	 * @param boolean true to start the timer when page loads.
	 */
	public function setStartTimerOnLoad($value)
	{
		$this->setViewState('StartTimerOnLoad', 
				TPropertyValue::ensureBoolean($value), false);
	}

	/**
	 * @return boolean true to start the timer when page loads.
	 */
	public function getStartTimerOnLoad()
	{
		return $this->getViewState('StartTimerOnLoad', false);
	}

	/**
	 * @return array list of timer options for client-side.
	 */
	protected function getTriggerOptions()
	{
		$options['ID'] = $this->getClientID();
		$options['EventTarget']= $this->getUniqueID();
		$options['Interval'] = $this->getInterval();
		$options['DecayRate'] = $this->getDecayRate();
		$options['DecayType'] = $this->getDecayType();
		return $options;
	}

	/**
	 * Registers the javascript code for initializing the active control.
	 * @param THtmlWriter the renderer.
	 */
	public function render($writer)
	{
		parent::render($writer);
		$this->getActiveControl()->registerCallbackClientScript(
			$this->getClientClassName(), $this->getTriggerOptions());
		if($this->getStartTimerOnLoad())
			$this->startTimer();
	}

	/**
	 * @return string corresponding javascript class name for TTimeTriggeredCallback.
	 */
	protected function getClientClassName()
	{
		return 'Prado.WebUI.TTimeTriggeredCallback';
	}
}

?>