let polishSchemas = null; $.ajax({ url: '/static/schemas/polish.json', success: data => polishSchemas = data }); let searchBar = $('#search-bar'); searchBar.autocomplete({ source: (request, response) => { $.ajax({ url: '/langs/polish/words?like=' + request.term + '&limit=20&offset=0', success: data => response(data) }) } }); $('#search-form').on('submit', (e) => { e.preventDefault(); let word = e.target[0].value getWord(word); }); function getWord(word) { $.ajax({ url: '/langs/polish/words/' + word, success: (data) => { $('#ajax-content').html(generateHtml(word, data)) searchBar.autocomplete('close'); }, error: err => console.error(err) }) } function getCells(forms, tags) { if(tags.length === 0) return undefined; let cells = forms.filter(form => tags.every(value => form.tags.includes(value)) ); cells.forEach(cell => cell.used = true ); if(cells.length === 0) return undefined; return cells; } function generateList(data) { let html = ''; return html; } function generateTable(schemas, pos, forms) { let schema = schemas.find(schema => schema.pos === pos); // No schema was provided by the server - fallback to a list if(!schema) return generateList(forms); let html = '
'; html += ''; schema.rows.forEach(row => { html += ''; row.forEach(cell => { if('display' in cell) { html += ``; } else { let cells = getCells(forms, cell.tags); let content = cells ? cells.map(cell => cell.form).join(',
') : '-'; html += ``; } }); html += ''; }); html += '
${cell.display}${content}
'; html += '
'; let unusedCells = forms.filter(cell => !cell.used); if(schema.ignoreUnused) { unusedCells = unusedCells.filter(cell => !schema.ignoreUnused.map(tags => tags.every(tag => cell.tags.includes(tag))) ); } if(unusedCells.length > 0) { html += '

Other

'; html += generateList(unusedCells); } return html; } function generateHtml(word, data) { let html = ''; if(data.length === 0) { html += `

Not found: ${word}

`; } else { data.forEach(entry => { html += `

${entry.word} (${entry.pos})

` if('senses' in entry) { if('tags' in entry.senses[0]) { html += '
' html += entry.senses[0].tags.map(tag => `${tag}`).join(', '); html += '
' } html += '

Senses

'; html += '
    '; entry.senses.forEach(sense => { html += '
  1. ' if('form_of' in sense) { let word = sense.form_of[0].word; html += sense.glosses[0].replace(new RegExp(`of ${word}$`), ''); html += ` of ${word}`; } else { html += sense.glosses[0]; } html += '
  2. '; }) html += '
'; } if('forms' in entry) { if(entry.pos === 'verb') { let conjugation = entry.forms.filter(form => 'source' in form && form.source === 'Conjugation'); if(conjugation.length > 0) { html += '

Conjugation

'; html += generateTable(polishSchemas, entry.pos, conjugation); } } else { let declension = entry.forms.filter(form => 'source' in form && form.source === 'Declension'); if(declension.length > 0) { html += '

Declension

'; html += generateTable(polishSchemas, entry.pos, declension); } } } }); } return html; } $(document).ready(function() { $('#search-bar').select(); });