|
| 1 | +// module |
| 2 | +const { ipcRenderer } = require('electron'); |
| 3 | +const axios = require('axios'); |
| 4 | +const alertMsg = require('../components/ui/alert'); |
| 5 | + |
| 6 | +// DOM element |
| 7 | +const techList = document.getElementById('tech-list'); |
| 8 | +const searchForm = document.getElementById('search-form'); |
| 9 | +const searchInput = document.getElementById('search-tech'); |
| 10 | + |
| 11 | +// init ajax function |
| 12 | +const libraryList = async () => { |
| 13 | + // clear before results |
| 14 | + techList.innerHTML = ''; |
| 15 | + |
| 16 | + try { |
| 17 | + const { data } = await axios.get('https://api.cdnjs.com/libraries', { |
| 18 | + params: { |
| 19 | + search: searchInput.value, |
| 20 | + fields: 'version' |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + data.results.forEach((library, i) => { |
| 25 | + |
| 26 | + // copy link button |
| 27 | + const linkButton = document.createElement('button'); |
| 28 | + linkButton.classList.add('btn-copy'); |
| 29 | + linkButton.textContent = '📋'; |
| 30 | + |
| 31 | + // click event |
| 32 | + linkButton.addEventListener('click', async () => { |
| 33 | + await navigator.clipboard.writeText(library.latest); |
| 34 | + alertMsg('Copied', 'alert-info'); |
| 35 | + }); |
| 36 | + |
| 37 | + // row and field elements |
| 38 | + const techRow = document.createElement('tr'); |
| 39 | + |
| 40 | + const numberField = document.createElement('td'); |
| 41 | + numberField.textContent = i + 1; |
| 42 | + |
| 43 | + const nameField = document.createElement('td'); |
| 44 | + nameField.textContent = library.name; |
| 45 | + |
| 46 | + const versionField = document.createElement('td'); |
| 47 | + versionField.textContent = library.version; |
| 48 | + |
| 49 | + const copyLinkField = document.createElement('td'); |
| 50 | + copyLinkField.classList.add('btn-container'); |
| 51 | + copyLinkField.appendChild(linkButton); |
| 52 | + |
| 53 | + // append fields |
| 54 | + techRow.append(numberField, nameField, versionField, copyLinkField); |
| 55 | + |
| 56 | + techList.append(techRow); |
| 57 | + }); |
| 58 | + } catch(err) { |
| 59 | + alertMsg(err.message, 'alert-danger'); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +// event key |
| 64 | +searchForm.addEventListener('submit', e => { |
| 65 | + e.preventDefault(); |
| 66 | + |
| 67 | + searchInput.value !== '' |
| 68 | + ? libraryList() |
| 69 | + : alertMsg('this field is required', 'alert-info'); |
| 70 | + |
| 71 | + searchForm.reset(); |
| 72 | +}); |
| 73 | + |
| 74 | +ipcRenderer.on('clear-results', () => { |
| 75 | + techList.innerHTML = ''; |
| 76 | +}); |
| 77 | + |
0 commit comments