Difference between revisions of "MediaWiki:Gadget-calculator-anatomyPhysiology.js"

From WikiAnesthesia
Line 1: Line 1:
/**
* @author Chris Rishel
*/
( function() {
( function() {
     var moduleId = 'anatomyPhysiology';
     var COOKIE_EXPIRATION = 12 * 60 * 60;


     mw.calculators.addUnitsBases( {
     var TYPE_NUMBER = 'number';
        bpm: {
    var TYPE_STRING = 'string';
            toString: function( units ) {
                units = units.replace( 'bpm', 'beats/min' );


                 return units;
    var VALID_TYPES = [
        TYPE_NUMBER,
        TYPE_STRING
    ];
 
    var DEFAULT_CALCULATION_CLASS = 'SimpleCalculation';
    var DEFAULT_CALCULATOR_CLASS = 'SimpleCalculator';
 
    // Polyfill to fetch unit's base. This may become unnecessary in a future version of math.js
    math.Unit.prototype.getBase = function() {
        for( var iBase in math.Unit.BASE_UNITS ) {
            if( this.equalBase( math.Unit.BASE_UNITS[ iBase ] ) ) {
                 return iBase;
            }
        }
 
        return null;
    };
 
 
    mw.calculators = {
        calculators: {},
        calculations: {},
        objectClasses: {},
        units: {},
        unitsBases: {},
        variables: {},
        addCalculations: function( calculationData, className ) {
            className = className ? className : DEFAULT_CALCULATION_CLASS;
 
            var calculations = mw.calculators.createCalculatorObjects( className, calculationData );
 
            for( var calculationId in calculations ) {
                var calculation = calculations[ calculationId ];
 
                mw.calculators.calculations[ calculationId ] = calculation;
 
                mw.calculators.calculations[ calculationId ].setDependencies();
             }
             }
         },
         },
         hgb: {
         addCalculators: function( moduleId, calculatorData, className ) {
             toString: function( units ) {
             className = className ? className : DEFAULT_CALCULATOR_CLASS;
                 units = units.replace( 'hgbperdL', '/dL' );
 
                 units = units.replace( 'pcthct', '%' );
            for( var calculatorId in calculatorData ) {
                 calculatorData[ calculatorId ].module = moduleId;
 
                // Make sure the calculations have been defined
                for( var iCalculation in calculatorData[ calculatorId ].calculations ) {
                    var calculationId = calculatorData[ calculatorId ].calculations[ iCalculation ];
 
                    if( !mw.calculators.getCalculation( calculationId ) ) {
                        throw new Error( 'Calculator "' + calculatorId + '" references calculation "' + calculationId + '" which is not defined' );
                    }
                 }
            }
 
            var calculators = mw.calculators.createCalculatorObjects( className, calculatorData );
 
            // Initalize the calculators property for the module
            if( !mw.calculators.calculators.hasOwnProperty( moduleId ) ) {
                mw.calculators.calculators[ moduleId ] = {};
            }
 
            // Store the calculators
            for( var calculatorId in calculators ) {
                mw.calculators.calculators[ moduleId ][ calculatorId ] = calculators[ calculatorId ];


                 return units;
                 mw.calculators.calculators[ moduleId ][ calculatorId ].render();
             }
             }
         },
         },
         o2: {
         addUnitsBases: function( unitsBaseData ) {
            toString: function( units ) {
            var unitsBases = mw.calculators.createCalculatorObjects( 'UnitsBase', unitsBaseData );
                units = units.replace( 'pcto2', '%' );


                 return units;
            for( var unitsBaseId in unitsBases ) {
                 mw.calculators.unitsBases[ unitsBaseId ] = unitsBases[ unitsBaseId ];
             }
             }
         },
         },
         temperature: {
         addUnits: function( unitsData ) {
             toString: function( units ) {
             var units = mw.calculators.createCalculatorObjects( 'Units', unitsData );
                 units = units.replace( 'deg', '°' );
 
            for( var unitsId in units ) {
                 if( mw.calculators.units.hasOwnProperty( unitsId ) ) {
                    continue;
                }
 
                try {
                    var unitData = {
                        aliases: units[ unitsId ].aliases,
                        baseName: units[ unitsId ].baseName ? units[ unitsId ].baseName.toUpperCase() : units[ unitsId ].baseName,
                        definition: units[ unitsId ].definition,
                        prefixes: units[ unitsId ].prefixes,
                        offset: units[ unitsId ].offset,
                    };
 
                    math.createUnit( unitsId, unitData );
                } catch( e ) {
                    console.warn( e.message );
                }


                 return units;
                 mw.calculators.units[ units ] = units[ unitsId ];
             }
             }
         }
         },
    } );
        addVariables: function( variableData ) {
            var variables = mw.calculators.createCalculatorObjects( 'Variable', variableData );
 
            for( var variableId in variables ) {
                mw.calculators.variables[ variableId ] = variables[ variableId ];
 
                var cookieValue = mw.calculators.getCookieValue( variableId );


    mw.calculators.addUnits( {
                if( cookieValue ) {
        bpm: {
                    try {
             basename: 'bpm'
                        // isValueValid will throw an error if invalid, so the catch clause is our else condition
                        if( mw.calculators.variables[ variableId ].isValueValid( cookieValue ) ) {
                            mw.calculators.variables[ variableId ].setValue( cookieValue );
                        }
                    } catch( e ) {
                        // Unset the cookie value since for whatever reason it's no longer valid.
                        mw.calculators.setCookieValue( variableId, null );
                    }
                }
             }
         },
         },
         pcthct: {
         createCalculatorObjects: function( className, objectData ) {
             baseName: 'hgb'
             if( !mw.calculators.objectClasses.hasOwnProperty( className ) ) {
                throw new Error( 'Invalid class name "' + className + '"' );
            }
 
            var objects = {};
 
            for( var objectId in objectData ) {
                var propertyValues = objectData[ objectId ];
 
                // Id can either be specified using the 'id' property, or as the property name in objectData
                if( propertyValues.hasOwnProperty( 'id' ) ) {
                    objectId = propertyValues.id;
                }
                else {
                    propertyValues.id = objectId;
                }
 
                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
            }
 
            return objects;
         },
         },
         pcto2: {
         createInputGroup: function( variableIds ) {
             baseName: 'o2'
             var $form = $( '<form>', {
        },
 
        ghgbperdL: {
            } );
            baseName: 'hgb',
 
             prefixes: 'short',
            var $formRow = $( '<div>', {
             definition: '3 pcthct'
                class: 'form-row'
        }
             } ).css( 'flex-wrap', 'nowrap' );
    } );
 
             for( var iVariableId in variableIds ) {
                var variableId = variableIds[ iVariableId ];
 
                if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                    throw new Error( 'Invalid variable name "' + variableId + '"' );
                }
 
                $formRow.append( mw.calculators.variables[ variableId ].createInput() );
            }


    mw.calculators.addVariables( {
            return $form.append( $formRow );
        caseDuration: {
            name: 'Case duration',
            type: 'number',
            abbreviation: 'Duration',
            maxLength: 3,
            units: [
                'hr',
                'min'
            ]
         },
         },
         hct: {
         getCookieKey: function( variableId ) {
             name: 'Current hematocrit',
             return 'calculators-var-' + variableId;
            type: 'number',
            abbreviation: 'Current hct',
            defaultValue: '45 pcthct',
            maxLength: 4,
            units: [
                'pcthct',
                'ghgbperdL'
            ]
         },
         },
         heartRate: {
         getCookieValue: function( varId ) {
             name: 'Heart rate',
             var cookieValue = mw.cookie.get( mw.calculators.getCookieKey( varId ) );
            type: 'number',
 
             abbreviation: 'HR',
             if( !cookieValue ) {
            defaultValue: '60 bpm',
                return null;
            maxLength: 4,
             }
             units: [
 
                'bpm'
             return cookieValue;
             ]
         },
         },
         hgb: {
         getCalculation: function( calculationId ) {
             name: 'Hemoglobin',
             if( mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
            type: 'number',
                return mw.calculators.calculations[ calculationId ];
             abbreviation: 'HgB',
             } else {
            defaultValue: '13 ghgbperdL',
                 return null;
            maxLength: 4,
             }
            units: [
                 'pcthct',
                'ghgbperdL'
             ]
         },
         },
         minHct: {
         getCalculator: function( moduleId, calculatorId ) {
             name: 'Minimum hematocrit',
             if( mw.calculators.calculators.hasOwnProperty( moduleId ) &&
            type: 'number',
                mw.calculators.calculators[ moduleId ].hasOwnProperty( calculatorId ) ) {
            abbreviation: 'Min hct',
                return mw.calculators.calculators[ moduleId ][ calculatorId ];
             defaultValue: '21 pcthct',
             } else {
            maxLength: 4,
                 return null;
            units: [
             }
                'pcthct',
                 'ghgbperdL'
             ]
         },
         },
         paCO2: {
         getUnitsByBase: function( value ) {
             name: 'PaCO<sub>2</sub>',
             if( typeof value !== 'object' || !value.hasOwnProperty( 'units' ) ) {
            type: 'number',
                return null;
             abbreviation: 'PaCO<sub>2</sub>',
             }
             defaultValue: '40 mmHg',
 
             maxLength: 3,
             var unitsByBase = {};
            units: [
 
                 'mmHg'
             for( var iUnits in value.units ) {
             ]
                var units = value.units[ iUnits ];
 
                 unitsByBase[ units.unit.base.key.toLowerCase() ] = units.prefix.name + units.unit.name;
            }
 
             return unitsByBase;
         },
         },
         saO2: {
         getUnitsString: function( value ) {
             name: 'SaO<sub>2</sub>',
             if( typeof value !== 'object' ) {
             type: 'number',
                return null;
            abbreviation: 'SaO<sub>2</sub>',
            }
             defaultValue: '100 pcto2',
 
             maxLength: 3,
            var unitsString = value.formatUnits();
            units: [
 
                 'pcto2'
            var reDenominator = /\/\s?\((.*)\)/;
             ]
            var denominatorMatches = unitsString.match( reDenominator );
 
            if( denominatorMatches ) {
                var denominatorUnits = denominatorMatches[ 1 ];
 
                unitsString = unitsString.replace( reDenominator, '/' + denominatorUnits.replace( ' ', '/' ) );
             }
 
            unitsString = unitsString
                .replace( /\s/g, '' )
                .replace( /(\^(\d+))/g, '<sup>$2</sup>' );
 
            var unitsBase = value.getBase();
 
             if( unitsBase ) {
                unitsBase = unitsBase.toLowerCase();
 
                if( mw.calculators.unitsBases.hasOwnProperty( unitsBase ) &&
                    typeof mw.calculators.unitsBases[ unitsBase ].toString === 'function' ) {
                    unitsString = mw.calculators.unitsBases[ unitsBase ].toString( unitsString );
                }
             } else {
                // TODO nasty hack to fix weight units in compound units which have no base
                unitsString = unitsString.replace( 'kgwt', 'kg' );
                 unitsString = unitsString.replace( 'ug', 'mcg' );
            }
 
             return unitsString;
         },
         },
         smvO2: {
         getValueDecimals: function( value ) {
             name: 'SmvO<sub>2</sub>',
             // Supports either numeric values or math objects
             type: 'number',
            if( mw.calculators.isValueMathObject( value ) ) {
             abbreviation: 'SmvO<sub>2</sub>',
                value = mw.calculators.getValueNumber( value );
             defaultValue: '75 pcto2',
            }
             maxLength: 3,
 
             units: [
             if( typeof value !== 'number' ) {
                'pcto2'
                return null;
             ]
            }
 
             // Convert the number to a string, reverse, and count the number of characters up to the period.
             var decimals = value.toString().split('').reverse().join('').indexOf( '.' );
 
             // If no decimal is present, will be set to -1 by indexOf. If so, set to 0.
             decimals = decimals > 0 ? decimals : 0;
 
             return decimals;
         },
         },
         npoTime: {
         getValueNumber: function( value, decimals ) {
             name: 'Time spent NPO',
             if( typeof value !== 'object' ) {
             type: 'number',
                return null;
             abbreviation: 'NPO time',
            }
             defaultValue: '8 hr',
 
             maxLength: 2,
            // Remove floating point errors
             units: [
             var number = math.round( value.toNumber(), 10 );
                'hr'
 
             ]
             var absNumber = math.abs( number );
 
             if( absNumber >= 10 ) {
                decimals = 0;
             } else {
                decimals = -math.floor( math.log10( absNumber ) ) + 1;
             }
 
             return math.round( number, decimals );
         },
         },
         surgicalTrauma: {
         getValueString: function( value, decimals ) {
             name: 'Severity of surgical trauma',
             if( !mw.calculators.isValueMathObject( value ) ) {
             type: 'string',
                return null;
             abbreviation: 'Surgical trauma',
             }
             defaultValue: 'Minimal',
 
             options: [
             var valueNumber = mw.calculators.getValueNumber( value, decimals );
                 'Minimal',
             var valueUnits = mw.calculators.getUnitsString( value );
                 'Moderate',
 
                 'Severe'
             if( math.abs( math.log10( valueNumber ) ) > 3 ) {
            ]
                 var valueUnitsByBase = mw.calculators.getUnitsByBase( value );
        },
 
        temperature: {
                 var oldSIUnit;
            name: 'Temperature',
 
            type: 'number',
                 if( valueUnitsByBase.hasOwnProperty( 'mass' ) ) {
            abbreviation: 'Temp',
                    oldSIUnit = valueUnitsByBase.mass;
            defaultValue: '37 degC',
                } else if( valueUnitsByBase.hasOwnProperty( 'volume' ) ) {
            minValue: '20 degC',
                    oldSIUnit = valueUnitsByBase.volume;
            maxValue: '44 degC',
                }
            maxLength: 5,
 
            units: [
                if( oldSIUnit ) {
                'degC',
                    // This new value should simplify to the optimal SI prefix.
                'degF'
                    // We need to create a completely new unit from the formatted (i.e. simplified) value
            ]
                    var newSIValue = math.unit( math.unit( valueNumber + ' ' + oldSIUnit ).format() );
        }
    } );


                    // There is a bug in mathjs where formatUnits() won't simplify the units, only format() will.
                    var newSIUnit = newSIValue.formatUnits();


                    if( newSIUnit !== oldSIUnit ) {
                        var newValue = math.unit( newSIValue.toNumber() + ' ' + value.formatUnits().replace( oldSIUnit, newSIUnit ) );


    mw.calculators.addCalculations( {
                        valueNumber = mw.calculators.getValueNumber( newValue, decimals );
        bmi: {
                        valueUnits = mw.calculators.getUnitsString( newValue );
            name: 'Body mass index',
                     }
            abbreviation: 'BMI',
            data: {
                variables: {
                     required: [ 'weight', 'height' ]
                 }
                 }
            },
            digits: 0,
            units: 'kg/m^2',
            formula: '<math>\\mathrm{BMI} = \\frac{\\mathrm{mass_{kg}}}{{\\mathrm{height_{m}}}^2}</math>',
            link: '[[Body mass index]]',
            references: [],
            calculate: function( data ) {
                return data.weight.toNumber( 'kgwt' ) / Math.pow( data.height.toNumber( 'm' ), 2 );
             }
             }
            var valueString = String( valueNumber );
            if( valueUnits ) {
                valueString += ' ' + valueUnits;
            }
            return valueString;
         },
         },
         bsa: {
         getVariable: function( variableId ) {
             name: 'Body surface area',
             if( mw.calculators.variables.hasOwnProperty( variableId ) ) {
             abbreviation: 'BSA',
                return mw.calculators.variables[ variableId ];
             data: {
            } else {
                 variables: {
                return null;
                    required: [ 'weight', 'height' ]
            }
        },
        hasData: function( dataType, dataId ) {
             if( mw.calculators.hasOwnProperty( dataType ) &&
                mw.calculators[ dataType ].hasOwnProperty( dataId ) ) {
                return true;
             } else {
                 return false;
            }
        },
        initialize: function() {
            $( '.calculator' ).each( function() {
                var gadgetModule = 'ext.gadget.calculator-' + $( this ).attr( 'data-module' );
 
                if( gadgetModule && mw.loader.getState( gadgetModule ) === 'registered' ) {
                    mw.loader.load( gadgetModule );
                 }
                 }
             },
             } );
            digits: 2,
        },
             units: 'm^2',
        isMobile: function() {
             formula: '<math>\\mathrm{BSA} = \\sqrt{\\frac{\\mathrm{weight_{kg}}*\\mathrm{height_{cm}}}{3600}}</math>',
             return window.matchMedia( 'only screen and (max-width: 760px)' ).matches;
            link: false,
        },
             references: [
        isValueMathObject: function( value ) {
                'Mosteller RD. Simplified calculation of body-surface area. N Engl J Med. 1987 Oct 22;317(17):1098. doi: 10.1056/NEJM198710223171717. PMID: 3657876.'
             return value && value.hasOwnProperty( 'value' );
             ],
        },
            calculate: function( data ) {
        setCookieValue: function( variableId, value ) {
                 return Math.sqrt( data.height.toNumber( 'cm' ) * data.weight.toNumber( 'kgwt' ) / 3600 );
             mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                expires: COOKIE_EXPIRATION
             } );
        },
        setValue: function( variableId, value ) {
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                 return false;
            }
 
            if( mw.calculators.variables[ variableId ].setValue( value ) ) {
                mw.calculators.setCookieValue( variableId, value );
 
                return true;
             }
             }
            return false;
         },
         },
         ebv: {
         uniqueValues: function( value, index, self ) {
             name: 'Estimated blood volume',
             return self.indexOf( value ) === index;
             abbreviation: 'EBV',
        }
            data: {
    };
                 variables: {
 
                     required: [ 'weight', 'age' ]
    /**
    * Class CalculatorObject
    *
    * @param {Object} properties
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.CalculatorObject}
    * @constructor
    */
    mw.calculators.objectClasses.CalculatorObject = function( properties, propertyValues ) {
        propertyValues = propertyValues ? propertyValues : {};
 
        if( properties ) {
             if( properties.hasOwnProperty( 'required' ) ) {
                 for( var iRequiredProperty in properties.required ) {
                     var requiredProperty = properties.required[ iRequiredProperty ];
 
                    if( !propertyValues || !propertyValues.hasOwnProperty( requiredProperty ) ) {
                        console.error( 'Missing required property "' + requiredProperty + '"' );
                        console.log( propertyValues );
 
                        return null;
                    }
 
                    this[ requiredProperty ] = propertyValues[ requiredProperty ];
 
                    delete propertyValues[ requiredProperty ];
                 }
                 }
             },
             }
            digits: 0,
 
             units: 'mL',
             if( properties.hasOwnProperty( 'optional' ) ) {
            formula: '',
                 for( var iOptionalProperty in properties.optional ) {
            references: [
                    var optionalProperty = properties.optional[ iOptionalProperty ];
                'Morgan & Mikhail\'s Clinical Anesthesiology. 5e. p1168'
            ],
            calculate: function( data ) {
                 var weight = data.weight.toNumber( 'kgwt' );
                var age = data.age.toNumber( 'yo' );


                var ebvPerKg;
                    if( propertyValues && propertyValues.hasOwnProperty( optionalProperty ) ) {
                        this[ optionalProperty ] = propertyValues[ optionalProperty ];


                if( age >= 1 ) {
                        delete propertyValues[ optionalProperty ];
                     if( data.gender === 'F' ) {
                     } else if( typeof this[ optionalProperty ] === 'undefined' ) {
                         ebvPerKg = 65;
                         this[ optionalProperty ] = null;
                    } else {
                        ebvPerKg = 75;
                     }
                     }
                } else if( age >= 1/12 ) {
                    ebvPerKg = 80;
                } else if( age >= 0 ) {
                    ebvPerKg = 85;
                } else {
                    ebvPerKg = 95;
                 }
                 }
            }
            var invalidProperties = Object.keys( propertyValues );


                 return weight * ebvPerKg;
            if( invalidProperties.length ) {
                 console.warn( 'Unsupported properties defined for ' + typeof this + ' with id "' + this.id + '": ' + invalidProperties.join( ', ' ) );
             }
             }
         },
         }
         fluidMaintenanceRate: {
    };
             name: 'Fluid maintenance rate',
 
             abbreviation: 'Fluid maint.',
    mw.calculators.objectClasses.CalculatorObject.prototype.getProperties = function() {
             data: {
         return {
                variables: {
             required: [],
                    required: [ 'weight' ]
             optional: []
                }
        };
             },
    };
             digits: 0,
 
            units: 'mL/hr',
    mw.calculators.objectClasses.CalculatorObject.prototype.mergeProperties = function( inheritedProperties, properties ) {
             formula: '',
        var uniqueValues = function( value, index, self ) {
             references: [
             return self.indexOf( value ) === index;
                 'Miller\'s Anesthesia 7e, section IV, pg. 1728'
        };
 
        properties.required = inheritedProperties.required.concat( properties.required ).filter( uniqueValues );
        properties.optional = inheritedProperties.optional.concat( properties.optional ).filter( uniqueValues );
 
        return properties;
    };
 
 
 
 
    /**
    * Class UnitsBase
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.UnitsBase}
    * @constructor
    */
    mw.calculators.objectClasses.UnitsBase = function( propertyValues ) {
        var properties = {
            required: [
                'id'
             ],
             optional: [
                'toString'
             ]
        };
 
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    };
 
    mw.calculators.objectClasses.UnitsBase.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
 
 
 
    /**
    * Class Units
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.Units}
    * @constructor
    */
    mw.calculators.objectClasses.Units = function( propertyValues ) {
        var properties = {
             required: [
                 'id'
             ],
             ],
             calculate: function( data ) {
             optional: [
                 var weight = data.weight.toNumber( 'kgwt' );
                'aliases',
                'baseName',
                'definition',
                'offset',
                'prefixes'
            ]
        };
 
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    };
 
    mw.calculators.objectClasses.Units.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
 
 
 
    /**
    * Class Variable
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.Variable}
    * @constructor
    */
    mw.calculators.objectClasses.Variable = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        if( VALID_TYPES.indexOf( this.type ) === -1 ) {
            throw new Error( 'Invalid type "' + this.type + '" for variable "' + this.id + '"' );
        }
 
        // Accept options as either an array of strings, or an object with ids as keys and display text as values
        if( Array.isArray( this.options ) ) {
            var options = {};
 
            for( var iOption in this.options ) {
                 var option = this.options[ iOption ];
 
                options[ option ] = option;
            }
 
            this.options = options;
        }
 
        this.calculations = [];
 
        if( this.defaultValue ) {
            this.defaultValue = this.prepareValue( this.defaultValue );
        }
 
        this.value = null;
    };
 
    mw.calculators.objectClasses.Variable.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.Variable.prototype.addCalculation = function( calculationId ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            return;
        }
 
        this.calculations.push( calculationId );
    };
 
    mw.calculators.objectClasses.Variable.prototype.createInput = function( inputOptions ) {
        if( !inputOptions ) {
            inputOptions = {};
        }
 
        inputOptions.class = inputOptions.hasOwnProperty( 'class' ) ? inputOptions.class : '';
        inputOptions.hideLabel = inputOptions.hasOwnProperty( 'hideLabel' ) ? inputOptions.hideLabel : false;
        inputOptions.hideLabelMobile = inputOptions.hasOwnProperty( 'hideLabelMobile' ) ? inputOptions.hideLabelMobile : false;
        inputOptions.inline = inputOptions.hasOwnProperty( 'inline' ) ? inputOptions.inline : false;
        inputOptions.inputClass = inputOptions.hasOwnProperty( 'inputClass' ) ? inputOptions.inputClass : '';
 
        var variableId = this.id;
        var inputId = 'calculator-input-' + variableId;
 
        var inputContainerTag = inputOptions.inline ? '<span>' : '<div>';
 
        var inputContainerAttributes = {
            class: 'form-group mb-0 calculator-container-input'
        };
 
        inputContainerAttributes.class += inputOptions.class ? ' ' + inputOptions.class : '';
        inputContainerAttributes.class += ' calculator-container-input-' + variableId;


                // Uses 4-2-1 rule
        var inputContainerCss = {};
                var maintenanceRate = 4 * Math.min( weight, 10 );


                if( weight > 10 ) {
        // Initialize label attributes
                    maintenanceRate += 2 * Math.min( weight - 10, 10 );
        var labelAttributes = {
            for: inputId,
            html: this.getLabelString()
        };
 
        if( inputOptions.hideLabel || ( inputOptions.hideLabelMobile && mw.calculators.isMobile() ) ) {
            labelAttributes.class = 'sr-only';
        }
 
        var labelCss = {};
 
        if( inputOptions.inline ) {
            inputContainerTag = '<span>';
 
            inputContainerCss[ 'align-items' ] = 'center';
            inputContainerCss[ 'display' ] = 'flex';
            inputContainerCss[ 'height' ] = 'calc(1.5em + 0.75rem + 2px)';
 
            labelAttributes.html += ':&nbsp;';
            labelCss[ 'margin-bottom' ] = 0;
        }
 
        // Create the input container
        var $inputContainer = $( inputContainerTag, inputContainerAttributes ).css( inputContainerCss );
 
        var $label = $( '<label>', labelAttributes ).css( labelCss );
 
        $inputContainer.append( $label );
 
        var value = this.getValue();
 
        if( this.type === TYPE_NUMBER ) {
            // Initialize the primary units variables (needed for handlers, even if doesn't have units)
            var unitsId = null;
            var $unitsContainer = null;
 
            var inputValue = '';
 
            if( mw.calculators.isValueMathObject( value ) ) {
                var number = value.toNumber();
 
                if( number ) {
                    inputValue = number;
                 }
                 }
            } else {
                inputValue = value;
            }


                 if( weight > 20) {
            // Initialize input options
                    maintenanceRate += weight - 20;
            var inputAttributes = {
                 }
                 id: inputId,
                class: 'form-control calculator-input calculator-input-text',
                type: 'text',
                autocomplete: 'off',
                inputmode: 'decimal',
                 value: inputValue
            };


                 return maintenanceRate;
            // Configure additional options
            if( this.maxLength ) {
                 inputAttributes.maxlength = this.maxLength;
             }
             }
        },
        intraopFluids: {
            name: 'Intraoperative fluid dosing',
            abbreviation: 'Intraop fluids',
            data: {
                calculations: {
                    required: [ 'fluidMaintenanceRate' ]
                },
                variables: {
                    required: [ 'weight', 'npoTime', 'surgicalTrauma' ]
                }
            },
            type: 'string',
            references: [
                'Corcoran T, Rhodes JE, Clarke S, Myles PS, Ho KM. Perioperative fluid management strategies in major surgery: a stratified meta-analysis. Anesth Analg. 2012 Mar;114(3):640-51. doi: 10.1213/ANE.0b013e318240d6eb. Epub 2012 Jan 16. PMID: 22253274.'
            ],
            calculate: function( data ) {
                var weight = data.weight.toNumber( 'kgwt' );
                var maintenanceRate = data.fluidMaintenanceRate.toNumber( 'mL/hr' );
                var npoTime = data.npoTime.toNumber( 'hr' );
                var surgicalTrauma = data.surgicalTrauma;


                 var output = '';
            // Add any additional classes to the input
            inputAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';
 
            // Add the input id to the list of classes
            inputAttributes.class += ' ' + inputId;
 
            // If the variable has units, create the units input
            if( this.hasUnits() ) {
                 // Set the units id
                unitsId = inputId + '-units';
 
                var unitsValue = mw.calculators.isValueMathObject( value ) ? value.formatUnits() : null;
 
                var unitsInputAttributes = {
                    id: unitsId
                };


                 var npoDeficit = npoTime * maintenanceRate;
                 // Create the units container
                $unitsContainer = $( '<div>', {
                    class: 'input-group-append'
                } ).css( 'align-items', 'center' );


                 var surgicalLossMin, surgicalLossMax;
                 if( this.units.length === 1 ) {
                    unitsInputAttributes.type = 'hidden';
                    unitsInputAttributes.value = this.units[ 0 ];


                if( surgicalTrauma === 'Minimal' ) {
                    $unitsContainer
                    surgicalLossMin = 2 * weight;
                        .css( 'padding', '0 0.5em' )
                    surgicalLossMax = 4 * weight;
                        .append( mw.calculators.getUnitsString( math.unit( '0 ' + this.units[ 0 ] ) ) )
                } else if( surgicalTrauma === 'Moderate' ) {
                        .append( $( '<input>', unitsInputAttributes ) );
                    surgicalLossMin = 4 * weight;
                    surgicalLossMax = 6 * weight;
                 } else {
                 } else {
                     surgicalLossMin = 6 * weight;
                     // Initialize the units input options
                     surgicalLossMax = 8 * weight;
                    unitsInputAttributes.class = 'custom-select calculator-input-select';
 
                    // Add any additional classes to the input
                    unitsInputAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';
 
                    unitsInputAttributes.class = unitsInputAttributes.class + ' ' + unitsId;
 
                    var $unitsInput = $( '<select>', unitsInputAttributes )
                        .on( 'change', function() {
                            var numberValue = $( '#' + inputId ).val();
 
                            var newValue = numberValue ? numberValue + ' ' + $( this ).val() : null;
 
                            mw.calculators.setValue( variableId, newValue );
                        } );
 
                     for( var iUnits in this.units ) {
                        var units = this.units[ iUnits ];
 
                        var unitsOptionAttributes = {
                            html: mw.calculators.getUnitsString( math.unit( '0 ' + units ) ),
                            value: units
                        };
 
                        if( units === unitsValue ) {
                            unitsOptionAttributes.selected = true;
                        }
 
                        $unitsInput.append( $( '<option>', unitsOptionAttributes ) );
                    }
 
                    $unitsContainer.append( $unitsInput );
                 }
                 }
            }
            // Create the input and add handlers
            var $input = $( '<input>', inputAttributes )
                .on( 'input', function() {
                    var numberValue = $( this ).val();
                    var newValue = numberValue ? numberValue : null;
                    if( newValue && unitsId ) {
                        newValue = newValue + ' ' + $( '#' + unitsId ).val();
                    }


                var firstHour = Math.round( npoDeficit / 2 ) + maintenanceRate;
                    mw.calculators.setValue( variableId, newValue );
                var nextHoursMin = Math.round( npoDeficit / 4 ) + maintenanceRate + surgicalLossMin;
                 } );
                 var nextHoursMax = Math.round( npoDeficit / 4 ) + maintenanceRate + surgicalLossMax;
                var remainingHoursMin = maintenanceRate + surgicalLossMin;
                var remainingHoursMax = maintenanceRate + surgicalLossMax;


                output += 'NPO deficit: ' + Math.round( npoDeficit ) + ' mL<br/>';
            // Create the input group
                output += 'Surgical losses: ' + surgicalLossMin + '-' + surgicalLossMax + ' mL/hr<br/>';
            var $inputGroup = $( '<div>', {
                output += '1st hour: ' + firstHour + ' mL<br/>';
                 class: 'input-group'
                 output += '2nd hour: ' + nextHoursMin + '-' + nextHoursMax + ' mL<br/>';
            } ).append( $input );
                output += '3rd hour: ' + nextHoursMin + '-' + nextHoursMax + ' mL<br/>';
                output += '4+ hours: ' + remainingHoursMin + '-' + remainingHoursMax + ' mL<br/>';


                 return output;
            if( $unitsContainer ) {
                 $inputGroup.append( $unitsContainer );
             }
             }
         },
 
        maxAbl: {
            $inputContainer.append( $inputGroup );
             name: 'Maximum allowable blood loss',
         } else if( this.type === TYPE_STRING ) {
            abbreviation: 'Max ABL',
             if( this.hasOptions() ) {
            data: {
                var optionKeys = Object.keys( this.options );
                 calculations: {
 
                     required: [ 'ebv' ]
                 if( optionKeys.length === 1 ) {
                 },
                     $inputContainer.append( this.options[ optionKeys[ 0 ] ] );
                variables: {
                 } else {
                     required: [ 'weight', 'age', 'hct', 'minHct' ]
                    var selectAttributes = {
                        id: inputId,
                        class: 'custom-select calculator-input calculator-input-select'
                    };
 
                    // Add any additional classes to the input
                     selectAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';
 
                    var $select = $( '<select>', selectAttributes )
                        .on( 'change', function() {
                            mw.calculators.setValue( variableId, $( this ).val() );
                        } );
 
                    for( var optionId in this.options ) {
                        var displayText = this.options[ optionId ];
 
                        var optionAttributes = {
                            value: optionId,
                            text: displayText
                        };
 
                        if( optionId === value ) {
                            optionAttributes.selected = true;
                        }
 
                        $select.append( $( '<option>', optionAttributes ) );
                    }
 
                    $inputContainer.append( $select );
                 }
                 }
             },
             }
            digits: 0,
        }
             units: 'mL',
 
            formula: '',
        return $inputContainer;
            references: [
    };
                 'Morgan & Mikhail\'s Clinical Anesthesiology. 5e. p1168'
 
    mw.calculators.objectClasses.Variable.prototype.getLabelString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };
 
    mw.calculators.objectClasses.Variable.prototype.getProperties = function() {
        return {
             required: [
                'id',
                'name',
                 'type'
             ],
             ],
             calculate: function( data ) {
             optional: [
                var currentHct = data.hct.toNumber( 'pcthct' );
                'abbreviation',
                var minHct = data.minHct.toNumber( 'pcthct' );
                'defaultValue',
                'maxLength',
                'maxValue',
                'minValue',
                'options',
                'units'
            ]
        };
    };
 
    mw.calculators.objectClasses.Variable.prototype.getValue = function() {
        if( this.value !== null ) {
            return this.value;
        } else if( this.defaultValue !== null ) {
            return this.defaultValue;
        } else {
            return null;
        }
    };


                if( currentHct < minHct ) {
    mw.calculators.objectClasses.Variable.prototype.getValueString = function() {
                    return '-';
        return String( this.getValue() );
                }
    };
 
    mw.calculators.objectClasses.Variable.prototype.hasOptions = function() {
        return this.options !== null;
    };
 
    mw.calculators.objectClasses.Variable.prototype.hasUnits = function() {
        return this.units !== null;
    };
 
    mw.calculators.objectClasses.Variable.prototype.hasValue = function() {
        var value = this.getValue();
 
        if( value === null ||
            ( mw.calculators.isValueMathObject( value ) && !value.toNumber() ) ) {
            return false;
        }
 
        return true;
    };
 
    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
        return mw.calculators.isValueMathObject( this.value );
    };
 
    mw.calculators.objectClasses.Variable.prototype.isValueValid = function( value ) {
        if( value === null ) {
            return true;
        }


                return data.ebv.toNumber( 'mL' ) * ( currentHct - minHct ) / currentHct;
        if( this.type === TYPE_NUMBER ) {
            if( typeof value !== 'object' ) {
                value = math.unit( value );
             }
             }
        },
 
        minUop: {
            if( this.hasUnits() ) {
            name: 'Minimum urine output',
                var valueUnits = value.formatUnits();
            abbreviation: 'Min UOP',
 
            data: {
                if( !valueUnits ) {
                 variables: {
                    throw new Error( 'Could not set value for "' + this.id + '": Value must define units' );
                     required: [ 'weight', 'age' ],
                 } else if( this.units.indexOf( valueUnits ) === -1 ) {
                    optional: [ 'caseDuration' ]
                     throw new Error( 'Could not set value for "' + this.id + '": Units "' + valueUnits + '" are not valid for this variable' );
                 }
                 }
             },
             }
            type: 'string',
        } else if( this.hasOptions() ) {
            formula: '',
            if( !this.options.hasOwnProperty( value ) ) {
            references: [
                 throw new Error( 'Could not set value "' + value + '" for "' + this.id + '": Value must define be one of: ' + Object.keys( this.options ).join( ', ' ) );
                'Klahr S, Miller SB. Acute oliguria. N Engl J Med. 1998 Mar 5;338(10):671-5. doi: 10.1056/NEJM199803053381007. PMID: 9486997.',
            }
                'Arant BS Jr. Postnatal development of renal function during the first year of life. Pediatr Nephrol. 1987 Jul;1(3):308-13. doi: 10.1007/BF00849229. PMID: 3153294.'
        }
            ],
 
            calculate: function( data ) {
        return true;
                 var weight = data.weight.toNumber( 'kgwt' );
    };
                var age = data.age.toNumber( 'yo' );
                var caseDuration = data.caseDuration ? data.caseDuration.toNumber( 'hr' ) : null;


                var minUop;
    mw.calculators.objectClasses.Variable.prototype.prepareValue = function( value ) {
        if( !this.isValueValid( value ) ) {
            // isValueValid will throw a meaningful error to the console
            return null;
        }


                if( age > 1 ) {
        if( value !== null ) {
                    minUop = 0.5 * weight;
            if( this.type === TYPE_NUMBER ) {
                 } else {
                 if( typeof value !== 'object' ) {
                     minUop = 1 * weight;
                     value = math.unit( value );
                 }
                 }
            }
        }
        return value;
    };
    mw.calculators.objectClasses.Variable.prototype.setValue = function( value ) {
        this.value = this.prepareValue( value );
        this.valueUpdated();
        return true;
    };


                if( caseDuration ) {
    mw.calculators.objectClasses.Variable.prototype.valueUpdated = function() {
                    minUop = minUop * caseDuration + ' mL';
        for( var iCalculation in this.calculations ) {
                } else {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
                    minUop = minUop + ' mL/hr';
                }


                 return minUop;
            if( calculation ) {
                 calculation.render();
             }
             }
         },
         }
         systolicBloodPressure: {
    }
             name: 'Systolic blood pressure',
 
            abbreviation: 'SBP',
 
            data: {
 
                variables: {
    /**
                    required: [ 'age' ]
    * Class AbstractCalculation
                }
    * @param {Object} propertyValues
            },
    * @returns {mw.calculators.objectClasses.AbstractCalculation}
             type: 'string',
    * @constructor
            references: [
    */
                 'Baby Miller 6e, ch. 16, pg. 550'
    mw.calculators.objectClasses.AbstractCalculation = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        this.initialize();
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.addCalculation = function( calculationId ) {
         if( this.calculations.indexOf( calculationId ) !== -1 ) {
             return;
        }
 
        this.calculations.push( calculationId );
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.doRender = function() {};
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerClass = function() {
        return 'calculator-calculation-' + this.id;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getLabelString = function() {
        return this.id;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties = function() {
        return {
             required: [
                'id',
                 'calculate'
             ],
             ],
             calculate: function( data ) {
             optional: [
                var age = data.age.toNumber( 'yo' );
                'data',
                'description',
                'onRender',
                'onRendered',
                'references',
                'type'
            ]
        };
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getValue = function() {
        // For now, we always need to recalculate, since the calculation may not be rendered but still required by
        // other calculations (i.e. drug dosages using lean body weight).
        this.recalculate();
 
        return this.value;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.hasInfo = function() {
        return false;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.hasValue = function() {
        if( this.value === null ||
            ( this.isValueMathObject() && !this.value.toNumber() ) ) {
            return false;
        }
 
        return true;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationData = function() {
        return this.data;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues = function() {
        var calculationData = this.getCalculationData();
 
        var data = {};
        var missingRequiredData = '';
        var calculationId, calculation, variableId, variable;


                var systolicMin, systolicMax, diastolicMin, diastolicMax, meanMin, meanMax;
        for( var iRequiredCalculation in calculationData.calculations.required ) {
            calculationId = calculationData.calculations.required[ iRequiredCalculation ];
            calculation = mw.calculators.getCalculation( calculationId );


                if( age >= 16 ) {
            if( !calculation ) {
                    systolicMin = 100;
                 throw new Error( 'Invalid required calculation "' + calculationId + '" for calculation "' + this.id + '"' );
                    systolicMax = 125;
            } else if( !calculation.hasValue() ) {
                 } else if( age >= 13 ) {
                 if( missingRequiredData ) {
                    systolicMin = 95;
                     missingRequiredData = missingRequiredData + ', ';
                    systolicMax = 120;
                } else if( age >= 9 ) {
                    systolicMin = 90;
                    systolicMax = 115;
                } else if( age >= 6 ) {
                    systolicMin = 85;
                    systolicMax = 105;
                } else if( age >= 3 ) {
                    systolicMin = 80;
                    systolicMax = 100;
                } else if( age >= 1 ) {
                    systolicMin = 75;
                    systolicMax = 95;
                } else if( age >= 6 / 12 ) {
                    systolicMin = 70;
                    systolicMax = 90;
                 } else if( age >= 1 / 12 ) {
                     systolicMin = 65;
                    systolicMax = 85;
                } else {
                    systolicMin = 60;
                    systolicMax = 75;
                 }
                 }
                missingRequiredData = missingRequiredData + calculation.getLabelString();
            } else {
                data[ calculationId ] = calculation.value;
             }
             }
         }
         }
    } );


    // Cardiovascular
        for( var iRequiredVariable in calculationData.variables.required ) {
    mw.calculators.addCalculations( {
            variableId = calculationData.variables.required[ iRequiredVariable ];
        vO2: {
            variable = mw.calculators.getVariable( variableId );
            name: 'VO<sub>2</sub>',
 
            abbreviation: 'VO<sub>2</sub>',
            if( !variable ) {
             data: {
                throw new Error( 'Invalid required variable "' + variableId + '" for calculation "' + this.id + '"' );
                 calculations: {
             } else if( !variable.hasValue() ) {
                     required: [ 'bsa' ]
                 if( missingRequiredData ) {
                },
                     missingRequiredData = missingRequiredData + ', ';
                variables: {
                    optional: [ 'age' ]
                 }
                 }
            },
            units: 'mL/min',
            references: [],
            calculate: function( data ) {
                var bsa = data.bsa.toNumber();
                var age = data.age ? data.age.toNumber( 'yr' ) : null;


                 if( age >= 70 ) {
                 missingRequiredData = missingRequiredData + variable.getLabelString();
                    return 110 * bsa;
            } else {
                 } else {
                data[ variableId ] = variable.getValue();
                     return 125 * bsa;
            }
        }
 
        if( missingRequiredData ) {
            this.message = missingRequiredData + ' required';
 
            return false;
        }
 
        for( var iOptionalCalculation in calculationData.calculations.optional ) {
            calculationId = calculationData.calculations.optional[ iOptionalCalculation ];
            calculation = mw.calculators.getCalculation( calculationId );
 
            if( !calculation ) {
                throw new Error( 'Invalid optional calculation "' + calculationId + '" for calculation "' + this.id + '"' );
            }
 
            data[ calculationId ] = calculation.hasValue() ? calculation.value : null;
        }
 
        for( var iOptionalVariable in calculationData.variables.optional ) {
            variableId = calculationData.variables.optional[ iOptionalVariable ];
            variable = mw.calculators.getVariable( variableId );
 
            if( !variable ) {
                throw new Error( 'Invalid optional variable "' + variableId + '" for calculation "' + this.id + '"' );
            }
 
            data[ variableId ] = variable.hasValue() ? variable.getValue() : null;
        }
 
        return data;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
        if( typeof this.calculate !== 'function' ) {
            throw new Error( 'calculate() must be a function for Calculation "' + this.id + '"' );
        }
 
        // Initialize array to store calculation ids which depend on this calculation's value
        this.calculations = [];
 
        this.data = new mw.calculators.objectClasses.CalculationData( this.getCalculationData() );
 
        this.type = this.type ? this.type : TYPE_NUMBER;
 
        this.message = null;
        this.value = null;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
        return mw.calculators.isValueMathObject( this.value );
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.recalculate = function() {
        this.message = '';
        this.value = null;
 
        var data = this.getCalculationDataValues();
 
        if( data === false ) {
            this.valueUpdated();
 
            return false;
        }
 
        try {
            var value = this.calculate( data );
 
            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                 if( this.units ) {
                     value = value + ' ' + this.units;
                 }
                 }
                this.value = math.unit( value );
            } else {
                this.value = value;
             }
             }
         },
         } catch( e ) {
        cardiacOutputFick: {
             console.warn( e.message );
             name: 'Cardiac output (Fick)',
 
             abbreviation: 'CO (Fick)',
            this.message = e.message;
            data: {
            this.value = null;
                variables: {
        } finally {
                    required: [ 'saO2', 'smvO2', 'hgb' ]
             this.valueUpdated();
                },
        }
                calculations: {
 
                    required: [ 'vO2' ]
        return true;
                }
    };
            },
 
            units: 'L/min',
 
             formula: '<math>\\mathrm{CO_{Fick}}=\\frac{VO_2}{(S_aO_2 - S_{mv}O_2) * H_b * 13.4}</math>',
 
            link: false,
    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
            references: [],
        this.recalculate();
            calculate: function( data ) {
 
                var vO2 = data.vO2.toNumber( 'mL/min' );
        if( typeof this.onRender === 'function' ) {
                var saO2 = data.saO2.toNumber() / 100;
            this.onRender();
                var smvO2 = data.smvO2.toNumber() / 100;
        }
                var hgb = data.hgb.toNumber( 'ghgbperdL' );
 
        this.doRender();
 
        if( typeof this.onRendered === 'function' ) {
             this.onRendered();
        }
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.setDependencies = function() {
        this.data = this.getCalculationData();
 
        var calculationIds = this.data.calculations.required.concat( this.data.calculations.optional );
 
        for( var iCalculationId in calculationIds ) {
            var calculationId = calculationIds[ iCalculationId ];


                return vO2 / ( ( saO2 - smvO2 ) * hgb * 13.4 );
            if( !mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
                throw new Error('Calculation "' + calculationId + '" does not exist for calculation "' + this.id + '"');
             }
             }
        },
        cardiacIndex: {
            name: 'Cardiac index',
            abbreviation: 'CI',
            data: {
                calculations: {
                    required: [ 'bsa', 'cardiacOutputFick' ]
                }
            },
            units: 'L/min/m^2',
            formula: '<math>\\mathrm{CI}=\\frac{\\mathrm{CO}}{\\mathrm{BSA}}</math>',
            link: false,
            references: [],
            calculate: function( data ) {
                var cardiacOutput = data.cardiacOutputFick.toNumber( 'L/min' );
                var bsa = data.bsa.toNumber( 'm^2' );


                 return cardiacOutput / bsa;
            mw.calculators.calculations[ calculationId ].addCalculation( this.id );
        }
 
        var variableIds = this.data.variables.required.concat( this.data.variables.optional );
 
        for( var iVariableId in variableIds ) {
            var variableId = variableIds[ iVariableId ];
 
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                 throw new Error('Variable "' + variableId + '" does not exist for calculation "' + this.id + '"');
             }
             }
        },
        strokeVolume: {
            name: 'Stroke volume',
            abbreviation: 'SV',
            data: {
                variables: {
                    required: [ 'heartRate' ]
                },
                calculations: {
                    required: [ 'cardiacOutputFick' ]
                }
            },
            units: 'mL',
            formula: '<math>\\mathrm{SV}=\\frac{\\mathrm{CO}}{\\mathrm{HR}}</math>',
            link: false,
            references: [],
            calculate: function( data ) {
                var cardiacOutput = data.cardiacOutputFick.toNumber( 'mL/min' );
                var heartRate = data.heartRate.toNumber();


                 return cardiacOutput / heartRate;
            mw.calculators.variables[ variableId ].addCalculation( this.id );
        }
 
        this.recalculate();
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.valueUpdated = function() {
        for( var iCalculation in this.calculations ) {
            calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
 
            if( calculation ) {
                 calculation.render();
             }
             }
         }
         }
     } );
     };


    // Neuro
    mw.calculators.addCalculations( {
        brainMass: {
            name: 'Brain mass',
            data: {
                variables: {
                    optional: [ 'age', 'gender' ]
                }
            },
            digits: 0,
            units: 'gwt',
            references: [
                'Dekaban AS. Changes in brain weights during the span of human life: relation of brain weights to body heights and body weights. Ann Neurol. 1978 Oct;4(4):345-56. doi: 10.1002/ana.410040410. PMID: 727739.'
            ],
            calculate: function( data ) {
                var age = data.age ? data.age.toNumber( 'yr' ) : null;
                var gender = data.gender ? data.gender : null;


                var brainMassFemale = 1290;
                var brainMassMale = 1450;


                if( age !== null ) {
    /**
                    if( age <= 10 / 365 ) {
    * Class CalculationData
                        // <=10 days
    * @param {Object} propertyValues
                        brainMassFemale = 360;
    * @returns {mw.calculators.objectClasses.CalculationData}
                        brainMassMale = 380;
    * @constructor
                    } else if( age <= 4 * 30 / 365 ) {
    */
                        // Less than 4 months. This is a gap in the reported data of the paper, so linearly interpolate?
    mw.calculators.objectClasses.CalculationData = function( propertyValues ) {
                        var ageFactor = 1 - ( 4 * 30 / 365 - age ) / ( 4 * 30 / 365 - 10 / 365 );
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        var dataTypes = this.getDataTypes();
        var dataRequirements = this.getDataRequirements();
 
        // Iterate through the supported data types (e.g. calculation, variable) to initialize the structure
        for( var iDataType in dataTypes ) {
            var dataType = dataTypes[ iDataType ];
 
            if( !this[ dataType ] ) {
                this[ dataType ] = {
                    optional: [],
                    required: []
                };
            } else {
                // Iterate through the requirement levels (i.e. optional, required) to initialize the structure
                for( var iDataRequirement in dataRequirements ) {
                    var dataRequirement = dataRequirements[ iDataRequirement ];


                        brainMassFemale = 360 + ageFactor * ( 580 - 360 );
                     if( this[ dataType ].hasOwnProperty( dataRequirement ) ) {
                        brainMassMale = 380 + ageFactor * ( 640 - 380 );
                         for( var iDataId in this[ dataType ][ dataRequirement ] ) {
                     } else if( age <= 8 * 30 / 365 ) {
                            var dataId = this[ dataType ][ dataRequirement ][ iDataId ];
                        // <=8 months
                         }
                        brainMassFemale = 580;
                        brainMassMale = 640;
                    } else if( age <= 18 * 30 / 365 ) {
                        // <=18 months
                        brainMassFemale = 940;
                        brainMassMale = 970;
                    } else if( age <= 30 * 30 / 365 ) {
                         // <=30 months
                        brainMassFemale = 1040;
                        brainMassMale = 1120;
                    } else if( age <= 43 * 30 / 365 ) {
                        // <=43 months
                        brainMassFemale = 1090;
                         brainMassMale = 1270;
                    } else if( age <= 5 ) {
                        brainMassFemale = 1150;
                        brainMassMale = 1300;
                    } else if( age <= 7 ) {
                        brainMassFemale = 1210;
                        brainMassMale = 1330;
                    } else if( age <= 9 ) {
                        brainMassFemale = 1180;
                        brainMassMale = 1370;
                    } else if( age <= 12 ) {
                        brainMassFemale = 1260;
                        brainMassMale = 1440;
                    } else if( age <= 15 ) {
                        brainMassFemale = 1280;
                        brainMassMale = 1410;
                    } else if( age <= 18 ) {
                        brainMassFemale = 1340;
                        brainMassMale = 1440;
                    } else if( age <= 21 ) {
                        brainMassFemale = 1310;
                        brainMassMale = 1450;
                    } else if( age <= 30 ) {
                        brainMassFemale = 1300;
                        brainMassMale = 1440;
                    } else if( age <= 40 ) {
                        brainMassFemale = 1290;
                        brainMassMale = 1440;
                    } else if( age <= 50 ) {
                        brainMassFemale = 1290;
                        brainMassMale = 1430;
                    } else if( age <= 55 ) {
                        brainMassFemale = 1280;
                        brainMassMale = 1410;
                    } else if( age <= 60 ) {
                        brainMassFemale = 1250;
                        brainMassMale = 1370;
                    } else if( age <= 65 ) {
                        brainMassFemale = 1240;
                        brainMassMale = 1370;
                    } else if( age <= 70 ) {
                        brainMassFemale = 1240;
                        brainMassMale = 1360;
                    } else if( age <= 75 ) {
                        brainMassFemale = 1230;
                        brainMassMale = 1350;
                    } else if( age <= 80 ) {
                        brainMassFemale = 1190;
                        brainMassMale = 1330;
                    } else if( age <= 85 ) {
                        brainMassFemale = 1170;
                        brainMassMale = 1310;
                     } else {
                     } else {
                         brainMassFemale = 1140;
                         this[ dataType ][ dataRequirement ] = [];
                        brainMassMale = 1290;
                     }
                     }
                 }
                 }
            }
        }
    };


                 if( gender === 'F' ) {
    mw.calculators.objectClasses.CalculationData.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                    return brainMassFemale;
 
                 } else if( gender === 'M' ) {
    mw.calculators.objectClasses.CalculationData.prototype.getDataRequirements = function() {
                    return brainMassMale;
        return [
                 } else {
            'optional',
                     return ( brainMassFemale + brainMassMale ) / 2;
            'required'
        ];
    };
 
    mw.calculators.objectClasses.CalculationData.prototype.getDataTypes = function() {
        return [
            'calculations',
            'variables'
        ];
    };
 
    mw.calculators.objectClasses.CalculationData.prototype.getProperties = function() {
        return {
            required: [],
            optional: [
                'calculations',
                'variables'
            ]
        };
    };
 
 
 
    mw.calculators.objectClasses.CalculationData.prototype.merge = function() {
        var mergedData = new mw.calculators.objectClasses.CalculationData();
 
        var data = [ this ].concat( Array.prototype.slice.call( arguments ) );
 
        var dataTypes = this.getDataTypes();
 
        for( var iData in data ) {
            for( var iDataType in dataTypes ) {
                 var dataType = dataTypes[ iDataType ];
 
                mergedData[ dataType ].required = mergedData[ dataType ].required
                    .concat( data[ iData ][ dataType ].required )
                    .filter( mw.calculators.uniqueValues );
 
                mergedData[ dataType ].optional = mergedData[ dataType ].optional
                    .concat( data[ iData ][ dataType ].optional )
                    .filter( mw.calculators.uniqueValues );
            }
        }
 
        return mergedData;
    };
 
 
 
 
 
    /**
    * Class SimpleCalculation
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.SimpleCalculation}
    * @constructor
    */
    mw.calculators.objectClasses.SimpleCalculation = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        this.initialize();
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
 
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.hasInfo = function() {
        return this.description || this.formula || this.references.length;
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelHtml = function() {
        var labelHtml = this.getLabelString();
 
        if( this.link ) {
            var href = this.link;
 
            // Detect internal links (this isn't great)
            var matches = href.match( /\[\[(.*?)\]\]/ );
 
            if( matches ) {
                href = mw.util.getUrl( matches[ 1 ] );
            }
 
            labelHtml = $( '<a>', {
                href: href,
                text: labelHtml
            } )[ 0 ].outerHTML;
        }
 
        return labelHtml;
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.getProperties = function() {
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
 
        return this.mergeProperties( inheritedProperties, {
            required: [
                'name'
            ],
            optional: [
                'abbreviation',
                'digits',
                'formula',
                 'link',
                'units'
            ]
        } );
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
        if( this.message ) {
            return this.message;
        } else if( typeof this.value === 'object' && this.value.hasOwnProperty( 'value' ) ) {
            return mw.calculators.getValueString( this.value );
        } else {
            return String( this.value );
        }
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.doRender = function() {
        var $calculationContainer = $( '.' + this.getContainerClass() );
 
        if( !$calculationContainer.length ) {
            return;
        }
 
        var valueString = this.getValueString();
 
        var inputVariableIds = this.data.variables.required.concat( this.data.variables.optional );
        var missingVariableInputs = [];
 
        for( var iInputVariableId in inputVariableIds ) {
            var variableId = inputVariableIds[ iInputVariableId ];
 
            if( !$( '#calculator-input-' + variableId ).length ) {
                missingVariableInputs.push( variableId );
            }
        }
 
        var calculation = this;
 
        $calculationContainer.each( function() {
            $( this ).empty();
 
            var isTable = this.tagName.toLowerCase() === 'tr';
 
            var $infoButton = null;
 
            if( calculation.hasInfo() ) {
                $infoButton = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + calculation.getContainerClass() + '-info',
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': calculation.getContainerClass() + '-info'
                 } )
                    .append( $( '<i>', {
                        class: 'far fa-question-circle'
                     } ) );
            }
 
            var labelHtml = calculation.getLabelHtml();
 
            if( isTable ) {
                if( calculation.hasInfo() ) {
                    labelHtml += $( '<span>', {
                        class: 'calculator-SimpleCalculator-info'
                    } ).append( $infoButton )[ 0 ].outerHTML;
                 }
                 }
                $( this )
                    .append( $( '<th>', {
                        class: 'calculator-SimpleCalculator-calculation-cell',
                        html: labelHtml
                    } ) )
                    .append( $( '<td>', {
                        class: 'calculator-SimpleCalculator-value-cell',
                        html: valueString
                    } ) );
            } else {
                $( this )
                    .append( labelHtml + $infoButton[ 0 ].outerHTML + ': ' + valueString );
             }
             }
        },
 
        cerebralBloodVolume: {
            if( calculation.hasInfo() ) {
            name: 'Cerebral blood volume',
                var infoHtml = '';
            abbreviation: 'CBV',
 
            data: {
                if( calculation.description ) {
                 calculations: {
                    infoHtml += $( '<p>', {
                     required: [ 'brainMass' ]
                        html: calculation.description
                    } )[ 0 ].outerHTML;
                }
 
                 if( calculation.formula ) {
                     infoHtml += $( '<span>', {
                        class: calculation.getContainerClass() + '-formula'
                    } )[ 0 ].outerHTML;
 
                    var api = new mw.Api();
 
                    api.parse( calculation.formula ).then( function( result ) {
                        $( '.' + calculation.getContainerClass() + '-formula' ).html( result );
                    } );
                 }
                 }
            },
            units: 'mL',
            description: '4 mL per 100g of brain mass',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                var brainMass = data.brainMass.toNumber( 'gwt' );


                 return 4 * brainMass / 100;
                 if( calculation.references.length ) {
            }
                    var $references = $( '<ol>' );
        },
 
        cerebralMetabolicRateFactor: {
                    for( var iReference in calculation.references ) {
            name: 'Cerebral metabolic rate factor',
                        $references.append( $( '<li>', {
            abbreviation: '%CMR',
                            text: calculation.references[ iReference ]
            data: {
                        } ) );
                variables: {
                    }
                     optional: [ 'temperature' ]
 
                     infoHtml += $references[ 0 ].outerHTML;
                 }
                 }
            },
            description: '7% change in CMR for every 1 &deg;C change in temperature',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                var temperature = data.temperature ? data.temperature.toNumber( 'degC' ) : null;


                 var cerebralMetabolicRateFactor = 1;
                 var infoContainerId = calculation.getContainerClass() + '-info';
                var $infoContainer = $( '#' + infoContainerId );


                 if( temperature ) {
                 if( $infoContainer.length ) {
                     cerebralMetabolicRateFactor += 0.07 * ( temperature - 37 );
                     $infoContainer.empty();
                 }
                 }


                 return cerebralMetabolicRateFactor;
                 if( isTable ) {
            }
                    $infoContainer = $( '<tr>', {
        },
                        id: infoContainerId,
        cerebralMetabolicRateO2: {
                        class: 'collapse'
            name: 'Cerebral metabolic rate (O<sub>2</sub>)',
                    } )
            abbreviation: 'CMRO<sub>2</sub>',
                        .append( $( '<td>', {
            data: {
                            colspan: 2
                 calculations: {
                        } ).append( infoHtml ) );
                     required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                 } else {
                },
                     $infoContainer = $( '<div>', {
                variables: {
                        id: infoContainerId,
                     optional: [ 'temperature' ]
                        class: 'collapse'
                     } ).append( infoHtml );
                 }
                 }
            },
            units: 'mL/min',
            description: '3 mL O<sub>2</sub>/min per 100g of brain mass. 7% change in CMR for every 1 &deg;C change in temperature.',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                // Temperature is included as an optional variable to generate the input.
                // It is used by cerebralMetabolicRateFactor, which is an internal calculation not typically shown.
                var brainMass = data.brainMass.toNumber( 'gwt' );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();


                 return cerebralMetabolicRateFactor * 3 * brainMass / 100;
                 $( this ).after( $infoContainer );
             }
             }
        },
 
        cerebralMetabolicRateGlucose: {
            if( missingVariableInputs.length ) {
            name: 'Cerebral metabolic rate (Glucose)',
                var variablesContainerClass = 'calculator-SimpleCalculator-variables ' + calculation.getContainerClass() + '-variables';
            abbreviation: 'CMR<sub>glu</sub>',
                var inputGroup = mw.calculators.createInputGroup( missingVariableInputs );
            data: {
 
                 calculations: {
                if( isTable ) {
                     required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
                    $variablesContainer =  $( '<tr>' )
                        .append( $( '<td>', {
                            class: variablesContainerClass,
                            colspan: 2
                        } ).append( inputGroup ) );
                 } else {
                     $variablesContainer = $( '<div>', {
                        class: variablesContainerClass
                    } ).append( inputGroup );
                 }
                 }
            },
            units: 'mg/min',
            description: '5 mg glucose/min per 100g of brain mass. 7% change in CMR for every 1 &deg;C change in temperature',
            references: [
                'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001'
            ],
            calculate: function( data ) {
                var brainMass = data.brainMass.toNumber( 'gwt' );
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();


                 return cerebralMetabolicRateFactor * 5 * brainMass / 100;
                 $( this ).after( $variablesContainer );
 
                missingVariableInputs = [];
             }
             }
         },
         } );
        cerebralBloodFlow: {
    };
            name: 'Cerebral blood flow',
 
            abbreviation: 'CBF',
 
            data: {
 
                calculations: {
 
                    required: [ 'brainMass', 'cerebralMetabolicRateFactor' ]
 
                },
    /**
                variables: {
    * Class AbstractCalculator
                    optional: [ 'paCO2' ]
    * @param {Object} propertyValues
                }
    * @returns {mw.calculators.objectClasses.AbstractCalculator}
            },
    * @constructor
            units: 'mL/min',
    */
            description: '<ul><li>50 mL/min per 100g of brain mass.</li><li>Every mmHg in PaCO2 changes CBF by 1.5 mL/min per 100g of brain mass.</li><li>Cerebral blood flow and cerebral metabolic rate are coupled. Factors that alter CMR (e.g. temperature) will proportionally alter CBF.</li>',
    mw.calculators.objectClasses.AbstractCalculator = function( propertyValues ) {
             references: [
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
                 'Tameem A, Krovvidi H, Cerebral physiology, Continuing Education in Anaesthesia Critical Care & Pain, Volume 13, Issue 4, August 2013, Pages 113–118, https://doi.org/10.1093/bjaceaccp/mkt001',
    };
                 'Brian JE Jr. Carbon dioxide and the cerebral circulation. Anesthesiology. 1998 May;88(5):1365-86. doi: 10.1097/00000542-199805000-00029. PMID: 9605698.'
 
    mw.calculators.objectClasses.AbstractCalculator.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.getCalculatorClass = function() {
        return '';
    };
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.getContainerClass = function() {
        return 'calculator-' + this.module + '-' + this.id;
    };
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties = function() {
        return {
             required: [
                 'id',
                'module',
                'name',
                 'calculations'
             ],
             ],
             calculate: function( data ) {
             optional: [
                var brainMass = data.brainMass.toNumber( 'gwt' );
                'onRender',
                var cerebralMetabolicRateFactor = data.cerebralMetabolicRateFactor.toNumber();
                'onRendered'
                var paCO2 = data.paCO2.toNumber( 'mmHg' );
            ]
        };
    };
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.render = function() {
        if( typeof this.onRender === 'function' ) {
            this.onRender();
        }
 
        this.doRender();
 
        if( typeof this.onRendered === 'function' ) {
            this.onRendered();
        }
    };
 
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.doRender = function() {};
 


                var cerebralBloodFlow = cerebralMetabolicRateFactor * 50 * brainMass / 100;


                if( paCO2 ) {
                    // CO2 reductions don't reduce CBF more than 50%
                    var minCerebralBloodFlow = cerebralBloodFlow / 2;


                    cerebralBloodFlow += 1.5 * brainMass / 100 * ( paCO2 - 40 );


                     cerebralBloodFlow = math.max( cerebralBloodFlow, minCerebralBloodFlow );
    /**
    * Class SimpleCalculator
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.SimpleCalculator}
    * @constructor
    */
    mw.calculators.objectClasses.SimpleCalculator = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };
 
    mw.calculators.objectClasses.SimpleCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype );
 
    mw.calculators.objectClasses.SimpleCalculator.prototype.doRender = function() {
        var $calculatorContainer = $( '.' + this.getContainerClass() );
 
        if( !$calculatorContainer.length ) {
            return;
        }
 
        $calculatorContainer.addClass( this.getCalculatorClass() );
 
        if( this.css ) {
            $calculatorContainer.css( this.css );
        }
 
        $calculatorContainer.empty();
 
        $calculatorContainer.append( $( '<h4>', {
            text: this.name
        } ) );
 
        var $calculationsContainer;
 
        if( this.table ) {
            $calculationsContainer = $( '<table>', {
                class: 'wikitable'
            } ).append( '<tbody>' );
 
            $calculationsContainer
                .append( $( '<tr>' )
                    .append(
                        $( '<th>', {
                            class: this.getCalculatorClass() + '-calculation-header'
                        } ).text( 'Calculation' ),
                        $( '<th>', {
                            class: this.getCalculatorClass() + '-value-header'
                        }  ).text( 'Value' )
                     )
                );
        } else {
            $calculationsContainer = $( '<div>' );
        }
 
        $calculatorContainer.append( $calculationsContainer );
 
        for( var iCalculationId in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
            var calculationContainerClass = calculation.getContainerClass();
 
            var $calculationContainer = $( '.' + calculationContainerClass );
 
            // If a container doesn't exist yet, add it
            if( !$calculationContainer.length ) {
                if( this.table ) {
                    $calculationContainer = $( '<tr>', {
                        class: calculationContainerClass
                    } );
                } else {
                    $calculationContainer = $( '<div>', {
                        class: calculationContainerClass
                    } );
                 }
                 }


                 return cerebralBloodFlow;
                 $calculationsContainer.append( $calculationContainer );
             }
             }
            calculation.render();
         }
         }
     } );
     };
 
    mw.calculators.objectClasses.SimpleCalculator.prototype.getCalculatorClass = function() {
        return 'calculator-SimpleCalculator';
    };
 
 
    mw.calculators.objectClasses.SimpleCalculator.prototype.getProperties = function() {
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();
 
        return this.mergeProperties( inheritedProperties, {
            required: [],
            optional: [
                'css',
                'table'
            ]
        } );
    };


     var tableMaxWidth = 600;
     mw.calculators.initialize();


    mw.calculators.addCalculators( moduleId, {
        anatomy: {
            name: 'Patient statistics',
            calculations: [
                'bmi',
                'bsa',
                'ibw',
                'lbw'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        },
        fluidManagement: {
            name: 'Fluid management',
            calculations: [
                'fluidMaintenanceRate',
                'intraopFluids',
                'ebv',
                'maxAbl',
                'minUop'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        },
        cardiovascular: {
            name: 'Cardiovascular',
            calculations: [
                'vO2',
                'cardiacOutputFick',
                'cardiacIndex',
                'strokeVolume'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        },
        neuro: {
            name: 'Neuro',
            calculations: [
                'brainMass',
                'cerebralBloodVolume',
                'cerebralMetabolicRateO2',
                'cerebralMetabolicRateGlucose',
                'cerebralBloodFlow'
            ],
            css: {
                'max-width': tableMaxWidth
            },
            table: true
        }
    } );
}() );
}() );

Revision as of 04:03, 21 August 2021

/**
 * @author Chris Rishel
 */
( function() {
    var COOKIE_EXPIRATION = 12 * 60 * 60;

    var TYPE_NUMBER = 'number';
    var TYPE_STRING = 'string';

    var VALID_TYPES = [
        TYPE_NUMBER,
        TYPE_STRING
    ];

    var DEFAULT_CALCULATION_CLASS = 'SimpleCalculation';
    var DEFAULT_CALCULATOR_CLASS = 'SimpleCalculator';

    // Polyfill to fetch unit's base. This may become unnecessary in a future version of math.js
    math.Unit.prototype.getBase = function() {
        for( var iBase in math.Unit.BASE_UNITS ) {
            if( this.equalBase( math.Unit.BASE_UNITS[ iBase ] ) ) {
                return iBase;
            }
        }

        return null;
    };


    mw.calculators = {
        calculators: {},
        calculations: {},
        objectClasses: {},
        units: {},
        unitsBases: {},
        variables: {},
        addCalculations: function( calculationData, className ) {
            className = className ? className : DEFAULT_CALCULATION_CLASS;

            var calculations = mw.calculators.createCalculatorObjects( className, calculationData );

            for( var calculationId in calculations ) {
                var calculation = calculations[ calculationId ];

                mw.calculators.calculations[ calculationId ] = calculation;

                mw.calculators.calculations[ calculationId ].setDependencies();
            }
        },
        addCalculators: function( moduleId, calculatorData, className ) {
            className = className ? className : DEFAULT_CALCULATOR_CLASS;

            for( var calculatorId in calculatorData ) {
                calculatorData[ calculatorId ].module = moduleId;

                // Make sure the calculations have been defined
                for( var iCalculation in calculatorData[ calculatorId ].calculations ) {
                    var calculationId = calculatorData[ calculatorId ].calculations[ iCalculation ];

                    if( !mw.calculators.getCalculation( calculationId ) ) {
                        throw new Error( 'Calculator "' + calculatorId + '" references calculation "' + calculationId + '" which is not defined' );
                    }
                }
            }

            var calculators = mw.calculators.createCalculatorObjects( className, calculatorData );

            // Initalize the calculators property for the module
            if( !mw.calculators.calculators.hasOwnProperty( moduleId ) ) {
                mw.calculators.calculators[ moduleId ] = {};
            }

            // Store the calculators
            for( var calculatorId in calculators ) {
                mw.calculators.calculators[ moduleId ][ calculatorId ] = calculators[ calculatorId ];

                mw.calculators.calculators[ moduleId ][ calculatorId ].render();
            }
        },
        addUnitsBases: function( unitsBaseData ) {
            var unitsBases = mw.calculators.createCalculatorObjects( 'UnitsBase', unitsBaseData );

            for( var unitsBaseId in unitsBases ) {
                mw.calculators.unitsBases[ unitsBaseId ] = unitsBases[ unitsBaseId ];
            }
        },
        addUnits: function( unitsData ) {
            var units = mw.calculators.createCalculatorObjects( 'Units', unitsData );

            for( var unitsId in units ) {
                if( mw.calculators.units.hasOwnProperty( unitsId ) ) {
                    continue;
                }

                try {
                    var unitData = {
                        aliases: units[ unitsId ].aliases,
                        baseName: units[ unitsId ].baseName ? units[ unitsId ].baseName.toUpperCase() : units[ unitsId ].baseName,
                        definition: units[ unitsId ].definition,
                        prefixes: units[ unitsId ].prefixes,
                        offset: units[ unitsId ].offset,
                    };

                    math.createUnit( unitsId, unitData );
                } catch( e ) {
                    console.warn( e.message );
                }

                mw.calculators.units[ units ] = units[ unitsId ];
            }
        },
        addVariables: function( variableData ) {
            var variables = mw.calculators.createCalculatorObjects( 'Variable', variableData );

            for( var variableId in variables ) {
                mw.calculators.variables[ variableId ] = variables[ variableId ];

                var cookieValue = mw.calculators.getCookieValue( variableId );

                if( cookieValue ) {
                    try {
                        // isValueValid will throw an error if invalid, so the catch clause is our else condition
                        if( mw.calculators.variables[ variableId ].isValueValid( cookieValue ) ) {
                            mw.calculators.variables[ variableId ].setValue( cookieValue );
                        }
                    } catch( e ) {
                        // Unset the cookie value since for whatever reason it's no longer valid.
                        mw.calculators.setCookieValue( variableId, null );
                    }
                }
            }
        },
        createCalculatorObjects: function( className, objectData ) {
            if( !mw.calculators.objectClasses.hasOwnProperty( className ) ) {
                throw new Error( 'Invalid class name "' + className + '"' );
            }

            var objects = {};

            for( var objectId in objectData ) {
                var propertyValues = objectData[ objectId ];

                // Id can either be specified using the 'id' property, or as the property name in objectData
                if( propertyValues.hasOwnProperty( 'id' ) ) {
                    objectId = propertyValues.id;
                }
                else {
                    propertyValues.id = objectId;
                }

                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
            }

            return objects;
        },
        createInputGroup: function( variableIds ) {
            var $form = $( '<form>', {

            } );

            var $formRow = $( '<div>', {
                class: 'form-row'
            } ).css( 'flex-wrap', 'nowrap' );

            for( var iVariableId in variableIds ) {
                var variableId = variableIds[ iVariableId ];

                if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                    throw new Error( 'Invalid variable name "' + variableId + '"' );
                }

                $formRow.append( mw.calculators.variables[ variableId ].createInput() );
            }

            return $form.append( $formRow );
        },
        getCookieKey: function( variableId ) {
            return 'calculators-var-' + variableId;
        },
        getCookieValue: function( varId ) {
            var cookieValue = mw.cookie.get( mw.calculators.getCookieKey( varId ) );

            if( !cookieValue ) {
                return null;
            }

            return cookieValue;
        },
        getCalculation: function( calculationId ) {
            if( mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
                return mw.calculators.calculations[ calculationId ];
            } else {
                return null;
            }
        },
        getCalculator: function( moduleId, calculatorId ) {
            if( mw.calculators.calculators.hasOwnProperty( moduleId ) &&
                mw.calculators.calculators[ moduleId ].hasOwnProperty( calculatorId ) ) {
                return mw.calculators.calculators[ moduleId ][ calculatorId ];
            } else {
                return null;
            }
        },
        getUnitsByBase: function( value ) {
            if( typeof value !== 'object' || !value.hasOwnProperty( 'units' ) ) {
                return null;
            }

            var unitsByBase = {};

            for( var iUnits in value.units ) {
                var units = value.units[ iUnits ];

                unitsByBase[ units.unit.base.key.toLowerCase() ] = units.prefix.name + units.unit.name;
            }

            return unitsByBase;
        },
        getUnitsString: function( value ) {
            if( typeof value !== 'object' ) {
                return null;
            }

            var unitsString = value.formatUnits();

            var reDenominator = /\/\s?\((.*)\)/;
            var denominatorMatches = unitsString.match( reDenominator );

            if( denominatorMatches ) {
                var denominatorUnits = denominatorMatches[ 1 ];

                unitsString = unitsString.replace( reDenominator, '/' + denominatorUnits.replace( ' ', '/' ) );
            }

            unitsString = unitsString
                .replace( /\s/g, '' )
                .replace( /(\^(\d+))/g, '<sup>$2</sup>' );

            var unitsBase = value.getBase();

            if( unitsBase ) {
                unitsBase = unitsBase.toLowerCase();

                if( mw.calculators.unitsBases.hasOwnProperty( unitsBase ) &&
                    typeof mw.calculators.unitsBases[ unitsBase ].toString === 'function' ) {
                    unitsString = mw.calculators.unitsBases[ unitsBase ].toString( unitsString );
                }
            } else {
                // TODO nasty hack to fix weight units in compound units which have no base
                unitsString = unitsString.replace( 'kgwt', 'kg' );
                unitsString = unitsString.replace( 'ug', 'mcg' );
            }

            return unitsString;
        },
        getValueDecimals: function( value ) {
            // Supports either numeric values or math objects
            if( mw.calculators.isValueMathObject( value ) ) {
                value = mw.calculators.getValueNumber( value );
            }

            if( typeof value !== 'number' ) {
                return null;
            }

            // Convert the number to a string, reverse, and count the number of characters up to the period.
            var decimals = value.toString().split('').reverse().join('').indexOf( '.' );

            // If no decimal is present, will be set to -1 by indexOf. If so, set to 0.
            decimals = decimals > 0 ? decimals : 0;

            return decimals;
        },
        getValueNumber: function( value, decimals ) {
            if( typeof value !== 'object' ) {
                return null;
            }

            // Remove floating point errors
            var number = math.round( value.toNumber(), 10 );

            var absNumber = math.abs( number );

            if( absNumber >= 10 ) {
                decimals = 0;
            } else {
                decimals = -math.floor( math.log10( absNumber ) ) + 1;
            }

            return math.round( number, decimals );
        },
        getValueString: function( value, decimals ) {
            if( !mw.calculators.isValueMathObject( value ) ) {
                return null;
            }

            var valueNumber = mw.calculators.getValueNumber( value, decimals );
            var valueUnits = mw.calculators.getUnitsString( value );

            if( math.abs( math.log10( valueNumber ) ) > 3 ) {
                var valueUnitsByBase = mw.calculators.getUnitsByBase( value );

                var oldSIUnit;

                if( valueUnitsByBase.hasOwnProperty( 'mass' ) ) {
                    oldSIUnit = valueUnitsByBase.mass;
                } else if( valueUnitsByBase.hasOwnProperty( 'volume' ) ) {
                    oldSIUnit = valueUnitsByBase.volume;
                }

                if( oldSIUnit ) {
                    // This new value should simplify to the optimal SI prefix.
                    // We need to create a completely new unit from the formatted (i.e. simplified) value
                    var newSIValue = math.unit( math.unit( valueNumber + ' ' + oldSIUnit ).format() );

                    // There is a bug in mathjs where formatUnits() won't simplify the units, only format() will.
                    var newSIUnit = newSIValue.formatUnits();

                    if( newSIUnit !== oldSIUnit ) {
                        var newValue = math.unit( newSIValue.toNumber() + ' ' + value.formatUnits().replace( oldSIUnit, newSIUnit ) );

                        valueNumber = mw.calculators.getValueNumber( newValue, decimals );
                        valueUnits = mw.calculators.getUnitsString( newValue );
                    }
                }
            }

            var valueString = String( valueNumber );

            if( valueUnits ) {
                valueString += ' ' + valueUnits;
            }

            return valueString;
        },
        getVariable: function( variableId ) {
            if( mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return mw.calculators.variables[ variableId ];
            } else {
                return null;
            }
        },
        hasData: function( dataType, dataId ) {
            if( mw.calculators.hasOwnProperty( dataType ) &&
                mw.calculators[ dataType ].hasOwnProperty( dataId ) ) {
                return true;
            } else {
                return false;
            }
        },
        initialize: function() {
            $( '.calculator' ).each( function() {
                var gadgetModule = 'ext.gadget.calculator-' + $( this ).attr( 'data-module' );

                if( gadgetModule && mw.loader.getState( gadgetModule ) === 'registered' ) {
                    mw.loader.load( gadgetModule );
                }
            } );
        },
        isMobile: function() {
            return window.matchMedia( 'only screen and (max-width: 760px)' ).matches;
        },
        isValueMathObject: function( value ) {
            return value && value.hasOwnProperty( 'value' );
        },
        setCookieValue: function( variableId, value ) {
            mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                expires: COOKIE_EXPIRATION
            } );
        },
        setValue: function( variableId, value ) {
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return false;
            }

            if( mw.calculators.variables[ variableId ].setValue( value ) ) {
                mw.calculators.setCookieValue( variableId, value );

                return true;
            }

            return false;
        },
        uniqueValues: function( value, index, self ) {
            return self.indexOf( value ) === index;
        }
    };

    /**
     * Class CalculatorObject
     *
     * @param {Object} properties
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.CalculatorObject}
     * @constructor
     */
    mw.calculators.objectClasses.CalculatorObject = function( properties, propertyValues ) {
        propertyValues = propertyValues ? propertyValues : {};

        if( properties ) {
            if( properties.hasOwnProperty( 'required' ) ) {
                for( var iRequiredProperty in properties.required ) {
                    var requiredProperty = properties.required[ iRequiredProperty ];

                    if( !propertyValues || !propertyValues.hasOwnProperty( requiredProperty ) ) {
                        console.error( 'Missing required property "' + requiredProperty + '"' );
                        console.log( propertyValues );

                        return null;
                    }

                    this[ requiredProperty ] = propertyValues[ requiredProperty ];

                    delete propertyValues[ requiredProperty ];
                }
            }

            if( properties.hasOwnProperty( 'optional' ) ) {
                for( var iOptionalProperty in properties.optional ) {
                    var optionalProperty = properties.optional[ iOptionalProperty ];

                    if( propertyValues && propertyValues.hasOwnProperty( optionalProperty ) ) {
                        this[ optionalProperty ] = propertyValues[ optionalProperty ];

                        delete propertyValues[ optionalProperty ];
                    } else if( typeof this[ optionalProperty ] === 'undefined' ) {
                        this[ optionalProperty ] = null;
                    }
                }
            }

            var invalidProperties = Object.keys( propertyValues );

            if( invalidProperties.length ) {
                console.warn( 'Unsupported properties defined for ' + typeof this + ' with id "' + this.id + '": ' + invalidProperties.join( ', ' ) );
            }
        }
    };

    mw.calculators.objectClasses.CalculatorObject.prototype.getProperties = function() {
        return {
            required: [],
            optional: []
        };
    };

    mw.calculators.objectClasses.CalculatorObject.prototype.mergeProperties = function( inheritedProperties, properties ) {
        var uniqueValues = function( value, index, self ) {
            return self.indexOf( value ) === index;
        };

        properties.required = inheritedProperties.required.concat( properties.required ).filter( uniqueValues );
        properties.optional = inheritedProperties.optional.concat( properties.optional ).filter( uniqueValues );

        return properties;
    };




    /**
     * Class UnitsBase
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.UnitsBase}
     * @constructor
     */
    mw.calculators.objectClasses.UnitsBase = function( propertyValues ) {
        var properties = {
            required: [
                'id'
            ],
            optional: [
                'toString'
            ]
        };

        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    };

    mw.calculators.objectClasses.UnitsBase.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );




    /**
     * Class Units
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.Units}
     * @constructor
     */
    mw.calculators.objectClasses.Units = function( propertyValues ) {
        var properties = {
            required: [
                'id'
            ],
            optional: [
                'aliases',
                'baseName',
                'definition',
                'offset',
                'prefixes'
            ]
        };

        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    };

    mw.calculators.objectClasses.Units.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );




    /**
     * Class Variable
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.Variable}
     * @constructor
     */
    mw.calculators.objectClasses.Variable = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        if( VALID_TYPES.indexOf( this.type ) === -1 ) {
            throw new Error( 'Invalid type "' + this.type + '" for variable "' + this.id + '"' );
        }

        // Accept options as either an array of strings, or an object with ids as keys and display text as values
        if( Array.isArray( this.options ) ) {
            var options = {};

            for( var iOption in this.options ) {
                var option = this.options[ iOption ];

                options[ option ] = option;
            }

            this.options = options;
        }

        this.calculations = [];

        if( this.defaultValue ) {
            this.defaultValue = this.prepareValue( this.defaultValue );
        }

        this.value = null;
    };

    mw.calculators.objectClasses.Variable.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.Variable.prototype.addCalculation = function( calculationId ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            return;
        }

        this.calculations.push( calculationId );
    };

    mw.calculators.objectClasses.Variable.prototype.createInput = function( inputOptions ) {
        if( !inputOptions ) {
            inputOptions = {};
        }

        inputOptions.class = inputOptions.hasOwnProperty( 'class' ) ? inputOptions.class : '';
        inputOptions.hideLabel = inputOptions.hasOwnProperty( 'hideLabel' ) ? inputOptions.hideLabel : false;
        inputOptions.hideLabelMobile = inputOptions.hasOwnProperty( 'hideLabelMobile' ) ? inputOptions.hideLabelMobile : false;
        inputOptions.inline = inputOptions.hasOwnProperty( 'inline' ) ? inputOptions.inline : false;
        inputOptions.inputClass = inputOptions.hasOwnProperty( 'inputClass' ) ? inputOptions.inputClass : '';

        var variableId = this.id;
        var inputId = 'calculator-input-' + variableId;

        var inputContainerTag = inputOptions.inline ? '<span>' : '<div>';

        var inputContainerAttributes = {
            class: 'form-group mb-0 calculator-container-input'
        };

        inputContainerAttributes.class += inputOptions.class ? ' ' + inputOptions.class : '';
        inputContainerAttributes.class += ' calculator-container-input-' + variableId;

        var inputContainerCss = {};

        // Initialize label attributes
        var labelAttributes = {
            for: inputId,
            html: this.getLabelString()
        };

        if( inputOptions.hideLabel || ( inputOptions.hideLabelMobile && mw.calculators.isMobile() ) ) {
            labelAttributes.class = 'sr-only';
        }

        var labelCss = {};

        if( inputOptions.inline ) {
            inputContainerTag = '<span>';

            inputContainerCss[ 'align-items' ] = 'center';
            inputContainerCss[ 'display' ] = 'flex';
            inputContainerCss[ 'height' ] = 'calc(1.5em + 0.75rem + 2px)';

            labelAttributes.html += ':&nbsp;';
            labelCss[ 'margin-bottom' ] = 0;
        }

        // Create the input container
        var $inputContainer = $( inputContainerTag, inputContainerAttributes ).css( inputContainerCss );

        var $label = $( '<label>', labelAttributes ).css( labelCss );

        $inputContainer.append( $label );

        var value = this.getValue();

        if( this.type === TYPE_NUMBER ) {
            // Initialize the primary units variables (needed for handlers, even if doesn't have units)
            var unitsId = null;
            var $unitsContainer = null;

            var inputValue = '';

            if( mw.calculators.isValueMathObject( value ) ) {
                var number = value.toNumber();

                if( number ) {
                    inputValue = number;
                }
            } else {
                inputValue = value;
            }

            // Initialize input options
            var inputAttributes = {
                id: inputId,
                class: 'form-control calculator-input calculator-input-text',
                type: 'text',
                autocomplete: 'off',
                inputmode: 'decimal',
                value: inputValue
            };

            // Configure additional options
            if( this.maxLength ) {
                inputAttributes.maxlength = this.maxLength;
            }

            // Add any additional classes to the input
            inputAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';

            // Add the input id to the list of classes
            inputAttributes.class += ' ' + inputId;

            // If the variable has units, create the units input
            if( this.hasUnits() ) {
                // Set the units id
                unitsId = inputId + '-units';

                var unitsValue = mw.calculators.isValueMathObject( value ) ? value.formatUnits() : null;

                var unitsInputAttributes = {
                    id: unitsId
                };

                // Create the units container
                $unitsContainer = $( '<div>', {
                    class: 'input-group-append'
                } ).css( 'align-items', 'center' );

                if( this.units.length === 1 ) {
                    unitsInputAttributes.type = 'hidden';
                    unitsInputAttributes.value = this.units[ 0 ];

                    $unitsContainer
                        .css( 'padding', '0 0.5em' )
                        .append( mw.calculators.getUnitsString( math.unit( '0 ' + this.units[ 0 ] ) ) )
                        .append( $( '<input>', unitsInputAttributes ) );
                } else {
                    // Initialize the units input options
                    unitsInputAttributes.class = 'custom-select calculator-input-select';

                    // Add any additional classes to the input
                    unitsInputAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';

                    unitsInputAttributes.class = unitsInputAttributes.class + ' ' + unitsId;

                    var $unitsInput = $( '<select>', unitsInputAttributes )
                        .on( 'change', function() {
                            var numberValue = $( '#' + inputId ).val();

                            var newValue = numberValue ? numberValue + ' ' + $( this ).val() : null;

                            mw.calculators.setValue( variableId, newValue );
                        } );

                    for( var iUnits in this.units ) {
                        var units = this.units[ iUnits ];

                        var unitsOptionAttributes = {
                            html: mw.calculators.getUnitsString( math.unit( '0 ' + units ) ),
                            value: units
                        };

                        if( units === unitsValue ) {
                            unitsOptionAttributes.selected = true;
                        }

                        $unitsInput.append( $( '<option>', unitsOptionAttributes ) );
                    }

                    $unitsContainer.append( $unitsInput );
                }
            }

            // Create the input and add handlers
            var $input = $( '<input>', inputAttributes )
                .on( 'input', function() {
                    var numberValue = $( this ).val();

                    var newValue = numberValue ? numberValue : null;

                    if( newValue && unitsId ) {
                        newValue = newValue + ' ' + $( '#' + unitsId ).val();
                    }

                    mw.calculators.setValue( variableId, newValue );
                } );

            // Create the input group
            var $inputGroup = $( '<div>', {
                class: 'input-group'
            } ).append( $input );

            if( $unitsContainer ) {
                $inputGroup.append( $unitsContainer );
            }

            $inputContainer.append( $inputGroup );
        } else if( this.type === TYPE_STRING ) {
            if( this.hasOptions() ) {
                var optionKeys = Object.keys( this.options );

                if( optionKeys.length === 1 ) {
                    $inputContainer.append( this.options[ optionKeys[ 0 ] ] );
                } else {
                    var selectAttributes = {
                        id: inputId,
                        class: 'custom-select calculator-input calculator-input-select'
                    };

                    // Add any additional classes to the input
                    selectAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';

                    var $select = $( '<select>', selectAttributes )
                        .on( 'change', function() {
                            mw.calculators.setValue( variableId, $( this ).val() );
                        } );

                    for( var optionId in this.options ) {
                        var displayText = this.options[ optionId ];

                        var optionAttributes = {
                            value: optionId,
                            text: displayText
                        };

                        if( optionId === value ) {
                            optionAttributes.selected = true;
                        }

                        $select.append( $( '<option>', optionAttributes ) );
                    }

                    $inputContainer.append( $select );
                }
            }
        }

        return $inputContainer;
    };

    mw.calculators.objectClasses.Variable.prototype.getLabelString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };

    mw.calculators.objectClasses.Variable.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name',
                'type'
            ],
            optional: [
                'abbreviation',
                'defaultValue',
                'maxLength',
                'maxValue',
                'minValue',
                'options',
                'units'
            ]
        };
    };

    mw.calculators.objectClasses.Variable.prototype.getValue = function() {
        if( this.value !== null ) {
            return this.value;
        } else if( this.defaultValue !== null ) {
            return this.defaultValue;
        } else {
            return null;
        }
    };

    mw.calculators.objectClasses.Variable.prototype.getValueString = function() {
        return String( this.getValue() );
    };

    mw.calculators.objectClasses.Variable.prototype.hasOptions = function() {
        return this.options !== null;
    };

    mw.calculators.objectClasses.Variable.prototype.hasUnits = function() {
        return this.units !== null;
    };

    mw.calculators.objectClasses.Variable.prototype.hasValue = function() {
        var value = this.getValue();

        if( value === null ||
            ( mw.calculators.isValueMathObject( value ) && !value.toNumber() ) ) {
            return false;
        }

        return true;
    };

    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
        return mw.calculators.isValueMathObject( this.value );
    };

    mw.calculators.objectClasses.Variable.prototype.isValueValid = function( value ) {
        if( value === null ) {
            return true;
        }

        if( this.type === TYPE_NUMBER ) {
            if( typeof value !== 'object' ) {
                value = math.unit( value );
            }

            if( this.hasUnits() ) {
                var valueUnits = value.formatUnits();

                if( !valueUnits ) {
                    throw new Error( 'Could not set value for "' + this.id + '": Value must define units' );
                } else if( this.units.indexOf( valueUnits ) === -1 ) {
                    throw new Error( 'Could not set value for "' + this.id + '": Units "' + valueUnits + '" are not valid for this variable' );
                }
            }
        } else if( this.hasOptions() ) {
            if( !this.options.hasOwnProperty( value ) ) {
                throw new Error( 'Could not set value "' + value + '" for "' + this.id + '": Value must define be one of: ' + Object.keys( this.options ).join( ', ' ) );
            }
        }

        return true;
    };

    mw.calculators.objectClasses.Variable.prototype.prepareValue = function( value ) {
        if( !this.isValueValid( value ) ) {
            // isValueValid will throw a meaningful error to the console
            return null;
        }

        if( value !== null ) {
            if( this.type === TYPE_NUMBER ) {
                if( typeof value !== 'object' ) {
                    value = math.unit( value );
                }
            }
        }

        return value;
    };

    mw.calculators.objectClasses.Variable.prototype.setValue = function( value ) {
        this.value = this.prepareValue( value );

        this.valueUpdated();

        return true;
    };

    mw.calculators.objectClasses.Variable.prototype.valueUpdated = function() {
        for( var iCalculation in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );

            if( calculation ) {
                calculation.render();
            }
        }
    }



    /**
     * Class AbstractCalculation
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.AbstractCalculation}
     * @constructor
     */
    mw.calculators.objectClasses.AbstractCalculation = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        this.initialize();
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.AbstractCalculation.prototype.addCalculation = function( calculationId ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            return;
        }

        this.calculations.push( calculationId );
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.doRender = function() {};

    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerClass = function() {
        return 'calculator-calculation-' + this.id;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getLabelString = function() {
        return this.id;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'calculate'
            ],
            optional: [
                'data',
                'description',
                'onRender',
                'onRendered',
                'references',
                'type'
            ]
        };
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getValue = function() {
        // For now, we always need to recalculate, since the calculation may not be rendered but still required by
        // other calculations (i.e. drug dosages using lean body weight).
        this.recalculate();

        return this.value;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.hasInfo = function() {
        return false;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.hasValue = function() {
        if( this.value === null ||
            ( this.isValueMathObject() && !this.value.toNumber() ) ) {
            return false;
        }

        return true;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationData = function() {
        return this.data;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues = function() {
        var calculationData = this.getCalculationData();

        var data = {};
        var missingRequiredData = '';
        var calculationId, calculation, variableId, variable;

        for( var iRequiredCalculation in calculationData.calculations.required ) {
            calculationId = calculationData.calculations.required[ iRequiredCalculation ];
            calculation = mw.calculators.getCalculation( calculationId );

            if( !calculation ) {
                throw new Error( 'Invalid required calculation "' + calculationId + '" for calculation "' + this.id + '"' );
            } else if( !calculation.hasValue() ) {
                if( missingRequiredData ) {
                    missingRequiredData = missingRequiredData + ', ';
                }

                missingRequiredData = missingRequiredData + calculation.getLabelString();
            } else {
                data[ calculationId ] = calculation.value;
            }
        }

        for( var iRequiredVariable in calculationData.variables.required ) {
            variableId = calculationData.variables.required[ iRequiredVariable ];
            variable = mw.calculators.getVariable( variableId );

            if( !variable ) {
                throw new Error( 'Invalid required variable "' + variableId + '" for calculation "' + this.id + '"' );
            } else if( !variable.hasValue() ) {
                if( missingRequiredData ) {
                    missingRequiredData = missingRequiredData + ', ';
                }

                missingRequiredData = missingRequiredData + variable.getLabelString();
            } else {
                data[ variableId ] = variable.getValue();
            }
        }

        if( missingRequiredData ) {
            this.message = missingRequiredData + ' required';

            return false;
        }

        for( var iOptionalCalculation in calculationData.calculations.optional ) {
            calculationId = calculationData.calculations.optional[ iOptionalCalculation ];
            calculation = mw.calculators.getCalculation( calculationId );

            if( !calculation ) {
                throw new Error( 'Invalid optional calculation "' + calculationId + '" for calculation "' + this.id + '"' );
            }

            data[ calculationId ] = calculation.hasValue() ? calculation.value : null;
        }

        for( var iOptionalVariable in calculationData.variables.optional ) {
            variableId = calculationData.variables.optional[ iOptionalVariable ];
            variable = mw.calculators.getVariable( variableId );

            if( !variable ) {
                throw new Error( 'Invalid optional variable "' + variableId + '" for calculation "' + this.id + '"' );
            }

            data[ variableId ] = variable.hasValue() ? variable.getValue() : null;
        }

        return data;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
        if( typeof this.calculate !== 'function' ) {
            throw new Error( 'calculate() must be a function for Calculation "' + this.id + '"' );
        }

        // Initialize array to store calculation ids which depend on this calculation's value
        this.calculations = [];

        this.data = new mw.calculators.objectClasses.CalculationData( this.getCalculationData() );

        this.type = this.type ? this.type : TYPE_NUMBER;

        this.message = null;
        this.value = null;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
        return mw.calculators.isValueMathObject( this.value );
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.recalculate = function() {
        this.message = '';
        this.value = null;

        var data = this.getCalculationDataValues();

        if( data === false ) {
            this.valueUpdated();

            return false;
        }

        try {
            var value = this.calculate( data );

            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                if( this.units ) {
                    value = value + ' ' + this.units;
                }

                this.value = math.unit( value );
            } else {
                this.value = value;
            }
        } catch( e ) {
            console.warn( e.message );

            this.message = e.message;
            this.value = null;
        } finally {
            this.valueUpdated();
        }

        return true;
    };



    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
        this.recalculate();

        if( typeof this.onRender === 'function' ) {
            this.onRender();
        }

        this.doRender();

        if( typeof this.onRendered === 'function' ) {
            this.onRendered();
        }
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.setDependencies = function() {
        this.data = this.getCalculationData();

        var calculationIds = this.data.calculations.required.concat( this.data.calculations.optional );

        for( var iCalculationId in calculationIds ) {
            var calculationId = calculationIds[ iCalculationId ];

            if( !mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
                throw new Error('Calculation "' + calculationId + '" does not exist for calculation "' + this.id + '"');
            }

            mw.calculators.calculations[ calculationId ].addCalculation( this.id );
        }

        var variableIds = this.data.variables.required.concat( this.data.variables.optional );

        for( var iVariableId in variableIds ) {
            var variableId = variableIds[ iVariableId ];

            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                throw new Error('Variable "' + variableId + '" does not exist for calculation "' + this.id + '"');
            }

            mw.calculators.variables[ variableId ].addCalculation( this.id );
        }

        this.recalculate();
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.valueUpdated = function() {
        for( var iCalculation in this.calculations ) {
            calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );

            if( calculation ) {
                calculation.render();
            }
        }
    };



    /**
     * Class CalculationData
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.CalculationData}
     * @constructor
     */
    mw.calculators.objectClasses.CalculationData = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        var dataTypes = this.getDataTypes();
        var dataRequirements = this.getDataRequirements();

        // Iterate through the supported data types (e.g. calculation, variable) to initialize the structure
        for( var iDataType in dataTypes ) {
            var dataType = dataTypes[ iDataType ];

            if( !this[ dataType ] ) {
                this[ dataType ] = {
                    optional: [],
                    required: []
                };
            } else {
                // Iterate through the requirement levels (i.e. optional, required) to initialize the structure
                for( var iDataRequirement in dataRequirements ) {
                    var dataRequirement = dataRequirements[ iDataRequirement ];

                    if( this[ dataType ].hasOwnProperty( dataRequirement ) ) {
                        for( var iDataId in this[ dataType ][ dataRequirement ] ) {
                            var dataId = this[ dataType ][ dataRequirement ][ iDataId ];
                        }
                    } else {
                        this[ dataType ][ dataRequirement ] = [];
                    }
                }
            }
        }
    };

    mw.calculators.objectClasses.CalculationData.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.CalculationData.prototype.getDataRequirements = function() {
        return [
            'optional',
            'required'
        ];
    };

    mw.calculators.objectClasses.CalculationData.prototype.getDataTypes = function() {
        return [
            'calculations',
            'variables'
        ];
    };

    mw.calculators.objectClasses.CalculationData.prototype.getProperties = function() {
        return {
            required: [],
            optional: [
                'calculations',
                'variables'
            ]
        };
    };



    mw.calculators.objectClasses.CalculationData.prototype.merge = function() {
        var mergedData = new mw.calculators.objectClasses.CalculationData();

        var data = [ this ].concat( Array.prototype.slice.call( arguments ) );

        var dataTypes = this.getDataTypes();

        for( var iData in data ) {
            for( var iDataType in dataTypes ) {
                var dataType = dataTypes[ iDataType ];

                mergedData[ dataType ].required = mergedData[ dataType ].required
                    .concat( data[ iData ][ dataType ].required )
                    .filter( mw.calculators.uniqueValues );

                mergedData[ dataType ].optional = mergedData[ dataType ].optional
                    .concat( data[ iData ][ dataType ].optional )
                    .filter( mw.calculators.uniqueValues );
            }
        }

        return mergedData;
    };





    /**
     * Class SimpleCalculation
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.SimpleCalculation}
     * @constructor
     */
    mw.calculators.objectClasses.SimpleCalculation = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );

        this.initialize();
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );


    mw.calculators.objectClasses.SimpleCalculation.prototype.hasInfo = function() {
        return this.description || this.formula || this.references.length;
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelHtml = function() {
        var labelHtml = this.getLabelString();

        if( this.link ) {
            var href = this.link;

            // Detect internal links (this isn't great)
            var matches = href.match( /\[\[(.*?)\]\]/ );

            if( matches ) {
                href = mw.util.getUrl( matches[ 1 ] );
            }

            labelHtml = $( '<a>', {
                href: href,
                text: labelHtml
            } )[ 0 ].outerHTML;
        }

        return labelHtml;
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getProperties = function() {
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();

        return this.mergeProperties( inheritedProperties, {
            required: [
                'name'
            ],
            optional: [
                'abbreviation',
                'digits',
                'formula',
                'link',
                'units'
            ]
        } );
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
        if( this.message ) {
            return this.message;
        } else if( typeof this.value === 'object' && this.value.hasOwnProperty( 'value' ) ) {
            return mw.calculators.getValueString( this.value );
        } else {
            return String( this.value );
        }
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.doRender = function() {
        var $calculationContainer = $( '.' + this.getContainerClass() );

        if( !$calculationContainer.length ) {
            return;
        }

        var valueString = this.getValueString();

        var inputVariableIds = this.data.variables.required.concat( this.data.variables.optional );
        var missingVariableInputs = [];

        for( var iInputVariableId in inputVariableIds ) {
            var variableId = inputVariableIds[ iInputVariableId ];

            if( !$( '#calculator-input-' + variableId ).length ) {
                missingVariableInputs.push( variableId );
            }
        }

        var calculation = this;

        $calculationContainer.each( function() {
            $( this ).empty();

            var isTable = this.tagName.toLowerCase() === 'tr';

            var $infoButton = null;

            if( calculation.hasInfo() ) {
                $infoButton = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + calculation.getContainerClass() + '-info',
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': calculation.getContainerClass() + '-info'
                } )
                    .append( $( '<i>', {
                        class: 'far fa-question-circle'
                    } ) );
            }

            var labelHtml = calculation.getLabelHtml();

            if( isTable ) {
                if( calculation.hasInfo() ) {
                    labelHtml += $( '<span>', {
                        class: 'calculator-SimpleCalculator-info'
                    } ).append( $infoButton )[ 0 ].outerHTML;
                }

                $( this )
                    .append( $( '<th>', {
                        class: 'calculator-SimpleCalculator-calculation-cell',
                        html: labelHtml
                    } ) )
                    .append( $( '<td>', {
                        class: 'calculator-SimpleCalculator-value-cell',
                        html: valueString
                    } ) );
            } else {
                $( this )
                    .append( labelHtml + $infoButton[ 0 ].outerHTML + ': ' + valueString );
            }

            if( calculation.hasInfo() ) {
                var infoHtml = '';

                if( calculation.description ) {
                    infoHtml += $( '<p>', {
                        html: calculation.description
                    } )[ 0 ].outerHTML;
                }

                if( calculation.formula ) {
                    infoHtml += $( '<span>', {
                        class: calculation.getContainerClass() + '-formula'
                    } )[ 0 ].outerHTML;

                    var api = new mw.Api();

                    api.parse( calculation.formula ).then( function( result ) {
                        $( '.' + calculation.getContainerClass() + '-formula' ).html( result );
                    } );
                }

                if( calculation.references.length ) {
                    var $references = $( '<ol>' );

                    for( var iReference in calculation.references ) {
                        $references.append( $( '<li>', {
                            text: calculation.references[ iReference ]
                        } ) );
                    }

                    infoHtml += $references[ 0 ].outerHTML;
                }

                var infoContainerId = calculation.getContainerClass() + '-info';
                var $infoContainer = $( '#' + infoContainerId );

                if( $infoContainer.length ) {
                    $infoContainer.empty();
                }

                if( isTable ) {
                    $infoContainer = $( '<tr>', {
                        id: infoContainerId,
                        class: 'collapse'
                    } )
                        .append( $( '<td>', {
                            colspan: 2
                        } ).append( infoHtml ) );
                } else {
                    $infoContainer = $( '<div>', {
                        id: infoContainerId,
                        class: 'collapse'
                    } ).append( infoHtml );
                }

                $( this ).after( $infoContainer );
            }

            if( missingVariableInputs.length ) {
                var variablesContainerClass = 'calculator-SimpleCalculator-variables ' + calculation.getContainerClass() + '-variables';
                var inputGroup = mw.calculators.createInputGroup( missingVariableInputs );

                if( isTable ) {
                    $variablesContainer =  $( '<tr>' )
                        .append( $( '<td>', {
                            class: variablesContainerClass,
                            colspan: 2
                        } ).append( inputGroup ) );
                } else {
                    $variablesContainer = $( '<div>', {
                        class: variablesContainerClass
                    } ).append( inputGroup );
                }

                $( this ).after( $variablesContainer );

                missingVariableInputs = [];
            }
        } );
    };





    /**
     * Class AbstractCalculator
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.AbstractCalculator}
     * @constructor
     */
    mw.calculators.objectClasses.AbstractCalculator = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };

    mw.calculators.objectClasses.AbstractCalculator.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );

    mw.calculators.objectClasses.AbstractCalculator.prototype.getCalculatorClass = function() {
        return '';
    };

    mw.calculators.objectClasses.AbstractCalculator.prototype.getContainerClass = function() {
        return 'calculator-' + this.module + '-' + this.id;
    };

    mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'module',
                'name',
                'calculations'
            ],
            optional: [
                'onRender',
                'onRendered'
            ]
        };
    };

    mw.calculators.objectClasses.AbstractCalculator.prototype.render = function() {
        if( typeof this.onRender === 'function' ) {
            this.onRender();
        }

        this.doRender();

        if( typeof this.onRendered === 'function' ) {
            this.onRendered();
        }
    };


    mw.calculators.objectClasses.AbstractCalculator.prototype.doRender = function() {};





    /**
     * Class SimpleCalculator
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.SimpleCalculator}
     * @constructor
     */
    mw.calculators.objectClasses.SimpleCalculator = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };

    mw.calculators.objectClasses.SimpleCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype );

    mw.calculators.objectClasses.SimpleCalculator.prototype.doRender = function() {
        var $calculatorContainer = $( '.' + this.getContainerClass() );

        if( !$calculatorContainer.length ) {
            return;
        }

        $calculatorContainer.addClass( this.getCalculatorClass() );

        if( this.css ) {
            $calculatorContainer.css( this.css );
        }

        $calculatorContainer.empty();

        $calculatorContainer.append( $( '<h4>', {
            text: this.name
        } ) );

        var $calculationsContainer;

        if( this.table ) {
            $calculationsContainer = $( '<table>', {
                class: 'wikitable'
            } ).append( '<tbody>' );

            $calculationsContainer
                .append( $( '<tr>' )
                    .append(
                        $( '<th>', {
                            class: this.getCalculatorClass() + '-calculation-header'
                        } ).text( 'Calculation' ),
                        $( '<th>', {
                            class: this.getCalculatorClass() + '-value-header'
                        }  ).text( 'Value' )
                    )
                );
        } else {
            $calculationsContainer = $( '<div>' );
        }

        $calculatorContainer.append( $calculationsContainer );

        for( var iCalculationId in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
            var calculationContainerClass = calculation.getContainerClass();

            var $calculationContainer = $( '.' + calculationContainerClass );

            // If a container doesn't exist yet, add it
            if( !$calculationContainer.length ) {
                if( this.table ) {
                    $calculationContainer = $( '<tr>', {
                        class: calculationContainerClass
                    } );
                } else {
                    $calculationContainer = $( '<div>', {
                        class: calculationContainerClass
                    } );
                }

                $calculationsContainer.append( $calculationContainer );
            }

            calculation.render();
        }
    };

    mw.calculators.objectClasses.SimpleCalculator.prototype.getCalculatorClass = function() {
        return 'calculator-SimpleCalculator';
    };


    mw.calculators.objectClasses.SimpleCalculator.prototype.getProperties = function() {
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();

        return this.mergeProperties( inheritedProperties, {
            required: [],
            optional: [
                'css',
                'table'
            ]
        } );
    };

    mw.calculators.initialize();

}() );