Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <button onclick="renderFeed()">Click</button>
<header></header>
<main id="root"></main>
<section></section>
              
            
!

CSS

              
                
              
            
!

JS

              
                /** Eine Variante die funktioniert, aber noch nicht so wirklich rund wirkt. */
class UiComponent {
 
  /** Position im DOM muss immer festgelegt werden */ 
  set position(strPos){
    this.strPosition = strPos;
  }
  
  get position(){
    if(this.strPosition === undefined){
      this.strPosition = ''
    }
    return this.strPosition;
  }
  
  /** 
   * template muss festgelegt werden, wenn renderData() verwendet wird. 
   */
  set template(objTemplate){
    this.objTemplate = objTemplate;
  }
  
  /** Bietet die Möglichkeit auch eigenen Code unabhängig von fertigen Komponenten als Template verwenden zu können. 
   * Um fertige Komponenten zu nutzen:
   *  - class UiFeed extends UiComponent   
   *  ...
   */
  get template(){
    return {
      parent: this.objTemplate.parent,
      child: `<%child%> %value% <%child%>`,
    }
  }
  
//       set replacePlaceholder (match, p1, p2, p3, offset, string) {
        
//         console.log('p1: ', p1);
//         // console.log('p1: ', `'${p1}_keyValue'`);
        
//         console.log('this.objTemplate: ', this.objTemplate);
        
//         // console.log('arrObjDataElement 1: ', arrObjDataElement);
        
//         if(`${p1}_keyValue` in this.objTemplate){
//           // console.log('keyValue: ', `${p1}_keyValue`);
//           // console.log('arrObjDataElement 1: ', arrObjDataElement);
//           // console.log('arrObjDataElement 2: ', arrObjDataElement[this.objTemplate[p1]]);
//           this.replacedValue = arrObjDataElement[this.objTemplate[`${p1}_keyValue`]];  
//         }
        
//         if(`${p1}_key` in this.objTemplate){
//           // return arrObjDataElement[this.objTemplate[p1]];  
//           // Object.keys(objDatasets)[0]
//           console.log('key: ', `${p1}_key`);
//           this.replacedValue = this.objTemplate[`${p1}_key`];
//         }
        
//       }  
  
  
//   set replacePlaceholder(){
//     return this.replacedValue;
//   }
  /*
   * ObjData = [{},{}]
   */  
  readArrObjects(arrObjData){
    
    let generatedTemplate = '';
    
    for(let arrObjDataElement of arrObjData){
      
      this.replacePlaceholder = (match, p1, p2, p3, offset, string) => {
        
        console.log('p1: ', p1);    
        console.log('this.objTemplate: ', this.objTemplate);
        
        if(`${p1}_keyValue` in this.objTemplate){
          return arrObjDataElement[this.objTemplate[`${p1}_keyValue`]];  
        }
        
        if(`${p1}_key` in this.objTemplate){
          console.log('key: ', `${p1}_key`);
          return this.objTemplate[`${p1}_key`];
        }
        
      } 
      
      let templateGetData = this.template.child.replace(/%([^%]*)%/g, this.replacePlaceholder);
      
      generatedTemplate += templateGetData;

    }
    generatedTemplate = `<${this.template.parent}>${generatedTemplate}</${this.template.parent}>`; 
    this.render(generatedTemplate);
    console.log('generatedTemplate: ',generatedTemplate);
    return generatedTemplate;
  }
  
  /*
   * ObjData = {key:[],key:[]}
   */
  renderObjData(ObjData){
    let arrDom;
    let generatedTemplate = '';
    let templateGetData = '';
    
    for(let ObjDataElement in ObjData){
      
      console.log('ObjDataElement: ', ObjDataElement);
      
      this.replacePlaceholder = (match, p1, p2, p3, offset, string) => {

        // console.log('p1: ', p1);
        // console.log('this.objTemplate: ', this.objTemplate);

        if(`${p1}_keyValue` in this.objTemplate){
          return arr;  
        }

        if(`${p1}_key` in this.objTemplate){
          // console.log('key: ', `${p1}_key`);
          return ObjDataElement;
          // return this.objTemplate[`${p1}_key`];
        }

      }       

        templateGetData = this.template.parent.replace(/%([^%]*)%/g, this.replacePlaceholder);
        generatedTemplate += templateGetData;
      
      for(var arr of ObjData[ObjDataElement]){
        templateGetData = this.template.child.replace(/%([^%]*)%/g, this.replacePlaceholder);
        generatedTemplate += templateGetData;
      }
    
    }
    // arrDom = `<${this.template.parent}>${arrDom}</${this.template.parent}>`; 
    // this.render(arrDom);
    console.log('generatedTemplate: ',generatedTemplate);
    return generatedTemplate;
  }  
  
