TJuiDialog   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 166
ccs 0
cts 84
cp 0
rs 10
wmc 21

15 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 3 1
A getOptions() 0 7 2
A addAttributesToRender() 0 9 1
A triggerClientMethod() 0 5 1
A render() 0 7 3
A raiseCallbackEvent() 0 3 1
A getValidEvents() 0 3 1
A getPostBackOptions() 0 15 4
A close() 0 3 1
A getWidgetID() 0 3 1
A getValidOptions() 0 3 1
A onOpen() 0 3 1
A __construct() 0 4 1
A setGroupingText() 0 3 1
A getWidget() 0 3 1
1
<?php
2
3
/**
4
 * TJuiDialog class file.
5
 *
6
 * @author  David Otto <ottodavid[at]gmx[dot]net>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web\UI\JuiControls;
12
13
use Prado\Exceptions\TNotSupportedException;
14
use Prado\Web\Javascripts\TJavaScript;
15
use Prado\Web\UI\ActiveControls\ICallbackEventHandler;
16
use Prado\Web\UI\ActiveControls\TActivePanel;
17
use Prado\Web\UI\ActiveControls\TCallbackEventParameter;
18
19
/**
20
 * TJuiDialog class.
21
 *
22
 * TJuiDialog is an extension to {@see \Prado\Web\UI\ActiveControls\TActivePanel} based on jQuery-UI's
23
 * {@see http://jqueryui.com/dialog/ Dialog} widget.
24
 *
25
 *
26
 * ```php
27
 * <com:TJuiDialog
28
 *	ID="dlg1"
29
 * >
30
 * contents
31
 * </com:TJuiDialog>
32
 * ```
33
 *
34
 * @author David Otto <ottodavid[at]gmx[dot]net>
35
 * @since 3.3
36
 */
