PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/yiisoft/yii2/widgets/Pjax.php

https://gitlab.com/makkooz/nikestreetbeat
PHP | 199 lines | 95 code | 17 blank | 87 comment | 9 complexity | ee728517e5030e9c23a04ea18db955dc MD5 | raw file
  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\widgets;
  8. use Yii;
  9. use yii\base\Widget;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Html;
  12. use yii\helpers\Json;
  13. use yii\web\Response;
  14. /**
  15. * Pjax is a widget integrating the [pjax](https://github.com/yiisoft/jquery-pjax) jQuery plugin.
  16. *
  17. * Pjax only deals with the content enclosed between its [[begin()]] and [[end()]] calls, called the *body content* of the widget.
  18. * By default, any link click or form submission (for those forms with `data-pjax` attribute) within the body content
  19. * will trigger an AJAX request. In responding to the AJAX request, Pjax will send the updated body content (based
  20. * on the AJAX request) to the client which will replace the old content with the new one. The browser's URL will then
  21. * be updated using pushState. The whole process requires no reloading of the layout or resources (js, css).
  22. *
  23. * You may configure [[linkSelector]] to specify which links should trigger pjax, and configure [[formSelector]]
  24. * to specify which form submission may trigger pjax.
  25. *
  26. * You may disable pjax for a specific link inside the container by adding `data-pjax="0"` attribute to this link.
  27. *
  28. * The following example shows how to use Pjax with the [[\yii\grid\GridView]] widget so that the grid pagination,
  29. * sorting and filtering can be done via pjax:
  30. *
  31. * ```php
  32. * use yii\widgets\Pjax;
  33. *
  34. * Pjax::begin();
  35. * echo GridView::widget([...]);
  36. * Pjax::end();
  37. * ```
  38. *
  39. * @author Qiang Xue <qiang.xue@gmail.com>
  40. * @since 2.0
  41. */
  42. class Pjax extends Widget
  43. {
  44. /**
  45. * @var array the HTML attributes for the widget container tag. The following special options are recognized:
  46. *
  47. * - `tag`: string, the tag name for the container. Defaults to `div`
  48. *
  49. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  50. */
  51. public $options = [];
  52. /**
  53. * @var string|false the jQuery selector of the links that should trigger pjax requests.
  54. * If not set, all links within the enclosed content of Pjax will trigger pjax requests.
  55. * If set to false, no code will be registered to handle links.
  56. * Note that if the response to the pjax request is a full page, a normal request will be sent again.
  57. */
  58. public $linkSelector;
  59. /**
  60. * @var string|false the jQuery selector of the forms whose submissions should trigger pjax requests.
  61. * If not set, all forms with `data-pjax` attribute within the enclosed content of Pjax will trigger pjax requests.
  62. * If set to false, no code will be registered to handle forms.
  63. * Note that if the response to the pjax request is a full page, a normal request will be sent again.
  64. */
  65. public $formSelector;
  66. /**
  67. * @var boolean whether to enable push state.
  68. */
  69. public $enablePushState = true;
  70. /**
  71. * @var boolean whether to enable replace state.
  72. */
  73. public $enableReplaceState = false;
  74. /**
  75. * @var integer pjax timeout setting (in milliseconds). This timeout is used when making AJAX requests.
  76. * Use a bigger number if your server is slow. If the server does not respond within the timeout,
  77. * a full page load will be triggered.
  78. */
  79. public $timeout = 1000;
  80. /**
  81. * @var boolean|integer how to scroll the page when pjax response is received. If false, no page scroll will be made.
  82. * Use a number if you want to scroll to a particular place.
  83. */
  84. public $scrollTo = false;
  85. /**
  86. * @var array additional options to be passed to the pjax JS plugin. Please refer to the
  87. * [pjax project page](https://github.com/yiisoft/jquery-pjax) for available options.
  88. */
  89. public $clientOptions;
  90. /**
  91. * @inheritdoc
  92. */
  93. public function init()
  94. {
  95. if (!isset($this->options['id'])) {
  96. $this->options['id'] = $this->getId();
  97. }
  98. if ($this->requiresPjax()) {
  99. ob_start();
  100. ob_implicit_flush(false);
  101. $view = $this->getView();
  102. $view->clear();
  103. $view->beginPage();
  104. $view->head();
  105. $view->beginBody();
  106. if ($view->title !== null) {
  107. echo Html::tag('title', Html::encode($view->title));
  108. }
  109. } else {
  110. $options = $this->options;
  111. $tag = ArrayHelper::remove($options, 'tag', 'div');
  112. echo Html::beginTag($tag, array_merge([
  113. 'data-pjax-container' => '',
  114. 'data-pjax-push-state' => $this->enablePushState,
  115. 'data-pjax-replace-state' => $this->enableReplaceState,
  116. 'data-pjax-timeout' => $this->timeout,
  117. 'data-pjax-scrollto' => $this->scrollTo,
  118. ], $options));
  119. }
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. public function run()
  125. {
  126. if (!$this->requiresPjax()) {
  127. echo Html::endTag(ArrayHelper::remove($this->options, 'tag', 'div'));
  128. $this->registerClientScript();
  129. return;
  130. }
  131. $view = $this->getView();
  132. $view->endBody();
  133. // Do not re-send css files as it may override the css files that were loaded after them.
  134. // This is a temporary fix for https://github.com/yiisoft/yii2/issues/2310
  135. // It should be removed once pjax supports loading only missing css files
  136. $view->cssFiles = null;
  137. $view->endPage(true);
  138. $content = ob_get_clean();
  139. // only need the content enclosed within this widget
  140. $response = Yii::$app->getResponse();
  141. $response->clearOutputBuffers();
  142. $response->setStatusCode(200);
  143. $response->format = Response::FORMAT_HTML;
  144. $response->content = $content;
  145. $response->send();
  146. Yii::$app->end();
  147. }
  148. /**
  149. * @return boolean whether the current request requires pjax response from this widget
  150. */
  151. protected function requiresPjax()
  152. {
  153. $headers = Yii::$app->getRequest()->getHeaders();
  154. return $headers->get('X-Pjax') && $headers->get('X-Pjax-Container') === '#' . $this->options['id'];
  155. }
  156. /**
  157. * Registers the needed JavaScript.
  158. */
  159. public function registerClientScript()
  160. {
  161. $id = $this->options['id'];
  162. $this->clientOptions['push'] = $this->enablePushState;
  163. $this->clientOptions['replace'] = $this->enableReplaceState;
  164. $this->clientOptions['timeout'] = $this->timeout;
  165. $this->clientOptions['scrollTo'] = $this->scrollTo;
  166. $options = Json::htmlEncode($this->clientOptions);
  167. $js = '';
  168. if ($this->linkSelector !== false) {
  169. $linkSelector = Json::htmlEncode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
  170. $js .= "jQuery(document).pjax($linkSelector, \"#$id\", $options);";
  171. }
  172. if ($this->formSelector !== false) {
  173. $formSelector = Json::htmlEncode($this->formSelector !== null ? $this->formSelector : '#' . $id . ' form[data-pjax]');
  174. $js .= "\njQuery(document).on('submit', $formSelector, function (event) {jQuery.pjax.submit(event, '#$id', $options);});";
  175. }
  176. $view = $this->getView();
  177. PjaxAsset::register($view);
  178. if ($js !== '') {
  179. $view->registerJs($js);
  180. }
  181. }
  182. }