PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbModalManager.php

https://github.com/max-rautkin/yii-admincrud
PHP | 109 lines | 55 code | 16 blank | 38 comment | 7 complexity | 516c7bad82e6f8f8d44785043e14b694 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * TbModalMaster class file.
  4. * @author Thiago Otaviani Vidal <thiagovidal@gmail.com>
  5. * @copyright Copyright &copy; Thiago Otaviani Vidal 2012
  6. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7. * @package bootstrap.widgets
  8. * @since 0.9.3
  9. */
  10. /**
  11. * Bootstrap modal master widget.
  12. * @see https://github.com/jschr/bootstrap-modal/
  13. */
  14. class TbModalManager extends CWidget
  15. {
  16. /**
  17. * @var boolean indicates whether to automatically open the modal when initialized. Defaults to 'false'.
  18. */
  19. public $autoOpen = false;
  20. /**
  21. * @var boolean indicates whether the modal should use transitions. Defaults to 'true'.
  22. */
  23. public $fade = true;
  24. /**
  25. * @var array the options for the Bootstrap Javascript plugin.
  26. */
  27. public $options = array();
  28. /**
  29. * @var string[] the Javascript event handlers.
  30. */
  31. public $events = array();
  32. /**
  33. * @var array the HTML attributes for the widget container.
  34. */
  35. public $htmlOptions = array();
  36. /**
  37. * Initializes the widget.
  38. */
  39. public function init()
  40. {
  41. if (!isset($this->htmlOptions['id'])) {
  42. $this->htmlOptions['id'] = $this->getId();
  43. }
  44. if ($this->autoOpen === false && !isset($this->options['show'])) {
  45. $this->options['show'] = false;
  46. }
  47. $classes = array('modal');
  48. if ($this->fade === true) {
  49. $classes[] = 'fade';
  50. }
  51. if (!empty($classes)) {
  52. $classes = implode(' ', $classes);
  53. if (isset($this->htmlOptions['class'])) {
  54. $this->htmlOptions['class'] .= ' ' . $classes;
  55. } else {
  56. $this->htmlOptions['class'] = $classes;
  57. }
  58. }
  59. echo CHtml::openTag('div', $this->htmlOptions);
  60. }
  61. /**
  62. * Runs the widget.
  63. */
  64. public function run()
  65. {
  66. echo CHtml::closeTag('div');
  67. $this->registerClientScript($this->htmlOptions['id']);
  68. }
  69. /**
  70. * Registers required
  71. *
  72. * @param integer $id
  73. */
  74. public function registerClientScript($id)
  75. {
  76. Yii::app()->bootstrap->registerAssetJs('bootstrap-modalmanager.js', CClientScript::POS_HEAD);
  77. Yii::app()->bootstrap->registerAssetCss('bootstrap-modalmanager.css');
  78. $options = !empty($this->format) ? CJavaScript::encode(array('format' => $this->format)) : '';
  79. ob_start();
  80. echo "jQuery('#{$id}').modalmanager({$options})";
  81. foreach ($this->events as $event => $handler) {
  82. echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
  83. }
  84. Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), ob_get_clean() . ';');
  85. foreach ($this->events as $name => $handler) {
  86. $handler = CJavaScript::encode($handler);
  87. Yii::app()->getClientScript()->registerScript(
  88. __CLASS__ . '#' . $id . '_' . $name,
  89. "jQuery('#{$id}').on('{$name}', {$handler});"
  90. );
  91. }
  92. }
  93. }