  render(domComponent){
      document.querySelector(this.position).innerHTML = domComponent;
  }
  

}

class UiFeed extends UiComponent {
  
  set template(objTemplate){
    this.objTemplate = objTemplate;
  }
  
  get template(){
    switch(this.objTemplate.name) {
        case 'default':
          return {
            parent: 'ul',
            // child: dataset => `<li><h2>%title%<h2></li><li><h5>%item1%</5></li>`
            child: `<li><h2>%title%<h2></li><li><h5>%item1%</5></li>`
          }
          break;
        case 'threeItems':
          return {
            parent: 'ul',
            child: `<li><h2>%title%<h2></li><li><h5>%item1%</5></li><li><h5>%item2%</5></li>`
          } 
          break;  
        default:
          return {
            parent: 'ul',
            child: `<li><h2>%title%<h2></li><li><h5>%item1%</5></li>`
          }          
    }    
  }

}

class UiFilter extends UiComponent {
  
  set template(objTemplate){
    this.objTemplate = objTemplate;
  }
  
  get template(){

    return {
      parent: '<h2>%title%</h2>',
      child: `<h3>%item1%</h3>`
    }
  }
  
}

const arrMovies = [
  {
    Titel: 'Prometheus',
    Genre: 'Science-Fiction,Fantasy',
    Tags: 'Favorit,Weltall'
  },
  {
    Titel: 'Arrival',
    Genre: '',
    Tags: 'Weltall'
  },
  {
    Titel: 'Pirates of the Caribbean Salazars Rache',
    Genre: 'Abenteuer,Fantasy',
    Tags: 'Piraten,Meer'
  },
  {
    Titel: 'Shutter Island',
    Genre: 'Drama,Thriller',
    Tags: 'Favorit,De Caprio'
  }  
];

const arrMovies2 = [
  [
    'Prometheus',
    'Science-Fiction,Fantasy',
    'Favorit,Weltall'
  ],
  [
    'Arrival',
    '',
    'Weltall'
  ],
  [
    'Pirates of the Caribbean Salazars Rache',
    'Abenteuer,Fantasy',
    'Piraten,Meer'
  ],
  [
    'Shutter Island',
    'Drama,Thriller',
    'Favorit,De Caprio'
  ]  
];

const objFilter = {
  Genres: ['Action', 'Adventure', 'Drama', 'Fantasy', 'Science-Fiction', 'Superheroes', 'Thriller'],
  Tags: ['Batman', 'Comic', 'Favorite', 'Marvel', 'New York', 'Space', 'War']
}

let cFeed = new UiFeed();

/** Jede Komponente hat ihre eigenes Objekt. Bei UiFeed muss man eben wissen, dass man diese Daten übergeben muss. */ 
// cFeed.template = {
//   name:'default',
//   title_keyValue:'Titel',
//   // item1_keyValue:'Genre',
//   // title_key:'Titel',
//   item1_key:'Genre'  
// };

/** 
Das UiFeed Template ist flexibel was das Array angeht. Auch wenn es das Array eine andere Verschachtelung als das oben hätte
z.b const arrMovies = ['Prometheus','Science-Fiction,Fantasy','Favorit,Weltall'] könnte man das Template verwenden
cFeed.template = {
  name:'default',
  title:arrMovies[0],
  item1:arrMovies[1]
};
*/
// cFeed.position = 'nav'
// cFeed.position = 'main#root';
// // cFeed.domFromArray = arrMovies;
// cFeed.readArrObjects(arrMovies);
// function renderFeed(){
//   cFeed.renderArrData(arrMovies);
// }


// let cFeed2 = new UiFeed();
// cFeed2.template = {
//   name:'default',
//   title:[0],
//   item1:[1]
// };
// cFeed2.position = 'main#root';
// cFeed2.renderArrData(arrMovies2);

let cFilter = new UiFilter();
cFilter.template = {
  name:'default',
  // title_keyValue:'Titel',
  item1_keyValue: true,
  title_key: true,
  // item1_key: true  
};
cFilter.position = 'main#root';
cFilter.renderObjData(objFilter);

// console.log('cFeedRes: ',return cFeedRes);

// let compHeadline = new UiComponent();
// compHeadline.position = 'header';
// compHeadline.render('<h2>Movie Database</h2>');

// /** Bei dem Template für UiComponent muss man wissen, dass man diese Daten übergeben muss */
// let uiSection = new UiComponent();
// uiSection.template = {
//   parent: 'div',
//   child: 'p',
//   // child: '<p><span>',
//   value:'Titel'
// };
// uiSection.position = 'section';
// let uiSectionRes = uiSection.renderArrData(arrMovies);
// // console.log('uiSection: ',uiSectionRes);


              
            
!
999px

Console