2016-01-12 13:36:58 -08:00
|
|
|
DDH.conversions = DDH.conversions || {};
|
|
|
|
|
2017-04-25 02:05:44 -07:00
|
|
|
(function(DDH) {
|
|
|
|
"use strict";
|
|
|
|
|
2017-04-25 12:51:30 -07:00
|
|
|
String.prototype.capitalize = function() {
|
|
|
|
return this.charAt(0).toUpperCase() + this.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the input / output fields
|
2017-04-26 10:41:45 -07:00
|
|
|
var $convert_left,
|
|
|
|
$convert_right;
|
2017-04-25 02:05:44 -07:00
|
|
|
|
2017-04-25 12:51:30 -07:00
|
|
|
var $selects;
|
2017-04-25 02:05:44 -07:00
|
|
|
|
2017-04-25 12:51:30 -07:00
|
|
|
// conversion classes and units
|
|
|
|
var Units = {
|
|
|
|
length: {
|
|
|
|
name: "Length",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['meter', 'cm', 'inch', 'foot', 'yard', 'mile', 'link', 'rod', 'chain', 'angstrom', 'mil'],
|
|
|
|
defaults: ['cm', 'meter']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
surface: {
|
|
|
|
name: "Surface",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['m2', 'sqin', 'sqft', 'sqyd', 'sqmi', 'sqrd', 'sqch', 'sqmil', 'acre', 'hectare'],
|
|
|
|
defaults: ['m2', 'sqin']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
volume: {
|
|
|
|
name: "Volume",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['litre', 'cc', 'cuin', 'cuft', 'cuyd', 'teaspoon', 'tablespoon'],
|
|
|
|
defaults: ['tablespoon', 'litre']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
liquid_volume: {
|
|
|
|
name: "Liquid Volume",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['minim', 'fluiddram', 'fluidounce', 'gill', 'cup', 'pint', 'quart', 'gallon', 'beerbarrel', 'oilbarrel', 'hogshead', 'drop'],
|
|
|
|
defaults: ['minim', 'fluiddram']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
angles: {
|
|
|
|
name: "Angles",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['rad', 'deg', 'grad', 'cycle', 'arcsec', 'arcmin'],
|
|
|
|
defaults: ['rad', 'deg']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
time: {
|
|
|
|
name: "Time",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade', 'century', 'millennium'],
|
|
|
|
defaults: ['second', 'minute']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
mass: {
|
|
|
|
name: "Mass",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['gram', 'tonne', 'ton', 'grain', 'dram', 'ounce', 'poundmass', 'hundredweight', 'stick', 'stone'],
|
|
|
|
defaults: ['gram', 'tonne']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
temperature: {
|
|
|
|
name: "Temperature",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['kelvin', 'celsius', 'fahrenheit', 'rankine'],
|
|
|
|
defaults: ['celsius', 'fahrenheit']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
force: {
|
|
|
|
name: "Force",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['newton', 'dyne', 'poundforce', 'kip'],
|
|
|
|
defaults: ['newton', 'dyne']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
energy: {
|
|
|
|
name: "Energy",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['joule', 'erg', 'Wh', 'BTU', 'electronvolt'],
|
|
|
|
defaults: ['joule', 'erg']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
power: {
|
|
|
|
name: "Power",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['watt', 'hp'],
|
|
|
|
defaults: ['watt', 'hp']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
pressure: {
|
|
|
|
name: "Pressure",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['Pa', 'psi', 'atm', 'torr', 'bar', 'mmHg', 'mmH2O', 'cmH2O'],
|
|
|
|
defaults: ['Pa', 'psi']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
electricity_magnetism: {
|
|
|
|
name: "Electricity and magnetism",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['ampere', 'coulomb', 'watt', 'volt', 'ohm', 'farad', 'weber', 'tesla', 'henry', 'siemens', 'electronvolt'],
|
|
|
|
defaults: ['ampere', 'coulomb']
|
2017-04-25 12:51:30 -07:00
|
|
|
},
|
|
|
|
binary: {
|
|
|
|
name: "Binary",
|
2017-04-26 10:41:45 -07:00
|
|
|
units: ['bit', 'byte'],
|
|
|
|
defaults: ['bit', 'byte']
|
2017-04-25 12:51:30 -07:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 02:05:44 -07:00
|
|
|
|
2017-04-25 12:51:30 -07:00
|
|
|
// Convertor: the object that handles the conversions
|
|
|
|
var Converter = {
|
|
|
|
firstUnit: "",
|
|
|
|
secondUnit: "",
|
2017-04-26 08:39:13 -07:00
|
|
|
firstValue: "",
|
|
|
|
secondValue: "",
|
2017-04-25 12:51:30 -07:00
|
|
|
|
|
|
|
getFirstUnit: function() {
|
|
|
|
this.firstUnit = $("select.zci__conversions_left-select").val();
|
|
|
|
},
|
|
|
|
|
2017-04-26 08:39:13 -07:00
|
|
|
getFirstValue: function() {
|
|
|
|
this.firstValue = $("#zci__conversions-left-in").val();
|
|
|
|
},
|
|
|
|
|
2017-04-25 12:51:30 -07:00
|
|
|
getSecondUnit: function() {
|
|
|
|
this.secondUnit = $("select.zci__conversions_right-select").val();
|
|
|
|
},
|
|
|
|
|
2017-04-26 08:39:13 -07:00
|
|
|
getSecondValue: function() {
|
|
|
|
this.secondValue = $("#zci__conversions-right-in").val();
|
|
|
|
},
|
|
|
|
|
|
|
|
setup: function() {
|
2017-04-25 12:51:30 -07:00
|
|
|
this.getFirstUnit();
|
|
|
|
this.getSecondUnit();
|
2017-04-26 08:39:13 -07:00
|
|
|
this.getFirstValue();
|
|
|
|
this.getSecondValue();
|
|
|
|
},
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
convert: function(side) {
|
2017-04-26 08:39:13 -07:00
|
|
|
this.setup();
|
2017-04-26 10:41:45 -07:00
|
|
|
if(side === "right") {
|
|
|
|
console.log("on the left");
|
|
|
|
var expression = this.firstValue + " " + this.firstUnit + " to " + this.secondUnit;
|
|
|
|
var conversion = math.eval(expression).toString().replace(/[A-Za-z]+/, '').trim();
|
|
|
|
$convert_right.val(conversion);
|
|
|
|
} else {
|
|
|
|
console.log("on the right");
|
|
|
|
var expression = this.secondValue + " " + this.secondUnit + " to " + this.firstUnit;
|
|
|
|
var conversion = math.eval(expression).toString().replace(/[A-Za-z]+/, '').trim();
|
|
|
|
$convert_left.val(conversion);
|
|
|
|
}
|
2017-04-25 12:51:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSelects(key) {
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
// removes all the options
|
2017-04-25 12:51:30 -07:00
|
|
|
$(".zci__conversions_left-select").empty();
|
|
|
|
$(".zci__conversions_right-select").empty();
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
// adds the new conversion units to the selects
|
2017-04-25 12:51:30 -07:00
|
|
|
for(var i = 0 ; i < Units[key].units.length ; i++) {
|
2017-04-26 10:41:45 -07:00
|
|
|
$selects.append(
|
|
|
|
'<option value="' + Units[key].units[i] + '">' +
|
|
|
|
Units[key].units[i].capitalize() +
|
|
|
|
'</option>'
|
|
|
|
);
|
2017-04-25 12:51:30 -07:00
|
|
|
}
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
// set defaults. written this way for readability
|
|
|
|
$(".zci__conversions_left-select").val(Units[key].defaults[0]);
|
|
|
|
$(".zci__conversions_right-select").val(Units[key].defaults[1]);
|
2017-04-25 12:51:30 -07:00
|
|
|
}
|
|
|
|
|
2017-04-26 08:39:13 -07:00
|
|
|
function resetToOne() {
|
2017-04-26 10:41:45 -07:00
|
|
|
// sets the left unit to 1
|
2017-04-26 08:39:13 -07:00
|
|
|
$("input#zci__conversions-left-in").val("1");
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:05:44 -07:00
|
|
|
DDH.conversions.build = function(ops) {
|
2017-04-25 12:51:30 -07:00
|
|
|
|
2017-04-25 02:05:44 -07:00
|
|
|
return {
|
|
|
|
signal: "high",
|
|
|
|
onShow: function() {
|
|
|
|
DDG.require('math.js', function() {
|
2017-04-25 12:51:30 -07:00
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
$convert_left = $(".zci__conversions_left input");
|
|
|
|
$convert_right = $(".zci__conversions_right input");
|
2017-04-25 12:51:30 -07:00
|
|
|
$selects = $(".zci__conversion-container select");
|
2017-04-26 10:41:45 -07:00
|
|
|
var $select_right = $(".zci__conversions_right-select");
|
|
|
|
var $select_left = $(".zci__conversions_left-select");
|
2017-04-25 12:51:30 -07:00
|
|
|
var $unitSelector = $(".zci__conversions_bottom-select");
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
updateSelects('length');
|
2017-04-25 12:51:30 -07:00
|
|
|
|
|
|
|
// adds the different unit types to the selector
|
|
|
|
var unitKeys = Object.keys(Units);
|
|
|
|
$.each(unitKeys, function(_key, value) {
|
|
|
|
$unitSelector.append('<option value="'+value+'">'+Units[value].name+'</option>');
|
|
|
|
});
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
|
|
|
|
$convert_left.keyup(function(e) {
|
|
|
|
if(this.value !== "") {
|
|
|
|
Converter.convert("right");
|
|
|
|
} else {
|
|
|
|
Converter.convert("right");
|
|
|
|
}
|
2017-04-25 12:51:30 -07:00
|
|
|
});
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
$convert_right.keyup(function(e) {
|
2017-04-25 12:51:30 -07:00
|
|
|
if(this.value !== "") {
|
2017-04-26 10:41:45 -07:00
|
|
|
Converter.convert("left");
|
2017-04-25 12:51:30 -07:00
|
|
|
} else {
|
2017-04-26 10:41:45 -07:00
|
|
|
Converter.convert("left");
|
2017-04-25 12:51:30 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-26 10:41:45 -07:00
|
|
|
$select_right.change(function() {
|
|
|
|
Converter.convert("right");
|
|
|
|
});
|
|
|
|
|
|
|
|
$select_left.change(function() {
|
|
|
|
Converter.convert("left");
|
|
|
|
});
|
|
|
|
|
|
|
|
$unitSelector.change(function() {
|
|
|
|
resetToOne();
|
|
|
|
updateSelects(this.value);
|
|
|
|
Converter.convert("left");
|
2017-04-25 12:51:30 -07:00
|
|
|
});
|
|
|
|
|
2017-04-25 02:05:44 -07:00
|
|
|
});
|
2017-04-25 12:51:30 -07:00
|
|
|
|
|
|
|
}// on show
|
|
|
|
}; // return
|
|
|
|
}; // DDH.conversions.build
|
2017-04-25 02:05:44 -07:00
|
|
|
})(DDH);
|