1 | <?php |
||
45 | class Pjax extends Widget |
||
46 | { |
||
47 | /** |
||
48 | * @var array the HTML attributes for the widget container tag. The following special options are recognized: |
||
49 | * |
||
50 | * - `tag`: string, the tag name for the container. Defaults to `div` |
||
51 | * This option is available since version 2.0.7. |
||
52 | * See also [[\yii\helpers\Html::tag()]]. |
||
53 | * |
||
54 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
55 | */ |
||
56 | public $options = []; |
||
57 | /** |
||
58 | * @var string|false the jQuery selector of the links that should trigger pjax requests. |
||
59 | * If not set, all links within the enclosed content of Pjax will trigger pjax requests. |
||
60 | * If set to false, no code will be registered to handle links. |
||
61 | * Note that if the response to the pjax request is a full page, a normal request will be sent again. |
||
62 | */ |
||
63 | public $linkSelector; |
||
64 | /** |
||
65 | * @var string|false the jQuery selector of the forms whose submissions should trigger pjax requests. |
||
66 | * If not set, all forms with `data-pjax` attribute within the enclosed content of Pjax will trigger pjax requests. |
||
67 | * If set to false, no code will be registered to handle forms. |
||
68 | * Note that if the response to the pjax request is a full page, a normal request will be sent again. |
||
69 | */ |
||
70 | public $formSelector; |
||
71 | /** |
||
72 | * @var string The jQuery event that will trigger form handler. Defaults to "submit". |
||
73 | * @since 2.0.9 |
||
74 | */ |
||
75 | public $submitEvent = 'submit'; |
||
76 | /** |
||
77 | * @var bool whether to enable push state. |
||
78 | */ |
||
79 | public $enablePushState = true; |
||
80 | /** |
||
81 | * @var bool whether to enable replace state. |
||
82 | */ |
||
83 | public $enableReplaceState = false; |
||
84 | /** |
||
85 | * @var int pjax timeout setting (in milliseconds). This timeout is used when making AJAX requests. |
||
86 | * Use a bigger number if your server is slow. If the server does not respond within the timeout, |
||
87 | * a full page load will be triggered. |
||
88 | */ |
||
89 | public $timeout = 1000; |
||
90 | /** |
||
91 | * @var bool|int how to scroll the page when pjax response is received. If false, no page scroll will be made. |
||
92 | * Use a number if you want to scroll to a particular place. |
||
93 | */ |
||
94 | public $scrollTo = false; |
||
95 | /** |
||
96 | * @var array additional options to be passed to the pjax JS plugin. Please refer to the |
||
97 | * [pjax project page](https://github.com/yiisoft/jquery-pjax) for available options. |
||
98 | */ |
||
99 | public $clientOptions; |
||
100 | /** |
||
101 | * @inheritdoc |
||
102 | * @internal |
||
103 | */ |
||
104 | public static $counter = 0; |
||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | public static $autoIdPrefix = 'p'; |
||
109 | |||
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 2 | public function init() |
|
115 | { |
||
116 | 2 | if (!isset($this->options['id'])) { |
|
117 | 2 | $this->options['id'] = $this->getId(); |
|
118 | } |
||
119 | |||
120 | 2 | if ($this->requiresPjax()) { |
|
121 | 1 | ob_start(); |
|
122 | 1 | ob_implicit_flush(false); |
|
123 | 1 | $view = $this->getView(); |
|
124 | 1 | $view->clear(); |
|
125 | 1 | $view->beginPage(); |
|
126 | 1 | $view->head(); |
|
127 | 1 | $view->beginBody(); |
|
128 | 1 | if ($view->title !== null) { |
|
129 | 1 | echo Html::tag('title', Html::encode($view->title)); |
|
130 | } |
||
131 | } else { |
||
132 | 1 | $options = $this->options; |
|
133 | 1 | $tag = ArrayHelper::remove($options, 'tag', 'div'); |
|
134 | 1 | echo Html::beginTag($tag, array_merge([ |
|
135 | 1 | 'data-pjax-container' => '', |
|
136 | 1 | 'data-pjax-push-state' => $this->enablePushState, |
|
137 | 1 | 'data-pjax-replace-state' => $this->enableReplaceState, |
|
138 | 1 | 'data-pjax-timeout' => $this->timeout, |
|
139 | 1 | 'data-pjax-scrollto' => $this->scrollTo, |
|
140 | 1 | ], $options)); |
|
141 | } |
||
142 | 2 | } |
|
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 1 | public function run() |
|
148 | { |
||
149 | 1 | if (!$this->requiresPjax()) { |
|
150 | 1 | echo Html::endTag(ArrayHelper::remove($this->options, 'tag', 'div')); |
|
151 | 1 | $this->registerClientScript(); |
|
152 | |||
153 | 1 | return; |
|
154 | } |
||
155 | |||
156 | $view = $this->getView(); |
||
157 | $view->endBody(); |
||
158 | |||
159 | // Do not re-send css files as it may override the css files that were loaded after them. |
||
160 | // This is a temporary fix for https://github.com/yiisoft/yii2/issues/2310 |
||
161 | // It should be removed once pjax supports loading only missing css files |
||
162 | $view->cssFiles = null; |
||
|
|||
163 | |||
164 | $view->endPage(true); |
||
165 | |||
166 | $content = ob_get_clean(); |
||
167 | |||
168 | // only need the content enclosed within this widget |
||
169 | $response = Yii::$app->getResponse(); |
||
170 | $response->clearOutputBuffers(); |
||
171 | $response->setStatusCode(200); |
||
172 | $response->format = Response::FORMAT_HTML; |
||
173 | $response->content = $content; |
||
174 | $response->getHeaderCollection()->setDefault('X-Pjax-Url', Yii::$app->request->url); |
||
175 | $response->send(); |
||
176 | |||
177 | Yii::$app->end(); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return bool whether the current request requires pjax response from this widget |
||
182 | */ |
||
183 | 2 | protected function requiresPjax() |
|
188 | |||
189 | /** |
||
190 | * Registers the needed JavaScript. |
||
191 | */ |
||
192 | 1 | public function registerClientScript() |
|
220 | } |
||
221 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..