summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TDropContainer.php
blob: 00ebf46118602854fff231dc7ec71ca424ac2792 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
 * TDropContainer class file
 * 
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license http://www.pradosoft.com/license
 * @license http://www.pradosoft.com/license
 * @package System.Web.UI.ActiveControls
 */

/**
 * Load active control adapter.
 */
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
/**
 * Load active panel.
 */
Prado::using('System.Web.UI.ActiveControls.TActivePanel');


/**
 * TDropContainer is a panel where TDraggable controls can be dropped.
 * When a TDraggable component is dropped into a TDropContainer, the {@link OnDrop OnDrop} event is raised.
 * The {@link TDropContainerEventParameter} param will contain the dropped control. 
 * 
 * Properties :
 * 
 * <b>{@link setAcceptCssClass AcceptCssClass}</b> : a coma delimited classname of elements that the drop container can accept.
 * <b>{@link setHoverCssClass HoverCssClass}</b>: CSS classname of the container when a draggable element hovers over the container.
 * 
 * Events:
 * 
 * <b>{@link OnDrop OnDrop}</b> : raised when a TDraggable control is dropped. The dropped control id is encapsulated in the event parameter,
 * as well as mouse coordinates and key modifiers status
 *
 * 
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license http://www.pradosoft.com/license
 * @package System.Web.UI.ActiveControls
 */
class TDropContainer extends TPanel implements IActiveControl, ICallbackEventHandler 
{	
	private $_container=null;
	
	/**
	 * Creates a new callback control, sets the adapter to
	 * TActiveControlAdapter. If you override this class, be sure to set the
	 * adapter appropriately by, for example, by calling this constructor.
	 */
	public function __construct()
	{
		parent::__construct();
		$this->setAdapter(new TActiveControlAdapter($this));
	}

	/**
	 * @return TBaseActiveControl standard active control options.
	 */
	public function getActiveControl()
	{
		return $this->getAdapter()->getBaseActiveControl();
	}

	/**
	 * @return TCallbackClientSide client side request options.
	 */
	public function getClientSide()
	{
		return $this->getAdapter()->getBaseActiveControl()->getClientSide();
	}

	/**
	 * Gets the Css class name that this container can accept.
	 * @return string
	 */
	public function getAcceptCssClass()
	{
		return $this->getViewState('Accepts', '');
	}

	/**
	 * Sets the Css class name that this container can accept.
	 * @param string comma delimited css class names.
	 */
	public function setAcceptCssClass($value)
	{
		$this->setViewState('Accepts', TPropertyValue::ensureArray($value), '');
	}
	
	/**
	 * Sets the Css class name used when a draggble element is hovering
	 * over this container.
	 * @param string css class name during draggable hover.
	 */
	public function setHoverCssClass($value)
	{
		$this->setViewState('HoverClass', $value, '');
	}

	/**
	 * Gets the Css class name used when a draggble element is hovering
	 * over this container.
	 * @return string css class name during draggable hover.
	 */
	public function getHoverCssClass()
	{
		return $this->getViewState('HoverClass', '');
	}
	
	
	/**
	 * Raises callback event. This method is required bu {@link ICallbackEventHandler}
	 * interface.
	 * It raises the {@link onDrop OnDrop} event, then, the {@link onCallback OnCallback} event
	 * This method is mainly used by framework and control developers.
	 * @param TCallbackEventParameter the parameter associated with the callback event
	 */
	public function raiseCallbackEvent($param)
	{
		$this->onDrop($param->getCallbackParameter());
		$this->onCallback($param);
	}
	
	/**
	 * Raises the onDrop event. 
	 * The drop parameters are encapsulated into a {@link TDropContainerEventParameter}
	 * 
	 * @param object $dropControlId
	 */
	public function onDrop ($dropParams)
	{
		$this->raiseEvent('OnDrop', $this, new TDropContainerEventParameter ($dropParams));
		
	}
	
	/**
	 * This method is invoked when a callback is requested. The method raises
	 * 'OnCallback' event to fire up the event handlers. If you override this
	 * method, be sure to call the parent implementation so that the event
	 * handler can be invoked.
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
	 */
	public function onCallback($param)
	{
		$this->raiseEvent('OnCallback', $this, $param);
	}
	
	/**
	 * Gets the post back options for this textbox.
	 * @return array
	 */
	protected function getPostBackOptions()
	{
		$options['ID'] = $this->getClientID();
		$options['EventTarget'] = $this->getUniqueID();

		$options['accept'] = $this->getAcceptCssClass();
		$options['hoverclass'] = $this->getHoverCssClass();
		return $options;
	}
	
	/**
	 * Gets the name of the javascript class responsible for performing postback for this control.
	 * This method overrides the parent implementation.
	 * @return string the javascript class name
	 */
	protected function getClientClassName()
	{
		return 'Prado.WebUI.DropContainer';
	}	

