This repository was archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCiinfiniteScroll.php
More file actions
121 lines (102 loc) · 4.23 KB
/
CiinfiniteScroll.php
File metadata and controls
121 lines (102 loc) · 4.23 KB
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
<?php
/**
* This extension uses the infinite scroll jQuery plugin, from
* http://www.infinite-scroll.com/ to create an infinite scrolling pagination,
* like in twitter.
*
* It uses javascript to load and parse the new pages, but gracefully degrade
* in cases where javascript is disabled and the users will still be able to
* access all the pages.
*
* Based on http://www.yiiframework.com/extension/yiinfinite-scroll by @author davi_alexandre
* @author Charles R. Portwood II <charlesportwoodii@ethreal.net>
*/
class CiinfiniteScroll extends CBasePager
{
public $contentSelector = '#content';
private $_options = array(
'url' => null,
'debug' => false,
'defaultCallback' => 'js:function(text, data) { }',
'errorCallback' => 'js:function(data) { $(".infinite_navigation").fadeOut(); }',
'loading' => array(
'img' => 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
'finishedMsg' => null,
'msgText' => 'Loading more content...',
'selector' => null,
'speed' => 'fast',
),
'param' => array(
'getParam' => null,
'param' => null
),
);
private $_default_options = array(
'navSelector' => 'div.infinite_navigation',
'nextSelector' => 'div.infinite_navigation a:first',
'bufferPx' => '300',
);
private $_callback = null;
public function init()
{
Yii::setPathOfAlias('ext.yiinfinite-scroll.assets', Yii::getPathOfAlias('vendor.charlesportwoodii.ciinfinite-scroll.assets'));
$this->getPages()->validateCurrentPage = false;
$this->_options['loadingImg'] = Yii::app()->baseUrl.'/images/infinite-loading.gif';
parent::init();
}
public function run()
{
$this->registerClientScript();
$this->createInfiniteScrollScript();
$this->renderNavigation();
if($this->getPages()->getPageCount() > 0 && $this->theresNoMorePages())
throw new CHttpException(404);
}
public function __get($name)
{
if(array_key_exists($name, $this->_options))
return $this->_options[$name];
return parent::__get($name);
}
public function __set($name, $value)
{
if(array_key_exists($name, $this->_options))
{
return $this->_options[$name] = $value;
}
return parent::__set($name, $value);
}
public function registerClientScript() {
$url = CHtml::asset(Yii::getPathOfAlias('ext.yiinfinite-scroll.assets').'/jquery.infinitescroll.min.js');
Yii::app()->clientScript->registerScriptFile($url, CCLientScript::POS_END);
}
private function createInfiniteScrollScript()
{
Yii::app()->clientScript->registerScript(uniqid(), "$('{$this->contentSelector}').infinitescroll(".$this->buildInifiniteScrollOptions().");");
}
private function buildInifiniteScrollOptions()
{
$options = array_merge($this->_options, $this->_default_options);
$options = array_filter( $options );
$options = CJavaScript::encode($options);
return $options;
}
private function renderNavigation()
{
$next_link = CHtml::link('<strong style="width: 93px;">Load More</strong>
<span></span>',$this->createPageUrl($this->getCurrentPage()+1), array('id' => 'more', 'escape' => true));
Yii::app()->clientScript->registerScript(uniqid() . 'bind-scroll', "$('#more').click(function(e) { e.preventDefault(); $('#posts').infinitescroll('retrieve'); $('#posts').infinitescroll('updateUrl'); });");
echo '<div class="infinite_navigation">'.$next_link.'</div>';
}
private function theresNoMorePages()
{
return $this->getPages()->getCurrentPage() >= $this->getPages()->getPageCount();
}
public function createPageUrl($id=1)
{
$queryParam = NULL;
if (Cii::get($this->_options['param'], 'getParam', NULL) != NULL)
$queryParam = '?' . Cii::get($this->_options['param'], 'getParam', NULL) . '=' . Cii::get($this->_options['param'], 'param', NULL);
return '/'.$this->_options['url'] .'/' . ($id+1) . $queryParam;
}
}