stories.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. ?>
  3. <link rel="stylesheet" href="/js/zuck/dist/zuck.css">
  4. <link rel="stylesheet" href="/js/zuck/dist/skins/snapgram.css">
  5. <div class="stories_cont">
  6. <div id="stories"></div>
  7. </div>
  8. <script src="/js/zuck/dist/zuck.js"></script>
  9. <?php use app\models\News;
  10. $stories = Yii::$app->cache->getOrSet("site-stories",function (){
  11. $data = [];
  12. $news = News::find()->limit(10);
  13. foreach ($news->all() as $news_item)
  14. {
  15. /**
  16. * @var $news_item News
  17. */
  18. $comments = [];
  19. foreach ($news_item->getCommentsAll()->limit(5)->all() as $comment){
  20. /**
  21. * @var $comment \app\models\base\Comments
  22. */
  23. $comments[]=[
  24. "message"=>$comment->message,
  25. "published_at"=>$comment->publishedAt
  26. ];
  27. }
  28. $data[]=[
  29. "id"=>$news_item->uid,
  30. "photo"=>$news_item->preview->getUrl(\app\models\base\Image::SIZE_680x383),
  31. "name"=>false,
  32. "link"=>$news_item->url,
  33. "lastUpdated"=>strtotime($news_item->dt_pub),
  34. "seen"=>false,
  35. "published_at"=>$news_item->publishedAt,
  36. "items"=>[
  37. [
  38. "id"=>$news_item->preview->getUrl(\app\models\base\Image::SIZE_680x383),
  39. "type"=>"photo",
  40. "length"=>100,
  41. "lastUpdated"=>strtotime($news_item->dt_pub),
  42. "src"=>$news_item->preview->getUrl(\app\models\base\Image::SIZE_1040x586),
  43. "preview"=>$news_item->preview->getUrl(\app\models\base\Image::SIZE_680x383),
  44. "link"=>$news_item->url,
  45. "linkText"=>$news_item->lid,
  46. "seen"=> false,
  47. "lid"=>$news_item->lid,
  48. "post_title"=>$news_item->title,
  49. "comments"=>base64_encode(json_encode($comments))
  50. ]
  51. ]
  52. ];
  53. }
  54. return json_encode($data);
  55. },1);
  56. ?>
  57. <script>
  58. const get = function (array, what) {
  59. if (array) {
  60. return array[what] || '';
  61. } else {
  62. return '';
  63. }
  64. };
  65. const renderComments = function (comments){
  66. let html = "";
  67. var j;
  68. try{
  69. let commentsArr = JSON.parse(atob(comments));
  70. if("object" === typeof commentsArr && commentsArr.length>0){
  71. html+='<div class="comments">';
  72. for(let i = 0; i < commentsArr.length; i++){
  73. j = commentsArr[i];
  74. html+=`<div class="comment"><div class="message">${j.message}</div><div class="published_at">${j.published_at}</div></div>`
  75. }
  76. html+='</div>';
  77. }
  78. } catch (e){
  79. }
  80. return html;
  81. }
  82. let stories = new Zuck("stories",{
  83. skin: 'snapgram',
  84. avatars: false, // shows user photo instead of last story item preview
  85. list: false, // displays a timeline instead of carousel
  86. openEffect: true, // enables effect when opening story
  87. cubeEffect: false, // enables the 3d cube effect when sliding story
  88. autoFullScreen: false, // enables fullscreen on mobile browsers
  89. backButton: true, // adds a back button to close the story viewer
  90. backNative: false, // uses window history to enable back button on browsers/android
  91. previousTap: true, // use 1/3 of the screen to navigate to previous item when tap the story
  92. localStorage: true, // set true to save "seen" position. Element must have a id to save properly.
  93. reactive: false, // set true if you use frameworks like React to control the timeline (see react.sample.html)
  94. rtl: false, // enable/disable RTL
  95. paginationArrows:false,
  96. "stories": <?=$stories?>,
  97. template: {
  98. "viewerItemBody": function (index, currentIndex, item) {
  99. return `<div
  100. class="item ${get(item, 'seen') === true ? 'seen' : ''} ${currentIndex === index ? 'active' : ''}"
  101. data-time="${get(item, 'time')}" data-type="${get(item, 'type')}" data-index="${index}" data-item-id="${get(item, 'id')}">
  102. <div class="title"><h2><a href="${item.link}">${item.post_title}</a></h2></div>
  103. ${renderComments(item.comments)}
  104. ${
  105. get(item, 'type') === 'video'
  106. ? `<video class="media" muted webkit-playsinline playsinline preload="auto" src="${get(item, 'src')}" ${get(item, 'type')}></video>
  107. <b class="tip muted">${option('language', 'unmute')}</b>`
  108. : `<div class="blur-bg" style="background-image: url('${get(item, 'src')}') "></div><div class="image-container" ><img loading="auto" class="media" src="${get(item, 'src')}" ${get(item, 'type')} /></div>
  109. `}
  110. ${
  111. get(item, 'link')
  112. ? `<a class="tip link" href="${get(item, 'link')}" rel="noopener" target="_blank">
  113. ${!get(item, 'linkText') || get(item, 'linkText') === '' ? option('language', 'visitLink') : get(item, 'linkText')}
  114. </a>`
  115. : ''
  116. }
  117. </div>`;
  118. },
  119. viewerItem: function (storyData, currentStoryItem) {
  120. var date = new Date(storyData.lastUpdated*1000);
  121. return `<div class="story-viewer">
  122. <div class="head">
  123. <div class="left">
  124. <a class="back">&lsaquo;</a>
  125. <span class="item-preview">
  126. <img lazy="eager" class="profilePhoto" src="${get(storyData, 'photo')}" />
  127. </span>
  128. <div class="info">
  129. <strong class="name">${get(storyData, 'name')}</strong>
  130. <span class="time">${date.toLocaleDateString()}</span>
  131. </div>
  132. </div>
  133. <div class="right">
  134. <span class="time">${get(currentStoryItem, 'timeAgo')}</span>
  135. <span class="loading"></span>
  136. <a class="close" tabIndex="2">&times;</a>
  137. </div>
  138. </div>
  139. <div class="slides-pointers">
  140. <div class="wrap"></div>
  141. </div>
  142. </div>`;
  143. },
  144. }
  145. });
  146. var stories_container = document.getElementById("stories");
  147. stories_container.onmousewheel = function(e){
  148. e.preventDefault();
  149. stories_container.scrollLeft+=e.wheelDeltaY
  150. return false;
  151. }
  152. </script>