const params = { 'dluo': 'DLUO', 'lot': 'NÂș de lot', 'q': 'Poids net (g)', 'teneur': 'Teneur en fruits (%)', 'f': 'QuantitĂ© de fruits pour 100g (g)', } var tikats; function addProduct(tikette) { const zett = tikette; const appbody = $("#appbody"); const block = $('
'); for (let sub in zett.subs) { block.append($(`
`)); } const loader = $('
') .hide(); const action = $('
') .append($('generate') .click(() => { const subs = block.find(':text') .toArray() .reduce((obj, el) => ({...obj, [el.name]: el.value}), {}); const req = { template: zett.prototempalte, designation: zett.designation, description: zett.description, ingredients: zett.ingredients, color: zett.color, AB: zett.ab, // mind the capitalization here subs, landscape: zett.landscape, }; loader.show(); $('.btn').addClass("disabled"); $.post(backend_api + 'generate', JSON.stringify(req)) .then(data => { const pdfbtn = $(`open pdf`); action.append(pdfbtn); }) .catch(err => { console.error(err); }) .always(() => { loader.hide(); $('.btn').removeClass('disabled'); }); }) .append(loader)); const deleteAction = $('delete'); deleteAction.click(() => { const req = { id: zett.id, }; $.post(backend_api + 'deletetikette', JSON.stringify(req)).then(reload); return false; }); appbody .append($('
  • ') .append($('
    ') .append(`
    ${zett.title}
    `) .append($('') .append(deleteAction))) .append($('
    ') .append(block) .append(action))); } function setCategories() { const katsel = $('#new-type'); katsel.empty(); for (let kat of tikats) { katsel.append($(``)); } } function loadAll(zetikettes) { const appbody = $("#appbody"); appbody.empty(); for (let zett of zetikettes) { addProduct(zett); } setCategories(); $('.collapsible').collapsible(); $('.modal').modal(); $('select').formSelect(); konami(); $('#new-add').off('click').click(() => { const title = $("#new-name").val(); const category_id = $("#new-type").val(); const designation = $("#new-designation").val(); const ingredients = $("#new-ingredients").val(); const description = $("#new-description").val(); const color = $("#new-color").val().substring(1); const ab = $("#new-organic").is(":checked") ? 'visible' : 'none'; const req = { title, category_id, designation, ingredients, description, color, ab, }; $.post(backend_api + 'newtikette', JSON.stringify(req)).then(reload); }); } function konami() { var k = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65], n = 0; $(document).keydown(function (e) { if (e.keyCode === k[n++]) { if (n === k.length) { document.location.href = backend_api + 'admin'; } } else { n = 0; } }); } async function reload() { try { const resp = await $.ajax({ url: backend_api + 'list', timeout: 1000, }); tikats = (await $.ajax({ url: backend_api + 'categories', timeout: 1000, })).tikats.sort((a, b) => a.name > b.name ? 1 : -1); loadAll(resp.tikettes.sort((a, b) => (a.title < b.title) ? -1 : 1)); } catch(e) { const appbody = $("#appbody"); appbody.append(`
  • Could not reach backend server`); throw e; } } $(document).ready(reload);