Pjax.php 4.26 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\widgets;

use Yii;
use yii\base\Widget;
12
use yii\helpers\Html;
Qiang Xue committed
13
use yii\helpers\Json;
14
use yii\web\Response;
Qiang Xue committed
15 16

/**
17 18 19 20 21
 * Pjax is a widget integrating the [pjax](https://github.com/defunkt/jquery-pjax) jQuery plugin.
 *
 * Pjax captures the link clicks in the content enclosed between its [[begin()]] and [[end()]] calls,
 * turns them into AJAX requests, and replaces the enclosed content with the corresponding AJAX response.
 *
Carsten Brandt committed
22
 * The following example makes the [[\yii\gridview\GridView]] widget support updating via AJAX:
23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * ```php
 * use yii\widgets\Pjax;
 *
 * Pjax::begin();
 * echo GridView::widget([...]);
 * Pjax::end();
 * ```
 *
 * Clicking the sorting and pagination links in the grid will trigger AJAX-based updating of the grid content.
 * Moreover, if the grid view has turned on filtering, the filtering will also be performed via AJAX.
 *
 * By default, Pjax enables [[enablePushState|push state]], which means the browser's current URL will
 * be updated when an AJAX request is made by Pjax.
Qiang Xue committed
37
 *
38 39 40
 * Pjax can also be used for submitting forms, check the
 * [pjax documentation](https://github.com/defunkt/jquery-pjax#pjaxsubmit) for it.
 *
Qiang Xue committed
41 42 43 44 45
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Pjax extends Widget
{
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	/**
	 * @var array the HTML attributes for the widget container tag.
	 */
	public $options = [];
	/**
	 * @var string the jQuery selector of the links that should trigger pjax requests.
	 * If not set, all links within the enclosed content of Pjax will trigger pjax requests.
	 * Note that the pjax response to a link is a full page, a normal request will be sent again.
	 */
	public $linkSelector;
	/**
	 * @var boolean whether to enable push state.
	 */
	public $enablePushState = true;
	/**
	 * @var boolean whether to enable replace state.
	 */
	public $enableReplaceState = false;
64 65 66 67 68 69 70 71 72
	/**
	 * @var integer pjax timeout setting (in milliseconds)
	 */
	public $timeout = 1000;
	/**
	 * @var array additional options to be passed to the pjax JS plugin. Please refer to
	 * [pjax project page](https://github.com/defunkt/jquery-pjax) for available options.
	 */
	public $clientOptions;
Qiang Xue committed
73

74 75 76
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
77 78
	public function init()
	{
79 80 81 82
		if (!isset($this->options['id'])) {
			$this->options['id'] = $this->getId();
		}

Qiang Xue committed
83 84
		ob_start();
		ob_implicit_flush(false);
85 86 87 88 89 90 91 92 93

		if ($this->requiresPjax()) {
			$view = $this->getView();
			$view->clear();
			$view->beginPage();
			$view->head();
			$view->beginBody();
		}
		echo Html::beginTag('div', $this->options);
Qiang Xue committed
94 95
	}

96 97 98
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
99 100
	public function run()
	{
101 102 103 104 105 106 107
		echo Html::endTag('div');
		if ($requiresPjax = $this->requiresPjax()) {
			$view = $this->getView();
			$view->endBody();
			$view->endPage(true);
		}

Qiang Xue committed
108 109
		$content = ob_get_clean();

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		if ($requiresPjax) {
			// only need the content enclosed within this widget
			$response = Yii::$app->getResponse();
			$level = ob_get_level();
			$response->clearOutputBuffers();
			$response->setStatusCode(200);
			$response->format = Response::FORMAT_HTML;
			$response->content = $content;
			$response->send();

			// re-enable output buffer to capture content after this widget
			for (; $level > 0; --$level) {
				ob_start();
				ob_implicit_flush(false);
			}
Qiang Xue committed
125 126
		} else {
			$this->registerClientScript();
127
			echo $content;
Qiang Xue committed
128 129 130
		}
	}

131 132 133 134 135 136 137 138 139
	/**
	 * @return boolean whether the current request requires pjax response from this widget
	 */
	protected function requiresPjax()
	{
		$headers = Yii::$app->getRequest()->getHeaders();
		return $headers->get('X-Pjax') && ($selector = $headers->get('X-Pjax-Container')) === '#' . $this->getId();
	}

Qiang Xue committed
140 141 142 143 144
	/**
	 * Registers the needed JavaScript.
	 */
	public function registerClientScript()
	{
145
		$id = $this->options['id'];
146 147 148 149
		$this->clientOptions['push'] = $this->enablePushState;
		$this->clientOptions['replace'] = $this->enableReplaceState;
		$this->clientOptions['timeout'] = $this->timeout;
		$options = Json::encode($this->clientOptions);
150
		$linkSelector = Json::encode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
Qiang Xue committed
151 152
		$view = $this->getView();
		PjaxAsset::register($view);
153
		$js = "jQuery(document).pjax($linkSelector, \"#$id\", $options);";
Qiang Xue committed
154 155 156
		$view->registerJs($js);
	}
}