Advertisement
DarkVss

Untitled

Jan 18th, 2023
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const baseBlock = {
  2.     type    : "(string) block type",
  3.     value   : {},
  4.     options : {
  5.         anchor : "(string) id for block or null",
  6.         styles : {
  7.             width         : "(int) 30-100 in percent",
  8.             blockPosition : "(string) left/center/right",
  9.             padding       : {
  10.                 top    : "(int) 1 - 10",
  11.                 bottom : "(int) 1 - 10",
  12.             },
  13.             background    : {
  14.                 type  : "color",
  15.                 value : "(string) color as HEX",
  16.             },
  17.         },
  18.     },
  19. }
  20.  
  21. class Block {
  22.     static DEFAULT_VALUE = {};
  23.  
  24.     /**
  25.      * Prop description
  26.      *
  27.      * @type {Object}
  28.      */
  29.     #value = {};
  30.  
  31.     /**
  32.      * Anchor for block wrapper int html
  33.      *
  34.      * @type {null|string}
  35.      */
  36.     #optionsAnchor = null;
  37.  
  38.     /**
  39.      * Prop description
  40.      *
  41.      * @type {null|int}
  42.      */
  43.     #optionsStyleWidth = null;
  44.  
  45.     /**
  46.      * Prop description
  47.      *
  48.      * @type {null|string}
  49.      */
  50.     #optionsStyleBlockPosition = null;
  51.  
  52.     /**
  53.      * Prop description
  54.      *
  55.      * @type {null|int}
  56.      */
  57.     #optionsStylePaddingTop = null;
  58.  
  59.     /**
  60.      * Prop description
  61.      *
  62.      * @type {null|int}
  63.      */
  64.     #optionsStylePaddingBottom = null;
  65.  
  66.     /**
  67.      * Prop description
  68.      *
  69.      * @type {null|string}
  70.      */
  71.     #optionsStyleBackgroundType = null;
  72.  
  73.     /**
  74.      * Prop description
  75.      *
  76.      * @type {null|stirng}
  77.      */
  78.     #optionsStyleBackgroundValue = null;
  79.  
  80.     /**
  81.      *
  82.      * @param {null|Object} value content value
  83.      */
  84.     constructor(value = null) {
  85.         this.value = Object.assign(this.#value, value || this.constructor.name.DEFAULT_VALUE);
  86.     }
  87.  
  88.     /*** Setter examople start ***/
  89.     /**
  90.      * Set anchor for block wrapper int html
  91.      *
  92.      * @param value
  93.      *
  94.      * @returns {Block}
  95.      */
  96.     setOptionAnchor(value) {
  97.         this.#optionsAnchor = value;
  98.  
  99.         return this;
  100.     }
  101.  
  102.     /************ OR ***************/
  103.    
  104.     /**
  105.      * Set anchor for block wrapper int html
  106.      *
  107.      * @param value
  108.      *
  109.      * @returns {Block}
  110.      */
  111.     setAnchor(value) {
  112.         this.#optionsAnchor = value;
  113.  
  114.         return this;
  115.     }
  116.     /*** Setter example end ***/
  117.  
  118.     /**
  119.      * Init new instance by JSON-object by block notation
  120.      *
  121.      * @param {Object} data
  122.      *
  123.      * @return self
  124.      */
  125.     static initNew(data){
  126.         let instance = new this.constructor.name();
  127.        
  128.         retrun instance.parce(data);
  129.     }
  130.  
  131.     /**
  132.      * Parse to current instance by JSON-object by block notation
  133.      *
  134.      * @param {Object} data
  135.      *
  136.      * @return self
  137.      */
  138.     parse(data){
  139.         /**
  140.          * main parce processing (value,standard options)
  141.          */
  142.        
  143.         this.#parseByChild(data);
  144.     }
  145.    
  146.     #parseByChild(data){
  147.         /**
  148.          * to do nothing - implement only in child classes
  149.          */
  150.     }
  151.  
  152.     /**
  153.      * get block content as HTML
  154.      * @returns {string} HTML as string
  155.      */
  156.     toHTML() {
  157.         throw new Error("Method getHTML not implemented");
  158.     }
  159.  
  160.     /**
  161.      * get block content as JSON
  162.      * @returns {} content as object
  163.      */
  164.     toJSON() {
  165.         let classname = this.constructor.name;
  166.  
  167.         return {
  168.             type    : classname.substring(0, 1).toLowerCase() + classname.substring(1),
  169.             value   : this.value,
  170.             options : {
  171.                 anchor : this.#optionsAnchor,
  172.                 /**
  173.                  * other struct ...
  174.                  */
  175.             },
  176.         };
  177.     }
  178. }
  179.  
  180. class TitleBlock extends Block {
  181.     static DEFAULT_VALUE = {
  182.         pretitle : null,
  183.         title    : "Headline H2",
  184.         subtitle : null,
  185.     };
  186.  
  187.     /**** Specific data example start ****/
  188.     /**
  189.      * Prop descriptor
  190.      *
  191.      * @type {null|string}
  192.      */
  193.     #valueTitle = null;
  194.    
  195.     /*** Setter examople start ***/
  196.     /**
  197.      * Set anchor for block wrapper int html
  198.      *
  199.      * @param value
  200.      *
  201.      * @returns {Block}
  202.      */
  203.     setValueTitle(value) {
  204.         this.#valueTitle = value;
  205.  
  206.         return this;
  207.     }
  208.  
  209.     /************ OR ***************/
  210.  
  211.     /**
  212.      * Set anchor for block wrapper int html
  213.      *
  214.      * @param value
  215.      *
  216.      * @returns {Block}
  217.      */
  218.     setTitle(value) {
  219.         this.#valueTitle = value;
  220.  
  221.         return this;
  222.     }
  223.     /*** Setter example end ***/
  224.    
  225.    
  226.     /******** ================ OR ================ ********/
  227.  
  228.     /*** Setter examople start ***/
  229.     /**
  230.      * Set anchor for block wrapper int html
  231.      *
  232.      * @param value
  233.      *
  234.      * @returns {Block}
  235.      */
  236.     setValueTitle(value) {
  237.         this.#value.title = value;
  238.  
  239.         return this;
  240.     }
  241.  
  242.     /************ OR ***************/
  243.  
  244.     /**
  245.      * Set anchor for block wrapper int html
  246.      *
  247.      * @param value
  248.      *
  249.      * @returns {Block}
  250.      */
  251.     setTitle(value) {
  252.         this.#title = value;
  253.  
  254.         return this;
  255.     }
  256.  
  257.     /**** Specific data example start ****/
  258.  
  259.  
  260.     #parseByChild(data){
  261.         /**
  262.          * child parce processing (value,standard options)
  263.          */
  264.     }
  265. }
  266.  
  267. document.addEventListener("DOMContentLoaded", function () {
  268.     let titleBlock = TitleBlock.initNew(
  269.         {
  270.             pretitle : "pretitle",
  271.             title    : "Headline H1",
  272.             subtitle : "Lorem Ipsum bla bla bla",
  273.         },
  274.         {
  275.             anchor : "title",
  276.             level  : 1,
  277.             styles : {
  278.                 background : {
  279.                     type  : "color",
  280.                     value : "#f3f3f3",
  281.                 },
  282.             },
  283.         },
  284.     );
  285.  
  286.     console.log(titleBlock.toHTML());
  287.     console.log(titleBlock.toJSON());
  288. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement