User:Aspallar/common.js

importArticles({   type: 'script',    articles: [        'u:dev:UnsafeScripts/code.js'    ] });

$(function {    $('#mw-clearyourcache').remove; });

(function ($) {   'use strict';

// don't run if this version disabled on page if ($('#mdw-disabled-js').attr('data-deckexport-3-2-0')) return;

var importData; var labelOption;

function ImportData { var importCards = []; var altImportCards = []; var sideboardCards = [];

function baseDisplayName(card) { return card.name + ' (' + card.set + ') ' + card.cardNumber; }

function importDisplayName(card) { return card.num + ' ' + baseDisplayName(card); }

function rarityValue(rarity) { return { 'Common': 0, 'Uncommon': 1, 'Rare': 2, 'Mythic Rare': 3 }[rarity]; }

function findCardByName(name) { for (var k = 0, l = importCards.length; k < l; k++) { if (importCards[k].name === name) return importCards[k]; }           for (k = 0, l = sideboardCards.length; k < l; k++) { if (sideboardCards[k].name === name) return sideboardCards[k]; }           return null; }

function findAllCardsByName(cards, name) { return cards.reduce(function (result, card) {               if (card.name === name) result.push(card);                return result;            }, []); }

function parseCardData(dataString) { if (dataString !== null && dataString.length > 0) importCards = JSON.parse(dataString); }

function parseSideboardData(dataString) { if (dataString !== null && dataString.length > 0) sideboardCards = JSON.parse(dataString); }

function parseAltCardData(dataString) { altImportCards = []; if (dataString !== null && dataString.length > 0) { var cards = JSON.parse(dataString); cards.forEach(function(card) {                   if (!altImportCards.some(function (altCard) {                        return card.name === altCard.name &&                            card.set === altCard.set;                    })) altImportCards.push(card);                }); altImportCards.sort(function (a, b) {                   return a.name.localeCompare(b.name);                }); }       }

function swapCards(altCardIndex) { var newCard = altImportCards[altCardIndex]; var oldCard = findCardByName(newCard.name); var oldSet = oldCard.set; var oldCardNumber = oldCard.cardNumber; var oldCardRarity = oldCard.rarity;

findAllCardsByName(importCards, newCard.name).forEach(function (card) {               card.set = newCard.set;                card.cardNumber = newCard.cardNumber;                card.rarity = newCard.rarity;            }); findAllCardsByName(sideboardCards, newCard.name).forEach(function (card) {               card.set = newCard.set;                card.cardNumber = newCard.cardNumber;                card.rarity = newCard.rarity;            });

newCard.set = oldSet; newCard.cardNumber = oldCardNumber; newCard.rarity = oldCardRarity;

return baseDisplayName(newCard); }

function adjustToCheapest { for (var k = 0, l = altImportCards.length; k < l; k++) { var card = findCardByName(altImportCards[k].name); if (rarityValue(altImportCards[k].rarity) < rarityValue(card.rarity)) swapCards(k); }       }

return { swapCards: function (altCardIndex) { return swapCards(altCardIndex); },           getRarityTotals: function  { var totals = {}; importCards.forEach(function (card) {                   var rarity = card.rarity;                    if (rarity !== 'Basic Land')                        totals[rarity] = (totals[rarity] || 0) + card.num;                }); return totals; },           text: function  { var text = ''; importCards.forEach(function(card) {                   text += importDisplayName(card) + '\n';                }); if (sideboardCards.length > 0) { text += '\n'; sideboardCards.forEach(function (card) {                       text += importDisplayName(card) + '\n';                    }); }               return text; },           getAltOptions: function  { var options = document.createDocumentFragment; altImportCards.forEach(function (card) {                   $(' ').html(baseDisplayName(card)).appendTo(options);                }); return options; },           hasAlternatives: function  { return altImportCards.length > 0; },           initialize: function(deckData, sideboardData, altData) { parseCardData(deckData); parseSideboardData(sideboardData); parseAltCardData(altData); adjustToCheapest; }       };    } // End ImportData

function sizeTextareaToContents(textarea) { textarea.style.overflow = 'hidden'; textarea.style.height = 'auto'; textarea.style.height = textarea.scrollHeight + 'px'; }

function setImportData { importData.initialize(           $('#mdw-chartdata-pre').text,            $('#mdw-sideboard-data').text,            $('#mdw-alt-carddata').text        ); }

function rarityEntry(label, value) { return ' ' + label +': ' + value + ' ';   }

function rarityContents(rarities) { var text = ''; if (rarities.Common) text += rarityEntry('Common', rarities.Common); if (rarities.Uncommon) text += rarityEntry('Uncommon', rarities.Uncommon); if (rarities.Rare) text += rarityEntry('Rare', rarities.Rare); if (rarities['Mythic Rare']) text += rarityEntry('Mythic Rare', rarities['Mythic Rare']); if (text.length > 0) text = text.substring(0, text.length - ' '.length); return text; }

function setRarityContents { $('#mdw-import-rarity').html(rarityContents(importData.getRarityTotals)); }

function onClickCopy { var importText = document.getElementById('mdw-arena-export-contents'); importText.select; document.execCommand('copy'); $('#mdw-copied-message').html(' copied to clipboard.'); setTimeout(function {            $('#mdw-copied-message').html('');        }, 1200); }

function onClickReset { setImportData; $('#mdw-import-reset') .prop('disabled', true); $('#mdw-import-select') .html('') .append(labelOption) .append(importData.getAltOptions); $('#mdw-arena-export-contents') .html(importData.text); setRarityContents; }

function onSelectAlternative { /* jshint -W040 */ // allow old school jquery use of this var text = importData.swapCards(this.selectedIndex - 1); this.options[this.selectedIndex].text = text; $('#mdw-arena-export-contents').html(importData.text); $('#mdw-import-reset').prop('disabled', false); setRarityContents; this.selectedIndex = 0; }

function setupAlternativesUi(container) { if (container === null || !importData.hasAlternatives) return;

labelOption = $(' Select alternative cards -- ');

var altSelect = $(' ') .append(labelOption) .append(importData.getAltOptions) .change(onSelectAlternative);

var resetButton = $('') .click(onClickReset);

$(container).append(altSelect).append(resetButton).css('display', 'block'); }

function setupImportUi(container) { var elements = $(               '' +                ' ' +                ' ' +                '' +                importData.text +                ' '        ); $(container).append(elements); sizeTextareaToContents(document.getElementById('mdw-arena-export-contents')); setRarityContents; $('#mdw-copy-export').click(onClickCopy); }

function initialize { var arenaImportContainer = document.getElementById('mdw-arena-export-div'); if (arenaImportContainer === null) return;

importData = new ImportData; setImportData; setupImportUi(arenaImportContainer); setupAlternativesUi(document.getElementById('mdw-arena-export-alt-div')); }

$(document).ready(initialize);

}(jQuery));