summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/ActiveControls/TDraggable.php
blob: 830200e33a73062e34b3ca2fa54851fb5f502c07 (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
<?php
/**
 * TDraggable class file
 *
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 */

/**
 * TDraggable is a control which can be dragged
 *
 * Warning: this class is deprecatd and will be removed in a future release.
 * We suggest you to investigate using {@link TJuiDraggable} instead.
 *
 * This control will make "draggable" control.
 * Properties :
 *
 * <b>{@link setGhosting Ghosting}</b> : If set to "Ghosting" or "True", the dragged element will be cloned, and the clone will be dragged.
 * If set to "SuperGhosting", the element will be cloned, and attached to body, so it can be dragged outside of its parent.
 * If set to "None" of "False" (default), the element itself is dragged
 * <b>{@link setRevert Revert}</b>: Set to True if you want your dragged element to revert to its initial position if not dropped on a valid area.
 * <b>{@link setConstraint Constraint}</b>: Set this to Horizontal or Vertical if you want to constraint your move in one direction.
 * <b>{@link setHandle Handle}</b>:
 *
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 * @deprecated Use TJuiDraggable instead
 */
class TDraggable extends TPanel
{
	/**
	 * Set the handle id or css class
	 * @param string
	 */
	public function setHandle ($value)
	{
		$this->setViewState('DragHandle', TPropertyValue::ensureString($value), null);
	}

	/**
	 * Get the handle id or css class
	 * @return string
	 */
	public function getHandle ()
	{
		return $this->getViewState('DragHandle', null);
	}

	/**
	 * Determine if draggable element should revert to it orginal position
	 * upon release in an non-droppable container.
	 * Since 3.2, Revert property can be set to one of the value of {@link TDraggableRevertOption} enumeration.
	 *   o 'True' or 'Revert' : The draggable will revert to it's original position
	 *   o 'False' or 'None' : The draggable won't revert to it's original position
	 *   o 'Failure' : The draggable will only revert if it's dropped on a non droppable area
	 * @return TDraggableRevertOption true to revert
	 */
	public function getRevert()
	{
		return $this->getViewState('Revert', TDraggableRevertOptions::Revert);
	}

	/**
	 * Sets whether the draggable element should revert to it orginal position
	 * upon release in an non-droppable container.
	 * Since 3.2, Revert property can be set to one of the value of {@link TDraggableRevertOption} enumeration.
	 *   o 'True' or 'Revert' : The draggable will revert to it's original position
	 *   o 'False' or 'None' : The draggable won't revert to it's original position
	 *   o 'Failure' : The draggable will only revert if it's dropped on a non droppable area
	 * @param boolean true to revert
	 */
	public function setRevert($value)
	{
		if (strcasecmp($value,'true')==0 || $value===true)
			$value=TDraggableRevertOptions::Revert;
		elseif (strcasecmp($value,'false')==0 || $value===false)
			$value=TDraggableRevertOptions::None;
		$this->setViewState('Revert', TPropertyValue::ensureEnum($value, 'TDraggableRevertOptions'), true);
	}

	/**
	 * Determine if the element should be cloned when dragged
	 * If true, Clones the element and drags the clone, leaving the original in place until the clone is dropped.
	 * Defaults to false
	 * 	Since 3.2, Ghosting can be set to one of the value of {@link TDraggableGhostingOptions} enumeration.
	 *  o "True" or "Ghosting" means standard pre-3.2 ghosting mechanism
	 *  o "SuperGhosting" use the Superghosting patch by Christopher Williams, which allow elements to be dragged from an
	 *    scrollable list
	 *  o "False" or "None" means no Ghosting options
	 *
	 * @return TDraggableGhostingOption to clone the element
	 */
	public function getGhosting ()
	{
		return $this->getViewState('Ghosting', TDraggableGhostingOptions::None);
	}

	/**
	 * Sets wether the element should be cloned when dragged
	 * If true, Clones the element and drags the clone, leaving the original in place until the clone is dropped.
	 * Defaults to false
	 *
	 * Since 3.2, Ghosting can be set to one of the value of {@link TDraggableGhostingOptions} enumeration.
	 *  o "True" or "Ghosting" means standard pre-3.2 ghosting mechanism
	 *  o "SuperGhosting" use the Superghosting patch by Christopher Williams, which allow elements to be dragged from an
	 *    scrollable list
	 *  o "False" or "None" means no Ghosting options
	 *
	 */
	public function setGhosting ($value)
	{
		if (strcasecmp($value,'true')==0 || $value===true)
			$value=TDraggableGhostingOptions::Ghosting;
		elseif (strcasecmp($value,'false')==0 || $value===false)
			$value=TDraggableGhostingOptions::None;
		$this->setViewState('Ghosting', TPropertyValue::ensureEnum($value, 'TDraggableGhostingOptions'), TDraggableGhostingOptions::None);
	}

	/**
	 * Determine if the element should be constrainted in one direction or not
	 * @return CDraggableConstraint
	 */
	public function getConstraint()
	{
		return $this->getViewState('Constraint', TDraggableConstraint::None);
	}

	/**
	 * Set wether the element should be constrainted in one direction
	 * @param CDraggableConstraint
	 */
	public function setConstraint($value)
	{
		$this->setViewState('Constraint', TPropertyValue::ensureEnum($value, 'TDraggableConstraint'), TDraggableConstraint::None);
	}

	/**
	 * 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);

		$cs=$this->getPage()->getClientScript();
		if ($this->getGhosting()==TDraggableGhostingOptions::SuperGhosting)
			$cs->registerPradoScript('dragdropextra');
		else
			$cs->registerPradoScript('dragdrop');
		$writer->addAttribute('id',$this->getClientID());
		$options=TJavascript::encode($this->getPostBackOptions());
		$class=$this->getClientClassName();
		$code="new {$class}('{$this->getClientId()}', {$options}) ";
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
	}

	/**
	 * 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 'Draggable';
	}

	/**
	 * Gets the post back options for this textbox.
	 * @return array
	 */
	protected function getPostBackOptions()
	{
		$options['ID'] = $this->getClientID();

		if (($handle=$this->getHandle())!== null) $options['handle']=$handle;
		if (($revert=$this->getRevert())===TDraggableRevertOptions::None)
			$options['revert']=false;
		elseif ($revert==TDraggableRevertOptions::Revert)
			$options['revert']=true;
		else
			$options['revert']=strtolower($revert);
		if (($constraint=$this->getConstraint())!==TDraggableConstraint::None) $options['constraint']=strtolower($constraint);
		switch ($this->getGhosting())
		{
			case TDraggableGhostingOptions::SuperGhosting:
				$options['superghosting']=true;
				break;
			case TDraggableGhostingOptions::Ghosting:
				$options['ghosting']=true;
				break;
		}

		return $options;
	}

}

/**
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 */
class TDraggableConstraint extends TEnumerable
{
	const None='None';
	const Horizontal='Horizontal';
	const Vertical='Vertical';
}

/**
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 */
class TDraggableGhostingOptions extends TEnumerable
{
	const None='None';
	const Ghosting='Ghosting';
	const SuperGhosting='SuperGhosting';
}

/**
 * @author Christophe BOULAIN (Christophe.Boulain@gmail.com)
 * @copyright Copyright &copy; 2008, PradoSoft
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 */
class TDraggableRevertOptions extends TEnumerable
{
	const None='None';
	const Revert='Revert';
	const Failure='Failure';
}