mirror of
https://github.com/picocss/pico.git
synced 2025-04-20 16:46:14 -04:00
JavaScript modularization
This commit is contained in:
parent
366e0a55d6
commit
9575624834
21 changed files with 1629 additions and 1048 deletions
|
@ -1,131 +0,0 @@
|
|||
/*!
|
||||
* Grid Interaction
|
||||
*
|
||||
* Pico.css - https://picocss.com
|
||||
* Copyright 2019-2021 - Licensed under MIT
|
||||
*/
|
||||
|
||||
(function() {
|
||||
|
||||
/**
|
||||
* Config
|
||||
*/
|
||||
|
||||
var grid = {
|
||||
columnsCurrent: 4,
|
||||
columnsMin: 1,
|
||||
columnsMax: 12,
|
||||
targetButtons: '#grids article', // Buttons inserted before target
|
||||
targetGrid: '#grids .grid', // Grid target
|
||||
targetCode: '#grids pre code', // Code target
|
||||
selectorAdd: '#grids button.add', // Add button selector in Dom
|
||||
selectorRemove: '#grids button.remove', // Remove Button selector in Dom
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Init
|
||||
*/
|
||||
|
||||
initGridInteraction();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Init grid interaction
|
||||
*/
|
||||
|
||||
function initGridInteraction() {
|
||||
|
||||
// Add buttons
|
||||
addButtons();
|
||||
|
||||
// Add button listener
|
||||
document.querySelector(grid.selectorAdd).addEventListener('click', function() {
|
||||
addColumn();
|
||||
}, false);
|
||||
|
||||
// Remove button listener
|
||||
document.querySelector(grid.selectorRemove).addEventListener('click', function() {
|
||||
removeColumn();
|
||||
}, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add buttons
|
||||
*/
|
||||
|
||||
function addButtons() {
|
||||
var buttons = document.createElement('P');
|
||||
buttons.innerHTML = '<button class="secondary add">'
|
||||
+ '<svg xmlns="http://www.w3.org/2000/svg" height="16px" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">'
|
||||
+ '<line x1="12" y1="5" x2="12" y2="19">'
|
||||
+ '</line><line x1="5" y1="12" x2="19" y2="12">'
|
||||
+ '</line>'
|
||||
+ '</svg>'
|
||||
+ ' Add column'
|
||||
+ '</button>'
|
||||
|
||||
+ '<button class="secondary remove">'
|
||||
+ '<svg xmlns="http://www.w3.org/2000/svg" height="16px" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">'
|
||||
+ '<line x1="5" y1="12" x2="19" y2="12"></line>'
|
||||
+ '</svg>'
|
||||
+ ' Remove column'
|
||||
+ '</button>';
|
||||
document.querySelector(grid.targetButtons).before(buttons);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add column
|
||||
*/
|
||||
|
||||
function addColumn() {
|
||||
if (grid.columnsCurrent < grid.columnsMax) {
|
||||
grid.columnsCurrent++;
|
||||
generateGrid(grid.columnsCurrent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove column
|
||||
*/
|
||||
|
||||
function removeColumn() {
|
||||
if (grid.columnsCurrent > grid.columnsMin) {
|
||||
grid.columnsCurrent--;
|
||||
generateGrid(grid.columnsCurrent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Generate grid
|
||||
*
|
||||
* @param {number} cols
|
||||
*/
|
||||
|
||||
function generateGrid(cols) {
|
||||
|
||||
var colsHTML = '';
|
||||
var colsCode = '';
|
||||
var colsCodePref = '<<b>div</b> <i>class</i>=<u>"grid"</u>>\n';
|
||||
var colsCodeSuff = '</<b>div</b>>';
|
||||
|
||||
for (var i=0; i<cols; i++) {
|
||||
colsHTML += '<div>' + (i+1) + '</div>';
|
||||
colsCode += ' <<b>div</b>>' + (i+1) + '</<b>div</b>>\n';
|
||||
}
|
||||
|
||||
document.querySelector(grid.targetGrid).innerHTML = colsHTML;
|
||||
document.querySelector(grid.targetCode).innerHTML = colsCodePref+colsCode+colsCodeSuff;
|
||||
}
|
||||
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue