/* * Grid * * Pico.css - https://picocss.com * Copyright 2019-2021 - Licensed under MIT */ const grid = { // Config buttons: { text: { add: 'Add column', remove: 'Remove column', }, target: '#grid article', }, grid: { current: 4, min: 1, max: 12, gridTarget: '#grid .grid', codeTarget: '#grid pre code', }, // Init init() { this.addButtons(); this.generateGrid(); }, // Add buttons addButtons() { // Insert buttons let buttons = document.createElement('P'); buttons.innerHTML = ` `; document.querySelector(this.buttons.target).before(buttons); // Add button listener document.querySelector('#grid button.add').addEventListener('click', () => { this.addColumn(); }, false); // Remove button listener document.querySelector('#grid button.remove').addEventListener('click', () => { this.removeColumn(); }, false); }, // Generate grid generateGrid() { // Config let htmlInner = ''; let codeInner = '<div class="grid">\n'; // Build for (let col = 0; col < this.grid.current; col++) { htmlInner += '
' + (col + 1) + '
'; codeInner += ' <div>' + (col + 1) + '</div>\n'; } // Display codeInner += '</div>'; document.querySelector(this.grid.gridTarget).innerHTML = htmlInner; document.querySelector(this.grid.codeTarget).innerHTML = codeInner; }, // Add column addColumn() { if (this.grid.current < this.grid.max) { this.grid.current++; this.generateGrid(); } }, // Remove column removeColumn() { if (this.grid.current > this.grid.min) { this.grid.current--; this.generateGrid(); } }, }; // Init grid.init();