1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8 namespace foundation\widgets;
9
10 use foundation\helpers\ArrayHelper;
11 use foundation\helpers\Html;
12 use foundation\enum\Enum;
13
14 /**
15 * Reveal renders the markup for the modal dialog plugin of Foundation
16 *
17 * @see http://foundation.zurb.com/docs/components/reveal.html
18 *
19 * @author Antonio Ramirez <amigo.cobos@gmail.com>
20 * @package foundation\widgets
21 */
22 class Reveal extends base\Widget
23 {
24 /**
25 * @var string the size of the modal
26 */
27 public $size = Enum::DIALOG_MEDIUM;
28 /**
29 * @var string header. The string will be wrapped on a H2 tag
30 */
31 public $header;
32 /**
33 * @var array the header html options
34 */
35 public $headerHtmlOptions = array();
36 /**
37 * @var string the body of the modal
38 */
39 public $body;
40 /**
41 * @var array the plugin options. The following is the list of supported options and their default values:
42 *
43 * ´´´php
44 * array(
45 * 'animation' => 'fadeAndPop',
46 * 'animationSpeed' => 250,
47 * 'closeOnBackgroundClick' => true,
48 * 'dismissModalClass' => 'close-reveal-modal',
49 * 'bgClass' => 'reveal-modal-bg',
50 * 'bg' => 'js:$("reveal-modal-bg")', // jquery element
51 * 'css' => array(
52 * 'open' => array(
53 * 'opacity' => 0,
54 * 'visibility' => 'visible',
55 * 'display' => 'block'
56 * ),
57 * 'close' => array(
58 * 'opacity': 1,
59 * 'visibility': 'hidden',
60 * 'display': 'none'
61 * ),
62 * )
63 * ´´´
64 */
65 public $pluginOptions = array();
66 /**
67 * @var string[] the events of the modal. The following list is the supported events:
68 *
69 * - open
70 * - opened
71 * - close
72 * - closed
73 */
74 public $events = array();
75 /**
76 * @var array the options for rendering the toggle button tag.
77 * The toggle button is used to toggle the visibility of the modal window.
78 * If this property is null, no toggle button will be rendered.
79 *
80 * The following special options are supported:
81 *
82 * - tag: string, the tag name of the button. Defaults to 'button'.
83 * - label: string, the label of the button. Defaults to 'Show'.
84 *
85 * The rest of the options will be rendered as the HTML attributes of the button tag.
86 * Please refer to the [Modal plugin help](http://foundation.zurb.com/docs/components/reveal.html)
87 * for the supported HTML attributes.
88 */
89 public $toggleButton;
90 /**
91 * @var array the options for rendering the close button tag.
92 * The close button is displayed in the header of the modal window. Clicking
93 * on the button will hide the modal window. If this is null, no close button will be rendered.
94 *
95 * The following special options are supported:
96 *
97 * - tag: string, the tag name of the button. Defaults to 'button'.
98 * - label: string, the label of the button. Defaults to '×'.
99 * - options: array, the html options fo the button. Forced attribute: array('class' => 'close-reveal-modal')
100 *
101 * The rest of the options will be rendered as the HTML attributes of the button tag.
102 * Please refer to the [Modal plugin help](http://foundation.zurb.com/docs/components/reveal.html)
103 * for the supported HTML attributes.
104 */
105 public $closeButton = array();
106
107 /**
108 * Initializes the widget
109 */
110 public function init()
111 {
112 $this->assets = array(
113 'js' => YII_DEBUG ? 'foundation/foundation.reveal.js' : 'foundation.min.js'
114 );
115
116 Html::addCssClass($this->htmlOptions, Enum::DIALOG);
117 $this->setId(ArrayHelper::removeValue($this->htmlOptions, 'id', $this->getId()));
118 $this->registerClientScript();
119 parent::init();
120
121 $this->htmlOptions['id'] = $this->getId();
122
123 echo $this->renderToggleButton() . "\n";
124 echo \CHtml::openTag('div', $this->htmlOptions) . "\n";
125 echo $this->renderHeader() . "\n";
126 }
127
128 /**
129 * Renders the widget
130 */
131 public function run()
132 {
133 echo "\n" . $this->body . "\n";
134 echo $this->renderCloseButton() . "\n";
135 echo \CHtml::closeTag('div') . "\n";
136 }
137
138 /**
139 * @return string
140 */
141 public function renderHeader()
142 {
143 return \CHtml::tag('h2', $this->headerHtmlOptions, $this->header);
144 }
145
146 /**
147 * Renders the toggle button
148 * @return null|string the rendering result
149 */
150 public function renderToggleButton()
151 {
152 if ($this->toggleButton !== null) {
153 $tag = ArrayHelper::removeValue($this->toggleButton, 'tag', 'button');
154 $label = ArrayHelper::removeValue($this->toggleButton, 'label', 'Show');
155 if ($tag === 'button' && !isset($this->toggleButton['type'])) {
156 $this->toggleButton['type'] = 'button';
157 }
158 $this->toggleButton['data-reveal-id'] = $this->getId();
159 return \CHtml::tag($tag, $this->toggleButton, $label);
160 } else {
161 return null;
162 }
163 }
164
165 /**
166 * Renders the close button.
167 * @return string the rendering result
168 */
169 protected function renderCloseButton()
170 {
171 if ($this->closeButton !== null) {
172 $tag = ArrayHelper::removeValue($this->closeButton, 'tag', 'a');
173 $label = ArrayHelper::removeValue($this->closeButton, 'label', '×');
174 if ($tag === 'button' && !isset($this->closeButton['type'])) {
175 $this->closeButton['type'] = 'button';
176 }
177 Html::addCssClass($this->closeButton, Enum::DIALOG_CLOSE);
178 return \CHtml::tag($tag, $this->closeButton, $label);
179 } else {
180 return null;
181 }
182 }
183
184 /**
185 * Registers plugin options and events (if any)
186 */
187 public function registerClientScript()
188 {
189 if (!empty($this->pluginOptions)) {
190 $options = \CJavaScript::encode($this->pluginOptions);
191 \Yii::app()->clientScript
192 ->registerScript('Reveal#' . $this->getId(), "$(document).foundation('reveal', {$options});");
193 }
194
195 if (!empty($this->events)) {
196 $this->registerEvents("#{$this->getId()}", $this->events);
197 }
198
199 // move the reveal to the end of the body tag
200 \Yii::app()->clientScript
201 ->registerScript('Reveal#placement#' . $this->getId(), "$('#{$this->getId()}').appendTo(document.body);");
202 }
203 }