37
class TJuiDialog extends TActivePanel implements IJuiOptions, ICallbackEventHandler
38
{
39
	protected $_options;
40
41
	/**
42
	 * Creates a new callback control, sets the adapter to
43
	 * TActiveControlAdapter. If you override this class, be sure to set the
44
	 * adapter appropriately by, for example, by calling this constructor.
45
	 */
46
	public function __construct()
47
	{
48
		parent::__construct();
49
		$this->setAdapter(new TJuiControlAdapter($this));
50
	}
51
52
	/**
53
	 * @return string the name of the jQueryUI widget method
54
	 */
55
	public function getWidget()
56
	{
57
		return 'dialog';
58
	}
59
60
	/**
61
	 * @return string the clientid of the jQueryUI widget element
62
	 */
63
	public function getWidgetID()
64
	{
65
		return $this->getClientID();
66
	}
67
68
	/**
69
	 * Object containing defined javascript options
70
	 * @return TJuiControlOptions
71
	 */
72
	public function getOptions()
73
	{
74
		if (($options = $this->getViewState('JuiOptions')) === null) {
75
			$options = new TJuiControlOptions($this);
76
			$this->setViewState('JuiOptions', $options);
77
		}
78
		return $options;
79
	}
80
81
	/**
82
	 * Array containing valid javascript options
83
	 * @return array
84
	 */
85
	public function getValidOptions()
86
	{
87
		return ['appendTo', 'autoOpen', 'buttons', 'closeOnEscape', 'closeText', 'dialogClass', 'draggable', 'height', 'hide', 'minHeight', 'minWidth', 'maxHeight', 'maxWidth', 'modal', 'position', 'resizable', 'show', 'title', 'width'];
88
	}
89
90
	/**
91
	 * Array containing valid javascript events
92
	 * @return array
93
	 */
94
	public function getValidEvents()
95
	{
96
		return ['beforeClose', 'close', 'create', 'drag', 'dragStart', 'dragStop', 'focus', 'open', 'resize', 'resizeStart', 'resizeStop'];
97
	}
98
99
	/**
100
	 * @return array list of callback options.
101
	 */
102
	protected function getPostBackOptions()
103
	{
104
		$options = $this->getOptions()->toArray();
105
		// always make the dialog a child of the form, or its inner inputs won't be collected
106
		if (!isset($options['appendTo'])) {
107
			$options['appendTo'] = 'form:first';
108
		}
109
110
		foreach ($this->getControls() as $control) {
111
			if ($control instanceof TJuiDialogButton) {
112
				$options['buttons'][] = $control->getPostBackOptions();
113
			}
114
		}
115
116
		return $options;
117
	}
118
119
	/**
120
	 * Ensure that the ID attribute is rendered and registers the javascript code
121
	 * for initializing the active control.
122
	 * @param mixed $writer
123
	 */
124
	protected function addAttributesToRender($writer)
125
	{
126
		parent::addAttributesToRender($writer);
127
128
		$writer->addAttribute('id', $this->getClientID());
129
		$options = TJavaScript::encode($this->getPostBackOptions());
130
		$cs = $this->getPage()->getClientScript();
131
		$code = "jQuery('#" . $this->getWidgetID() . "')." . $this->getWidget() . "(" . $options . ");";
132
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
133
	}
134
135
	/**
136
	 * Raises callback event. This method is required by the {@see \Prado\Web\UI\ActiveControls\ICallbackEventHandler}
137
	 * interface.
138
	 * @param TCallbackEventParameter $param the parameter associated with the callback event
139
	 */
140
	public function raiseCallbackEvent($param)
141
	{
142
		$this->getOptions()->raiseCallbackEvent($param);
143
	}
144
145
	/**
146
	 * Raises the OnCreate event
147
	 * @param object $params event parameters
148
	 */
149
	public function onOpen($params)
150
	{
151
		$this->raiseEvent('OnOpen', $this, $params);
152
	}
153
154
	/**
155
	 * Open this dialog
156
	 */
157
	public function open()
158
	{
159
		$this->triggerClientMethod('open');
160
	}
161
162
	/**
163
	 * Close this dialog
164
	 */
165
	public function close()
166
	{
167
		$this->triggerClientMethod('close');
168
	}
169
170
	/**
171
	 * Executes a method on the dialog
172
	 * @param string $method name
173
	 */
174
	private function triggerClientMethod($method)
175
	{
176
		$cs = $this->getPage()->getClientScript();
177
		$code = "jQuery(document).ready(function() { jQuery('#" . $this->getClientId() . "').dialog('" . $method . "'); })";
178
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
179
	}
180
181
	/**
182
	 * Rendering as a fieldset is not supported for TJuiDialog.
183
	 * @param string $value the legend text. If this value is not empty, the panel will be rendered as a fieldset.
184
	 * @throws TNotSupportedException not supported for TJuiDialog.
185
	 */
186
	public function setGroupingText($value)
187
	{
188
		throw new TNotSupportedException('Rendering as a fieldset is not supported for {0}.', $this::class);
189
	}
190
191
	/**
192
	 * Overrides parent implementation to just render the inner contents and avoid replacing the element itself when
193
	 * updating clientside, because replacing/removing will cause jqueryui to fire destroy on the original dialog extension.
194
	 * @param \Prado\Web\UI\THtmlWriter $writer html writer
195
	 */
196
	public function render($writer)
197
	{
198
		if ($this->getHasPreRendered() && $this->getActiveControl()->canUpdateClientSide()) {
199
			parent::renderContents($writer);
200
			$this->getPage()->getCallbackClient()->replaceContent($this, $writer, false);
0 ignored issues
show
Bug introduced by
$writer of type Prado\Web\UI\THtmlWriter is incompatible with the type string expected by parameter $content of Prado\Web\UI\ActiveContr...cript::replaceContent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
			$this->getPage()->getCallbackClient()->replaceContent($this, /** @scrutinizer ignore-type */ $writer, false);
Loading history...
201
		} else {
202
			parent::render($writer);
203
		}
204
	}
205
}
206