|
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\Widget;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\helpers\Json;
- use yii\web\Response;
-
-
- class Pjax extends Widget
- {
-
-
- public $options = [];
-
-
- public $linkSelector;
-
-
- public $formSelector;
-
-
- public $submitEvent = 'submit';
-
-
- public $enablePushState = true;
-
-
- public $enableReplaceState = false;
-
-
- public $timeout = 1000;
-
-
- public $scrollTo = false;
-
-
- public $clientOptions;
-
-
-
-
- public function init()
- {
- if (!isset($this->options['id'])) {
- $this->options['id'] = $this->getId();
- }
-
- if ($this->requiresPjax()) {
- ob_start();
- ob_implicit_flush(false);
- $view = $this->getView();
- $view->clear();
- $view->beginPage();
- $view->head();
- $view->beginBody();
- if ($view->title !== null) {
- echo Html::tag('title', Html::encode($view->title));
- }
- } else {
- $options = $this->options;
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- echo Html::beginTag($tag, array_merge([
- 'data-pjax-container' => '',
- 'data-pjax-push-state' => $this->enablePushState,
- 'data-pjax-replace-state' => $this->enableReplaceState,
- 'data-pjax-timeout' => $this->timeout,
- 'data-pjax-scrollto' => $this->scrollTo,
- ], $options));
- }
- }
-
-
-
- public function run()
- {
- if (!$this->requiresPjax()) {
- echo Html::endTag(ArrayHelper::remove($this->options, 'tag', 'div'));
- $this->registerClientScript();
-
- return;
- }
-
- $view = $this->getView();
- $view->endBody();
-
-
-
-
- $view->cssFiles = null;
-
- $view->endPage(true);
-
- $content = ob_get_clean();
-
-
- $response = Yii::$app->getResponse();
- $response->clearOutputBuffers();
- $response->setStatusCode(200);
- $response->format = Response::FORMAT_HTML;
- $response->content = $content;
- $response->send();
-
- Yii::$app->end();
- }
-
-
-
- protected function requiresPjax()
- {
- $headers = Yii::$app->getRequest()->getHeaders();
-
- return $headers->get('X-Pjax') && explode(' ', $headers->get('X-Pjax-Container'))[0] === '#' . $this->options['id'];
- }
-
-
-
- public function registerClientScript()
- {
- $id = $this->options['id'];
- $this->clientOptions['push'] = $this->enablePushState;
- $this->clientOptions['replace'] = $this->enableReplaceState;
- $this->clientOptions['timeout'] = $this->timeout;
- $this->clientOptions['scrollTo'] = $this->scrollTo;
- if (!isset($this->clientOptions['container'])) {
- $this->clientOptions['container'] = "#$id";
- }
- $options = Json::htmlEncode($this->clientOptions);
- $js = '';
- if ($this->linkSelector !== false) {
- $linkSelector = Json::htmlEncode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
- $js .= "jQuery(document).pjax($linkSelector, $options);";
- }
- if ($this->formSelector !== false) {
- $formSelector = Json::htmlEncode($this->formSelector !== null ? $this->formSelector : '#' . $id . ' form[data-pjax]');
- $submitEvent = Json::htmlEncode($this->submitEvent);
- $js .= "\njQuery(document).on($submitEvent, $formSelector, function (event) {jQuery.pjax.submit(event, $options);});";
- }
- $view = $this->getView();
- PjaxAsset::register($view);
-
- if ($js !== '') {
- $view->registerJs($js);
- }
- }
- }
|