	/**
	 * Registers clientscripts
	 *
	 * This method overrides the parent implementation and is invoked before render.
	 * @param mixed event parameter
	 */
	public function onPreRender($param)
	{
		parent::onPreRender($param);
	}

	/**
	 * Ensure that the ID attribute is rendered and registers the javascript code
	 * for initializing the active control.
	 */
	protected function addAttributesToRender($writer)
	{
		parent::addAttributesToRender($writer);
		$writer->addAttribute('id',$this->getClientID());

		$this->getPage()->getClientScript()->registerPradoScript('dragdrop');
		$this->getActiveControl()->registerCallbackClientScript(
			$this->getClientClassName(), $this->getPostBackOptions());
	}
	
	/**
	 * Creates child control
	 * Override parent implementation to create a container which will contain all
	 * child controls. This container will be a TActivePanel, in order to allow user
	 * to update its content on callback.
	 */
	public function createChildControls ()
	{
		if ($this->_container===null)
		{
			$this->_container=Prado::CreateComponent('System.Web.UI.ActiveControls.TActivePanel');
			$this->_container->setId($this->getId(false).'_content');
			parent::getControls()->add($this->_container);
		}
	}
	
	/**
	 * Override parent implementation to return the container control collection.
	 *
	 * @return TControlCollection
	 */
	public function getControls()
	{
		$this->ensureChildControls();
		return $this->_container->getControls();
	}
	
	/**
	 * Renders and replaces the panel's content on the client-side.
	 * When render() is called before the OnPreRender event, such as when render()
	 * is called during a callback event handler, the rendering
	 * is defered until OnPreRender event is raised.
	 * @param THtmlWriter html writer
	 */
	public function render ($writer)
	{
		if($this->getHasPreRendered())
		{
			parent::render($writer);
			if($this->getActiveControl()->canUpdateClientSide())
				$this->getPage()->getCallbackClient()->replaceContent($this->_container,$writer);
		}
		else
		{
			$this->getPage()->getAdapter()->registerControlToRender($this->_container,$writer);
		}
	}
			
}

/**
 * TDropContainerEventParameter class
 * 
 * TDropContainerEventParameter encapsulate the parameter
 * data for <b>OnDrop</b> event of TDropContainer components
 * 
 * @author Christophe BOULAIN (Christophe.Boulain@ceram.fr)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license http://www.pradosoft.com/license
 * @package System.Web.UI.ActiveControls
 */
class TDropContainerEventParameter extends TEventParameter
{
	private $_dragElementId;
	private $_screenX;
	private $_screenY;
	private $_offsetX;
	private $_offsetY;
	private $_clientX;
	private $_clientY;
	private $_shiftKey;
	private $_ctrlKey;
	private $_altKey;

	public function __construct($dropParams)
	{
		$this->_dragElementId = $dropParams->DragElementID;
		$this->_screenX = $dropParams->ScreenX;
		$this->_screenY = $dropParams->ScreenY;
		$this->_offsetX = isset($dropParams->OffsetX) ? $dropParams->OffsetX : false;
		$this->_offsetY = isset($dropParams->OffsetY) ? $dropParams->OffsetY : false;
		$this->_clientX = $dropParams->ClientX;
		$this->_clientY = $dropParams->ClientY;
		$this->_shiftKey = TPropertyValue::ensureBoolean($dropParams->ShiftKey);
		$this->_ctrlKey = TPropertyValue::ensureBoolean($dropParams->CtrlKey);
		$this->_altKey = TPropertyValue::ensureBoolean($dropParams->AltKey);
	}

	public function getDragElementId()			{ return $this->_dragElementId; }
	public function getScreenX()				{ return $this->_screenX; }
	public function getScreenY()				{ return $this->_screenY; }
	public function getOffsetX()				{ return $this->_offsetX; }
	public function geOffsetY()					{ return $this->_offsetY; }
	public function getClientX()				{ return $this->_clientX; }
	public function getClientY()				{ return $this->_clientY; }
	public function getShiftKey()				{ return $this->_shiftKey; }
	public function getCtrlKey()				{ return $this->_ctrlKey; }
	public function getAltKey()					{ return $this->_altKey; }

	/**
	 * GetDroppedControl
	 *
	 * Compatibility method to get the dropped control
	 * @return TControl dropped control, or null if not found
	 */
	 public function getDroppedControl ()
	 {
		 $control=null;
		 $service=prado::getApplication()->getService();
		 if ($service instanceof TPageService)
		 {
			// Find the control
			// Warning, this will not work if you have a '_' in your control Id !
			$dropControlId=str_replace(TControl::CLIENT_ID_SEPARATOR,TControl::ID_SEPARATOR,$this->_dragElementId);
			$control=$service->getRequestedPage()->findControl($dropControlId);
		 }
		 return $control;
	 }
}