Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use Yii;
  9. use yii\helpers\Json;
  10. /**
  11. * BootstrapWidgetTrait is the trait, which provides basic for all bootstrap widgets features.
  12. *
  13. * Note: class, which uses this trait must declare public field named `options` with the array default value:
  14. *
  15. * ```php
  16. * class MyWidget extends \yii\base\Widget
  17. * {
  18. * use BootstrapWidgetTrait;
  19. *
  20. * public $options = [];
  21. * }
  22. * ```
  23. *
  24. * This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.
  25. *
  26. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @author Paul Klimov <klimov.paul@gmail.com>
  29. * @since 2.0.6
  30. */
  31. trait BootstrapWidgetTrait
  32. {
  33. /**
  34. * @var array the options for the underlying Bootstrap JS plugin.
  35. * Please refer to the corresponding Bootstrap plugin Web page for possible options.
  36. * For example, [this page](http://getbootstrap.com/javascript/#modals) shows
  37. * how to use the "Modal" plugin and the supported options (e.g. "remote").
  38. */
  39. public $clientOptions = [];
  40. /**
  41. * @var array the event handlers for the underlying Bootstrap JS plugin.
  42. * Please refer to the corresponding Bootstrap plugin Web page for possible events.
  43. * For example, [this page](http://getbootstrap.com/javascript/#modals) shows
  44. * how to use the "Modal" plugin and the supported events (e.g. "shown").
  45. */
  46. public $clientEvents = [];
  47. /**
  48. * Initializes the widget.
  49. * This method will register the bootstrap asset bundle. If you override this method,
  50. * make sure you call the parent implementation first.
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. if (!isset($this->options['id'])) {
  56. $this->options['id'] = $this->getId();
  57. }
  58. }
  59. /**
  60. * Registers a specific Bootstrap plugin and the related events
  61. * @param string $name the name of the Bootstrap plugin
  62. */
  63. protected function registerPlugin($name)
  64. {
  65. $view = $this->getView();
  66. BootstrapPluginAsset::register($view);
  67. $id = $this->options['id'];
  68. if ($this->clientOptions !== false) {
  69. $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
  70. $js = "jQuery('#$id').$name($options);";
  71. $view->registerJs($js);
  72. }
  73. $this->registerClientEvents();
  74. }
  75. /**
  76. * Registers JS event handlers that are listed in [[clientEvents]].
  77. * @since 2.0.2
  78. */
  79. protected function registerClientEvents()
  80. {
  81. if (!empty($this->clientEvents)) {
  82. $id = $this->options['id'];
  83. $js = [];
  84. foreach ($this->clientEvents as $event => $handler) {
  85. $js[] = "jQuery('#$id').on('$event', $handler);";
  86. }
  87. $this->getView()->registerJs(implode("\n", $js));
  88. }
  89. }
  90. /**
  91. * @return \yii\web\View the view object that can be used to render views or view files.
  92. * @see yii\base\Widget::getView()
  93. */
  94. abstract function getView();
  95. }