MediaWiki:ImportDeck.js

// ========================================================================== // ImportDeck // // Version 1.0.0 // Author: Aspallar // // Provides a user friendly way to import a deck from Magic Arena // without the user having to edit any wikitext. // // ** Please do not edit this code directly in the wikia. // ** Instead use the git repository https://github.com/Aspallar/WikiLua // (function ($) {   /* global mw*/    'use strict';

if (document.getElementById('mdw-import-deck') === null || $('#mdw-disabled-js').attr('importdeck-1-0-0')) return;

var newDeckTemplate = '';

function getBaseUrl { return mw.config.get('wgArticlePath').replace('$1', ''); }

function buildUrl(page, query) { return getBaseUrl + page + (query ? '?' + $.param(query) : ''); }

function removeIncludes(contents) { return contents.replace(/<[\/]?noinclude>|<[\/]?includeonly>/g, ''); }

function invalidTitle(title) { return /#|\?/g.test(title); }

function showWorking { $('#mdw-working').show; }

function hideWorking { $('#mdw-working').hide; }

function showForm { $('#mdw-import-deck').fadeIn(400); }

function showBadEntries(badEntries) { var content = badEntries.join(' '); $('#mdw-import-badentries-content').html(content); $('#mdw-import-badentries').fadeIn(400); }

function hideBadEntries { $('#mdw-import-badentries').hide; }

function disableButton { $('#mdw-import-button').prop('disabled', true); }

function enableButton { $('#mdw-import-button').prop('disabled', false); }

function getNewDeckTemplate { // var deferred = $.Deferred; // deferred.resolve('start/n$1/nend '); // return deferred.promise; return $.get(buildUrl('Template:NewDeck', {action: 'raw'})); }

function displayError(name, message) { $('#mdw-import-' + name + '-error').html('* ' + message); return false; }

function unexpectedError(message) { displayError('unexpected', 'An unexpected error occurred: ' + message); }

function validateDeckName { var name = $('#mdw-import-deckname').val.trim; if (name.length === 0) return displayError('deckname', 'A deck name is required.'); else if (invalidTitle(name)) return displayError('deckname', 'Invalid deck name.'); else return true; }

function validateDeckDef { var deckdef = $('#mdw-import-deckdef').val.trim; if (deckdef.length === 0) return displayError('deckdef', 'You must enter a deck definition'); else return true; }

function redirectToTitle(title) { var url = mw.config.get('wgArticlePath').replace('$1', title); window.location = url; }

function handleCreateError(error) { enableButton; if (error.code === 'articleexists') { $('#mdw-import-deckname-error').html('* Deck name already in use.'); $('#mdw-import-deckname').focus; } else if (error.code === 'invalidtitle') { $('#mdw-import-deckname-error').html('* Invalid deck name.'); $('#mdw-import-deckname').focus; } else { unexpectedError(error.info); }   }

function createDeckPage(name, deckEntries) { disableButton; showWorking; mw.loader.using('mediawiki.api').then(function {            var content = newDeckTemplate.replace('$1', deckEntries.join('\n'));            var title = 'Decks/' + name;            new mw.Api.post({ action: 'edit', title: title, summary: 'Imported deck', createonly: 'yes', text: content, token: mw.user.tokens.get('editToken') }).done(function(result) { hideWorking; if (result.error === undefined) { redirectToTitle(title); } else { handleCreateError(result.error); }           }).fail(function(code, result) { unexpectedError(code + (code === 'http' ? ' ' + result.textStatus : '')); });       });

}

function parseDeckDef(deckdef) { var result = { validEntries: [], badEntries: [], sideboardCount: 0 }; deckdef.split('\n').forEach(function (entry) {           entry = entry.trim;            if (entry.length === 0) {                ++result.sideboardCount;                result.validEntries.push('--- sideboard ---');            } else if (/^\d+\s+.*\S.*$/.test(entry)) {                result.validEntries.push(entry);            } else {                result.badEntries.push(entry);            }        }); return result; }

function clickImport { hideBadEntries; $('.mdw-error').html(''); var nameValid = validateDeckName ; var deckdefValid = validateDeckDef; if (deckdefValid) { var result = parseDeckDef($('#mdw-import-deckdef').val.trim); if (result.badEntries.length > 0) showBadEntries(result.badEntries); if (result.sideboardCount > 1) displayError('deckdef', 'Import contains more than one sideboard.'); if (nameValid && result.badEntries.length === 0 && result.sideboardCount <= 1) createDeckPage($('#mdw-import-deckname').val, result.validEntries); }   }

function createForm { $('#mdw-import-deckname-span') .append(''); $('#mdw-import-deckdef-span') .append(' '); $('#mdw-import-button-span') .append('') .click(clickImport); }

function initialize { $('#mdw-working').html($(' ', { src: mw.config.get('stylepath') + '/common/images/ajax.gif' }));       showWorking; getNewDeckTemplate.done(function (template) {           newDeckTemplate = removeIncludes(template);            createForm;            hideWorking;            showForm;            $('#mdw-import-deckname').focus;        }).fail(function  {            hideWorking;            $('#mdw-import-deck').html(' Failed to load new deck template. ');           showForm;        }); }

$(document).ready(initialize);

}(jQuery));