PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/yiinfinite-scroll/YiinfiniteScroller.php

https://bitbucket.org/DrMabuse/cms_app
PHP | 122 lines | 88 code | 19 blank | 15 comment | 4 complexity | ec1fe88eb46d46a1c86d1de722b0ceb9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This extension uses the infinite scroll jQuery plugin, from
  4. * http://www.infinite-scroll.com/ to create an infinite scrolling pagination,
  5. * like in twitter.
  6. *
  7. * It uses javascript to load and parse the new pages, but gracefully degrade
  8. * in cases where javascript is disabled and the users will still be able to
  9. * access all the pages.
  10. *
  11. * @author davi_alexandre
  12. */
  13. class YiinfiniteScroller extends CBasePager {
  14. public $contentSelector = '#content';
  15. private $_options = array(
  16. 'loadingImg' => null,
  17. 'loadingText' => null,
  18. 'donetext' => null,
  19. 'itemSelector' => null,
  20. 'errorCallback' => null,
  21. );
  22. private $_default_options = array(
  23. 'navSelector' => 'div.infinite_navigation',
  24. 'nextSelector' => 'div.infinite_navigation a:first',
  25. 'bufferPx' => '300',
  26. );
  27. public function init() {
  28. $this->getPages()->validateCurrentPage = false;
  29. parent::init();
  30. }
  31. public function run() {
  32. $this->registerClientScript();
  33. $this->createInfiniteScrollScript();
  34. $this->renderNavigation();
  35. if($this->getPages()->getPageCount() > 0 && $this->theresNoMorePages()) {
  36. throw new CHttpException(404);
  37. }
  38. }
  39. public function __get($name) {
  40. if(array_key_exists($name, $this->_options)) {
  41. return $this->_options[$name];
  42. }
  43. return parent::__get($name);
  44. }
  45. public function __set($name, $value) {
  46. if(array_key_exists($name, $this->_options)) {
  47. return $this->_options[$name] = $value;
  48. }
  49. return parent::__set($name, $value);
  50. }
  51. public function registerClientScript() {
  52. $url = CHtml::asset(Yii::getPathOfAlias('ext.yiinfinite-scroll.assets').'/jquery.infinitescroll.min.js');
  53. $masonry = CHtml::asset(Yii::getPathOfAlias('ext.yiinfinite-scroll.assets').'/jquery.masonry.min.js');
  54. Yii::app()->clientScript->registerScriptFile($url);
  55. Yii::app()->clientScript->registerScriptFile($masonry);
  56. }
  57. private function createInfiniteScrollScript() {
  58. Yii::app()->clientScript->registerScript(
  59. uniqid(),
  60. " var container = $('#content');
  61. var item = '.post';
  62. container.imagesLoaded(function () {
  63. container.masonry({
  64. itemSelector:item
  65. });
  66. });
  67. container.infinitescroll({
  68. 'loadingText':'Loading...',
  69. 'donetext':'This is the end... my only friend, the end',
  70. 'itemSelector':'.post',
  71. 'navSelector':'div.infinite_navigation',
  72. 'nextSelector':'div.infinite_navigation a:first',
  73. 'bufferPx':'300'
  74. },
  75. // trigger Masonry as a callback
  76. function (newElements) {
  77. // hide new items while they are loading
  78. var newElems = $(newElements).css({ opacity:0 });
  79. // ensure that images load before adding to masonry layout
  80. newElems.imagesLoaded(function () {
  81. // show elems now they're ready
  82. newElems.animate({ opacity:1 });
  83. container.masonry('appended', newElems, true);
  84. });
  85. }
  86. );"
  87. );
  88. }
  89. private function buildInifiniteScrollOptions() {
  90. $options = array_merge($this->_options, $this->_default_options);
  91. $options = array_filter( $options );
  92. $options = CJavaScript::encode($options);
  93. return $options;
  94. }
  95. private function renderNavigation() {
  96. $next_link = CHtml::link('next',$this->createPageUrl($this->getCurrentPage(false)+1));
  97. echo '<div class="infinite_navigation">'.$next_link.'</div>';
  98. }
  99. private function theresNoMorePages() {
  100. return $this->getPages()->getCurrentPage() >= $this->getPages()->getPageCount();
  101. }
  102. }
  103. ?>