Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   NOT_SKIPPED_ITEMS_LENGTH,
  3.   COUNT_FIRST_ITEMS
  4. } from '../../../../logic/reducer/teaser';
  5. import { templatePattern } from './templatePattern';
  6. import cloneDeep from 'lodash.clonedeep';
  7. import chunkArray from 'lodash.chunk';
  8.  
  9. const countColumnBlock = { left: [3, 1, 2, 2, 2], right: [2, 2, 2, 1, 3] };
  10. const heightColumnBlock = {
  11.   left: [250, 310, 310, 310, 310],
  12.   right: [310, 310, 310, 310, 250]
  13. };
  14.  
  15. export const generateDataForRender = ({ newsHeight }) => {
  16.   let height = newsHeight;
  17.   let countSkipLines = 1;
  18.   let countSkipGlobalBlocks = 1;
  19.   while (height > 0) {
  20.     if (countSkipLines === 5) {
  21.       countSkipLines = 0;
  22.       countSkipGlobalBlocks++;
  23.     }
  24.     height -= heightColumnBlock.left[countSkipLines];
  25.     countSkipLines++;
  26.   }
  27.   const additionalHeight = -height % 100;
  28.   const countXsBlocks = Math.floor(-height / 100);
  29.   const renderParams = {
  30.     countXsBlocks,
  31.     countSkipLines,
  32.     countSkipGlobalBlocks
  33.   };
  34.  
  35.   const firstBlockExample = cloneDeep(templatePattern[0]);
  36.   let firstBlockForRender = firstBlockExample.slice(0, 4);
  37.   for (let i = 0; i < renderParams.countXsBlocks; i++) {
  38.     firstBlockForRender.push({ className: '_xs' });
  39.   }
  40.   const countNewsForBlock =
  41.     renderParams.countSkipLines === 5
  42.       ? 0
  43.       : countColumnBlock.left
  44.           .slice(renderParams.countSkipLines, firstBlockExample.length)
  45.           .reduce((a, b) => a + b);
  46.   firstBlockForRender = [
  47.     ...firstBlockForRender,
  48.     ...firstBlockExample.slice(
  49.       firstBlockExample.length - countNewsForBlock,
  50.       firstBlockExample.length
  51.     )
  52.   ];
  53.  
  54.   return { firstBlockForRender, renderParams, additionalHeight };
  55. };
  56.  
  57. export const generateChunkedNews = (
  58.   newsList,
  59.   firstBlockForRender,
  60.   viewNews,
  61.   renderParams,
  62.   isOneColumn
  63. ) => {
  64.   if (viewNews) {
  65.     if (firstBlockForRender && newsList.length >= COUNT_FIRST_ITEMS) {
  66.       const { countSkipGlobalBlocks } = renderParams;
  67.       const duplicatedNews = cloneDeep(newsList);
  68.       const skippedItems =
  69.         (templatePattern.reduce((sum, item) => sum + item.length, 0) /
  70.           templatePattern.length) *
  71.           countSkipGlobalBlocks -
  72.         (firstBlockForRender.length - 1);
  73.       const importantNews = duplicatedNews.splice(0, COUNT_FIRST_ITEMS);
  74.       let chunkedNews = [
  75.         [...importantNews.splice(0, NOT_SKIPPED_ITEMS_LENGTH), viewNews]
  76.       ];
  77.  
  78.       // в одну колонку
  79.       if (isOneColumn) {
  80.         chunkedNews = appendItemsInOneColumn({
  81.           newsList: [...importantNews, ...duplicatedNews],
  82.           chunkedNews
  83.         });
  84.         // Вставляем вокруг новости
  85.       } else {
  86.         chunkedNews = appendItemsAround({
  87.           newsList: [...importantNews, ...duplicatedNews],
  88.           skippedItems,
  89.           chunkedNews,
  90.           renderParams,
  91.           firstBlockForRender
  92.         });
  93.       }
  94.  
  95.       return chunkedNews;
  96.     } else {
  97.       return [[null, null, null, viewNews]];
  98.     }
  99.   } else {
  100.     return chunkArray(newsList, templatePattern[0].length);
  101.   }
  102. };
  103.  
  104. export const appendItemsAround = ({
  105.   newsList: newsListOriginal,
  106.   skippedItems,
  107.   chunkedNews: news,
  108.   renderParams,
  109.   firstBlockForRender
  110. }) => {
  111.   let chunkedNews = cloneDeep(news);
  112.   const listNews = cloneDeep(newsListOriginal);
  113.   // Important news
  114.   // Проставляем сначала в правую колонку по высоте скипнутых тизеров
  115.   const rightNewsList = listNews.splice(
  116.     0,
  117.     (renderParams.countSkipGlobalBlocks - 1) * templatePattern[1].length +
  118.       countColumnBlock.right
  119.         .slice(0, renderParams.countSkipLines)
  120.         .reduce((sum, el) => sum + el, 0)
  121.   );
  122.   const iterate = Math.ceil(rightNewsList.length / 5);
  123.   for (let i = 1; i < iterate; i++) {
  124.     if (i % templatePattern.length !== 0) {
  125.       const patternCount = templatePattern[i % templatePattern.length].length;
  126.       chunkedNews[i] = rightNewsList.length
  127.         ? rightNewsList.splice(0, patternCount)
  128.         : [];
  129.     } else {
  130.       chunkedNews[i] = null;
  131.     }
  132.   }
  133.  
  134.   // Разбиваем остаток построчно по колонкам
  135.   if (listNews.length) {
  136.     if (renderParams.countXsBlocks) {
  137.       chunkedNews[0].push(...listNews.splice(0, renderParams.countXsBlocks));
  138.     }
  139.  
  140.     const newsByLines = [];
  141.     let b = 0;
  142.     for (
  143.       let i = renderParams.countSkipLines;
  144.       listNews.length > 0;
  145.       i = i >= countColumnBlock.left.length - 1 ? 0 : i + 1
  146.     ) {
  147.       const arrLeft = listNews
  148.         .splice(0, countColumnBlock.left[i])
  149.         .map(el => ({ ...el, b: b++ }));
  150.       newsByLines.push(arrLeft);
  151.       if (listNews.length) {
  152.         const arrRight = listNews
  153.           .splice(0, countColumnBlock.right[i])
  154.           .map(el => ({ ...el, b: b++ }));
  155.         newsByLines.push(arrRight);
  156.       }
  157.     }
  158.     console.log(chunkedNews);
  159.  
  160.     for (let i = 0; i < newsByLines.length; i++) {
  161.       const indexTemplate = i % templatePattern.length;
  162.       let index =
  163.         Math.floor(chunkedNews.length / templatePattern.length) * 2 -
  164.         (templatePattern.length - indexTemplate);
  165.       if (chunkedNews[index] === null) {
  166.         index = index - (renderParams.countSkipGlobalBlocks - 1) * 2;
  167.       }
  168.       const templateLength =
  169.         index === 0
  170.           ? firstBlockForRender.length
  171.           : templatePattern[indexTemplate].length;
  172.       console.log(chunkedNews, index, chunkedNews[index]);
  173.       if (chunkedNews[index].length < templateLength) {
  174.         chunkedNews[index].push(...newsByLines[i]);
  175.         console.log(chunkedNews[index].length === templateLength)
  176.         if (
  177.           chunkedNews[index].length === templateLength &&
  178.           chunkedNews[index === 0 ? renderParams.countSkipGlobalBlocks * 2 : index + templatePattern.length] !== null
  179.         ) {
  180.           chunkedNews[index === 0 ? renderParams.countSkipGlobalBlocks * 2 : index + templatePattern.length] = [];
  181.         }
  182.       } else {
  183.         chunkedNews[index + templatePattern.length] = [...newsByLines[i]];
  184.       }
  185.     }
  186.   }
  187.   console.log(chunkedNews);
  188.   return chunkedNews;
  189. };
  190.  
  191. const appendItemsInOneColumn = data => {
  192.   let chunkedNews = cloneDeep(data.chunkedNews);
  193.   const newsList = cloneDeep(data.newsList);
  194.   // Всё в первую колонку
  195.   if (chunkedNews[0] && chunkedNews[0].length < templatePattern[0].length) {
  196.     const additionalNews = newsList.splice(
  197.       0,
  198.       templatePattern[0].length - chunkedNews[0].length
  199.     );
  200.     chunkedNews[0] = [...chunkedNews[0], ...additionalNews];
  201.   }
  202.  
  203.   if (newsList.length) {
  204.     const iterate = Math.ceil(newsList.length / 5) + 1;
  205.     for (let i = 1; i < iterate; i++) {
  206.       if (i % 2 === 1) {
  207.         chunkedNews[i] = null;
  208.       } else {
  209.         chunkedNews[i] = newsList.length ? newsList.splice(0, 10) : [];
  210.       }
  211.     }
  212.   }
  213.   return chunkedNews;
  214. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement