Overview

Packages

  • bootstrap
    • behaviors
    • components
    • form
    • gii
    • helpers
    • widgets

Classes

  • TbActiveForm
  • TbAffix
  • TbAlert
  • TbBreadcrumb
  • TbButtonColumn
  • TbCollapse
  • TbDataColumn
  • TbDetailView
  • TbGridView
  • TbHeroUnit
  • TbListView
  • TbModal
  • TbNav
  • TbNavbar
  • TbPager
  • TbScrollspy
  • TbTabs
  • TbThumbnails
  • TbTypeAhead
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * TbModal class file.
  4:  * @author Antonio Ramirez <ramirez.cobos@gmail.com>
  5:  * @copyright Copyright &copy; Christoffer Niska 2013-
  6:  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7:  * @package bootstrap.widgets
  8:  */
  9: 
 10: Yii::import('bootstrap.behaviors.TbWidget');
 11: 
 12: /**
 13:  * Bootstrap modal widget.
 14:  */
 15: class TbModal extends CWidget
 16: {
 17:     /**
 18:      * @var array the HTML options for the view container tag.
 19:      */
 20:     public $htmlOptions = array();
 21: 
 22:     /**
 23:      * @var array  The additional HTML attributes of the button that will show the modal. If empty array, only
 24:      * the markup of the modal will be rendered on the page, so users can easily call the modal manually with their own
 25:      * scripts. The following special attributes are available:
 26:      * <ul>
 27:      *    <li>label: string, the label of the button</li>
 28:      * </ul>
 29:      *
 30:      * For available options of the button trigger, see http://twitter.github.com/bootstrap/javascript.html#modals.
 31:      */
 32:     public $buttonOptions = array();
 33: 
 34:     /**
 35:      * @var boolean indicates whether the modal should use transitions. Defaults to 'true'.
 36:      */
 37:     public $fade = true;
 38: 
 39:     /**
 40:      * @var bool $keyboard, closes the modal when escape key is pressed.
 41:      */
 42:     public $keyboard = true;
 43: 
 44:     /**
 45:      * @var bool $show, shows the modal when initialized.
 46:      */
 47:     public $show = false;
 48: 
 49:     /**
 50:      * @var mixed includes a modal-backdrop element. Alternatively, specify `static` for a backdrop which doesn't close
 51:      * the modal on click.
 52:      */
 53:     public $backdrop = true;
 54: 
 55:     /**
 56:      * @var mixed the remote url. If a remote url is provided, content will be loaded via jQuery's load method and
 57:      * injected into the .modal-body of the modal.
 58:      */
 59:     public $remote;
 60: 
 61:     /**
 62:      * @var string a javascript function that will be invoked immediately when the `show` instance method is called.
 63:      */
 64:     public $onShow;
 65: 
 66:     /**
 67:      * @var string a javascript function that will be invoked when the modal has been made visible to the user
 68:      *     (will wait for css transitions to complete).
 69:      */
 70:     public $onShown;
 71: 
 72:     /**
 73:      * @var string a javascript function that will be invoked immediately when the hide instance method has been called.
 74:      */
 75:     public $onHide;
 76: 
 77:     /**
 78:      * @var string a javascript function that will be invoked when the modal has finished being hidden from the user
 79:      *     (will wait for css transitions to complete).
 80:      */
 81:     public $onHidden;
 82: 
 83:     /**
 84:      * @var string[] the Javascript event handlers.
 85:      */
 86:     protected $events = array();
 87: 
 88:     /**
 89:      * @var array $options the plugin options.
 90:      */
 91:     protected $options = array();
 92: 
 93:     /**
 94:      * @var string
 95:      */
 96:     public $closeText = TbHtml::CLOSE_TEXT;
 97: 
 98:     /**
 99:      * @var string header content
100:      */
101:     public $header;
102: 
103:     /**
104:      * @var string body of modal
105:      */
106:     public $content;
107: 
108:     /**
109:      * @var string footer content
110:      */
111:     public $footer;
112: 
113:     /**
114:      * Widget's initialization method
115:      */
116:     public function init()
117:     {
118:         $this->attachBehavior('TbWidget', new TbWidget);
119: 
120:         TbArray::defaultValue('id', $this->getId(), $this->htmlOptions);
121:         TbArray::defaultValue('role', 'dialog', $this->htmlOptions);
122:         TbArray::defaultValue('tabindex', '-1', $this->htmlOptions);
123: 
124:         TbHtml::addCssClass('modal hide', $this->htmlOptions);
125:         if ($this->fade) {
126:             TbHtml::addCssClass('fade', $this->htmlOptions);
127:         }
128: 
129:         if (is_array($this->footer)) {
130:             $this->footer = implode('&nbsp;', $this->footer);
131:         }
132: 
133:         $this->initOptions();
134:         $this->initEvents();
135:     }
136: 
137:     /**
138:      * Initialize events if any
139:      */
140:     public function initEvents()
141:     {
142:         foreach (array('onShow', 'onShown', 'onHide', 'onHidden') as $event) {
143:             if ($this->$event !== null) {
144:                 $modalEvent = strtolower(substr($event, 2));
145:                 if ($this->$event instanceof CJavaScriptExpression) {
146:                     $this->events[$modalEvent] = $this->$event;
147:                 } else {
148:                     $this->events[$modalEvent] = new CJavaScriptExpression($this->$event);
149:                 }
150:             }
151:         }
152:     }
153: 
154:     /**
155:      * Initialize plugin options.
156:      * ***Important***: The display of the button overrides the initialization of the modal bootstrap widget.
157:      */
158:     public function initOptions()
159:     {
160:         if ($remote = TbArray::popValue('remote', $this->options)) {
161:             $this->options['remote'] = CHtml::normalizeUrl($remote);
162:         }
163: 
164:         TbArray::defaultValue('backdrop', $this->backdrop, $this->options);
165:         TbArray::defaultValue('keyboard', $this->keyboard, $this->options);
166:         TbArray::defaultValue('show', $this->show, $this->options);
167:     }
168: 
169:     /**
170:      * Widget's run method
171:      */
172:     public function run()
173:     {
174:         $this->renderModal();
175:         $this->renderButton();
176:         $this->registerClientScript();
177:     }
178: 
179:     /**
180:      * Renders the button
181:      */
182:     public function renderButton()
183:     {
184:         if (!empty($this->buttonOptions) && is_array($this->buttonOptions)) {
185:             TbArray::defaultValue('data-toggle', 'modal', $this->buttonOptions);
186: 
187:             if ($this->remote !== null) {
188:                 $this->buttonOptions['data-remote'] = CHtml::normalizeUrl($this->remote);
189:             }
190: 
191:             $selector = '#' . $this->htmlOptions['id'];
192:             $label = TbArray::popValue('label', $this->buttonOptions, 'button');
193:             $attr = isset($this->buttonOptions['data-remote']) ? 'data-target' : 'href';
194:             TbArray::defaultValue($attr, $selector, $this->buttonOptions);
195:             echo TbHtml::button($label, $this->buttonOptions);
196:         }
197:     }
198: 
199:     /**
200:      * Renders the modal markup
201:      */
202:     public function renderModal()
203:     {
204:         echo TbHtml::openTag('div', $this->htmlOptions) . PHP_EOL;
205: 
206:         $this->renderModalHeader();
207:         $this->renderModalBody();
208:         $this->renderModalFooter();
209: 
210:         echo '</div>' . PHP_EOL;
211:     }
212: 
213:     /**
214:      * Renders the header HTML markup of the modal
215:      */
216:     public function renderModalHeader()
217:     {
218:         echo '<div class="modal-header">' . PHP_EOL;
219:         if ($this->closeText) {
220:             echo TbHtml::closeButton($this->closeText, array('data-dismiss' => 'modal'));
221:         }
222:         echo TbHtml::tag('h3', array(), $this->header);
223:         echo '</div>' . PHP_EOL;
224:     }
225: 
226:     /**
227:      * Renders the HTML markup for the body of the modal
228:      */
229:     public function renderModalBody()
230:     {
231:         echo '<div class="modal-body">' . PHP_EOL;
232:         echo $this->content;
233:         echo '</div>' . PHP_EOL;
234:     }
235: 
236:     /**
237:      * Renders the HTML markup for the footer of the modal
238:      */
239:     public function renderModalFooter()
240:     {
241: 
242:         echo '<div class="modal-footer">' . PHP_EOL;
243:         echo $this->footer;
244:         echo '</div>' . PHP_EOL;
245:     }
246: 
247:     /**
248:      * Registers necessary client scripts.
249:      */
250:     public function registerClientScript()
251:     {
252:         $selector = '#' . $this->htmlOptions['id'];
253: 
254:         // do we render a button? If so, bootstrap will handle its behavior through its
255:         // mark-up, otherwise, register the plugin.
256:         if (empty($this->buttonOptions)) {
257:             $this->registerPlugin(TbApi::PLUGIN_MODAL, $selector, $this->options);
258:         }
259: 
260:         $this->registerEvents($selector, $this->events);
261:     }
262: 
263: }
Yiistrap API documentation generated by ApiGen 2.8.0