From c263a73c0ea5aa9895e9ff68fbc83bfc879a2535 Mon Sep 17 00:00:00 2001 From: Tri Nguyen Date: Tue, 22 Jul 2025 23:53:39 +1000 Subject: [PATCH 1/4] refactoring direct import --- web/js/directimport.js | 182 +++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 87 deletions(-) diff --git a/web/js/directimport.js b/web/js/directimport.js index 82f61507..b8c46467 100755 --- a/web/js/directimport.js +++ b/web/js/directimport.js @@ -1,101 +1,109 @@ -$(document).on('data.app', function() { - $('#btn-import').prop('disabled', false); +$(document).on("data.app", function () { + $("#btn-import").prop("disabled", false); }); -$(function() { - $('#analyzed').on({ - click: click_option - }, 'ul.dropdown-menu a'); - $('#analyzed').on({ - click: click_trash - }, 'a.glyphicon-trash'); +$(function () { + $("#analyzed").on( + { + click: clickOption, + }, + "ul.dropdown-menu a" + ); + $("#analyzed").on( + { + click: click_trash, + }, + "a.glyphicon-trash" + ); }); function click_trash(event) { - $(this).closest('li.list-group-item').remove(); - update_stats(); + $(this).closest("li.list-group-item").remove(); + updateStats(); } function do_import() { - $('#analyzed').empty(); - var content = $('textarea[name="content"]').val(); - var lines = content.split(/[\r\n]+/); - for(var i=0; i'+a+''); - } - update_stats(); + $("#analyzed").empty(); + const content = $('textarea[name="content"]').val(); + const lines = content.split(/[\r\n]+/); + + for (let i = 0; i < lines.length; i++) { + const cardRow = getCardRowForLine(lines[i], i); + if (!cardRow) continue; + $("#analyzed").append( + `
  • ${cardRow}
  • ` + ); + } + updateStats(); } -function import_one_line(line, lineNumber) { - var result = NRDB.fuzzy_search.lookup(line); - if(!result) return; - var options = result.cards, qty = result.qty; - var qty_text = "", qty_int = qty; - if(qty == null) { - options = $.grep(options, function (card) { - return card.type_code == "identity"; - }); - qty_int = 1; - } else { - qty_text = qty+"x "; - } +function getCardRowForLine(line, lineNumber) { + const results = NRDB.fuzzy_search.lookup(line); + if (!results || (results.cards.length === 0 && results.qty === null)) + return; + + const { cards, qty } = results; - if(options.length == 0) { - if(qty == null) return; - return 'No match for '+name+''; - } else if(options.length == 1) { - return '' - +qty_text+''+options[0].title+' '; - } else { - var text = '' - +qty_text+''+options[0].title+' '; - text += ''; - return text; - } + if (cards.length == 0) { + return `No match for ${line}`; + } else if (cards.length == 1) { + return `${cards[0].title}`; + } else { + return ` + ${qty}x ${cards[0].title} + + `; + } } -function click_option(event) { - var name = $(this).text(); - var code = $(this).data('code'); - var input = $(this).closest('li.list-group-item').find('input[type="hidden"]'); - input.val(input.val().replace(/^\d+/, code)); - $(this).closest('li.list-group-item').find('a.card').html(name+' ').data('code', code); - update_stats(); +function clickOption() { + const name = $(this).data('title'); + const code = $(this).data("code"); + const row = $(this).closest("li.list-group-item").find("a.card"); + row.data("code", code); + row.find(".title").text(name); + updateStats(); } -function update_stats() { - var deck = {}, size = 0, types = {}; - $('#analyzed input[type="hidden"]').each(function (index, element) { - var card = $(element).val().split(':'); - var code = card[0], qty = parseInt(card[1], 10); - deck[code] = qty; - var record = NRDB.data.cards.findById(code); - types[record.type.name] = types[record.type.name] || 0; - types[record.type.name] += qty; - }); - var html = ''; - $.each(types, function (key, value) { - if(key != "Identity") { - size+=value; - html += value+' '+key+'s.
    '; - } - }); - html = size+' Cards.
    '+html; - $('#stats').html(html); - if($('#analyzed li').length > 0) { - $('#btn-save').prop('disabled', false); - } else { - $('#btn-save').prop('disabled', true); - } + +function updateStats() { + let deckSize = 0; + const types = {}; + $("#analyzed .card").each(function (_, element) { + const card = $(element); + const code = card.data("code"); + const qty = parseInt(card.data("qty"), 10); + const record = NRDB.data.cards.findById(code); + types[record.type.name] = types[record.type.name] ?? 0 + qty; + }); + + let html = ""; + for (const [type, amount] of Object.entries(types)) { + if (type !== "Identity") { + deckSize += amount; + html += ` +
    ${amount} ${type}s
    + `; + } + } + html = `
    ${deckSize} Cards.
    ${html}`; + $("#stats").html(html); + if ($("#analyzed li").length > 0) { + $("#btn-save").prop("disabled", false); + } else { + $("#btn-save").prop("disabled", true); + } } function do_save() { - var deck = {}; - $('#analyzed input[type="hidden"]').each(function (index, element) { - var card = $(element).val().split(':'); - var code = card[0], qty = parseInt(card[1], 10); - deck[code] = qty; - }); - $('input[name="content"]').val(JSON.stringify(deck)); + const deck = {}; + $("#analyzed .card").each(function (_, element) { + const card = $(element); + const code = card.data("code"); + const qty = parseInt(card.data("qty"), 10); + deck[code] = qty; + }); + $('input[name="content"]').val(JSON.stringify(deck)); } From 2f0a0acb324e05a6aa96cf97a9697c3906a26671 Mon Sep 17 00:00:00 2001 From: Tri Nguyen Date: Wed, 23 Jul 2025 22:24:14 +1000 Subject: [PATCH 2/4] fix styling --- web/js/directimport.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/web/js/directimport.js b/web/js/directimport.js index b8c46467..2da1a51e 100755 --- a/web/js/directimport.js +++ b/web/js/directimport.js @@ -48,7 +48,11 @@ function getCardRowForLine(line, lineNumber) { return `${cards[0].title}`; } else { return ` - ${qty}x ${cards[0].title} + ${qty}x ${ + cards[0].title + }