refactoring

This commit is contained in:
Виктор Батищев 2020-11-01 17:18:35 +03:00
parent 9ee2107694
commit e0302794cf
4 changed files with 84 additions and 62 deletions

View File

@ -0,0 +1,61 @@
import { col, row } from '../src/utils.js'
export class Block {
constructor(value, options) {
this.value = value
this.options = options
}
toHTML() {
throw new Error(
'Для данного блока не найден подходящий метод для генерации HTML кода!'
)
}
}
export class TitleBlock extends Block {
constructor(value, options) {
super(value, options)
}
toHTML() {
const { options, value } = this
return row(col(`<${options.tag}>${value}</${options.tag}>`), options.styles)
}
}
export class TextBlock extends Block {
constructor(value, options) {
super(value, options)
}
toHTML() {
const { options, value } = this
return row(col(`<p style="margin-bottom: 0;">${value}</p>`), options.styles)
}
}
export class TextColumnsBlock extends Block {
constructor(value, options) {
super(value, options)
}
toHTML() {
const { options, value } = this
return row(value.map((item) => col(item)).join(''), options.styles)
}
}
export class ImageBlock extends Block {
constructor(value, options) {
super(value, options)
}
toHTML() {
const { alt, styles } = this.options
return row(
col(`<img src="./img/${this.value}" alt="${alt}" height="250">`),
styles
)
}
}

View File

@ -1,13 +1,4 @@
import './styles/main.css' import './styles/main.css'
import { model } from './model.js' import { model } from './model.js'
import * as Templates from './templates.js'
let generateHTML = (model) => { document.querySelector('#site').insertAdjacentHTML('afterbegin', model.join(''))
return model.map((block) => {
if (Templates[block.type]) return Templates[block.type](block)
})
}
document
.querySelector('#site')
.insertAdjacentHTML('afterbegin', generateHTML(model).join(''))

View File

@ -1,33 +1,27 @@
import {
TitleBlock,
TextBlock,
TextColumnsBlock,
ImageBlock
} from '../classes/blocks.js'
export const model = [ export const model = [
{ new TitleBlock('Алексей Плещеев', {
type: 'title',
value: 'Алексей Плещеев',
options: {
styles: `color: #fff; text-align: center; font-family: Georgia, serif; styles: `color: #fff; text-align: center; font-family: Georgia, serif;
font-size: 25px; letter-spacing: 2px; word-spacing: 2px; font-size: 25px; letter-spacing: 2px; word-spacing: 2px;
font-variant: small-caps; padding-top: 50px`, font-variant: small-caps; padding-top: 50px`,
tag: 'h2' tag: 'h2'
} }).toHTML(),
}, new TextBlock('Осень наступила, высохли цветы', {
{
type: 'text',
value: 'Осень наступила, высохли цветы',
options: {
styles: `text-align: center; font-family: Arial, Helvetica, sans-serif; styles: `text-align: center; font-family: Arial, Helvetica, sans-serif;
font-size: 20px; font-weight: 700; padding-top: 20px` font-size: 20px; font-weight: 700; padding-top: 20px`
} }).toHTML(),
}, new ImageBlock('golden_autumn.jpg', {
{
type: 'image',
value: 'golden_autumn.jpg',
options: {
styles: `text-align: center; padding-top: 20px`, styles: `text-align: center; padding-top: 20px`,
description: 'Иван Иванович Шишкин Ранняя осень 1889' alt: 'Иван Иванович Шишкин Ранняя осень 1889'
} }).toHTML(),
}, new TextColumnsBlock(
{ [
type: 'textColumns',
value: [
` Осень наступила, ` Осень наступила,
Высохли цветы, Высохли цветы,
И глядят уныло И глядят уныло
@ -48,10 +42,10 @@ export const model = [
Птички улетели Птички улетели
В теплые края.` В теплые края.`
], ],
options: { {
styles: `white-space: pre-wrap; text-align: justify; padding-top: 50px; styles: `white-space: pre-wrap; text-align: justify; padding-top: 50px;
font-family: Verdana; font-size: 16px; font-weight: 700; font-family: Verdana; font-size: 16px; font-weight: 700;
padding-bottom: 200px; padding-left: 150px; padding-right: 100px` padding-bottom: 200px; padding-left: 150px; padding-right: 100px`
} }
} ).toHTML()
] ]

View File

@ -1,24 +0,0 @@
import { col, row } from './utils.js'
export const title = (block) => {
const { options, value } = block
return row(col(`<${options.tag}>${value}</${options.tag}>`), options.styles)
}
export const text = (block) => {
const { options, value } = block
return row(col(`<p style="margin-bottom: 0;">${value}</p>`), options.styles)
}
export const textColumns = (block) => {
const { options, value } = block
return row(value.map((item) => col(item)).join(''), options.styles)
}
export const image = (block) => {
const { description, styles } = block.options
return row(
col(`<img src="./img/${block.value}" alt="${description}" height="250" />`),
styles
)
}