topic_widget.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @var $post \app\models\News
  4. * @var \yii\data\ActiveDataProvider $newsDataProvider
  5. * @var \app\models\base\NewsTopic | \app\models\base\Story $topic
  6. * @var \app\models\Design $design
  7. */
  8. $topic_picture = $topic->image;
  9. $url = '';
  10. if( $topic instanceof \app\models\base\NewsTopic ) $url = '/news/';
  11. if( $topic instanceof \app\models\base\Story ) $url = '/story/';
  12. if( $topic->ext != '' ){
  13. $ext = json_decode($topic->ext, false, JSON_THROW_ON_ERROR);
  14. if( isset( $ext->brightness ) ){
  15. $brightness = $ext->brightness;
  16. }
  17. }
  18. ?>
  19. <div class="row topic-widget widget" id="design-<?=$design->id?>">
  20. <div class="col-md-12">
  21. <div class="cont">
  22. <?php if($topic_picture instanceof \app\models\TopicImages || $topic_picture instanceof \app\models\StoryImages):?>
  23. <picture class="w-100"<?=isset($brightness)?' style="filter: brightness('.$brightness.'%);"':''?>>
  24. <source srcset="<?=$topic_picture->url?>" media="(min-width: 600px)">
  25. <source srcset="<?=$topic_picture->getUrl(\app\models\TopicImages::SIZE_SD_16_9)?>" media="(max-width: 500px)">
  26. <img src="<?=$topic_picture->url?>" alt="<?=htmlentities($topic->title,ENT_QUOTES)?>" class="w-100" loading="lazy">
  27. </picture>
  28. <?php endif;?>
  29. <div class="content">
  30. <div class="topic-title">
  31. <a href="<?=$url?><?=$design->url!=""?$design->url:$topic->url?>"><?=$design->title!=""?$design->title:$topic->title?></a>
  32. </div>
  33. <div class="swipe-icon"></div>
  34. <div class="topic-news">
  35. <?php
  36. echo Yii::$app->cache->getOrSet("main-page-topic-widget-".$design->id,function () use ($newsDataProvider){
  37. return \yii\widgets\ListView::widget([
  38. "dataProvider" => $newsDataProvider,
  39. 'options' => [
  40. 'tag' => false,
  41. 'class' => 'list-wrapper',
  42. 'id' => 'list-wrapper',
  43. ],
  44. "itemOptions"=>[
  45. "tag"=>false
  46. ],
  47. "itemView" => "_topic_widget_item",
  48. "layout" => "{items}"
  49. ]);
  50. },60);
  51. ?>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. <script>
  58. (function (){
  59. function scroll(container, value, duration) {
  60. if (duration <= 0) return;
  61. var difference = value - container.scrollLeft;
  62. var perTick = difference / duration * 10;
  63. setTimeout(function() {
  64. container.scrollLeft= container.scrollLeft + perTick;
  65. if (container.scrollLeft === value) return;
  66. scroll(container, value, duration - 10);
  67. }, 10);
  68. }
  69. const container = document.getElementById("design-<?=$design->id?>").getElementsByClassName('topic-news')[0];
  70. const items = container.getElementsByClassName('topic-widget-item');
  71. const style = items[0].currentStyle || window.getComputedStyle(items[0]);
  72. const itemWidth = parseInt(style.width.replace(/px/,""));
  73. const shift = itemWidth + parseInt(style.marginRight.replace(/px/,"")) * 2;
  74. const next = document.createElement('div');
  75. container.after(next);
  76. next.classList.add('navigation','next');
  77. const prev = document.createElement('div');
  78. container.before(prev);
  79. prev.classList.add('navigation','prev');
  80. prev.onclick = function (){scroll(container, container.scrollLeft + (shift * -1), 200); return true};
  81. next.onclick = function (){scroll(container, container.scrollLeft + shift , 200); return true};
  82. window.onSwipe(container,function (){scroll(container, container.scrollLeft + (shift * -1), 200); return true}, function (){scroll(container, container.scrollLeft + shift , 200); return true}, )
  83. })()
  84. </script>