MediaWiki:Common.js/exchangeCreate.js

/** * For creating exchange pages and modules * * @author Cqm * * @todo finish */


 * (function ($, mw) {

'use strict';

var conf = mw.config.get([           'skin',            'wgNamespaceNumber',            'wgServer',            'wgTitle'        ]),

self = { // placeholder for data data: {},

/**            * Startup method *            * Checks for the correct environment before proceeding * @example subpage of Module:Exchange * @example Exchange namespace page */           init: function  { if (                   // Module:Exchange subpage                    (conf.wgNamespaceNumber === 828 && conf.wgTitle.indexOf('Exchange/') === 0) ||                    // Exchange ns page                    conf.wgNamespaceNumber === 112 // @todo check number                ) { // load missing jquery ui theme in oasis, not loaded by default for some reason if (conf.skin === 'oasis') { mw.loader.load(conf.wgServer + '/resources/jquery.ui/themes/default/jquery.ui.theme.css', 'text/css'); }                   self.addButton; }           },

/**            * Adds a button to generate modal for creating new exchange pages */           addButton: function  { var noArticle = !!$('#noarticletext').length, item = conf.wgTitle.replace('Exchange/').replace('/Data'), text = (noArticle ? 'Create exchange page(s)' : 'Edit exchange data'), $anchor = $('') .addClass('wikia-button') .attr({                           id: 'rs-exchange-data',                            href: '#'                        }) .on('click', self.createModal) .text(text);

$('#WikiaPageHeader').append($anchor);

// @todo move to stylesheet mw.util.addCSS(                   '.rs-ex-input, .rs-ex-select {float:right;}' +                    '.rs-ex-input:not([type="checkbox"]) {width:211px;}' +                    '.rs-ex-input[type="checkbox"] {margin-right:202px;}' +                    '.rs-ex-select {width:215px}'                ); },

/**            * Creates a modal to input exchange data *            * @param e {jquery.event} */           createModal: function (e) { e.preventDefault;

var item = conf.wgTitle.replace('Exchange/', ).replace('/Data',  ), $dialog = $(' ') .addClass('dialog') .attr({                           id: 'rs-ex-dialog',                            title: 'Editing exchange data for ' + item                        }) .append(                           $(' ')                                .attr('id', 'rs-ex-status'),                            $(' ')                                .attr('id', 'rs-ex-table')                                .css({ width: '350px', margin: 'auto' })                               .append( $(' ')                                       .append(                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-item')                                                                .text('Item'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-item', type: 'text' })                                                       )                                                ),                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-id')                                                                .text('Item Id'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-id', type: 'number' })                                                       )                                                ),                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-icon')                                                                .text('Icon'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-icon', type: 'text' })                                                       )                                                ),                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-value')                                                                .text('Value'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-value', type: 'number' })                                                       )                                                ),                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-limit')                                                                .text('Limit'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-limit', type: 'number' })                                                       )                                                ),                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-members')                                                                .text('Members'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-members', type: 'checkbox' })                                                               .prop('checked', false)                                                        ) ),                                           $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-alchable')                                                                .text('Alchable'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-alchable', type: 'checkbox' })                                                               .prop('checked', false)                                                        ) ),                                           $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .text('Category'),                                                            $(' ')                                                                .addClass('rs-ex-select')                                                                .attr('id', 'rs-ex-category')                                                                .append( $(' ')                                                                       .val('Unknown') .text('Unknown'), $(' ')                                                                       .val('Ammo') .text('Ammo'), $(' ')                                                                       .val('Arrows') .text('Arrows'), $(' ')                                                                       .val('Bolts') .text('Bolts'), $(' ')                                                                       .val('Construction materials') .text('Construction materials'), $(' ')                                                                       .val('Construction products') .text('Construction products'), $(' ')                                                                       .val('Cooking ingredients') .text('Cooking ingredients'), $(' ')                                                                       .val('Costumes') .text('Costumes'), $(' ')                                                                       .val('Crafting materials') .text('Crafting materials'), $(' ')                                                                       .val('Familiars') .text('Familiars'), $(' ')                                                                       .val('Farming produce') .text('Farming produce'), $(' ')                                                                       .val('Fletching materials') .text('Fletching materials'), $(' ')                                                                       .val('Food and Drink') .text('Food and Drink'), $(' ')                                                                       .val('Herblore materials') .text('Herblore materials'), $(' ')                                                                       .val('Hunting equipments') .text('Hunting equipments'), $(' ')                                                                       .val('Hunting Produce') .text('Hunting Produce'), $(' ')                                                                       .val('Jewellry') .text('Jewellry'), $(' ')                                                                       .val('Mage armour') .text('Mage armour'), $(' ')                                                                       .val('Mage weapons') .text('Mage weapons'), $(' ')                                                                       .val('Melee armour - high level') .text('Melee armour - high level'), $(' ')                                                                       .val('Melee armour - low level') .text('Melee armour - low level'), $(' ')                                                                       .val('Melee armour - mid level') .text('Melee armour - mid level'), $(' ')                                                                       .val('Melee weapons - high level') .text('Melee weapons - high level'), $(' ')                                                                       .val('Melee weapons - low level') .text('Melee weapons - low level'), $(' ')                                                                       .val('Melee weapons - mid level') .text('Melee weapons - mid level'), $(' ')                                                                       .val('Mining and Smithing') .text('Mining and Smithing'), $(' ')                                                                       .val('Miscellaneous') .text('Miscellaneous'), $(' ')                                                                       .val('Pocket items') .text('Pocket item'), $(' ')                                                                       .val('Potions') .text('Potions'), $(' ')                                                                       .val('Prayer armour') .text('Prayer armour'), $(' ')                                                                       .val('Prayer materials') .text('Prayer materials'), $(' ')                                                                       .val('Range armour') .text('Range armour'), $(' ')                                                                       .val('Range weapons') .text('Range weapons'), $(' ')                                                                       .val('Runecrafting') .text('Runecrafting'), $(' ')                                                                       .val('Runes, Spells and Teleports') .text('Runes, Spells and Teleports'), $(' ')                                                                       .val('Seeds') .text('Seeds'), $(' ')                                                                       .val('Summoning scrolls') .text('Summoning scrolls'), $(' ')                                                                       .val('Tools and containers') .text('Tools and containers'), $(' ')                                                                       .val('Woodcutting product') .text('Woodcutting product') )                                                       )                                                ),                                            $(' ')                                                .append( $(' ')                                                       .append(                                                            $(' ')                                                                .addClass('rs-ex-label')                                                                .attr('for', 'rs-ex-examine')                                                                .text('Examine'),                                                            $(' ')                                                                .addClass('rs-ex-input')                                                                .attr({ id: 'rs-ex-examine', type: 'text' })                                                       )                                                )                                        )                                )                        );                $dialog .dialog({                       buttons: {                            Cancel: function  {                                $(this).dialog('close');                            },                            Submit: function  {                                console.log('Submitting data');                                self.createPages;                                $(this).dialog('close');                            }                        },                        dialogClass: 'rs-ex-dialog',                        draggable: false,                        modal: true,                        resizable: false,                        width: 500                    })

