1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TJuiSelectable class file. |
4
|
|
|
* |
5
|
|
|
* @author Fabio Bas <ctrlaltca[at]gmail[dot]com> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
8
|
|
|
* @package Prado\Web\UI\JuiControls |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\JuiControls; |
12
|
|
|
|
13
|
|
|
use Prado\Web\Javascripts\TJavaScript; |
14
|
|
|
use Prado\Web\Javascripts\TJavaScriptLiteral; |
15
|
|
|
use Prado\Web\UI\ActiveControls\ICallbackEventHandler; |
16
|
|
|
use Prado\Web\UI\ActiveControls\TActivePanel; |
17
|
|
|
use Prado\Web\UI\ActiveControls\TCallbackEventParameter; |
18
|
|
|
use Prado\Web\UI\TTemplate; |
19
|
|
|
use Prado\Web\UI\WebControls\TRepeater; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TJuiSelectable class. |
23
|
|
|
* |
24
|
|
|
* TJuiSelectable is an extension to {@link TActivePanel} based on jQuery-UI's |
25
|
|
|
* {@link http://jqueryui.com/selectable/ Selectable} interaction. |
26
|
|
|
* TJuiSelectable can be feed a {@link setDataSource DataSource} and will interally |
27
|
|
|
* render a {@link TRepeater} that displays items in an unordered list. |
28
|
|
|
* Items can be selected by clicking on them, individually or in a group. |
29
|
|
|
* |
30
|
|
|
* <code> |
31
|
|
|
* <style> |
32
|
|
|
* .ui-selecting { background: #FECA40; } |
33
|
|
|
* .ui-selected { background: #F39814; color: white; } |
34
|
|
|
* </style> |
35
|
|
|
* <com:TJuiSelectable ID="repeater1" /> |
36
|
|
|
* </code> |
37
|
|
|
* |
38
|
|
|
* <code> |
39
|
|
|
* $this->repeater1->DataSource=array('home', 'office', 'car', 'boat', 'plane'); |
40
|
|
|
* $this->repeater1->dataBind(); |
41
|
|
|
* </code> |
42
|
|
|
* @author Fabio Bas <ctrlaltca[at]gmail[dot]com> |
43
|
|
|
* @package Prado\Web\UI\JuiControls |
44
|
|
|
* @since 3.3 |
45
|
|
|
*/ |
46
|
|
|
class TJuiSelectable extends TActivePanel implements IJuiOptions, ICallbackEventHandler |
47
|
|
|
{ |
48
|
|
|
protected $_options; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates a new callback control, sets the adapter to |
52
|
|
|
* TActiveControlAdapter. If you override this class, be sure to set the |
53
|
|
|
* adapter appropriately by, for example, by calling this constructor. |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
parent::__construct(); |
58
|
|
|
$this->setAdapter(new TJuiControlAdapter($this)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string the name of the jQueryUI widget method |
63
|
|
|
*/ |
64
|
|
|
public function getWidget() |
65
|
|
|
{ |
66
|
|
|
return 'selectable'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string the clientid of the jQueryUI widget element |
71
|
|
|
*/ |
72
|
|
|
public function getWidgetID() |
73
|
|
|
{ |
74
|
|
|
return $this->getClientID() . '_0'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Object containing defined javascript options |
79
|
|
|
* @return TJuiControlOptions |
80
|
|
|
*/ |
81
|
|
|
public function getOptions() |
82
|
|
|
{ |
83
|
|
|
if (($options = $this->getViewState('JuiOptions')) === null) { |
84
|
|
|
$options = new TJuiControlOptions($this); |
85
|
|
|
$this->setViewState('JuiOptions', $options); |
86
|
|
|
} |
87
|
|
|
return $options; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Array containing valid javascript options |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getValidOptions() |
95
|
|
|
{ |
96
|
|
|
return ['appendTo', 'autoRefresh', 'cancel', 'delay', 'disabled', 'distance', 'filter', 'tolerance']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Array containing valid javascript events |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getValidEvents() |
104
|
|
|
{ |
105
|
|
|
return ['create', 'selected', 'selecting', 'start', 'stop', 'unselected', 'unselecting']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array list of callback options. |
110
|
|
|
*/ |
111
|
|
|
protected function getPostBackOptions() |
112
|
|
|
{ |
113
|
|
|
$options = $this->getOptions()->toArray(); |
114
|
|
|
// overload the "OnStop" event to add information about the current selected items |
115
|
|
|
if (isset($options['stop'])) { |
116
|
|
|
$options['stop'] = new TJavaScriptLiteral('function( event, ui ) { ui.index = new Array(); jQuery(\'#' . $this->getClientID() . ' .ui-selected\').each(function(idx, item){ ui.index.push(item.id) }); Prado.JuiCallback(' . TJavaScript::encode($this->getUniqueID()) . ', \'stop\', event, ui, this); }'); |
117
|
|
|
} |
118
|
|
|
return $options; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Ensure that the ID attribute is rendered and registers the javascript code |
123
|
|
|
* for initializing the active control. |
124
|
|
|
* @param mixed $writer |
125
|
|
|
*/ |
126
|
|
|
protected function addAttributesToRender($writer) |
127
|
|
|
{ |
128
|
|
|
parent::addAttributesToRender($writer); |
129
|
|
|
$writer->addAttribute('id', $this->getClientID()); |
130
|
|
|
$options = TJavaScript::encode($this->getPostBackOptions()); |
131
|
|
|
$cs = $this->getPage()->getClientScript(); |
132
|
|
|
$code = "jQuery('#" . $this->getWidgetID() . "')." . $this->getWidget() . "(" . $options . ");"; |
133
|
|
|
$cs->registerEndScript(sprintf('%08X', crc32($code)), $code); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Raises callback event. This method is required by the {@link ICallbackEventHandler} |
138
|
|
|
* interface. |
139
|
|
|
* @param TCallbackEventParameter $param the parameter associated with the callback event |
140
|
|
|
*/ |
141
|
|
|
public function raiseCallbackEvent($param) |
142
|
|
|
{ |
143
|
|
|
$this->getOptions()->raiseCallbackEvent($param); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Raises the OnCreate event |
148
|
|
|
* @param object $params event parameters |
149
|
|
|
*/ |
150
|
|
|
public function onCreate($params) |
151
|
|
|
{ |
152
|
|
|
$this->raiseEvent('OnCreate', $this, $params); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Raises the OnSelected event |
157
|
|
|
* @param object $params event parameters |
158
|
|
|
*/ |
159
|
|
|
public function onSelected($params) |
160
|
|
|
{ |
161
|
|
|
$this->raiseEvent('OnSelected', $this, $params); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Raises the OnSelecting event |
166
|
|
|
* @param object $params event parameters |
167
|
|
|
*/ |
168
|
|
|
public function onSelecting($params) |
169
|
|
|
{ |
170
|
|
|
$this->raiseEvent('OnSelecting', $this, $params); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Raises the OnStart event |
175
|
|
|
* @param object $params event parameters |
176
|
|
|
*/ |
177
|
|
|
public function onStart($params) |
178
|
|
|
{ |
179
|
|
|
$this->raiseEvent('OnStart', $this, $params); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Raises the OnStop event |
184
|
|
|
* @param object $params event parameters |
185
|
|
|
*/ |
186
|
|
|
public function onStop($params) |
187
|
|
|
{ |
188
|
|
|
$this->raiseEvent('OnStop', $this, $params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Raises the OnUnselected event |
193
|
|
|
* @param object $params event parameters |
194
|
|
|
*/ |
195
|
|
|
public function onUnselected($params) |
196
|
|
|
{ |
197
|
|
|
$this->raiseEvent('OnUnselected', $this, $params); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Raises the OnUnselecting event |
202
|
|
|
* @param object $params event parameters |
203
|
|
|
*/ |
204
|
|
|
public function onUnselecting($params) |
205
|
|
|
{ |
206
|
|
|
$this->raiseEvent('OnUnselecting', $this, $params); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @var ITemplate template for repeater items |
|
|
|
|
211
|
|
|
*/ |
212
|
|
|
private $_repeater; |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param array $data data source for Selectables. |
216
|
|
|
*/ |
217
|
|
|
public function setDataSource($data) |
218
|
|
|
{ |
219
|
|
|
$this->getSelectables()->setDataSource($data); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return TRepeater suggestion list repeater |
224
|
|
|
*/ |
225
|
|
|
public function getSelectables() |
226
|
|
|
{ |
227
|
|
|
if ($this->_repeater === null) { |
228
|
|
|
$this->_repeater = $this->createRepeater(); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
return $this->_repeater; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return TRepeater new instance of TRepater to render the list of Selectables. |
235
|
|
|
*/ |
236
|
|
|
protected function createRepeater() |
237
|
|
|
{ |
238
|
|
|
$repeater = new TRepeater; |
239
|
|
|
$repeater->setHeaderTemplate(new TJuiSelectableTemplate('<ul id="' . $this->getWidgetID() . '">')); |
240
|
|
|
$repeater->setFooterTemplate(new TJuiSelectableTemplate('</ul>')); |
241
|
|
|
$repeater->setItemTemplate(new TTemplate('<li id="<%# $this->ItemIndex %>"><%# $this->Data %></li>', null)); |
242
|
|
|
$repeater->setEmptyTemplate(new TJuiSelectableTemplate('<ul></ul>')); |
243
|
|
|
$this->getControls()->add($repeater); |
244
|
|
|
return $repeater; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|