User:Gaz Lloyd/mol

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to: navigation, search
var api = new mw.Api(), $wrapper, $buttons, $msgs, $map, i, $left, $right, $select, $button, curr = 1, width;

function get_map(wave) {
	var params = {
		action: 'parse',
		text: '{{Fight Kiln map|' + wave + '}}',
		prop: 'text',
		title: 'Fight Kiln map',
		disablepp: 'true'
	};
    $buttons.children().each(function () { $(this).prop('disabled',true); });
    $msgs.text('Loading...');
	api.post(params).done(
		function (response) {
            $map.html(response.parse.text['*']);
            $buttons.children().each(function () { $(this).prop('disabled',false); });
            $msgs.text('');
		}
	).fail(
		function (_,err) {
            $buttons.children().each(function () { $(this).prop('disabled',false); });
            $msgs.text('There was an error loading the map');
            console.log(err);
		}
	);
}

function check_buttons() {
    $right.prop('disabled', false);
    $left.prop('disabled', false);
    if (curr === 1) {
        $left.prop('disabled', true);
    } else if (curr === 37) {
        $right.prop('disabled', true);
    }
    $select.val(curr);
}
width = $('#kilnmap table').width();
$wrapper = $('#kilnmap')
    .css('width', (width+2) + 'px');
$buttons = $('<div id="kilnmap-buttons" style="text-align:center;">');
$msgs = $('<div id="kilnmap-msg" style="font-weight:bold; text-align:center;">');
$map = $('<div id="kilnmap-map">').html($wrapper.html());
$wrapper.html('')
    .append($buttons)
    .append($msgs)
    .append($map);

$left = $('<button id="kilnmap-button-left">')
    .text('←')
    .addClass('button');
$left.click(function () { curr--; get_map(curr); check_buttons(); });

$right = $('<button id="kilnmap-button-right">')
    .text('→')
    .addClass('button');
$right.click(function () { curr++; get_map(curr); check_buttons(); });

$select = $('<input id="kilnmap-field" type="number" value="1" style="text-align:center; width:3.5em; margin:auto 0.5em;">');
$select.change(function () { var $val = parseInt($(this).val(), 10); if (!isNaN($val) && $val <= 37 && $val >= 1) { curr = $val; get_map(curr); } else { $msgs.text('Error: please enter a number between 1 and 37.'); } check_buttons(); });

$buttons.append($left)
    .append($select)
    .append($right);

check_buttons();