Advertisement
avr39ripe

jsCssClassPekarskij

Mar 28th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. class CssClass {
  2.             constructor(nameCssClass) {
  3.                 this.nameCssClass = nameCssClass
  4.                 this.cssStyle = []
  5.             }
  6.  
  7.             getCssStyle(cssName) {
  8.                 return this.cssStyle.find((it) => {
  9.                     if (it.name == cssName) {
  10.                         return it;
  11.                     }
  12.                 })
  13.             }
  14.  
  15.             setCssStyle(cssName, value) {
  16.                 if (this.getCssStyle(cssName) == undefined) {
  17.                     return this.cssStyle.push({ name: cssName, value: value });
  18.                 } else {
  19.                     this.getCssStyle(cssName).value = value;
  20.                 }
  21.             }
  22.  
  23.             removeStyle(cssName) {
  24.                 return this.cssStyle.splice(this.cssStyle.findIndex(it => it.name === cssName), 1);
  25.             }
  26.  
  27.             toCssStyle() {
  28.                 return this.cssStyle.reduce((prev, it) => {
  29.                     return `${prev}${it.name}: ${it.value}; `
  30.                 }
  31.                     , '')
  32.             }
  33.  
  34.             getCss() {
  35.                 return `.${this.nameCssClass} { ${this.toCssStyle()} } `;
  36.             }
  37.         }
  38.  
  39.         let css1 = new CssClass('wrap');
  40.         css1.setCssStyle("display", "flex");
  41.  
  42.         let css2 = new CssClass('block');
  43.         css2.setCssStyle("width", "300px");
  44.         css2.setCssStyle("margin", "10px");
  45.         css2.setCssStyle("color", "red");
  46.         css2.removeStyle("margin");
  47.  
  48.         let css3 = new CssClass('img');
  49.         css3.setCssStyle("width", "100%");
  50.  
  51.         let result = [css1.getCss(), css2.getCss(), css3.getCss()].join('');
  52.         console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement