MediaWiki:Common.js/infoboxQty.js

/** * Infobox quantity script * Adds a number input box next to the price of Infobox Item, among others * * TODO: add infobox monster * * USAGE: * 100 coins * inside the span: no numbers! * */

/** * Detects support for * * @return {boolean} returns true if browser supports it * @source http://diveintohtml5.info/detect.html#input-types */ function inputNumber { var i = document.createElement('input'); i.setAttribute('type', 'number'); return i.type !== 'text'; } /** * generic one-value calc * compatible with most (switch) infoboxes made with Module:Infobox * (assuming the input only needs to depend on one value and only multiply that value) */ var input_width = inputNumber ? '65px' : '50px', i = 0; $('span.infobox-quantity').each(function {	var $this = $(this),	$div = $(' ')			.addClass('QtyCalcResult' + i)			.css('display', 'inline')			.append($this.text),	$input = $(' ')				.attr({ id: 'QtyCalc' + i,					type: 'number', 'data-calc-num': i,					'data-price-each': $this.text.replace(',',''), value: '1', maxlength: '10', min: '0', max: '9999999999', })				.css({ width: input_width, 'margin-right': '2px', });	$input.on('keyup click change mousewheel', function { var warn = '', warn2 = '', $this = $(this), j = $this.attr('data-calc-num'), val = 1; // check if nonnumeric entered (generally if type=number not supported) // if so, setup warnings if ($this.val.search(/[^0-9]/g) != -1) { warn = ''; warn2 = ' '; }		//sanitise val val = $this.val.replace(/[^0-9]/g, ''); //check if there's any numbers at all if (val.length === 0) { //if no numbers there at all, use 1 $('.QtyCalcResult' + j).html(warn + rs.addCommas($this.attr('data-price-each')) + warn2); } else { $('.QtyCalcResult' + j).html(warn + rs.addCommas($this.attr('data-price-each') * val) + warn2); }	});	$this.empty;	$this.append($input);	$this.append($div);	i++; });