Completed
Push — namespace2 ( 8a6673...791eac )
by Fabio
08:25
created

TJuiDialog::getPostBackOptions()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 0
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * TJuiDialog class file.
4
 *
5
 * @author  David Otto <ottodavid[at]gmx[dot]net>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2013-2015 PradoSoft
8
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9
 * @package Prado\Web\UI\JuiControls
10
 */
11
12
namespace Prado\Web\UI\JuiControls;
13
14
use Prado\Exceptions\TNotSupportedException;
15
use Prado\Web\Javascripts\TJavaScript;
16
use Prado\Web\UI\ActiveControls\ICallbackEventHandler;
17
use Prado\Web\UI\ActiveControls\TActivePanel;
18
19
/**
20
 * TJuiDialog class.
21
 *
22
 * TJuiDialog is an extension to {@link TActivePanel} based on jQuery-UI's
23
 * {@link http://jqueryui.com/dialog/ Dialog} widget.
24
 *
25
 *
26
 * <code>
27
 * <com:TJuiDialog
28
 *	ID="dlg1"
29
 * >
30
 * contents
31
 * </com:TJuiDialog>
32
 * </code>
33
 *
34
 * @author David Otto <ottodavid[at]gmx[dot]net>
35
 * @package Prado\Web\UI\JuiControls
36
 * @since 3.3
37
 */
38
class TJuiDialog extends TActivePanel implements IJuiOptions, ICallbackEventHandler
39
{
40
	protected $_options;
41
42
	/**
43
	 * Creates a new callback control, sets the adapter to
44
	 * TActiveControlAdapter. If you override this class, be sure to set the
45
	 * adapter appropriately by, for example, by calling this constructor.
46
	 */
47
	public function __construct()
48
	{
49
		parent::__construct();
50
		$this->setAdapter(new TJuiControlAdapter($this));
51
	}
52
53
	/**
54
	 * @return string the name of the jQueryUI widget method
55
	 */
56
	public function getWidget()
57
	{
58
	  return 'dialog';
59
	}
60
61
	/**
62
	 * @return string the clientid of the jQueryUI widget element
63
	 */
64
	public function getWidgetID()
65
	{
66
	  return $this->getClientID();
67
	}
68
69
	/**
70
	 * Object containing defined javascript options
71
	 * @return TJuiControlOptions
72
	 */
73
	public function getOptions()
74
	{
75
		if (($options=$this->getViewState('JuiOptions'))===null)
76
		{
77
		  $options=new TJuiControlOptions($this);
78
		  $this->setViewState('JuiOptions', $options);
79
		}
80
		return $options;
81
	}
82
83
	/**
84
	 * Array containing valid javascript options
85
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
86
	 */
87
	public function getValidOptions()
88
	{
89
		return array('appendTo', 'autoOpen', 'buttons', 'closeOnEscape', 'closeText', 'dialogClass', 'draggable', 'height', 'hide', 'minHeight', 'minWidth', 'maxHeight', 'maxWidth', 'modal', 'position', 'resizable', 'show', 'title', 'width');
90
	}
91
92
	/**
93
	 * Array containing valid javascript events
94
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
95
	 */
96
	public function getValidEvents()
97
	{
98
		return array('beforeClose', 'close', 'create', 'drag', 'dragStart', 'dragStop', 'focus', 'open', 'resize', 'resizeStart', 'resizeStop');
99
	}
100
101
	/**
102
	 * @return array list of callback options.
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\Prado\Collections\TMap? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
103
	 */
104
	protected function getPostBackOptions()
105
	{
106
		$options = $this->getOptions()->toArray();
107
		// always make the dialog a child of the form, or its inner inputs won't be collected
108
		if(!isset($options['appendTo']))
109
			$options['appendTo'] = 'form:first';
110
111
		foreach($this->getControls() as $control)
112
			if($control instanceof TJuiDialogButton)
113
				$options['buttons'][] = $control->getPostBackOptions();
114
115
		return $options;
116
	}
117
118
	/**
119
	 * Ensure that the ID attribute is rendered and registers the javascript code
120
	 * for initializing the active control.
121
	 */
122
	protected function addAttributesToRender($writer)
123
	{
124
		parent::addAttributesToRender($writer);
125
126
		$writer->addAttribute('id',$this->getClientID());
127
		$options=TJavascript::encode($this->getPostBackOptions());
128
		$cs=$this->getPage()->getClientScript();
129
		$code="jQuery('#".$this->getWidgetID()."').".$this->getWidget()."(".$options.");";
130
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
131
	}
132
133
	/**
134
	 * Raises callback event. This method is required by the {@link ICallbackEventHandler}
135
	 * interface.
136
	 * @param TCallbackEventParameter the parameter associated with the callback event
137
	 */
138
	public function raiseCallbackEvent($param)
139
	{
140
		$this->getOptions()->raiseCallbackEvent($param);
141
	}
142
143
	/**
144
	 * Raises the OnCreate event
145
	 * @param object $params event parameters
146
	 */
147
	public function onOpen ($params)
148
	{
149
		$this->raiseEvent('OnOpen', $this, $params);
150
	}
151
152
	/**
153
	 * Open this dialog
154
	 */
155
	public function open()
156
	{
157
		$this->triggerClientMethod('open');
158
	}
159
160
	/**
161
	 * Close this dialog
162
	 */
163
	public function close()
164
	{
165
		$this->triggerClientMethod('close');
166
	}
167
168
	private function triggerClientMethod($method)
169
	{
170
		$cs = $this->getPage()->getClientScript();
171
		$code = "jQuery(document).ready(function() { jQuery('#".$this->getClientId()."').dialog('".$method."'); })";
172
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
173
	}
174
175
	/**
176
	 * Rendering as a fieldset is not supported for TJuiDialog.
177
	 * @param string the legend text. If this value is not empty, the panel will be rendered as a fieldset.
178
	 * @throws TNotSupportedException not supported for TJuiDialog.
179
	 */
180
	public function setGroupingText($value)
181
	{
182
		throw new TNotSupportedException('Rendering as a fieldset is not supported for {0}.', get_class($this));
183
	}
184
185
	/**
186
	 * Overrides parent implementation to just render the inner contents and avoid replacing the element itself when
187
	 * updating clientside, because replacing/removing will cause jqueryui to fire destroy on the original dialog extension.
188
	 * @param THtmlWriter html writer
189
	 */
190
	public function render($writer)
191
	{
192
		if($this->getHasPreRendered() && $this->getActiveControl()->canUpdateClientSide())
193
		{
194
		  parent::renderContents($writer);
195
			$this->getPage()->getCallbackClient()->replaceContent($this, $writer, false);
196
		}
197
		else
198
			parent::render($writer);
199
	}
200
}
201