Skip to content

Commit

Permalink
Compose tag options
Browse files Browse the repository at this point in the history
  • Loading branch information
takayukister committed Aug 4, 2024
1 parent e0d86f5 commit b592f7e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion admin/includes/js/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 44 additions & 8 deletions admin/includes/js/src/tag-generator-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ const update = form => {
form.querySelectorAll(
'.tag'
).forEach( tag => {
let tagType = form.querySelector( '[name="tagtype"]' )?.value ||
tag.getAttribute( 'name' );
const basetype = form.querySelector( '[name="tagtype"]' )?.value ||
tag.name;

if ( tagType && form.querySelector( '[name="required"]:checked' ) ) {
tagType += '*';
if ( basetype ) {
tag.value = compose( basetype, form );
}

tag.value = compose( tagType, form );
} );

form.querySelectorAll(
Expand All @@ -55,10 +53,48 @@ const normalize = field => {
};


const compose = ( tagType, form ) => {
const compose = ( basetype, form ) => {
const name = form.querySelector( '[name="name"]' )?.value ?? '';
const scope = form.querySelector( `.scope.${ basetype }` ) ?? form;

const options = [];

scope.querySelectorAll(
'.option'
).forEach( input => {
if ( 'checkbox' === input.type ) {
if ( input.checked ) {
options.push( input.name );
}
} else if ( 'radio' === input.type ) {
if ( input.checked && ! input.classList.contains( 'default' ) ) {
options.push( `${ input.name }:${ input.value }` );
}
} else if ( '' !== input.value ) {
if ( input.classList.contains( 'filetype' ) ) {
options.push(
`${ input.name }:${ input.value.split( /[,|\s]+/ ).join( '|' ) }`
);
} else if ( input.classList.contains( 'color' ) ) {
options.push( `${ input.name }:#${ input.value }` );
} else if ( 'class' === input.name ) {
input.value.split( ' ' ).forEach( term => {
options.push( `class:${ term }` );
} );
} else {
options.push( `${ input.name }:${ input.value }` );
}
}
} );

if ( 'radio' === basetype ) {
options.push( 'default:1' );
}

const type = basetype +
( form.querySelector( '[name="required"]:checked' ) ? '*' : '' );

let composed = tagType + ' ' + name;
const composed = type + ' ' + name + ' ' + options.join( ' ' );

return `[${ composed.trim() }]`;
};
Expand Down

0 comments on commit b592f7e

Please sign in to comment.