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