]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/ActiveControls/TTimeTriggeredCallback.php
bc250521400a5a0c079d26fe7ae8241147f06314
[bacula/bacula] / gui / baculum / framework / Web / UI / ActiveControls / TTimeTriggeredCallback.php
1 <?php
2 /**
3  * TTimeTriggeredCallback class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Web.UI.ActiveControls
10  */
11
12 /**
13  * Load active callback control.
14  */
15 Prado::using('System.Web.UI.ActiveControls.TCallback');
16
17 /**
18  * TTimeTriggeredCallback class.
19  *
20  * TTimeTriggeredCallback sends callback request every {@link setInterval Interval} seconds.
21  * Upon each callback request, the {@link onCallback OnCallback} event is raised.
22  *
23  * The timer can be started by calling {@link startTimer()} and stopped using
24  * {@link stopTimer()}. The timer can be automatically started when
25  * {@link setStartTimerOnLoad StartTimerOnLoad} is true.
26  *
27  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
28  * @package System.Web.UI.ActiveControls
29  * @since 3.1
30  */
31 class TTimeTriggeredCallback extends TCallback
32 {
33         /**
34          * @return float seconds between callback requests. Default is 1 second.
35          */
36         public function getInterval()
37         {
38                 return $this->getViewState('Interval', 1);
39         }
40
41         /**
42          * @param float seconds between callback requests, must be a positive number, default is 1 second.
43          */
44         public function setInterval($value)
45         {
46                 $interval = TPropertyValue::ensureFloat($value);
47                 if($interval <= 0)
48                         throw new TConfigurationException('callback_interval_be_positive', $this->getID());
49                 $this->setViewState('Interval', $interval, 1);
50                 if ($this->getActiveControl()->canUpdateClientSide()){
51                         $client = $this->getPage()->getCallbackClient();
52                         $client->callClientFunction('Prado.WebUI.TTimeTriggeredCallback.setTimerInterval', array($this, $interval));
53                 }
54         }
55
56         /**
57          * Registers the javascript code to start the timer.
58          */
59         public function startTimer()
60         {
61                 $client = $this->getPage()->getCallbackClient();
62                 $client->callClientFunction('Prado.WebUI.TTimeTriggeredCallback.start', array($this));
63         }
64
65         /**
66          * Registers the javascript code to stop the timer.
67          */
68         public function stopTimer()
69         {
70                 $client = $this->getPage()->getCallbackClient();
71                 $client->callClientFunction('Prado.WebUI.TTimeTriggeredCallback.stop', array($this));
72         }
73
74         /**
75          * @param boolean true to start the timer when page loads.
76          */
77         public function setStartTimerOnLoad($value)
78         {
79                 $this->setViewState('StartTimerOnLoad',
80                                 TPropertyValue::ensureBoolean($value), false);
81         }
82
83         /**
84          * @return boolean true to start the timer when page loads.
85          */
86         public function getStartTimerOnLoad()
87         {
88                 return $this->getViewState('StartTimerOnLoad', false);
89         }
90
91         /**
92          * @return array list of timer options for client-side.
93          */
94         protected function getTriggerOptions()
95         {
96                 $options['ID'] = $this->getClientID();
97                 $options['EventTarget']= $this->getUniqueID();
98                 $options['Interval'] = $this->getInterval();
99                 return $options;
100         }
101
102         /**
103          * Registers the javascript code for initializing the active control.
104          * @param THtmlWriter the renderer.
105          */
106         public function render($writer)
107         {
108                 parent::render($writer);
109                 $this->getActiveControl()->registerCallbackClientScript(
110                         $this->getClientClassName(), $this->getTriggerOptions());
111                 if($this->getStartTimerOnLoad()){
112                         $id = $this->getClientID();
113                         $code = "Prado.WebUI.TTimeTriggeredCallback.start('{$id}');";
114                         $cs = $this->getPage()->getClientScript();
115                         $cs->registerEndScript("{$id}:start", $code);
116                 }
117         }
118
119         /**
120          * @return string corresponding javascript class name for TTimeTriggeredCallback.
121          */
122         protected function getClientClassName()
123         {
124                 return 'Prado.WebUI.TTimeTriggeredCallback';
125         }
126 }