Refactor: JS syntax

This commit is contained in:
Lucas Larroche 2021-11-08 00:06:11 +07:00
parent 5557876fce
commit 0c64ec721a
6 changed files with 129 additions and 211 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Add some magic to Pico docs * Customization
* *
* Pico.css - https://picocss.com * Pico.css - https://picocss.com
* Copyright 2019-2021 - Licensed under MIT * Copyright 2019-2021 - Licensed under MIT

View file

@ -1,12 +1,100 @@
/* /*
* Add some magic to Pico docs * Grid
* *
* Pico.css - https://picocss.com * Pico.css - https://picocss.com
* Copyright 2019-2021 - Licensed under MIT * Copyright 2019-2021 - Licensed under MIT
*/ */
// Imports const grid = {
import grid from './src/grid.js';
// Grid Interaction // 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 = `
<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}
</button>`;
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 = '&lt;<b>div</b> <i>class</i>=<u>"grid"</u>&gt;\n';
// Build
for (let col = 0; col < this.grid.current; col++) {
htmlInner += '<div>' + (col + 1) + '</div>';
codeInner += ' &lt;<b>div</b>&gt;' + (col + 1) + '&lt;/<b>div</b>&gt;\n';
}
// Display
codeInner += '&lt;/<b>div</b>&gt;';
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(); grid.init();

View file

@ -31,12 +31,7 @@ export const colorPicker = {
// Loop colors // Loop colors
for (const color in this.colors) { for (const color in this.colors) {
// Buttons // Buttons
innerButtons += innerButtons += `<button data-color="${color}" aria-label="Activate ${color} theme"></button>`;
'<button data-color="' +
color +
'" aria-label="Activate ' +
color +
' theme"></button>';
// Styles // Styles
innerStyles += ` innerStyles += `
@ -62,19 +57,13 @@ export const colorPicker = {
// Buttons listeners // Buttons listeners
this.buttons = document.querySelectorAll(this.selectorButton); this.buttons = document.querySelectorAll(this.selectorButton);
this.buttons.forEach( this.buttons.forEach( button => {
function (button) { button.addEventListener('click', event => {
button.addEventListener( let color = event.target.getAttribute('data-color');
'click', this.setActiveButton(color);
function (event) { this.generateTheme(color);
let color = event.target.getAttribute('data-color'); }, false);
this.setActiveButton(color); });
this.generateTheme(color);
}.bind(this),
false
);
}.bind(this)
);
// Insert CSS Styles // Insert CSS Styles
let containerStyles = document.createElement('STYLE'); let containerStyles = document.createElement('STYLE');
@ -87,16 +76,12 @@ export const colorPicker = {
// Set active button // Set active button
setActiveButton(color) { setActiveButton(color) {
// Remove all active states // Remove all active states
this.buttons.forEach( this.buttons.forEach( button => {
function (button) { button.removeAttribute('class');
button.removeAttribute('class'); });
}.bind(this)
);
// Set active state // Set active state
let buttonPicked = document.querySelector( let buttonPicked = document.querySelector(this.selectorButton + '[data-color="' + color + '"]');
this.selectorButton + '[data-color="' + color + '"]'
);
buttonPicked.setAttribute('class', 'picked'); buttonPicked.setAttribute('class', 'picked');
}, },
@ -116,18 +101,12 @@ export const colorPicker = {
'.inverse': data['inverse'], '.inverse': data['inverse'],
}; };
Object.keys(swaps).forEach( Object.keys(swaps).forEach( swap => {
function (swap) { let targets = document.querySelectorAll(this.selectorSection + ' ' + swap);
let targets = document.querySelectorAll( targets.forEach( target => {
this.selectorSection + ' ' + swap target.innerHTML = swaps[swap];
); });
targets.forEach( });
function (target) {
target.innerHTML = swaps[swap];
}.bind(this)
);
}.bind(this)
);
// 2. Update CSS Styles // 2. Update CSS Styles
const innerStyles = ` const innerStyles = `

View file

@ -1,107 +0,0 @@
/*
* Grid Interaction
*
* Pico.css - https://picocss.com
* Copyright 2019-2021 - Licensed under MIT
*/
export 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 = `
<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}
</button>`;
document.querySelector(this.buttons.target).before(buttons);
// Add button listener
document.querySelector('#grid button.add').addEventListener(
'click',
function () {
this.addColumn();
}.bind(this),
false
);
// Remove button listener
document.querySelector('#grid button.remove').addEventListener(
'click',
function () {
this.removeColumn();
}.bind(this),
false
);
},
// Generate grid
generateGrid() {
// Config
let htmlInner = '';
let codeInner = '&lt;<b>div</b> <i>class</i>=<u>"grid"</u>&gt;\n';
// Build
for (let col = 0; col < this.grid.current; col++) {
htmlInner += '<div>' + (col + 1) + '</div>';
codeInner += ' &lt;<b>div</b>&gt;' + (col + 1) + '&lt;/<b>div</b>&gt;\n';
}
// Display
codeInner += '&lt;/<b>div</b>&gt;';
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();
}
},
};
export default grid;

View file

@ -23,36 +23,21 @@ export const themeSwitcher = {
// Prefered color scheme // Prefered color scheme
get preferedColorScheme() { get preferedColorScheme() {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
return 'dark';
} else {
return 'light';
}
}, },
// Init switchers // Init switchers
initSwitchers() { initSwitchers() {
const buttons = document.querySelectorAll(this.buttonsTarget); const buttons = document.querySelectorAll(this.buttonsTarget);
buttons.forEach( buttons.forEach(button => {
function (button) { button.addEventListener('click', () => {
button.addEventListener( this.scheme == 'dark' ? this.scheme = 'light' : this.scheme = 'dark';
'click', }, false);
function (event) { });
if (this.scheme == 'dark') {
this.scheme = 'light';
} else {
this.scheme = 'dark';
}
}.bind(this),
false
);
}.bind(this)
);
}, },
// Add new button // Add new button
addButton(config) { addButton(config) {
// Insert Switcher
let button = document.createElement(config.tag); let button = document.createElement(config.tag);
button.className = config.class; button.className = config.class;
document.querySelector(config.target).appendChild(button); document.querySelector(config.target).appendChild(button);
@ -61,19 +46,11 @@ export const themeSwitcher = {
// Set scheme // Set scheme
set scheme(scheme) { set scheme(scheme) {
if (scheme == 'auto') { if (scheme == 'auto') {
if (this.preferedColorScheme == 'dark') { this.preferedColorScheme == 'dark' ? this._scheme = 'dark' : this._scheme = 'light';
this._scheme = 'dark';
} else {
this._scheme = 'light';
}
} }
// Set to Dark
else if (scheme == 'dark' || scheme == 'light') { else if (scheme == 'dark' || scheme == 'light') {
this._scheme = scheme; this._scheme = scheme;
} }
// Set to Apply theme
this.applyScheme(); this.applyScheme();
}, },
@ -84,22 +61,14 @@ export const themeSwitcher = {
// Apply scheme // Apply scheme
applyScheme() { applyScheme() {
// Root attribute
document.querySelector('html').setAttribute('data-theme', this.scheme); document.querySelector('html').setAttribute('data-theme', this.scheme);
// Buttons text
const buttons = document.querySelectorAll(this.buttonsTarget); const buttons = document.querySelectorAll(this.buttonsTarget);
let text;
buttons.forEach( buttons.forEach(
function (button) { button => {
if (this.scheme == 'dark') { const text = this.scheme == 'dark' ? this.change.dark : this.change.light;
text = this.change.dark;
} else {
text = this.change.light;
}
button.innerHTML = text; button.innerHTML = text;
button.setAttribute('aria-label', text.replace(/<[^>]*>?/gm, '')); button.setAttribute('aria-label', text.replace(/<[^>]*>?/gm, ''));
}.bind(this) }
); );
}, },
}; };

View file

@ -18,25 +18,14 @@ export const toggleNavigation = {
}, },
onToggleClick() { onToggleClick() {
this.toggleLink.addEventListener( this.toggleLink.addEventListener('click', event => {
'click', event.preventDefault();
function (event) { this.state == 'closed-on-mobile'
event.preventDefault(); ? this.state = 'open'
if (this.state == 'closed-on-mobile') { : this.state = 'closed-on-mobile';
this.state = 'open'; this.nav.removeAttribute('class');
} else { this.nav.classList.add(this.state);
this.state = 'closed-on-mobile'; }, false);
}
this.nav.removeAttribute('class');
this.nav.classList.add(this.state);
}.bind(this),
false
);
},
// Apply navigation state
applyState() {
}, },
// Get state // Get state