$dialog.dialog('open'); // work around for a jquery ui bug // see  // @todo is it worth filing this with Wikia? $('.rs-ex-dialog').find('.ui-button-text').each(function {                    var $this = $(this);

$this.text($this.parent.attr('text')); $this.css('padding', '0 1em'); });

// attempt to autofill data from the item's exchange module self.loadModuleData(item); },

/**            * Attempts to fill in existing data from the module page *            * @param item {string} The name of the item to get the data for */           loadModuleData: function (item) { // make sure first letter of item is uppercase // otherwise price data won't be found item = item.charAt(0).toUpperCase + item.slice(1);

(new mw.Api) .get({                       action: 'query',                        prop: 'revisions',                        titles: 'Module:Exchange/' + item,                        rvprop: 'content',                        redirects: ''                    }) .done(function (res) {                       var pages = res.query.pages,                            content,                            data;

if (pages[-1]) { // @todo ask to attempt to load data from GED API console.log('Module does not exist'); return; }

content = pages[Object.keys(pages)[0]].revisions[0]['*']; data = rs.parseExchangeModule(content); console.log(data);

// fill out form with data $('#rs-ex-item').val(data.item.replace(/'(.*?)'/, '$1')); $('#rs-ex-id').val(data.itemid); $('#rs-ex-icon').val(data.icon.replace(/'(.*?)'/, '$1')); $('#rs-ex-value').val(data.value); $('#rs-ex-limit').val(data.limit); $('#rs-ex-members').prop('checked', data.members === 'true'); $('#rs-ex-alchable').prop('checked', data.alchable !== 'false'); $('#rs-ex-category').val(data.category.replace(/'(.*?)'/, '$1')); $('#rs-ex-examine').val(data.examine.replace(/'(.*?)'/, '$1'));

self.data = data; });           },

/**            * Queries the GED API to find the required item data *            * @example  *            * If the data cannot be retrieved (due to anyorigin being difficult) * this links to the result page and asks the user to copy/paste the data into * a textbox */           getData: function  {},

/**            * Creates exchange modules and pages with the collected data */           createPages: function  { // merge form into data object self.updateData;

var data = self.data, table = 'return {\n' + '   itemId   = ' + data.itemid + ',\n' + '   price    = ' + data.price + ',\n' + '   last     = ' + data.last + ',\n' + '   date     = ' + data.date + ',\n' + '   lastDate = ' + data.lastdate + ',\n' + '   icon     = ' + data.icon + ',\n' + '   item     = ' + data.item + ',\n' + (data.alchable === 'false' ? '   alchable = false,\n' : '') + '   value    = ' + data.value + ',\n' + '   limit    = ' + data.limit + ',\n' + '   members  = ' + data.members + ',\n' + '   category = ' + data.category + ',\n' + '   examine  = ' + data.examine + ',\n' + '   usage    = ' + data.usage +'\n' + '}';

console.log(table);

},

/**            * Updates `self.data` with the values in the form */           updateData: function  { var $table = $('#rs-ex-table'), // object containing default data defaultData = { itemid: 'null', price: 'null', last: 'null', date: 'null', lastDate: 'null', icon: 'null', item: 'null', alchable: 'null', value: 'null', limit: 'null', members: 'null', category: 'null', examine: 'null', usage: '' },                   // object containign data filled out in the form formData = { itemid: $table.find('#rs-ex-id').val.trim, icon: $table.find('#rs-ex-icon').val.trim, item: $table.find('#rs-ex-item').val.trim, alchable: $table.find('#rs-ex-alchable').prop('checked').toString, value: $table.find('#rs-ex-value').val.trim, limit: $table.find('#rs-ex-limit').val.trim, members: $table.find('#rs-ex-members').prop('checked').toString, category: $table.find('#rs-ex-category').val, examine: $table.find('#rs-ex-examine').val.trim },                   x;

console.log(formData);

for (x in formData) { if (x === 'category') { // allow category to be unset if (formData[x].toLowerCase === 'unknown') { formData[x] = 'null'; // else add quotes } else { formData[x] = '\ + formData[x] + '\; }                   // delete keys with empty values } else if (formData[x] === '') { delete formData[x]; }

// add quotes to string keys // category is handled above if (['icon', 'item', 'examine'].indexOf(x) > -1) { // make sure we escape single quotes too formData[x] = '\ + formData[x].replace(/'/g, '\\\) + '\''; }               }

self.data = $.extend(defaultData, self.data, formData); console.log(self.data); }       };

// @todo remove mw.util dependency once CSS is move to stylesheet mw.loader.using(['jquery.ui.dialog', 'mediawiki.api', 'mediawiki.util'], function {        $(self.init);    });

}(jQuery, mediaWiki));