MediaWiki:Gadget-infoboxQty.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "/** * Infobox quantity script * Adds a number input box next to specific numbers in tables * Primary use case: the price in Infobox Item * * TODO: add infobox monster support (for what?) * * USAGE: * <td><span class="infobox-quantity" data-val-each="100" data-value="1"><span class="infobox-quantity-replace">100</span> coins</span></td> * Everything inside the td should be wrapped in the outer span, which has class=infobox-quantity and attr data-val-each=(value...")
 
No edit summary
 
Line 1: Line 1:
"use strict";

/**
/**
* Infobox quantity script
* Infobox quantity script
Line 20: Line 22:
*
*
* originally based on [[User:Joeytje50/monstercalc.js]]
* originally based on [[User:Joeytje50/monstercalc.js]]
*/
*/
$(function () {
$(function () {
mw.hook('wikipage.content').add(function init( $content ) {
mw.hook('wikipage.content').add(function init($content) {
// Delegated event so it works for any new inputs that get generated later on.
// Delegated event so it works for any new inputs that get generated later on.
$('body').on('keyup click change mousewheel', 'input.QtyCalc', function (event) {
$('body').on('keyup click change mousewheel', 'input.QtyCalc', function (event) {
var warn = '',
var warn = '',
warn2 = '',
warn2 = '',
val = 1,
val = 1,
$this = $(this),
$this = $(this),
$ifbq = $(event.currentTarget).parent(),
$ifbq = $(event.currentTarget).parent(),
$rep = $ifbq.find('span.infobox-quantity-replace'),
$rep = $ifbq.find('span.infobox-quantity-replace'),
each = $ifbq.attr('data-val-each'),
each = $ifbq.attr('data-val-each'),
formula = $ifbq.attr('data-formula');
formula = $ifbq.attr('data-formula');
// check if nonnumeric entered (generally if type=number not supported)
// check if nonnumeric entered (generally if type=number not supported)
// if so, setup warnings
// if so, setup warnings
if ($this.val().search(/[^0-9]/g) != -1) {
if ($this.val().search(/[^0-9]/g) != -1) {
warn = '<abbr title="non-numeric characters are ignored" class="explain">';
warn = '<abbr title="non-numeric characters are ignored" class="explain">';
warn2 = '</abbr>';
warn2 = '</abbr>';
}
}
//sanitise val, parse to int
//sanitise val, parse to int
val = parseInt($this.val().replace(/[^0-9]/g, ''));
val = parseInt($this.val().replace(/[^0-9]/g, ''));
if (isNaN(val)) {
if (isNaN(val)) {
val = 1; // invalid number -> just use 1
val = 1; // invalid number -> just use 1
}
}
$rep.html(warn + rswiki.addCommas(each * val) + warn2);
});
$rep.html(warn + rswiki.addCommas(each * val) + warn2);

});
/**
* generic one-value calc
/**
* compatible with most (switch) infoboxes made with [[Module:Infobox]]
* generic one-value calc
* Takes an argument `el` which must be a selector for the parent element, or the element itself.
* compatible with most (switch) infoboxes made with [[Module:Infobox]]
*/
* Takes an argument `el` which must be a selector for the parent element, or the element itself.
rswiki.initQtyBox = function (el) {
*/
$(el).find('td > span.infobox-quantity').not(':has(input)').each(function (i, e) {
rswiki.initQtyBox = function(el) {
var $self = $(e);
$(el).find('td > span.infobox-quantity').not(':has(input)').each(function(i,e){
var $self = $(e);
var $input = $('<input>').attr({
id: 'QtyCalc' + i,
var $input = $('<input>')
"class": 'QtyCalc',
.attr({
type: 'number',
id: 'QtyCalc'+i,
value: $self.data('value') || '1',
class: 'QtyCalc',
maxlength: '10',
type: 'number',
min: '0',
value: $self.data('value') || '1',
max: '9999999999'
maxlength: '10',
}).css({
min: '0',
width: '65px',
max: '9999999999',
'margin-right': '0.25em'
})
}).prependTo($self).trigger('change');
.css({
});
width: '65px',
};
'margin-right': '0.25em',

})
// init boxes in on the whole page
.prependTo($self)
rswiki.initQtyBox($content);
.trigger('change');
});
});
};
});
// init boxes in on the whole page
rswiki.initQtyBox( $content );
});
})

Latest revision as of 12:06, 20 October 2024

"use strict";

/**
 * Infobox quantity script
 * Adds a number input box next to specific numbers in tables
 * Primary use case: the price in Infobox Item
 * 
 * TODO: add infobox monster support (for what?)
 * 
 * USAGE:
 * <td><span class="infobox-quantity" data-val-each="100" data-value="1"><span class="infobox-quantity-replace">100</span> coins</span></td>
 * Everything inside the td should be wrapped in the outer span, which has class=infobox-quantity and attr data-val-each=(value of each item)
 * Inside that span, some part should be wrapped in another span with class=infobox-quantity-replace
 *     this is the part that gets replaced with the result
 * the result is input val * data-val-each
 * The data-value attribute is used as the default value. If it is not specified, the input is initialised at '1'.
 *
 * this is safe for use with switch infoboxes - the input is placed outside (before) the outer span, inside the td
 * [[MediaWiki:Common.js/switchInfobox2.js]] has an explicit exception to work with this and this only
 *     (that is, $("input+span"), with the entirety of the span being replaced on a switch)
 * 
 * 
 * originally based on [[User:Joeytje50/monstercalc.js]]
 */
$(function () {
  mw.hook('wikipage.content').add(function init($content) {
    // Delegated event so it works for any new inputs that get generated later on.
    $('body').on('keyup click change mousewheel', 'input.QtyCalc', function (event) {
      var warn = '',
        warn2 = '',
        val = 1,
        $this = $(this),
        $ifbq = $(event.currentTarget).parent(),
        $rep = $ifbq.find('span.infobox-quantity-replace'),
        each = $ifbq.attr('data-val-each'),
        formula = $ifbq.attr('data-formula');
      // check if nonnumeric entered (generally if type=number not supported)
      // if so, setup warnings
      if ($this.val().search(/[^0-9]/g) != -1) {
        warn = '<abbr title="non-numeric characters are ignored" class="explain">';
        warn2 = '</abbr>';
      }
      //sanitise val, parse to int
      val = parseInt($this.val().replace(/[^0-9]/g, ''));
      if (isNaN(val)) {
        val = 1; // invalid number -> just use 1
      }
      $rep.html(warn + rswiki.addCommas(each * val) + warn2);
    });

    /**
    * generic one-value calc
    * compatible with most (switch) infoboxes made with [[Module:Infobox]]
    * Takes an argument `el` which must be a selector for the parent element, or the element itself.
    */
    rswiki.initQtyBox = function (el) {
      $(el).find('td > span.infobox-quantity').not(':has(input)').each(function (i, e) {
        var $self = $(e);
        var $input = $('<input>').attr({
          id: 'QtyCalc' + i,
          "class": 'QtyCalc',
          type: 'number',
          value: $self.data('value') || '1',
          maxlength: '10',
          min: '0',
          max: '9999999999'
        }).css({
          width: '65px',
          'margin-right': '0.25em'
        }).prependTo($self).trigger('change');
      });
    };

    // init boxes in on the whole page
    rswiki.initQtyBox($content);
  });
});