This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace kop\y2sp; |
||
4 | |||
5 | use kop\y2sp\assets\InfiniteAjaxScrollAsset; |
||
6 | use Yii; |
||
7 | use yii\base\InvalidConfigException; |
||
8 | use yii\base\Widget; |
||
9 | use yii\helpers\ArrayHelper; |
||
10 | use yii\helpers\Json; |
||
11 | use yii\i18n\PhpMessageSource; |
||
12 | use yii\web\JsExpression; |
||
13 | use yii\web\View; |
||
14 | use yii\widgets\LinkPager; |
||
15 | |||
16 | /** |
||
17 | * ScrollPager turns your regular paginated page into an infinite scrolling page using AJAX. |
||
18 | * |
||
19 | * ScrollPager works with a [[Pagination]] object which specifies the totally number of pages and the current page number. |
||
20 | * |
||
21 | * <br> |
||
22 | * <i>Example usage:</i> |
||
23 | * <code> |
||
24 | * echo ListView::widget([ |
||
25 | * 'dataProvider' => $dataProvider, |
||
26 | * 'itemOptions' => ['class' => 'item'], |
||
27 | * 'itemView' => '_item_view', |
||
28 | * 'pager' => ['class' => \kop\y2sp\ScrollPager::className()] |
||
29 | * ]); |
||
30 | * </code> |
||
31 | * |
||
32 | * This widget is using {@link http://infiniteajaxscroll.com/ JQuery Infinite Ajax Scroll plugin}. |
||
33 | * |
||
34 | * @link http://kop.github.io/yii2-scroll-pager Y2SP project page. |
||
35 | * @license https://github.com/kop/yii2-scroll-pager/blob/master/LICENSE.md MIT |
||
36 | * |
||
37 | * @author Ivan Koptiev <[email protected]> |
||
38 | */ |
||
39 | class ScrollPager extends Widget |
||
40 | { |
||
41 | /** |
||
42 | * @const EXTENSION_TRIGGER IAS Extension "IASTriggerExtension". |
||
43 | */ |
||
44 | const EXTENSION_TRIGGER = 'IASTriggerExtension'; |
||
45 | |||
46 | /** |
||
47 | * @const EXTENSION_SPINNER IAS Extension "IASSpinnerExtension". |
||
48 | */ |
||
49 | const EXTENSION_SPINNER = 'IASSpinnerExtension'; |
||
50 | |||
51 | /** |
||
52 | * @const EXTENSION_NONE_LEFT IAS Extension "IASNoneLeftExtension". |
||
53 | */ |
||
54 | const EXTENSION_NONE_LEFT = 'IASNoneLeftExtension'; |
||
55 | |||
56 | /** |
||
57 | * @const EXTENSION_PAGING IAS Extension "IASPagingExtension". |
||
58 | */ |
||
59 | const EXTENSION_PAGING = 'IASPagingExtension'; |
||
60 | |||
61 | /** |
||
62 | * @const EXTENSION_HISTORY IAS Extension "IASHistoryExtension". |
||
63 | */ |
||
64 | const EXTENSION_HISTORY = 'IASHistoryExtension'; |
||
65 | |||
66 | /** |
||
67 | * @var string $container Enter the selector of the element containing your items that you want to paginate. |
||
68 | */ |
||
69 | public $container = '.list-view'; |
||
70 | |||
71 | /** |
||
72 | * @var string $item Enter the selector of the element that each item has. |
||
73 | * Make sure the elements are inside the container element. |
||
74 | */ |
||
75 | public $item = '.item'; |
||
76 | |||
77 | /** |
||
78 | * @var string $paginationSelector Enter the selector of the element containing the pagination. |
||
79 | */ |
||
80 | public $paginationSelector = '.list-view .pagination'; |
||
81 | |||
82 | /** |
||
83 | * @var string $next Enter the selector of the link element that links to the next page. |
||
84 | * The href attribute of this element will be used to get the items from the next page. |
||
85 | * Make sure there is only one(1) element that matches the selector. |
||
86 | */ |
||
87 | public $next = '.next a'; |
||
88 | |||
89 | /** |
||
90 | * @var int $delay Minimal number of milliseconds to stay in a loading state. |
||
91 | */ |
||
92 | public $delay = 600; |
||
93 | |||
94 | /** |
||
95 | * @var int $thresholdMargin On default IAS starts loading new items when you scroll to the latest .item element. |
||
96 | * The negativeMargin will be added to the items' offset, giving you the ability to load new items earlier |
||
97 | * (please note that the margin is always transformed to a negative integer). |
||
98 | * <br><br> |
||
99 | * <i>For example:</i> |
||
100 | * <br> |
||
101 | * Setting a negativeMargin of 250 means that IAS will start loading 250 pixel before the last item has scrolled into view. |
||
102 | */ |
||
103 | public $negativeMargin = 10; |
||
104 | |||
105 | /** |
||
106 | * @var string $triggerText Text of trigger the link. |
||
107 | * Default: "Load more items". |
||
108 | */ |
||
109 | public $triggerText; |
||
110 | |||
111 | /** |
||
112 | * @var string $triggerTemplate Allows you to override the trigger html template. |
||
113 | */ |
||
114 | public $triggerTemplate = '<div class="ias-trigger" style="text-align: center; cursor: pointer;"><a>{text}</a></div>'; |
||
115 | |||
116 | /** |
||
117 | * @var int $triggerOffset The number of pages which should load automatically. |
||
118 | * After that the trigger is shown for every subsequent page. |
||
119 | * <br><br> |
||
120 | * <i>For example:</i> |
||
121 | * <br> |
||
122 | * if you set the offset to 2, the pages 2 and 3 (page 1 is always shown) would load automatically and for every |
||
123 | * subsequent page the user has to press the trigger to load it. |
||
124 | */ |
||
125 | public $triggerOffset = 0; |
||
126 | |||
127 | /** |
||
128 | * @var string $spinnerSrc The src attribute of the spinner image. |
||
129 | */ |
||
130 | public $spinnerSrc; |
||
131 | |||
132 | /** |
||
133 | * @var string $spinnerTemplate Allows you to override the spinner html template. |
||
134 | */ |
||
135 | public $spinnerTemplate = '<div class="ias-spinner" style="text-align: center;"><img src="{src}"/></div>'; |
||
136 | |||
137 | /** |
||
138 | * @var string $noneLeftText Text of the "nothing left" message. |
||
139 | * Default: "You reached the end". |
||
140 | */ |
||
141 | public $noneLeftText; |
||
142 | |||
143 | /** |
||
144 | * @var string $noneLeftTemplate Allows you to override the "nothing left" message html template. |
||
145 | */ |
||
146 | public $noneLeftTemplate = '<div class="ias-noneleft" style="text-align: center;">{text}</div>'; |
||
147 | |||
148 | /** |
||
149 | * @var string $historyPrev Enter the selector of the link element that links to the previous page. |
||
150 | * The href attribute of this element will be used to get the items from the previous page. |
||
151 | * Make sure there is only one element that matches the selector. |
||
152 | */ |
||
153 | public $historyPrev = '.previous'; |
||
154 | |||
155 | /** |
||
156 | * @var string $overflowContainer A selector for "div" HTML element to use as an overflow container. |
||
157 | * @see http://infiniteajaxscroll.com/examples/overflow.html |
||
158 | */ |
||
159 | public $overflowContainer; |
||
160 | |||
161 | /** |
||
162 | * @var string|JsExpression $eventOnScroll Triggered when the visitors scrolls. |
||
163 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
164 | */ |
||
165 | public $eventOnScroll; |
||
166 | |||
167 | /** |
||
168 | * @var string|JsExpression $eventOnLoad Triggered when a new url will be loaded from the server. |
||
169 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
170 | */ |
||
171 | public $eventOnLoad; |
||
172 | |||
173 | /** |
||
174 | * @var string|JsExpression $eventOnLoaded Triggered after a new page was loaded from the server. |
||
175 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
176 | */ |
||
177 | public $eventOnLoaded; |
||
178 | |||
179 | /** |
||
180 | * @var string|JsExpression $eventOnRender Triggered before new items will be rendered. |
||
181 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
182 | */ |
||
183 | public $eventOnRender; |
||
184 | |||
185 | /** |
||
186 | * @var string|JsExpression $eventOnRendered Triggered after new items have rendered. |
||
187 | * Note: This event is only fired once. |
||
188 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
189 | */ |
||
190 | public $eventOnRendered; |
||
191 | |||
192 | /** |
||
193 | * @var string|JsExpression $eventOnNoneLeft Triggered when there are no more pages left. |
||
194 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
195 | */ |
||
196 | public $eventOnNoneLeft; |
||
197 | |||
198 | /** |
||
199 | * @var string|JsExpression $eventOnNext Triggered when the next page should be loaded. |
||
200 | * Happens before loading of the next page starts. With this event it is possible to cancel the loading of the next page. |
||
201 | * You can do this by returning false from your callback. |
||
202 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
203 | */ |
||
204 | public $eventOnNext; |
||
205 | |||
206 | /** |
||
207 | * @var string|JsExpression $eventOnReady Triggered when IAS and all the extensions have been initialized. |
||
208 | * @see http://infiniteajaxscroll.com/docs/events.html |
||
209 | */ |
||
210 | public $eventOnReady; |
||
211 | |||
212 | /** |
||
213 | * @var string|JsExpression $eventOnPageChange Triggered when a used scroll to another page. |
||
214 | * @see http://infiniteajaxscroll.com/docs/extension-paging.html |
||
215 | */ |
||
216 | public $eventOnPageChange; |
||
217 | |||
218 | /** |
||
219 | * @var array $enabledExtensions The list of the enabled plugin extensions. |
||
220 | */ |
||
221 | public $enabledExtensions = [ |
||
222 | self::EXTENSION_TRIGGER, |
||
223 | self::EXTENSION_SPINNER, |
||
224 | self::EXTENSION_NONE_LEFT, |
||
225 | self::EXTENSION_PAGING, |
||
226 | self::EXTENSION_HISTORY |
||
227 | ]; |
||
228 | |||
229 | /** |
||
230 | * @var \yii\data\Pagination The pagination object that this pager is associated with. |
||
231 | * You must set this property in order to make ScrollPager work. |
||
232 | */ |
||
233 | public $pagination; |
||
234 | |||
235 | /** |
||
236 | * Initializes the pager. |
||
237 | */ |
||
238 | public function init() |
||
239 | { |
||
240 | parent::init(); |
||
241 | |||
242 | // Register translations source |
||
243 | Yii::$app->i18n->translations = ArrayHelper::merge(Yii::$app->i18n->translations, [ |
||
244 | 'kop\y2sp' => [ |
||
245 | 'class' => PhpMessageSource::className(), |
||
246 | 'basePath' => '@vendor/kop/yii2-scroll-pager/messages', |
||
247 | 'fileMap' => [ |
||
248 | 'kop\y2sp' => 'general.php' |
||
249 | ] |
||
250 | ] |
||
251 | ]); |
||
252 | |||
253 | // Register required assets |
||
254 | $this->registerAssets(); |
||
255 | |||
256 | // Set default trigger text if not set |
||
257 | if ($this->triggerText === null) { |
||
258 | $this->triggerText = Yii::t('kop\y2sp', 'Load more items'); |
||
259 | } |
||
260 | |||
261 | // Set default "none left" message text if not set |
||
262 | if ($this->noneLeftText === null) { |
||
263 | $this->noneLeftText = Yii::t('kop\y2sp', 'You reached the end'); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Executes the widget. |
||
269 | * |
||
270 | * This overrides the parent implementation by initializing jQuery IAS and displaying the generated page buttons. |
||
271 | * |
||
272 | * @throws \yii\base\InvalidConfigException |
||
273 | * @return mixed |
||
274 | */ |
||
275 | public function run() |
||
276 | { |
||
277 | // Initialize jQuery IAS plugin |
||
278 | $pluginSettings = Json::encode([ |
||
279 | 'container' => $this->container, |
||
280 | 'item' => $this->item, |
||
281 | 'pagination' => $this->paginationSelector, |
||
282 | 'next' => $this->next, |
||
283 | 'delay' => $this->delay, |
||
284 | 'negativeMargin' => $this->negativeMargin |
||
285 | ]); |
||
286 | $initString = empty($this->overflowContainer) |
||
287 | ? "var {$this->id}_ias = jQuery.ias({$pluginSettings});" |
||
288 | : "var {$this->id}_ias = jQuery('{$this->overflowContainer}').ias({$pluginSettings});"; |
||
289 | $this->view->registerJs($initString, View::POS_READY, "{$this->id}_ias_main"); |
||
290 | |||
291 | // Register IAS extensions |
||
292 | $this->registerExtensions([ |
||
293 | [ |
||
294 | 'name' => self::EXTENSION_PAGING |
||
295 | ], |
||
296 | [ |
||
297 | 'name' => self::EXTENSION_SPINNER, |
||
298 | 'options' => |
||
299 | !empty($this->spinnerSrc) |
||
300 | ? ['html' => $this->spinnerTemplate, 'src' => $this->spinnerSrc] |
||
301 | : ['html' => $this->spinnerTemplate] |
||
302 | ], |
||
303 | [ |
||
304 | 'name' => self::EXTENSION_TRIGGER, |
||
305 | 'options' => [ |
||
306 | 'text' => $this->triggerText, |
||
307 | 'html' => $this->triggerTemplate, |
||
308 | 'offset' => $this->triggerOffset |
||
309 | ] |
||
310 | ], |
||
311 | [ |
||
312 | 'name' => self::EXTENSION_NONE_LEFT, |
||
313 | 'options' => [ |
||
314 | 'text' => $this->noneLeftText, |
||
315 | 'html' => $this->noneLeftTemplate |
||
316 | ] |
||
317 | ], |
||
318 | [ |
||
319 | 'name' => self::EXTENSION_HISTORY, |
||
320 | 'options' => [ |
||
321 | 'prev' => $this->historyPrev |
||
322 | ], |
||
323 | 'depends' => [ |
||
324 | self::EXTENSION_TRIGGER, |
||
325 | self::EXTENSION_PAGING |
||
326 | ] |
||
327 | ] |
||
328 | ]); |
||
329 | |||
330 | // Register event handlers |
||
331 | $this->registerEventHandlers([ |
||
332 | 'scroll' => [], |
||
333 | 'load' => [], |
||
334 | 'loaded' => [], |
||
335 | 'render' => [], |
||
336 | 'rendered' => [], |
||
337 | 'noneLeft' => [], |
||
338 | 'next' => [], |
||
339 | 'ready' => [], |
||
340 | 'pageChange' => [ |
||
341 | self::EXTENSION_PAGING |
||
342 | ] |
||
343 | ]); |
||
344 | |||
345 | // Render pagination links |
||
346 | echo LinkPager::widget([ |
||
347 | 'pagination' => $this->pagination, |
||
348 | 'options' => [ |
||
349 | 'class' => 'pagination hidden' |
||
350 | ] |
||
351 | ]); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Register required asset bundles. |
||
356 | * |
||
357 | * You can override this method in case if you want to use your own JQuery Infinite Ajax Scroll plugin files |
||
358 | * (for example, some forked plugin version). |
||
359 | */ |
||
360 | protected function registerAssets() |
||
361 | { |
||
362 | InfiniteAjaxScrollAsset::register($this->view); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Register jQuery IAS extensions. |
||
367 | * |
||
368 | * This method takes jQuery IAS extensions definition as a parameter and registers this extensions. |
||
369 | * |
||
370 | * @param array $config jQuery IAS extensions definition. |
||
371 | * @throws \yii\base\InvalidConfigException If extension dependencies are not met. |
||
372 | */ |
||
373 | protected function registerExtensions(array $config) |
||
374 | { |
||
375 | foreach ($config as $entry) { |
||
376 | |||
377 | // Parse config entry values |
||
378 | $name = ArrayHelper::getValue($entry, 'name', false); |
||
379 | $options = ArrayHelper::getValue($entry, 'options', ''); |
||
380 | $depends = ArrayHelper::getValue($entry, 'depends', []); |
||
381 | |||
382 | // If extension is enabled |
||
383 | if (in_array($name, $this->enabledExtensions)) { |
||
384 | |||
385 | // Make sure dependencies are met |
||
386 | View Code Duplication | if (!$this->checkEnabledExtensions($depends)) { |
|
0 ignored issues
–
show
|
|||
387 | throw new InvalidConfigException( |
||
388 | "Extension {$name} requires " . implode(', ', $depends) . " extensions to be enabled." |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | // Register extension |
||
393 | $options = Json::encode($options); |
||
394 | $this->view->registerJs( |
||
395 | "{$this->id}_ias.extension(new {$name}({$options}));", |
||
396 | View::POS_READY, |
||
397 | "{$this->id}_ias_{$name}" |
||
398 | ); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Register jQuery IAS event handlers. |
||
405 | * |
||
406 | * This method takes jQuery IAS event handlers definition as a parameter and registers this event handlers. |
||
407 | * |
||
408 | * @param array $config jQuery IAS event handlers definition. |
||
409 | * @throws \yii\base\InvalidConfigException If vent handlers dependencies are not met. |
||
410 | */ |
||
411 | protected function registerEventHandlers(array $config) |
||
412 | { |
||
413 | foreach ($config as $name => $depends) { |
||
414 | |||
415 | // If event is enabled |
||
416 | $eventName = 'eventOn' . ucfirst($name); |
||
417 | if (!empty($this->$eventName)) { |
||
418 | |||
419 | // Make sure dependencies are met |
||
420 | View Code Duplication | if (!$this->checkEnabledExtensions($depends)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
421 | throw new InvalidConfigException( |
||
422 | "The \"{$name}\" event requires " . implode(', ', $depends) . " extensions to be enabled." |
||
423 | ); |
||
424 | } |
||
425 | |||
426 | // Register event |
||
427 | $this->view->registerJs( |
||
428 | "{$this->id}_ias.on('{$name}', {$this->$eventName});", |
||
429 | View::POS_READY, |
||
430 | "{$this->id}_ias_event_{$eventName}" |
||
431 | ); |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Check whether the given extensions are enabled. |
||
438 | * |
||
439 | * @param string|array $extensions Single or multiple extensions names. |
||
440 | * @return bool Operation result. |
||
441 | */ |
||
442 | protected function checkEnabledExtensions($extensions) |
||
443 | { |
||
444 | $extensions = (array)$extensions; |
||
445 | if (empty($extensions)) { |
||
446 | return true; |
||
447 | } else { |
||
448 | return (count(array_intersect($this->enabledExtensions, $extensions)) == count($extensions)); |
||
449 | } |
||
450 | } |
||
451 | } |
||
452 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.