diff --git a/web/js/directimport.js b/web/js/directimport.js index 82f61507..94e734e8 100755 --- a/web/js/directimport.js +++ b/web/js/directimport.js @@ -1,101 +1,128 @@ -$(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+''); + $('#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}
  • ` + ) } - update_stats(); + 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 - if(options.length == 0) { - if(qty == null) return; - return 'No match for '+name+''; - } else if(options.length == 1) { - return '' - +qty_text+''+options[0].title+' '; + const { cards, qty } = results + + if (cards.length == 0) { + return `No match for ${line}` + } else if (cards.length == 1) { + return `${cards[0].title}` } else { - var text = '' - +qty_text+''+options[0].title+' '; - text += ''; - return text; + 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.
    '; + +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 = size+' Cards.
    '+html; - $('#stats').html(html); - if($('#analyzed li').length > 0) { - $('#btn-save').prop('disabled', false); + } + html = `
    ${deckSize} Cards.
    ${html}` + $('#stats').html(html) + if ($('#analyzed li').length > 0) { + $('#btn-save').prop('disabled', false) } else { - $('#btn-save').prop('disabled', true); + $('#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)) }