MediaWiki:Common.js/discalc.js

(function($, mw){   'use strict';    var console = window.console;    var settingsName = 'disassembly_calculator_settings';    var defaultSettings = {junk_reduction: 0}; //other defaults here, when there are some    var userSettings = {};    var junkreduction = [1, 0.99, 0.97, 0.95, 0.93, 0.91, 0.88, 0.86, 0.83, 0.8];

// fetch settings from localStorage function getSettings { var settings; try { // just in case something goes wrong settings = JSON.parse(window.localStorage.getItem(settingsName)); }       catch (err) { settings = {}; }       if (settings === null) { settings = {}; }       userSettings = $.extend({}, defaultSettings, settings); }

// update cached settings and put them back into localStorage // use this over directly setting to userSettings function updateSettings(k,v) { userSettings[k] = v;       window.localStorage.setItem(settingsName, JSON.stringify(userSettings)); }

// returns string version of input float, truncated to 2 decimal places //		with commas function twoDP(num) { if (num < 100) { return (Math.trunc(num * 100) / 100).toLocaleString('en-US'); } else { return (Math.trunc(num)).toLocaleString('en-US'); }   }

// main function function init {

// setup stuff // user settings from localStorage getSettings;

// select and supporting things var $select = $(' '); var $span = $(' '); var $table = $('#dis-calc-table'); $span.css('margin-left', '5px').text('Showing data for no junk reduction');

$select.attr({ name: 'dis-calc-select', id: 'dis-calc-select'}) .append($(' No junk reduction (1) ')) .append($(' Junk reduction 1 (34) ')) .append($(' Junk reduction 2 (49) ')) .append($(' Junk reduction 3 (64) ')) .append($(' Junk reduction 4 (69) ')) .append($(' Junk reduction 5 (78) ')) .append($(' Junk reduction 6 (83) ')) .append($(' Junk reduction 7 (91) ')) .append($(' Junk reduction 8 (95) ')) .append($(' Junk reduction 9 (105) '));

// begin change event $select.change(function {            $select.prop('disabled', true);            $span.text('Calculating...');            var $this = $(this);            var val = parseInt($this.val, 10);

$table.find('.dis-calc-row').each(function (i, e) { // for every row marked as a dis-calc-row in the table do               var $row = $(e);                var mats, basejunk, priceeach, rawchance, newjunk, newchance, newcost, newmph;

// get info from the row's data attrs mats = parseFloat($row.attr('data-dis-mats'), 10); basejunk = parseFloat($row.attr('data-dis-junk'), 10); priceeach = parseFloat($row.attr('data-dis-price'), 10); rawchance = parseFloat($row.attr('data-dis-raw'), 10);

// cancel if any of the rows aren't numbers - something went wrong if (isNaN(mats) || isNaN(basejunk) || isNaN(priceeach) || isNaN(rawchance)) { console.log("isNaN error with row"); console.log($row); return; }

// calculate new values newjunk = Math.trunc(10 * junkreduction[val] * basejunk) / 10; newchance = mats * (1 - newjunk / 100) * rawchance; newcost = priceeach * newchance / 100; newmph = 3000 * newchance / 100;

// apply new values $row.find('.data-dis-junkcell').text(newjunk + '%').attr('data-sort-value', newjunk); $row.find('.data-dis-chancecell').text(twoDP(newchance) + '%').attr('data-sort-value', newchance); $row.find('.data-dis-costcell').attr('data-sort-value', newcost).find('.coins').text(twoDP(newcost)); $row.find('.data-dis-mphcell').text(twoDP(newmph)).attr('data-sort-value', newmph); });

// reload table cache $table.trigger('updateSortableCache');

//set this as the user's setting updateSettings('junk_reduction', val);

//re-enable select now that we're done $select.prop('disabled', false); $span.text('Showing data for ' + (val === 0 ? 'no junk reduction' : 'junk reduction '+val)); }); //end change event

// add select to the page $('#dis-calc-dropdown').css('font-weight', 'bold').empty.append('Please select the junk reduction to display: ').append($select).append($span);

// set to user's settings - also writes the default settings if not already chosen $select.val(userSettings.junk_reduction).change; }

// run $(init); })(window.jQuery, window.mw);