picocss/docs/js/grid.js

108 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-10-31 21:33:19 +07:00
/*
* Grid
2021-10-31 21:33:19 +07:00
*
* Pico.css - https://picocss.com
2023-01-28 13:14:38 +07:00
* Copyright 2019-2023 - Licensed under MIT
2021-10-31 21:33:19 +07:00
*/
const grid = {
2021-11-08 00:06:11 +07:00
// Config
buttons: {
text: {
2024-01-27 13:53:19 +07:00
add: "Add column",
remove: "Remove column",
2021-11-08 00:06:11 +07:00
},
2024-01-27 13:53:19 +07:00
target: "#grid article",
2021-11-08 00:06:11 +07:00
},
grid: {
current: 4,
min: 1,
max: 12,
2024-01-27 13:53:19 +07:00
gridTarget: "#grid .grid",
codeTarget: "#grid pre code",
2021-11-08 00:06:11 +07:00
},
2021-10-31 21:33:19 +07:00
2021-11-08 00:06:11 +07:00
// Init
init() {
2024-01-27 13:53:19 +07:00
this.addButtons()
this.generateGrid()
2021-11-08 00:06:11 +07:00
},
// Add buttons
addButtons() {
// Insert buttons
2024-01-27 13:53:19 +07:00
let buttons = document.createElement("P")
2021-11-08 00:06:11 +07:00
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>
${this.buttons.text.add}
</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>
${this.buttons.text.remove}
2024-01-27 13:53:19 +07:00
</button>`
document.querySelector(this.buttons.target).before(buttons)
2021-11-08 00:06:11 +07:00
// Add button listener
2024-01-27 13:53:19 +07:00
document.querySelector("#grid button.add").addEventListener(
"click",
() => {
this.addColumn()
},
false
)
2021-11-08 00:06:11 +07:00
// Remove button listener
2024-01-27 13:53:19 +07:00
document.querySelector("#grid button.remove").addEventListener(
"click",
() => {
this.removeColumn()
},
false
)
2021-11-08 00:06:11 +07:00
},
// Generate grid
generateGrid() {
// Config
2024-01-27 13:53:19 +07:00
let htmlInner = ""
let codeInner = '&lt;<b>div</b> <i>class</i>=<u>"grid"</u>&gt;\n'
2021-11-08 00:06:11 +07:00
// Build
for (let col = 0; col < this.grid.current; col++) {
2024-01-27 13:53:19 +07:00
htmlInner += "<div>" + (col + 1) + "</div>"
codeInner += " &lt;<b>div</b>&gt;" + (col + 1) + "&lt;/<b>div</b>&gt;\n"
2021-11-08 00:06:11 +07:00
}
// Display
2024-01-27 13:53:19 +07:00
codeInner += "&lt;/<b>div</b>&gt;"
document.querySelector(this.grid.gridTarget).innerHTML = htmlInner
document.querySelector(this.grid.codeTarget).innerHTML = codeInner
2021-11-08 00:06:11 +07:00
},
// Add column
addColumn() {
if (this.grid.current < this.grid.max) {
2024-01-27 13:53:19 +07:00
this.grid.current++
this.generateGrid()
2021-11-08 00:06:11 +07:00
}
},
// Remove column
removeColumn() {
if (this.grid.current > this.grid.min) {
2024-01-27 13:53:19 +07:00
this.grid.current--
this.generateGrid()
2021-11-08 00:06:11 +07:00
}
},
2024-01-27 13:53:19 +07:00
}
2021-11-08 00:06:11 +07:00
// Init
2024-01-27 13:53:19 +07:00
grid.init()