MediaWiki:Gadget-infoboxQty.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
(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:
"use strict";
 
/**
* Infobox quantity script
Line 20 ⟶ 22:
*
* 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);
});
$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 $selfinput = $(e'<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 );
});
})

Navigation menu