Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- NOT_SKIPPED_ITEMS_LENGTH,
- COUNT_FIRST_ITEMS
- } from '../../../../logic/reducer/teaser';
- import { templatePattern } from './templatePattern';
- import cloneDeep from 'lodash.clonedeep';
- import chunkArray from 'lodash.chunk';
- const countColumnBlock = { left: [3, 1, 2, 2, 2], right: [2, 2, 2, 1, 3] };
- const heightColumnBlock = {
- left: [250, 310, 310, 310, 310],
- right: [310, 310, 310, 310, 250]
- };
- export const generateDataForRender = ({ newsHeight }) => {
- let height = newsHeight;
- let countSkipLines = 1;
- let countSkipGlobalBlocks = 1;
- while (height > 0) {
- if (countSkipLines === 5) {
- countSkipLines = 0;
- countSkipGlobalBlocks++;
- }
- height -= heightColumnBlock.left[countSkipLines];
- countSkipLines++;
- }
- const additionalHeight = -height % 100;
- const countXsBlocks = Math.floor(-height / 100);
- const renderParams = {
- countXsBlocks,
- countSkipLines,
- countSkipGlobalBlocks
- };
- const firstBlockExample = cloneDeep(templatePattern[0]);
- let firstBlockForRender = firstBlockExample.slice(0, 4);
- for (let i = 0; i < renderParams.countXsBlocks; i++) {
- firstBlockForRender.push({ className: '_xs' });
- }
- const countNewsForBlock =
- renderParams.countSkipLines === 5
- ? 0
- : countColumnBlock.left
- .slice(renderParams.countSkipLines, firstBlockExample.length)
- .reduce((a, b) => a + b);
- firstBlockForRender = [
- ...firstBlockForRender,
- ...firstBlockExample.slice(
- firstBlockExample.length - countNewsForBlock,
- firstBlockExample.length
- )
- ];
- return { firstBlockForRender, renderParams, additionalHeight };
- };
- export const generateChunkedNews = (
- newsList,
- firstBlockForRender,
- viewNews,
- renderParams,
- isOneColumn
- ) => {
- if (viewNews) {
- if (firstBlockForRender && newsList.length >= COUNT_FIRST_ITEMS) {
- const { countSkipGlobalBlocks } = renderParams;
- const duplicatedNews = cloneDeep(newsList);
- const skippedItems =
- (templatePattern.reduce((sum, item) => sum + item.length, 0) /
- templatePattern.length) *
- countSkipGlobalBlocks -
- (firstBlockForRender.length - 1);
- const importantNews = duplicatedNews.splice(0, COUNT_FIRST_ITEMS);
- let chunkedNews = [
- [...importantNews.splice(0, NOT_SKIPPED_ITEMS_LENGTH), viewNews]
- ];
- // в одну колонку
- if (isOneColumn) {
- chunkedNews = appendItemsInOneColumn({
- newsList: [...importantNews, ...duplicatedNews],
- chunkedNews
- });
- // Вставляем вокруг новости
- } else {
- chunkedNews = appendItemsAround({
- newsList: [...importantNews, ...duplicatedNews],
- skippedItems,
- chunkedNews,
- renderParams,
- firstBlockForRender
- });
- }
- return chunkedNews;
- } else {
- return [[null, null, null, viewNews]];
- }
- } else {
- return chunkArray(newsList, templatePattern[0].length);
- }
- };
- export const appendItemsAround = ({
- newsList: newsListOriginal,
- skippedItems,
- chunkedNews: news,
- renderParams,
- firstBlockForRender
- }) => {
- let chunkedNews = cloneDeep(news);
- const listNews = cloneDeep(newsListOriginal);
- // Important news
- // Проставляем сначала в правую колонку по высоте скипнутых тизеров
- const rightNewsList = listNews.splice(
- 0,
- (renderParams.countSkipGlobalBlocks - 1) * templatePattern[1].length +
- countColumnBlock.right
- .slice(0, renderParams.countSkipLines)
- .reduce((sum, el) => sum + el, 0)
- );
- const iterate = Math.ceil(rightNewsList.length / 5);
- for (let i = 1; i < iterate; i++) {
- if (i % templatePattern.length !== 0) {
- const patternCount = templatePattern[i % templatePattern.length].length;
- chunkedNews[i] = rightNewsList.length
- ? rightNewsList.splice(0, patternCount)
- : [];
- } else {
- chunkedNews[i] = null;
- }
- }
- // Разбиваем остаток построчно по колонкам
- if (listNews.length) {
- if (renderParams.countXsBlocks) {
- chunkedNews[0].push(...listNews.splice(0, renderParams.countXsBlocks));
- }
- const newsByLines = [];
- let b = 0;
- for (
- let i = renderParams.countSkipLines;
- listNews.length > 0;
- i = i >= countColumnBlock.left.length - 1 ? 0 : i + 1
- ) {
- const arrLeft = listNews
- .splice(0, countColumnBlock.left[i])
- .map(el => ({ ...el, b: b++ }));
- newsByLines.push(arrLeft);
- if (listNews.length) {
- const arrRight = listNews
- .splice(0, countColumnBlock.right[i])
- .map(el => ({ ...el, b: b++ }));
- newsByLines.push(arrRight);
- }
- }
- console.log(chunkedNews);
- for (let i = 0; i < newsByLines.length; i++) {
- const indexTemplate = i % templatePattern.length;
- let index =
- Math.floor(chunkedNews.length / templatePattern.length) * 2 -
- (templatePattern.length - indexTemplate);
- if (chunkedNews[index] === null) {
- index = index - (renderParams.countSkipGlobalBlocks - 1) * 2;
- }
- const templateLength =
- index === 0
- ? firstBlockForRender.length
- : templatePattern[indexTemplate].length;
- console.log(chunkedNews, index, chunkedNews[index]);
- if (chunkedNews[index].length < templateLength) {
- chunkedNews[index].push(...newsByLines[i]);
- console.log(chunkedNews[index].length === templateLength)
- if (
- chunkedNews[index].length === templateLength &&
- chunkedNews[index === 0 ? renderParams.countSkipGlobalBlocks * 2 : index + templatePattern.length] !== null
- ) {
- chunkedNews[index === 0 ? renderParams.countSkipGlobalBlocks * 2 : index + templatePattern.length] = [];
- }
- } else {
- chunkedNews[index + templatePattern.length] = [...newsByLines[i]];
- }
- }
- }
- console.log(chunkedNews);
- return chunkedNews;
- };
- const appendItemsInOneColumn = data => {
- let chunkedNews = cloneDeep(data.chunkedNews);
- const newsList = cloneDeep(data.newsList);
- // Всё в первую колонку
- if (chunkedNews[0] && chunkedNews[0].length < templatePattern[0].length) {
- const additionalNews = newsList.splice(
- 0,
- templatePattern[0].length - chunkedNews[0].length
- );
- chunkedNews[0] = [...chunkedNews[0], ...additionalNews];
- }
- if (newsList.length) {
- const iterate = Math.ceil(newsList.length / 5) + 1;
- for (let i = 1; i < iterate; i++) {
- if (i % 2 === 1) {
- chunkedNews[i] = null;
- } else {
- chunkedNews[i] = newsList.length ? newsList.splice(0, 10) : [];
- }
- }
- }
- return chunkedNews;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement