Spaces:
Running
Running
Download script.js from 5uck1t/codecraft-input-symphony: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/spaces/5uck1t/codecraft-input-symphony/resolve/main/script.js
- Command line
-
hf download hf://spaces/5uck1t/codecraft-input-symphony/script.js
-
curl -L -o script.js https://huggingface.co/spaces/5uck1t/codecraft-input-symphony/resolve/main/script.js
12.3 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize tabs | |
| const tabButtons = document.querySelectorAll('.tab-btn'); | |
| const tabContents = document.querySelectorAll('.tab-content'); | |
| tabButtons.forEach(button => { | |
| button.addEventListener('click', () => { | |
| // Remove active class from all buttons and contents | |
| tabButtons.forEach(btn => btn.classList.remove('active', 'text-primary-500')); | |
| tabButtons.forEach(btn => btn.classList.add('text-gray-400')); | |
| tabContents.forEach(content => content.classList.add('hidden')); | |
| // Add active class to clicked button and corresponding content | |
| button.classList.add('active', 'text-primary-500'); | |
| button.classList.remove('text-gray-400'); | |
| const tabId = button.getAttribute('data-tab'); | |
| document.getElementById(tabId).classList.remove('hidden'); | |
| }); | |
| }); | |
| // Add input field | |
| const addInputBtn = document.getElementById('add-input'); | |
| const inputContainer = document.getElementById('input-container'); | |
| addInputBtn.addEventListener('click', () => { | |
| const inputGroup = document.createElement('div'); | |
| inputGroup.className = 'input-group'; | |
| inputGroup.innerHTML = ` | |
| <div class="flex gap-2"> | |
| <input type="text" class="w-1/2 bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Field Name"> | |
| <input type="text" class="w-1/2 bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Field Type"> | |
| </div> | |
| <button class="remove-input mt-1 text-red-400 hover:text-red-300 text-sm flex items-center"> | |
| <i data-feather="trash-2" class="w-3 h-3 mr-1"></i> Remove | |
| </button> | |
| `; | |
| inputContainer.appendChild(inputGroup); | |
| feather.replace(); | |
| // Add event listener to new remove button | |
| const removeBtn = inputGroup.querySelector('.remove-input'); | |
| removeBtn.addEventListener('click', () => { | |
| inputGroup.remove(); | |
| }); | |
| }); | |
| // Remove input field | |
| document.addEventListener('click', function(e) { | |
| if (e.target.classList.contains('remove-input') || e.target.closest('.remove-input')) { | |
| const btn = e.target.classList.contains('remove-input') ? e.target : e.target.closest('.remove-input'); | |
| btn.parentElement.remove(); | |
| } | |
| }); | |
| // Download all files | |
| const downloadBtn = document.getElementById('download-btn'); | |
| downloadBtn.addEventListener('click', () => { | |
| // First generate the output if not already generated | |
| document.getElementById('generate-btn').click(); | |
| // Get all code contents | |
| const className = document.getElementById('class-name').value || 'MyClass'; | |
| const entityCode = document.querySelector('#entity textarea').value; | |
| const repositoryCode = document.querySelector('#repository textarea').value; | |
| const serviceCode = document.querySelector('#service textarea').value; | |
| const controllerCode = document.querySelector('#controller textarea').value; | |
| // Function to create and trigger download | |
| function download(filename, text) { | |
| const element = document.createElement('a'); | |
| element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | |
| element.setAttribute('download', filename); | |
| element.style.display = 'none'; | |
| document.body.appendChild(element); | |
| element.click(); | |
| document.body.removeChild(element); | |
| } | |
| // Download all files | |
| download(`${className}.java`, entityCode); | |
| download(`${className}Repository.java`, repositoryCode); | |
| download(`${className}Service.java`, serviceCode); | |
| download(`${className}Controller.java`, controllerCode); | |
| }); | |
| // Generate output | |
| const generateBtn = document.getElementById('generate-btn'); | |
| const outputTextarea = document.querySelector('#output textarea'); | |
| const previewContent = document.querySelector('.preview-content'); | |
| const rawContent = document.querySelector('.raw-content'); | |
| generateBtn.addEventListener('click', () => { | |
| const className = document.getElementById('class-name').value || 'MyClass'; | |
| const inputGroups = Array.from(document.querySelectorAll('.input-group')); | |
| const fields = inputGroups.map(group => { | |
| const inputs = group.querySelectorAll('input'); | |
| return { | |
| name: inputs[0].value || 'field', | |
| type: inputs[1].value || 'String' | |
| }; | |
| }); | |
| // Generate Entity | |
| let entityCode = `package com.example.entity;\n\n`; | |
| entityCode += `import jakarta.persistence.Entity;\n`; | |
| entityCode += `import jakarta.persistence.GeneratedValue;\n`; | |
| entityCode += `import jakarta.persistence.GenerationType;\n`; | |
| entityCode += `import jakarta.persistence.Id;\n\n`; | |
| entityCode += `@Entity\n`; | |
| entityCode += `public class ${className} {\n\n`; | |
| // Add fields with annotations if needed | |
| fields.forEach(field => { | |
| if (field.name.toLowerCase() === 'id') { | |
| entityCode += ` @Id\n`; | |
| entityCode += ` @GeneratedValue(strategy=GenerationType.IDENTITY)\n`; | |
| } | |
| entityCode += ` private ${field.type} ${field.name};\n`; | |
| }); | |
| // Add default constructor | |
| entityCode += `\n protected ${className}() {}\n\n`; | |
| // Add parameterized constructor | |
| entityCode += ` public ${className}(`; | |
| entityCode += fields.map(f => `${f.type} ${f.name}`).join(', '); | |
| entityCode += `) {\n`; | |
| fields.forEach(f => { | |
| entityCode += ` this.${f.name} = ${f.name};\n`; | |
| }); | |
| entityCode += ` }\n\n`; | |
| // Add toString() | |
| entityCode += ` @Override\n`; | |
| entityCode += ` public String toString() {\n`; | |
| entityCode += ` return String.format(\n`; | |
| entityCode += ` "${className}[`; | |
| entityCode += fields.map(f => `${f.name}=%s`).join(', '); | |
| entityCode += `]",\n`; | |
| entityCode += ` `; | |
| entityCode += fields.map(f => `${f.name}`).join(', '); | |
| entityCode += `);\n`; | |
| entityCode += ` }\n\n`; | |
| // Add getters and setters | |
| fields.forEach(field => { | |
| // Getter | |
| const getterName = field.type === 'boolean' ? `is${field.name.charAt(0).toUpperCase() + field.name.slice(1)}` | |
| : `get${field.name.charAt(0).toUpperCase() + field.name.slice(1)}`; | |
| entityCode += ` public ${field.type} ${getterName}() {\n`; | |
| entityCode += ` return ${field.name};\n`; | |
| entityCode += ` }\n\n`; | |
| // Setter | |
| entityCode += ` public void set${field.name.charAt(0).toUpperCase() + field.name.slice(1)}(${field.type} ${field.name}) {\n`; | |
| entityCode += ` this.${field.name} = ${field.name};\n`; | |
| entityCode += ` }\n\n`; | |
| }); | |
| entityCode += `}\n`; | |
| // Generate Repository | |
| let repositoryCode = `package com.example.repository;\n\n`; | |
| repositoryCode += `import com.example.entity.${className};\n`; | |
| repositoryCode += `import org.springframework.data.repository.CrudRepository;\n`; | |
| repositoryCode += `import org.springframework.stereotype.Repository;\n\n`; | |
| // Find ID type (default to Long if not found) | |
| let idType = 'Long'; | |
| const idField = fields.find(f => f.name.toLowerCase() === 'id'); | |
| if (idField) { | |
| idType = idField.type; | |
| } | |
| repositoryCode += `@Repository\n`; | |
| repositoryCode += `public interface ${className}Repository extends CrudRepository<${className}, ${idType}> {\n\n`; | |
| repositoryCode += `}\n`; | |
| // Generate Service | |
| let serviceCode = `package com.example.service;\n\n`; | |
| serviceCode += `import org.springframework.beans.factory.annotation.Autowired;\n`; | |
| serviceCode += `import org.springframework.stereotype.Service;\n`; | |
| serviceCode += `import java.util.Optional;\n`; | |
| serviceCode += `import java.util.List;\n\n`; | |
| serviceCode += `@Service\n`; | |
| serviceCode += `public class ${className}Service {\n\n`; | |
| serviceCode += ` @Autowired\n`; | |
| serviceCode += ` private ${className}Repository ${className.toLowerCase()}Repository;\n\n`; | |
| serviceCode += ` public List<${className}> findAll() {\n`; | |
| serviceCode += ` return (List<${className}>) ${className.toLowerCase()}Repository.findAll();\n`; | |
| serviceCode += ` }\n\n`; | |
| serviceCode += ` public Optional<${className}> findById(Long id) {\n`; | |
| serviceCode += ` return ${className.toLowerCase()}Repository.findById(id);\n`; | |
| serviceCode += ` }\n\n`; | |
| serviceCode += ` public ${className} save(${className} ${className.toLowerCase()}) {\n`; | |
| serviceCode += ` return ${className.toLowerCase()}Repository.save(${className.toLowerCase()});\n`; | |
| serviceCode += ` }\n\n`; | |
| serviceCode += ` public void delete(${className} ${className.toLowerCase()}) {\n`; | |
| serviceCode += ` ${className.toLowerCase()}Repository.delete(${className.toLowerCase()});\n`; | |
| serviceCode += ` }\n\n`; | |
| serviceCode += ` public void delete(Long id) {\n`; | |
| serviceCode += ` ${className.toLowerCase()}Repository.deleteById(id);\n`; | |
| serviceCode += ` }\n`; | |
| serviceCode += `}\n`; | |
| // Generate Controller | |
| let controllerCode = `package com.example.controller;\n\n`; | |
| controllerCode += `import org.springframework.beans.factory.annotation.Autowired;\n`; | |
| controllerCode += `import org.springframework.web.bind.annotation.*;\n`; | |
| controllerCode += `import java.util.List;\n`; | |
| controllerCode += `import java.util.Optional;\n\n`; | |
| controllerCode += `@RestController\n`; | |
| controllerCode += `@RequestMapping("/api/${className.toLowerCase()}s")\n`; | |
| controllerCode += `public class ${className}Controller {\n\n`; | |
| controllerCode += ` @Autowired\n`; | |
| controllerCode += ` private ${className}Service ${className.toLowerCase()}Service;\n\n`; | |
| controllerCode += ` @GetMapping\n`; | |
| controllerCode += ` public List<${className}> findAll() {\n`; | |
| controllerCode += ` return ${className.toLowerCase()}Service.findAll();\n`; | |
| controllerCode += ` }\n\n`; | |
| controllerCode += ` @GetMapping("/{id}")\n`; | |
| controllerCode += ` public Optional<${className}> findById(@PathVariable Long id) {\n`; | |
| controllerCode += ` return ${className.toLowerCase()}Service.findById(id);\n`; | |
| controllerCode += ` }\n\n`; | |
| controllerCode += ` @PostMapping\n`; | |
| controllerCode += ` public ${className} save(@RequestBody ${className} ${className.toLowerCase()}) {\n`; | |
| controllerCode += ` return ${className.toLowerCase()}Service.save(${className.toLowerCase()});\n`; | |
| controllerCode += ` }\n\n`; | |
| controllerCode += ` @DeleteMapping("/{id}")\n`; | |
| controllerCode += ` public void delete(@PathVariable Long id) {\n`; | |
| controllerCode += ` ${className.toLowerCase()}Service.delete(id);\n`; | |
| controllerCode += ` }\n`; | |
| controllerCode += `}\n`; | |
| // Update tabs | |
| document.querySelector('#entity textarea').value = entityCode; | |
| document.querySelector('#repository textarea').value = repositoryCode; | |
| document.querySelector('#service textarea').value = serviceCode; | |
| document.querySelector('#controller textarea').value = controllerCode; | |
| }); | |
| // Set first tab as active by default | |
| if (tabButtons.length > 0) { | |
| tabButtons[0].click(); | |
| } | |
| }); |