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

From WikiAnesthesia
 
(275 intermediate revisions by the same user not shown)
Line 3: Line 3:
  */
  */
( function() {
( function() {
     var DEFAULT_DRUG_COLOR = 'default';
     var COOKIE_EXPIRATION = 12 * 60 * 60;
    var DEFAULT_DRUG_POPULATION = 'general';
    var DEFAULT_DRUG_ROUTE = 'iv';


     mw.calculators.isValueDependent = function( value, variableId ) {
     var TYPE_NUMBER = 'number';
         // This may need generalized to support other variables in the future
    var TYPE_STRING = 'string';
         if( variableId === 'weight' ) {
 
             return value && value.formatUnits().match( /\/[\s(]*?kg/ );
    var VALID_TYPES = [
         } else {
        TYPE_NUMBER,
             throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' );
        TYPE_STRING
    ];
 
    var DEFAULT_CALCULATION_CLASS = 'SimpleCalculation';
 
    // Polyfill to convert to roman numerals
    math.roman = function( number ) {
         var romanOrders = {
            M: 1000,
            CM: 900,
            D: 500,
            CD: 400,
            C: 100,
            XC: 90,
            L: 50,
            XL: 40,
            X: 10,
            IX: 9,
            V: 5,
            IV: 4,
            I: 1
        };
 
         var roman = '';
 
        for( var iOrder in romanOrders ) {
             var numOfOrder = Math.floor(number / romanOrders[ iOrder ] );
            number -= numOfOrder * romanOrders[ iOrder ];
            roman += iOrder.repeat( numOfOrder );
        }
 
        return roman;
    };
 
    // 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;
     };
     };


    /**
    * Define units
    */
    mw.calculators.addUnitsBases( {
        concentration: {
            toString: function( units ) {
                units = units.replace( ' pct', '%' );
                units = units.replace( 'ug', 'mcg' );


                 return units;
    mw.calculators = {
        calculations: {},
        objectClasses: {},
        options: {},
        selectors: {
            calculationCategories: '.calculator-calculationcategory',
            calculations: '.calculator-calculation',
            calculatorOptions: '.calculator-options'
        },
        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();
 
                mw.calculators.calculations[ calculationId ].update();
             }
             }
         },
         },
         mass: {
         addUnitsBases: function( unitsBaseData ) {
            toString: function( units ) {
            var unitsBases = mw.calculators.createCalculatorObjects( 'UnitsBase', unitsBaseData );
                units = units.replace( 'ug', 'mcg' );


                 return units;
            for( var unitsBaseId in unitsBases ) {
                 mw.calculators.unitsBases[ unitsBaseId.toLowerCase() ] = unitsBases[ unitsBaseId ];
             }
             }
         }
         },
    } );
        addUnits: function( unitsData ) {
            var units = mw.calculators.createCalculatorObjects( 'Units', unitsData );
 
            for( var unitsId in units ) {
                if( mw.calculators.units.hasOwnProperty( unitsId ) ) {
                    continue;
                }
 
                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
                };
 
                try {
                    math.createUnit( unitsId, unitData );
                } catch( e ) {
                    console.warn( e.message );
                }


    mw.calculators.addUnits( {
                mw.calculators.units[ unitsId ] = units[ unitsId ];
        pct: {
             }
            baseName: 'concentration',
             definition: '10 mg/mL'
         },
         },
         vial: {
         addVariables: function( variableData ) {
             baseName: 'volume'
             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 ) {
    * DrugColor
                    // Try to set the variable value from the cookie value
    */
                    if( !mw.calculators.variables[ variableId ].setValue( cookieValue ) ) {
    mw.calculators.drugColors = {};
                        // 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 + '"' );
            }


    mw.calculators.addDrugColors = function( drugColorData ) {
            var objects = {};
        var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );


        for( var drugColorId in drugColors ) {
            for( var objectId in objectData ) {
            mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
                var propertyValues = objectData[ objectId ];
        }
    };


    mw.calculators.getDrugColor = function( drugColorId ) {
                // Id can either be specified using the 'id' property, or as the property name in objectData
        if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
                if( propertyValues.hasOwnProperty( 'id' ) ) {
            return mw.calculators.drugColors[ drugColorId ];
                    objectId = propertyValues.id;
        } else {
                }
            return null;
                else {
        }
                    propertyValues.id = objectId;
    };
                }


    /**
                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
    * Class DrugColor
             }
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugColor}
    * @constructor
    */
    mw.calculators.objectClasses.DrugColor = function( propertyValues ) {
        var properties = {
            required: [
                'id'
            ],
            optional: [
                'parentColor',
                'primaryColor',
                'highlightColor',
                'striped'
             ]
        };


         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            return objects;
         },
        createInputGroup: function( variableIds, global, maxInputsPerRow ) {
            var $form = $( '<form>', {
                novalidate: true
            } );


        this.parentColor = this.parentColor || this.id === DEFAULT_DRUG_COLOR ? this.parentColor : DEFAULT_DRUG_COLOR;
            var $formRow;
    };


    mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
            var inputOptions = {
                global: !!global
            };


    mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() {
            maxInputsPerRow = maxInputsPerRow ?
        if( !this.parentColor ) {
                maxInputsPerRow :
            return null;
                mw.calculators.getOptionValue( 'inputgroupmaxinputsperrow' );
        }


        var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );
            var inputCount = 0;


        if( !parentDrugColor ) {
            for( var iVariableId in variableIds ) {
            throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
                var variableId = variableIds[ iVariableId ];
        }


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


    mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
                if( inputCount % maxInputsPerRow === 0 ) {
        if( this.highlightColor ) {
                    if( $formRow ) {
            return this.highlightColor;
                        $form.append( $formRow );
        } else if( this.parentColor ) {
                    }
            return this.getParentDrugColor().getHighlightColor();
        }
    };


    mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
                    $formRow = $( '<div>', {
        if( this.primaryColor ) {
                        class: 'form-row calculator-inputGroup'
            return this.primaryColor;
                    } );
        } else if( this.parentColor ) {
                }
            return this.getParentDrugColor().getPrimaryColor();
        }
    };


    mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() {
                $formRow.append( mw.calculators.variables[ variableId ].createInput( inputOptions ) );
        if( this.striped !== null ) {
            return this.striped;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().isStriped();
        }
    };


                inputCount++;
            }


            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;
            }
        },
        getOptionValue: function( optionId ) {
            return mw.calculators.options.hasOwnProperty( optionId ) ?
                mw.calculators.options[ optionId ] :
                undefined;
        },
        getUnitsByBase: function( value ) {
            if( typeof value !== 'object' || !value.hasOwnProperty( 'units' ) ) {
                return null;
            }


    /**
            var unitsByBase = {};
    * DrugPopulation
 
    */
            for( var iUnits in value.units ) {
                var units = value.units[ iUnits ];


    mw.calculators.drugPopulations = {};
                // Some units are of a given dimension, but have no conversion definition
                // (e.g. 'units' for mass, 'vial' for volume, etc.). These units are added
                // by appending '_abstract' to the baseName of the unit definition. However,
                // the calculator should treat them as the same type of unit
                var baseId = units.unit.base.key.toLowerCase().replace( /_\w+/, '' );


    mw.calculators.addDrugPopulations = function( drugPopulationData ) {
                unitsByBase[ baseId ] = units.prefix.name + units.unit.name;
        var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );
            }


         for( var drugPopulationId in drugPopulations ) {
            return unitsByBase;
             mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
         },
        }
        getUnitsString: function( value ) {
    };
             if( typeof value !== 'object' ) {
                return null;
            }
 
            var unitsString = value.formatUnits();
 
            var reDenominator = /\/\s?\((.*)\)/;
            var denominatorMatches = unitsString.match( reDenominator );


    mw.calculators.getDrugPopulation = function( drugPopulationId ) {
            if( denominatorMatches ) {
        if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
                var denominatorUnits = denominatorMatches[ 1 ];
            return mw.calculators.drugPopulations[ drugPopulationId ];
        } else {
            return null;
        }
    };


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


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


    /**
            var unitsBase = value.getBase();
    * Class DrugPopulation
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugPopulation}
    * @constructor
    */
    mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) {
        var properties = {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'variables'
            ]
        };


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            if( unitsBase ) {
                unitsBase = unitsBase.toLowerCase();


        if( this.variables ) {
                if( mw.calculators.unitsBases.hasOwnProperty( unitsBase ) &&
            for( var variableId in this.variables ) {
                    typeof mw.calculators.unitsBases[ unitsBase ].toString === 'function' ) {
                if( !mw.calculators.getVariable( variableId ) ) {
                     unitsString = mw.calculators.unitsBases[ unitsBase ].toString( unitsString );
                     throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
                 }
                 }
            } 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' );
            }


                 this.variables[ variableId ].min = this.variables[ variableId ].hasOwnProperty( 'min' ) ?
            return unitsString;
                    math.unit( this.variables[ variableId ].min ) : null;
        },
        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;


                this.variables[ variableId ].max = this.variables[ variableId ].hasOwnProperty( 'max' ) ?
            return decimals;
                    math.unit( this.variables[ variableId ].max ) : null;
        },
        getValueNumber: function( value, decimals ) {
            if( !mw.calculators.isValueMathObject( value ) ) {
                return null;
             }
             }
        } else {
            this.variables = {};
        }
    };


    mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
            // Remove floating point errors
            var number = math.round( value.toNumber(), 10 );
 
            var absNumber = math.abs( number );
 
            if( absNumber >= 10 || absNumber === 0 ) {
                if( absNumber < 100 && absNumber !== math.round( absNumber ) && 2 * absNumber === math.round( 2 * absNumber ) ) {
                    // Special case to allow nearly-round decimals (e.g. 12.5)
 
                    decimals = 1;
                } else {
                    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() );


    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
                    // There is a bug in mathjs where formatUnits() won't simplify the units, only format() will.
        var inputData = new mw.calculators.objectClasses.CalculationData();
                    var newSIUnit = newSIValue.formatUnits();


        for( var variableId in this.variables ) {
                    if( newSIUnit !== oldSIUnit ) {
            inputData.variables.required.push( variableId );
                        value = math.unit( newSIValue.toNumber() + ' ' + value.formatUnits().replace( oldSIUnit, newSIUnit ) );
        }


        return inputData;
                        valueNumber = mw.calculators.getValueNumber( value, decimals );
    };
                        valueUnits = mw.calculators.getUnitsString( value );
                    }
                }
            }


    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationDataScore = function( dataValues ) {
            var valueString = String( valueNumber );
        // A return value of -1 indicates the data did not match the population definition


        for( var variableId in this.variables ) {
             if( valueUnits ) {
             if( !dataValues.hasOwnProperty( variableId ) ) {
                 valueString += ' ' + valueUnits;
                 return -1;
             }
             }


             if( this.variables[ variableId ].min &&
            var unitsId = value.formatUnits();
                 ( !dataValues[ variableId ] ||
 
                    !math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) ) {
             if( mw.calculators.units.hasOwnProperty( unitsId ) &&
                return -1;
                 typeof mw.calculators.units[ unitsId ].formatValue === 'function' ) {
                valueString = mw.calculators.units[ unitsId ].formatValue( valueString );
             }
             }


             if( this.variables[ variableId ].max &&
            return valueString;
                 ( !dataValues[ variableId ] ||
        },
                    !math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) ) {
        getVariable: function( variableId ) {
                 return -1;
             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() {
            // Change the menu item from "article" to "calculator"
            $( '#nav-article svg' ).addClass( 'fa-calculator' );
            $( '#nav-article .nav-label' ).html( 'Calculator' );
 
            // Wrap description in a collapse
            var descriptionCount = 0;
 
            $( '.calculator-description' ).each( function() {
                var descriptionContainerId = 'calculator-description-info';
 
                if( descriptionCount ) {
                    descriptionContainerId += '-' + descriptionCount;
                }
 
                var $descriptionLinkIcon = $( '<i>', {
                    class: 'far fa-question-circle fa-fw'
                } );
 
                var descriptionLinkString = '';


        // If the data matches the population definition, the score corresponds to the number of variables in the
                descriptionLinkString += $( this ).data( 'title' ) ? $( this ).data( 'title' ) : 'About this calculator';
        // population definition. This should roughly correspond to the specificity of the population.
        return Object.keys( this.variables ).length;
    };


    mw.calculators.objectClasses.DrugPopulation.prototype.toString = function() {
                var $descriptionLinkLabel = $( '<span>', {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
                    html: descriptionLinkString
    };
                } );


                var $descriptionLink = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + descriptionContainerId,
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': descriptionContainerId
                } ).append( $descriptionLinkIcon, $descriptionLinkLabel );


                var $descriptionContainer = $( '<div>', {
                    id: descriptionContainerId,
                    class: 'collapse calculator-description-info',
                    html: $( this ).html()
                } );


    /**
                $( this ).empty();
    * DrugRoute
    */
    mw.calculators.drugRoutes = {};


    mw.calculators.addDrugRoutes = function( drugRouteData ) {
                if( !descriptionCount ) {
        var drugRoutes = mw.calculators.createCalculatorObjects( 'DrugRoute', drugRouteData );
                    $descriptionLink.addClass( 'dropdown-item' );
                    $descriptionLinkLabel.addClass( 'nav-label' );


        for( var drugRouteId in drugRoutes ) {
                    $('#menuButton .dropdown-menu').prepend( $descriptionLink );
            mw.calculators.drugRoutes[ drugRouteId ] = drugRoutes[ drugRouteId ];
                } else {
        }
                    $descriptionLink.addClass( 'btn btn-outline-primary btn-sm' );
    };
                    $( this ).append( $descriptionLink );
                }


    mw.calculators.getDrugRoute = function( drugRouteId ) {
                $( this ).append( $descriptionContainer );
        if( mw.calculators.drugRoutes.hasOwnProperty( drugRouteId ) ) {
            return mw.calculators.drugRoutes[ drugRouteId ];
        } else {
            return null;
        }
    };


    /**
                 descriptionCount++;
    * Class DrugRoute
             } );
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugRoute}
    * @constructor
    */
    mw.calculators.objectClasses.DrugRoute = function( propertyValues ) {
        var properties = {
            required: [
                 'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default'
             ]
        };


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            // Set options
    };
            mw.calculators.setDefaultOptions();


    mw.calculators.objectClasses.DrugRoute.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
            var $optionsElement = $( mw.calculators.selectors.calculatorOptions );
            if( $optionsElement.length ) {
                $.each( $optionsElement.data(), function( optionId, value ) {
                    mw.calculators.setOptionValue( optionId, value );
                } );
            }


    mw.calculators.objectClasses.DrugRoute.prototype.toString = function() {
            mw.hook( 'calculators.initialized' ).fire();
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
        },
    };
        isMobile: function() {
            return window.matchMedia( 'only screen and (max-width: 760px)' ).matches;
        },
        isValueMathObject: function( value ) {
            return value && value.hasOwnProperty( 'value' );
        },
        prepareReferences: function( references ) {
            for( var iReference in references ) {
                var reference = references[ iReference ];


                // http(s)
                reference = reference.replace(
                    /(https?:\/\/[^\s]*)/gmi,
                    '<a href="$1" target="_blank">$1</a>'
                );


                // doi
                reference = reference.replace(
                    /doi: ([\w\d\.\/-]+)((\.\s)|$)/gmi,
                    'doi: <a href="https://doi.org/$1" target="_blank">$1</a>$2'
                );


                // PMCID
                reference = reference.replace(
                    /PMCID: PMC(\d+)/gmi,
                    'PMCID: <a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC$1/" target="_blank">PMC$1</a>'
                );


                // PMID
                reference = reference.replace(
                    /PMID: (\d+)/gmi,
                    'PMID: <a href="https://pubmed.ncbi.nlm.nih.gov/$1" target="_blank">$1</a>'
                );


                references[ iReference ] = reference;
            }


            return references;
        },
        setCookieValue: function( variableId, value ) {
            mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                expires: COOKIE_EXPIRATION
            } );
        },
        setDefaultOptions: function() {
            mw.calculators.setOptionValue( 'inputgroupmaxinputsperrow', 3 );
        },
        setOptionValue: function( optionId, value ) {
            mw.calculators.options[ optionId ] = value;


    /**
            return true;
    * DrugIndication
        },
    */
        setValue: function( variableId, value ) {
    mw.calculators.drugIndications = {};
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return false;
            }


    mw.calculators.addDrugIndications = function( drugIndicationData ) {
            if( !mw.calculators.variables[ variableId ].setValue( value ) ) {
        var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );
                return false;
            }


        for( var drugIndicationId in drugIndications ) {
             mw.calculators.setCookieValue( variableId, value );
             mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
        }
    };


    mw.calculators.getDrugIndication = function( drugIndicationId ) {
             return true;
        if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
         },
             return mw.calculators.drugIndications[ drugIndicationId ];
        uniqueValues: function( value, index, self ) {
         } else {
             return self.indexOf( value ) === index;
             return null;
         }
         }
     };
     };


     /**
     /**
     * Class DrugIndication
     * Class CalculatorObject
    *
    * @param {Object} properties
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugIndication}
     * @returns {mw.calculators.objectClasses.CalculatorObject}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
     mw.calculators.objectClasses.CalculatorObject = function( properties, propertyValues ) {
         var properties = {
         propertyValues = propertyValues ? propertyValues : {};
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default'
            ]
        };


         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         if( properties ) {
    };
            if( properties.hasOwnProperty( 'required' ) ) {
                for( var iRequiredProperty in properties.required ) {
                    var requiredProperty = properties.required[ iRequiredProperty ];


    mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                    if( !propertyValues || !propertyValues.hasOwnProperty( requiredProperty ) ) {
                        console.error( 'Missing required property "' + requiredProperty + '"' );
                        console.log( propertyValues );


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


                    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 ];
    * Drug
                    } else if( typeof this[ optionalProperty ] === 'undefined' ) {
    */
                        this[ optionalProperty ] = null;
    mw.calculators.drugs = {};
                    }
                }
            }


    mw.calculators.addDrugs = function( drugData ) {
            var invalidProperties = Object.keys( propertyValues );
        var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );


        for( var drugId in drugs ) {
            if( invalidProperties.length ) {
            mw.calculators.drugs[ drugId ] = drugs[ drugId ];
                console.warn( 'Unsupported properties defined for ' + typeof this + ' with id "' + this.id + '": ' + invalidProperties.join( ', ' ) );
            }
         }
         }
     };
     };


     mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
     mw.calculators.objectClasses.CalculatorObject.prototype.getProperties = function() {
         var drug = mw.calculators.getDrug( drugId );
         return {
            required: [],
            optional: []
        };
    };


         if( !drug ) {
    mw.calculators.objectClasses.CalculatorObject.prototype.mergeProperties = function( inheritedProperties, properties ) {
             throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
         var uniqueValues = function( value, index, self ) {
         }
             return self.indexOf( value ) === index;
         };


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


         var calculationId = 'drugDosage-' + drugId;
         return properties;
        var calculation = mw.calculators.getCalculation( calculationId );
    };


        if( !calculation ) {
            var calculationData = {};


            calculationData[ calculationId ] = {
                calculate: mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate,
                drug: drugId,
                type: 'drug'
            };


            mw.calculators.addCalculations( calculationData, 'DrugDosageCalculation' );


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


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


     mw.calculators.getDrug = function( drugId ) {
     mw.calculators.objectClasses.UnitsBase.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
        if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
 
            return mw.calculators.drugs[ drugId ];
        } else {
            return null;
        }
    };






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


         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    };
    mw.calculators.objectClasses.Units.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );


        if( !this.color ) {
            this.color = DEFAULT_DRUG_COLOR;
        }


        var color = mw.calculators.getDrugColor( this.color );


         if( !color ) {
    /**
             throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' );
    * 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 + '"' );
         }
         }


         this.color = color;
         // 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 = {};


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


    mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                options[ option ] = option;
            }


    mw.calculators.objectClasses.Drug.prototype.addDosages = function( drugDosageData ) {
            this.options = options;
         var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
         }


         for( var dosageId in dosages ) {
         this.calculations = [];
            dosages[ dosageId ].id = this.dosages.length;


             this.dosages.push( dosages[ dosageId ] );
        if( this.defaultValue ) {
             this.defaultValue = this.prepareValue( this.defaultValue );
         }
         }
    };


    mw.calculators.objectClasses.Drug.prototype.getIndications = function() {
        if( this.minValue ) {
         var indications = [];
            this.minValue = this.prepareValue( this.minValue );
         }


         for( var iDosage in this.dosages ) {
         if( this.maxValue ) {
             if( this.dosages[ iDosage ].indication ) {
             this.maxValue = this.prepareValue( this.maxValue );
                indications.push( this.dosages[ iDosage ].indication );
            }
         }
         }


         return indications.filter( mw.calculators.uniqueValues );
         this.message = null;
        this.valid = true;
 
        this.isValueSet = false;
        this.value = null;
     };
     };


     mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) {
     mw.calculators.objectClasses.Variable.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
        var populations = [];


        for( var iDosage in this.dosages ) {
    mw.calculators.objectClasses.Variable.prototype.addCalculation = function( calculationId ) {
            if( this.dosages[ iDosage ].population &&
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
            return;
                populations.push( this.dosages[ iDosage ].population );
            }
         }
         }


         return populations.filter( mw.calculators.uniqueValues );
         this.calculations.push( calculationId );
     };
     };


     mw.calculators.objectClasses.Drug.prototype.getRoutes = function( indicationId ) {
     mw.calculators.objectClasses.Variable.prototype.createInput = function( inputOptions ) {
         var routes = [];
         if( !inputOptions ) {
            inputOptions = {};
        }


         for( var iDosage in this.dosages ) {
         inputOptions.class = inputOptions.hasOwnProperty( 'class' ) ? inputOptions.class : '';
            if( this.dosages[ iDosage ].route &&
        inputOptions.global = inputOptions.hasOwnProperty( 'class' ) ? inputOptions.global : false;
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
        inputOptions.hideLabel = inputOptions.hasOwnProperty( 'hideLabel' ) ? inputOptions.hideLabel : false;
                 routes.push( this.dosages[ iDosage ].route );
        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;
 
        // If not creating a global input, assign an iterated id
        if( !inputOptions.global ) {
            var inputIdCount = 0;
 
            while( $( '#' + inputId + '-' + inputIdCount ).length ) {
                 inputIdCount++;
             }
             }
            inputId += '-' + inputIdCount;
         }
         }


         return routes.filter( mw.calculators.uniqueValues );
         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 = {};


    mw.calculators.objectClasses.Drug.prototype.getPreparations = function() {
        // Initialize label attributes
        return this.preparations.filter( mw.calculators.uniqueValues );
        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;';
    * DrugPreparation
             labelCss[ 'margin-bottom' ] = 0;
    */
    mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
        if( !mw.calculators.getDrug( drugId ) ) {
             throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
         }
         }


         for( var drugPreparationId in drugPreparationData ) {
         // Create the input container
             drugPreparationData[ drugPreparationId ].drug = drugId;
        var $inputContainer = $( inputContainerTag, inputContainerAttributes ).css( inputContainerCss );
        }
 
        var $label = $( '<label>', labelAttributes ).css( labelCss );
 
        $inputContainer.append( $label );
 
        // 'this' will be redefined for event handlers
        var variable = this;
        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 form-control-sm calculator-input calculator-input-text',
                type: 'text',
                autocomplete: 'off',
                inputmode: 'decimal',
                value: inputValue
            };
 
            // Configure additional options
            if( this.maxLength ) {
                inputAttributes.maxlength = this.maxLength;
            }


        var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData );
            // Add any additional classes to the input
            inputAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';


        for( var drugPreparationId in drugPreparations ) {
            // Add the input id to the list of classes
             mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
             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 = {
    * Class DrugPreparation
                    id: unitsId
    * @param {Object} propertyValues
                 };
    * @returns {mw.calculators.objectClasses.DrugPreparation}
    * @constructor
    */
    mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
        var properties = {
            required: [
                'drug',
                'id',
                'concentration'
            ],
            optional: [
                 'default',
                'dilutionRequired',
                'commonDilution'
            ]
        };


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
                // 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 ];


        this.concentration = this.concentration.replace( 'mcg', 'ug' );
                    $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 custom-select-sm calculator-input-select';


        this.concentration = math.unit( this.concentration );
                    // Add any additional classes to the input
    };
                    unitsInputAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';


    mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                    unitsInputAttributes.class = unitsInputAttributes.class + ' ' + unitsId;


    mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() {
                    var $unitsInput = $( '<select>', unitsInputAttributes )
        // The units of concentration will always be of the form "mass / volume"
                        .on( 'change', function() {
        // The regular expression matches all text leading up to the volume units
                            var numberValue = $( '#' + inputId ).val();
        return mw.calculators.getUnitsByBase( this.concentration ).volume;
    };


    mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() {
                            var newValue = numberValue ? numberValue + ' ' + $( this ).val() : null;
        return mw.calculators.getValueString( this.concentration );
    };


                            if( !mw.calculators.setValue( variableId, newValue ) ) {
                                if( variable.message ) {
                                    $( this ).parent().parent().parent().find( '.invalid-feedback' ).html( variable.message );
                                }


                                $( this ).parent().parent().addClass( 'is-invalid' );
                            } else {
                                $( this ).parent().parent().removeClass( 'is-invalid' );
                            }
                        } );


                    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 ) {
    * Class DrugDosage
                            unitsOptionAttributes.selected = true;
    * @param {Object} propertyValues
                        }
    * @returns {mw.calculators.objectClasses.DrugDosage}
    * @constructor
    */
    mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );


        var drugIndication = mw.calculators.getDrugIndication( this.indication );
                        $unitsInput.append( $( '<option>', unitsOptionAttributes ) );
                    }


        if( !drugIndication ) {
                    $unitsContainer.append( $unitsInput );
             throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
                }
        }
             }


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


        this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION;
                    var newValue = numberValue ? numberValue : null;


        var drugPopulation = mw.calculators.getDrugPopulation( this.population );
                    if( newValue && unitsId ) {
                        newValue = newValue + ' ' + $( '#' + unitsId ).val();
                    }


        if( !drugPopulation ) {
                    if( !mw.calculators.setValue( variableId, newValue ) ) {
            throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
                        if( variable.message ) {
        }
                            $( this ).parent().parent().find( '.invalid-feedback' ).html( variable.message );
                        }


        this.population = drugPopulation;
                        $( this ).parent().addClass( 'is-invalid' );
                    } else {
                        $( this ).parent().removeClass( 'is-invalid' );
                    }
                } );


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


        var drugRoute = mw.calculators.getDrugRoute( this.route );
            if( $unitsContainer ) {
                $inputGroup.append( $unitsContainer );
            }


         if( !drugRoute ) {
            $inputContainer.append( $inputGroup );
             throw new Error( 'Invalid route "' + this.route + '" for drug dosage' );
         } else if( this.type === TYPE_STRING ) {
        }
             if( this.hasOptions() ) {
                var optionKeys = Object.keys( this.options );


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


        // Add the dose objects to the drug
                    // Add any additional classes to the input
        var drugDoseData = this.dose;
                    selectAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';
        this.dose = [];


        this.addDoses( drugDoseData );
                    var $select = $( '<select>', selectAttributes )
    };
                        .on( 'change', function() {
                            if( !mw.calculators.setValue( variableId, $( this ).val() ) ) {
                                if( variable.message ) {
                                    $( this ).parent().parent().find( '.invalid-feedback' ).html( variable.message );
                                }


    mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                                $( this ).parent().addClass( 'is-invalid' );
                            } else {
                                $( this ).parent().removeClass( 'is-invalid' );
                            }


    mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
                        } );
        // Each dosage can have one or more associated doses. Ensure this value is an array.
        if( !Array.isArray( drugDoseData ) ) {
            drugDoseData = [ drugDoseData ];
        }


        var doses = mw.calculators.createCalculatorObjects( 'DrugDose', drugDoseData );
                    for( var optionId in this.options ) {
                        var displayText = this.options[ optionId ];


        for( var doseId in doses ) {
                        var optionAttributes = {
            doses[ doseId ].id = this.dose.length;
                            value: optionId,
                            text: displayText
                        };


            this.dose.push( doses[ doseId ] );
                        if( optionId == value ) {
        }
                            optionAttributes.selected = true;
    };
                        }


    mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() {
                        $select.append( $( '<option>', optionAttributes ) );
        var inputData = new mw.calculators.objectClasses.CalculationData();
                    }


        inputData = inputData.merge( this.population.getCalculationData() );
                    $inputContainer.append( $select );
                }
            }
        }


         for( var iDose in this.dose ) {
         if( $inputContainer.length ) {
             inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
             $inputContainer.append( $( '<div>', {
                class: 'invalid-feedback'
            } ) );
         }
         }


         return inputData;
         return $inputContainer;
     };
     };


     mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() {
     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 {
         return {
             required: [
             required: [
                'dose',
                 'id',
                 'id',
                 'indication'
                 'name',
                'type'
             ],
             ],
             optional: [
             optional: [
                 'description',
                 'abbreviation',
                 'population',
                 'defaultValue',
                 'route'
                 'maxLength',
                'maxValue',
                'minValue',
                'options',
                'units'
             ]
             ]
         };
         };
     }
     };


    mw.calculators.objectClasses.Variable.prototype.getValue = function() {
        if( !this.valid ) {
            return null;
        } else if( this.value !== null ) {
            return this.value;
        } else if( !this.isValueSet && 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() {
    * Class DrugDose
         var value = this.getValue();
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugDose}
    * @constructor
    */
    mw.calculators.objectClasses.DrugDose = function( propertyValues ) {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );


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


         for( var iMathProperty in mathProperties ) {
         return true;
            var mathProperty = mathProperties[ iMathProperty ];
    };


            if( this[ mathProperty ] ) {
    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
                // TODO consider making a UnitsBase.weight.fromString()
        return mw.calculators.isValueMathObject( this.value );
                this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' );
    };
                this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' );


                 this[ mathProperty ] = math.unit( this[ mathProperty ] );
    mw.calculators.objectClasses.Variable.prototype.prepareValue = function( value ) {
            } else {
        if( value !== null ) {
                this[ mathProperty ] = null;
            if( this.type === TYPE_NUMBER ) {
                 if( !mw.calculators.isValueMathObject( value ) ) {
                    value = math.unit( value );
                }
             }
             }
         }
         }


         if( this.weightCalculation ) {
         return value;
            var weightCalculation = mw.calculators.getCalculation( this.weightCalculation );
    };
 
    mw.calculators.objectClasses.Variable.prototype.setValue = function( value ) {
        // Set flag to prevent returning defaultValue in getValue()
        this.isValueSet = true;
 
        var validateResult = this.validateValue( value );
 
        this.valid = !!validateResult.valid;
        this.message = validateResult.message;


            if( !weightCalculation ) {
        if( !this.valid ) {
                throw new Error( 'Drug dose references weight calculation "' + this.weightCalculation + '" which is not defined' );
            this.value = null;
             }
             this.valueUpdated();


             this.weightCalculation = weightCalculation;
             return false;
         }
         }
        this.value = this.prepareValue( value );
        this.valueUpdated();
        return true;
     };
     };


     mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     mw.calculators.objectClasses.Variable.prototype.toString = function() {
        return this.getLabelString();
    };


     mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
     mw.calculators.objectClasses.Variable.prototype.validateValue = function( value ) {
         var calculationData = new mw.calculators.objectClasses.CalculationData();
         // Initialize valid flag to true. Will be set false if an error is found.
        result = {
            message: null,
            valid: true
        };


         var mathProperties = this.getMathProperties();
         // (At least for now) unsetting a variable is always valid
        if( value === null ) {
            return result;
        }


         // Look at dose properties to identify any variable dependence (e.g. weight-dependence)
         // Some errors which are plausibly from normal user input we will show as feedback on the input (e.g.
         for( var iMathProperty in mathProperties ) {
         // a numeric value that is below the minimum value. Errors which are unlikely to be from user input
            var mathProperty = mathProperties[ iMathProperty ];
        // and instead relate to developer issues (e.g. incorrect units in select boxes), only show on the console.
        var consoleWarnPrefix = 'Could not set value "' + value + '" for "' + this.id + '":';


             var dosePropertyValue = this[ mathProperty ];
        if( this.type === TYPE_NUMBER ) {
             if( !mw.calculators.isValueMathObject( value ) ) {
                value = math.unit( value );
            }


             // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
             var valueUnits;
            if( mw.calculators.isValueDependent( dosePropertyValue, 'weight' ) &&
 
                 calculationData.variables.optional.indexOf( 'weight' ) === -1 ) {
            if( this.hasUnits() ) {
                calculationData.variables.optional.push( 'weight' );
                valueUnits = value.formatUnits().replace( /\s/g, '' );
 
                if( !valueUnits ) {
                    // Unlikely to be a user error, so don't set message.
                    result.valid = false;
 
                    console.warn( consoleWarnPrefix + 'Value must define units' );
                 } else if( this.units.indexOf( valueUnits ) === -1 ) {
                    // Unlikely to be a user error, so don't set message.
                    result.valid = false;
 
                    console.warn( consoleWarnPrefix + 'Units "' + valueUnits + '" are not valid for this variable' );
                }
             }
             }
        }


         if( this.weightCalculation ) {
            if( this.minValue && math.smaller( value, this.minValue ) ) {
             calculationData.calculations.optional.push( this.weightCalculation.id );
                var minValueString = mw.calculators.getValueString( this.minValue );
 
                if( valueUnits && valueUnits != this.minValue.formatUnits() ) {
                    minValueString += ' (' + mw.calculators.getValueString( this.minValue.to( valueUnits ) ) + ')';
                }
 
                result.message = String( this ) + ' must be at least ' + minValueString;
                result.valid = false;
            } else if( this.maxValue && math.larger( value, this.maxValue ) ) {
                var maxValueString = mw.calculators.getValueString( this.maxValue );
 
                if( valueUnits && valueUnits != this.maxValue.formatUnits() ) {
                    maxValueString += ' (' + mw.calculators.getValueString( this.maxValue.to( valueUnits ) ) + ')';
                }
 
                result.message = String( this ) + ' must be less than ' + maxValueString;
                result.valid = false;
            }
         } else if( this.hasOptions() ) {
             if( !this.options.hasOwnProperty( value ) ) {
                // Unlikely to be a user error, so don't set message
                result.valid = false;
 
                console.warn( consoleWarnPrefix + 'Value must be one of: ' + Object.keys( this.options ).join( ', ' ) );
            }
         }
         }


         return calculationData;
         return result;
     };
     };


     mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
     mw.calculators.objectClasses.Variable.prototype.valueUpdated = function() {
         return [
         for( var iCalculation in this.calculations ) {
             'dose',
             var calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
            'min',
            'max',
            'absoluteMin',
            'absoluteMax'
        ];
    };


    mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
            if( calculation ) {
        return {
                 calculation.update();
            required: [
             }
                 'id'
         }
             ],
            optional: [
                'absoluteMin',
                'absoluteMax',
                'dose',
                'frequency',
                'min',
                'max',
                'name',
                'weightCalculation'
            ]
         };
     };
     };






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


Line 757: Line 1,172:
     };
     };


     mw.calculators.objectClasses.DrugDosageCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
     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() {
        throw new Error( 'AbstractCalculation child class "' + this.getClassName() + '" must implement doRender()' );
    };


     mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate = function( data ) {
     mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationData = function() {
         var value = {
         return this.data;
            population: null,
    };
            preparation: data.preparation,
            dose: []
        };


        // Determine which dosage to use
    mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues = function() {
         var populationScores = [];
         var calculationData = this.getCalculationData();


         for( var iDosage in data.drug.dosages ) {
         var missingRequiredData = this.getMissingRequiredData();
            var drugDosage = data.drug.dosages[ iDosage ];


            // If the indication and route do not match, set the score to -1
        if( missingRequiredData.length ) {
             var populationScore =
             this.message = missingRequiredData.join( ', ' ) + ' required';
                drugDosage.indication.id === data.indication.id && drugDosage.route.id === data.route.id ?
                drugDosage.population.getCalculationDataScore( data ) : -1;


             populationScores.push( populationScore );
             return false;
         }
         }


         var maxPopulationScore = Math.max.apply( null, populationScores );
         var data = {};


         if( maxPopulationScore < 0 ) {
         var calculationId, calculation, variableId, variable;
            return value;
        }


         // If there is more than one dosage with the same score, take the first.
         var calculations = calculationData.calculations.required.concat( calculationData.calculations.optional );
        // This allows the data editor to decide which is most important.
        var dosageId = populationScores.indexOf( maxPopulationScore );


         var dosage = data.drug.dosages[ dosageId ];
         for( var iRequiredCalculation in calculations ) {
            calculationId = calculations[ iRequiredCalculation ];
            calculation = mw.calculators.getCalculation( calculationId );


        value.population = dosage.population;
            // We shouldn't use getValue() since that triggers recalculate() which would cause an infinite loop
            data[ calculationId ] = calculation.value;
        }


         // A dosage may contain multiple doses (e.g. induction and maintenance)
         var variables = calculationData.variables.required.concat( calculationData.variables.optional );
        for( var iDose in dosage.dose ) {
            var dose = dosage.dose[ iDose ];
            var mathProperties = dose.getMathProperties();


            // Initialize value properties for dose
        for( var iRequiredVariable in variables ) {
             value.dose[ iDose ] = {
             variableId = variables[ iRequiredVariable ];
                massPerWeight: {
            variable = mw.calculators.getVariable( variableId );
                    decimals: 0
 
                },
            data[ variableId ] = variable.getValue();
                mass: {
        }
                    decimals: 0
                },
                name: dose.name,
                volume: {
                    decimals: 0
                },
                weightCalculation: dose.weightCalculation ? dose.weightCalculation : null
            };


            var weightValue = dose.weightCalculation ? dose.weightCalculation.value : data.weight;
        return data;
console.log(data);
    };
console.log(dose.weightCalculation);
console.log( weightValue );
            var massUnits;
            var volumeUnits;


            for( var iMathProperty in mathProperties ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getClassName = function() {
                var mathProperty = mathProperties[ iMathProperty ];
        throw new Error( 'AbstractCalculation child class must implement getClassName()' );
    };


                var doseValue = dose[ mathProperty ];
    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerClasses = function() {
        return this.getElementClasses();
    };


                if( doseValue ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerId = function() {
                    var doseValueDecimals = mw.calculators.getValueDecimals( doseValue );
        return this.getElementPrefix() + '-' + this.id;
                    var doseUnitsByBase = mw.calculators.getUnitsByBase( doseValue );
    };


                    if( doseUnitsByBase.hasOwnProperty( 'weight' ) ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getDescription = function() {
                        value.dose[ iDose ].massPerWeight[ mathProperty ] = doseValue;
        return this.description;
    };


                        // Set the decimals based upon the specified dose
    mw.calculators.objectClasses.AbstractCalculation.prototype.getElementPrefix = function( useClassName ) {
                        value.dose[ iDose ].massPerWeight.decimals = math.max(
        var elementPrefix = 'calculator-';
                            doseValueDecimals,
                            value.dose[ iDose ].massPerWeight.decimals
                        );


                        if( weightValue ) {
        elementPrefix += useClassName ? this.getClassName() : 'calculation';
                            massUnits = doseUnitsByBase.mass;


                            if( doseUnitsByBase.hasOwnProperty( 'time' ) ) {
        return elementPrefix;
                                massUnits += '/' + doseUnitsByBase.time;
    };
                            }


                            // For whatever reason math.format will simplify the units, but math.formatUnits will not
    mw.calculators.objectClasses.AbstractCalculation.prototype.getElementClasses = function( elementId ) {
                            // as a hack, we recreate a new unit value with the correct formatting of the result
        elementId = elementId ? '-' + elementId : '';
                            value.dose[ iDose ].mass[ mathProperty ] = math.unit( math.multiply( doseValue, weightValue ).format() ).to( massUnits );


                            // Mass should use one fewer decimals than massPerWeight (unless already 0)
        return this.getElementPrefix() + elementId + ' ' +
                            value.dose[ iDose ].mass.decimals = math.max(
            this.getElementPrefix( true ) + elementId + ' ' +
                                math.max( doseValueDecimals - 1, 0 ),
            this.getContainerId() + elementId;
                                value.dose[ iDose ].mass.decimals
    };
                            );
                        }
                    } else {
                        value.dose[ iDose ].mass[ mathProperty ] = doseValue;


                        // Set the decimals based upon the specified dose
    mw.calculators.objectClasses.AbstractCalculation.prototype.getFormula = function() {
                        value.dose[ iDose ].mass.decimals = math.max(
        return this.formula;
                            doseValueDecimals,
    };
                            value.dose[ iDose ].mass.decimals
                        );
                    }


                    if( data.preparation && value.dose[ iDose ].mass[ mathProperty ] ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getInfo = function( infoCount ) {
                        // Same hack as above to get units to simplify correctly
        var infoHtml = '';
                        var preparationUnitsByBase = mw.calculators.getUnitsByBase( data.preparation.concentration );


                        volumeUnits = preparationUnitsByBase.volume;
        var description = this.getDescription();


                        if( doseUnitsByBase.hasOwnProperty( 'time' ) ) {
        if( description ) {
                            volumeUnits += '/' + doseUnitsByBase.time;
            infoHtml += $( '<p>', {
                        }
                html: description
            } )[ 0 ].outerHTML;
        }


                        value.dose[ iDose ].volume[ mathProperty ] = math.unit( math.multiply( value.dose[ iDose ].mass[ mathProperty ], math.divide( 1, data.preparation.concentration ) ).format() ).to( volumeUnits );
        var formula = this.getFormula();


                        // value.dose[ iDose ].volume.decimals = 0;
        if( formula ) {
                    }
            infoHtml += $( '<div>', {
                 }
                 class: this.getElementClasses( 'formula' )
             }
             } )[ 0 ].outerHTML;
         }
         }


         return value;
         var references = this.getReferences();
    };
 
        if( references.length ) {
            var $references = $( '<ol>' );


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.doRender = function() {
            for( var iReference in references ) {
        var $calculationContainer = $( '.' + this.getContainerClass() );
                $references.append( $( '<li>', {
                    html: references[ iReference ]
                } ) );
            }


        if( !$calculationContainer.length ) {
            infoHtml += $( '<div>', {
             return;
                class: this.getElementClasses( 'references' )
             } ).append( $references )[ 0 ].outerHTML;
         }
         }


         $calculationContainer.empty();
         var infoContainerId = this.getContainerId() + '-info';


         // Label column
         if( infoCount ) {
        var labelAttributes = {};
            infoContainerId += '-' + infoCount;
         var labelCss = {};
         }


         if( this.drug.color.isStriped() ) {
         $infoContainer = $( '<div>', {
             labelCss[ 'background' ] = 'repeating-linear-gradient(135deg,rgba(0,0,0,0),rgba(0,0,0,0)10px,rgba(255,255,255,1)10px,rgba(255,255,255,1)20px),' + this.drug.color.getPrimaryColor();
             id: infoContainerId,
         } else {
            class: 'collapse row no-gutters border-top ' + this.getElementClasses( 'info' )
            labelCss[ 'background-color'] = this.drug.color.getPrimaryColor();
         } ).append( infoHtml );
        }


         var $label = $( '<th>', labelAttributes ).css( labelCss );
         return $infoContainer;
    };


        $label.append( this.getLabelHtml() );
    mw.calculators.objectClasses.AbstractCalculation.prototype.getInfoButton = function( infoCount ) {
        var infoContainerId = this.getContainerId() + '-info';


         var $infoButton = null;
         if( infoCount ) {
            infoContainerId += '-' + infoCount;
        }


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


            $label
    mw.calculators.objectClasses.AbstractCalculation.prototype.getMissingRequiredData = function() {
                .append( $( '<span>', {
        var calculationData = this.getCalculationData();
                    class: 'calculator-calculation-column-label-info'
                } )
                    .append( $infoButton )
                );
        }


         // Indication column
         var missingRequiredData = [];
         var $indication = $( '<td>' );
         var calculation, variable;


         var indications = this.drug.getIndications();
         for( var iRequiredCalculation in calculationData.calculations.required ) {
            calculation = mw.calculators.getCalculation( calculationData.calculations.required[ iRequiredCalculation ] );


        if( indications.length > 1 ) {
            if( !calculation.hasValue() ) {
            $indication.append( mw.calculators.getVariable( this.getVariableIds().indication  ).createInput({
                missingRequiredData = missingRequiredData.concat( calculation.getMissingRequiredData() );
                hideLabel: true
             }
             } ) );
        } else {
            $indication.append( String( indications[ 0 ] ) );
         }
         }


         var routes = this.drug.getRoutes();
         for( var iRequiredVariable in calculationData.variables.required ) {
            variable = mw.calculators.getVariable( calculationData.variables.required[ iRequiredVariable ] );


        if( routes.length > 1 ) {
            if( !variable.hasValue() ) {
            $indication.append( mw.calculators.getVariable( this.getVariableIds().route  ).createInput({
                 missingRequiredData.push( String( variable ) );
                 inline: true
            }
            } ) );
        } else {
            $indication.append( String( routes[ 0 ] ) );
         }
         }


         // Dosage column
         return missingRequiredData.filter( mw.calculators.uniqueValues );
        var $dosage = $( '<td>' );
    };


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


        var preparations = this.drug.getPreparations();
    mw.calculators.objectClasses.AbstractCalculation.prototype.getReferences = function() {
        var $preparationSelect;
        return this.references;
    };


        if( preparations.length > 1 ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getSearchString = function() {
            $preparationSelect = mw.calculators.getVariable( this.getVariableIds().preparation  ).createInput({
        var searchString = this.id;
                hideLabel: true,
 
                inline: true,
        searchString += this.searchData ? ' ' + this.searchData : '';
                inputClass: 'calculator-input-preparation'
            } );
        } else if( preparations.length === 1 ) {
            $preparationSelect = String( preparations[ 0 ] );
        }


         if( this.value.population && this.value.population.id !== DEFAULT_DRUG_POPULATION ) {
         return searchString.trim();
            $dosage.append( String( this.value.population ) + '<br />' );
    };
        }


        for( var iDose in this.value.dose ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getTitleHtml = function() {
            var doseValue = this.value.dose[ iDose ];
        return this.getTitleString();
    };


            if( doseValue.name ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.getTitleString = function() {
                $dosage.append( doseValue.name + '<br />' );
        return this.id;
            }
    };


            var massPerWeightHtml = '';
    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();


            if( doseValue.massPerWeight.hasOwnProperty( 'dose' ) ) {
        return this.value;
                massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.dose, doseValue.massPerWeight.decimals );
    };
            } else if( doseValue.massPerWeight.hasOwnProperty( 'min' ) &&
                doseValue.massPerWeight.hasOwnProperty( 'max' ) ) {
                massPerWeightHtml += mw.calculators.getValueNumber( doseValue.massPerWeight.min, doseValue.massPerWeight.decimals );
                massPerWeightHtml += dash;
                massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.max, doseValue.massPerWeight.decimals );
            }


            if( massPerWeightHtml ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.hasInfo = function() {
                if( doseValue.weightCalculation ) {
        return this.getDescription() || this.getFormula() || this.getReferences().length;
                    massPerWeightHtml += ' (' + doseValue.weightCalculation.getLabelString() + ')';
    };
                }


                $dosage.append( massPerWeightHtml + '<br />' );
    mw.calculators.objectClasses.AbstractCalculation.prototype.hasValue = function() {
            }
        if( this.value === null ||
            ( this.isValueMathObject() && !this.value.toNumber() ) ) {
            return false;
        }


            var massHtml = '';
        return true;
    };


            if( doseValue.mass.hasOwnProperty( 'dose' ) ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
                massHtml += mw.calculators.getValueString( doseValue.mass.dose, doseValue.mass.decimals );
        if( typeof this.calculate !== 'function' ) {
            } else if( doseValue.mass.hasOwnProperty( 'min' ) &&
            throw new Error( 'calculate() must be a function for Calculation "' + this.id + '"' );
                doseValue.mass.hasOwnProperty( 'max' ) ) {
        }
                massHtml += mw.calculators.getValueNumber( doseValue.mass.min, doseValue.mass.decimals );
                massHtml += dash;
                massHtml += mw.calculators.getValueString( doseValue.mass.max, doseValue.mass.decimals );
            }


            if( massHtml ) {
        // Initialize array to store calculation ids which depend on this calculation's value
                $dosage.append( massHtml + '<br />' );
        this.calculations = [];
            }


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


            if( doseValue.volume.hasOwnProperty( 'dose' ) ) {
        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
                volumeHtml += mw.calculators.getValueString( doseValue.volume.dose, doseValue.volume.decimals );
            } else if( doseValue.volume.hasOwnProperty( 'min' ) &&
                doseValue.volume.hasOwnProperty( 'max' ) ) {
                volumeHtml += mw.calculators.getValueNumber( doseValue.volume.min, doseValue.volume.decimals );
                volumeHtml += dash;
                volumeHtml += mw.calculators.getValueString( doseValue.volume.max, doseValue.volume.decimals );
            }


            if( volumeHtml ) {
        this.type = this.type ? this.type : TYPE_NUMBER;
                $dosage.append( volumeHtml );


                if( $preparationSelect ) {
        this.message = null;
                    $dosage.append( ' of ' );
        this.value = null;
                    $dosage.append( $preparationSelect );
                }


                $dosage.append( '<br />' );
        // Remove any placeholder content explicitly set in the markup (used for SEO).
            }
        $( '.' + this.getContainerId() ).empty();
        }
    };


         $calculationContainer
    mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
            .append(
         return mw.calculators.isValueMathObject( this.value );
                $label,
    };
                $indication,
                $dosage
            );


    mw.calculators.objectClasses.AbstractCalculation.prototype.parseFormula = function() {
        var formula = this.getFormula();


         return;
         if( !formula ) {
            return;
        }


        var api = new mw.Api();


        var containerId = this.getContainerId() + '-formula';


         var calculation = this;
         api.parse( formula ).then( function( result ) {
            $( '.' + containerId ).html( result );
        } );
    };


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


        var data = this.getCalculationDataValues();


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


            return false;
        }


             if( calculation.hasInfo() ) {
        try {
                var infoHtml = '';
             var value = this.calculate( data );


                 if( calculation.description ) {
            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                     infoHtml += $( '<p>', {
                 if( this.units ) {
                        html: calculation.description
                     value = value + ' ' + this.units;
                    } )[ 0 ].outerHTML;
                 }
                 }


                 if( calculation.formula ) {
                 this.value = math.unit( value );
                    infoHtml += $( '<span>', {
            } else {
                        class: calculation.getContainerClass() + '-formula'
                this.value = value;
                    } )[ 0 ].outerHTML;
            }
      } catch( e ) {
            console.warn( e.message );


                    var api = new mw.Api();
            this.message = e.message;
            this.value = null;
        } finally {
            this.valueUpdated();
        }


                    api.parse( calculation.formula ).then( function( result ) {
        return true;
                        $( '.' + 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;
    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
                }
        // Need to run rendering in setTimeout to allow browser events to remain responsive
        var calculation = this;


                var infoContainerId = calculation.getContainerClass() + '-info';
        setTimeout( function() {
                 var $infoContainer = $( '#' + infoContainerId );
            if( typeof calculation.onRender === 'function' ) {
                 calculation.onRender();
            }


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


                $infoContainer = $( '<tr>', {
            // Send API queries to parse LaTeX formulas
                    id: infoContainerId,
            calculation.parseFormula();
                    class: 'collapse'
                } )
                    .append( $( '<td>', {
                        colspan: 2
                    } ).append( infoHtml ) );


            mw.track( 'mw.calculators.CalculationRendered' );


                $( this ).after( $infoContainer );
            if( typeof calculation.onRendered === 'function' ) {
                calculation.onRendered();
             }
             }
         } );
         }, 0 );
     };
     };


     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationData = function() {
     mw.calculators.objectClasses.AbstractCalculation.prototype.setDependencies = function() {
         var inputData = new mw.calculators.objectClasses.CalculationData();
        this.data = this.getCalculationData();
 
         var calculationIds = this.data.calculations.required.concat( this.data.calculations.optional );
 
        for( var iCalculationId in calculationIds ) {
            var calculationId = calculationIds[ iCalculationId ];


        // Add variables created by this calculation
            if( !mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
        var variableIds = this.getVariableIds();
                throw new Error('Calculation "' + calculationId + '" does not exist for calculation "' + this.id + '"');
            }


        for( var variableType in variableIds ) {
             mw.calculators.calculations[ calculationId ].addCalculation( this.id );
             inputData.variables.optional.push( variableIds[ variableType ] );
         }
         }


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


         // Data is only actually required if it is required by every dosage for the drug.
         for( var iVariableId in variableIds ) {
        // Data marked as required by an individual dosage that does not appear in every
            var variableId = variableIds[ iVariableId ];
        // dosage will be converted to optional.
        var requiredInputData = new mw.calculators.objectClasses.CalculationData();


        // Need a way to tell the first iteration of the loop to initialize the required variables to a value that
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
        // is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't
                throw new Error('Variable "' + variableId + '" does not exist for calculation "' + this.id + '"');
        // reinitialize).
            }
        var initializeRequiredData = true;


        // Iterate through each dosage to determine variable dependency
             mw.calculators.variables[ variableId ].addCalculation( this.id );
        for( var iDosage in this.drug.dosages ) {
        }
             var dosageInputData = this.drug.dosages[ iDosage ].getCalculationData();


            inputData = inputData.merge( dosageInputData );
        this.recalculate();
    };


            for( var iDataType in dataTypes ) {
    mw.calculators.objectClasses.AbstractCalculation.prototype.toString = function() {
                var dataType = dataTypes[ iDataType ];
        return this.getTitleString();
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.update = function() {
        this.recalculate();
 
        this.render();
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.valueUpdated = function() {
        for( var iCalculation in this.calculations ) {
            calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );


                if( initializeRequiredData ) {
            if( calculation ) {
                    requiredInputData[ dataType ].required = inputData[ dataType ].required;
                 calculation.update();
                 } else {
                    // Data is only truly required if it is required by all dosage calculations, so use array intersection
                    requiredInputData[ dataType ].required = requiredInputData[ dataType ].required.filter( function( index ) {
                        return dosageInputData[ dataType ].required.indexOf( index ) !== -1;
                    } );
                }
             }
             }
        }
    };


            initializeRequiredData = false;
        }


    /**
    * 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 ) {
         for( var iDataType in dataTypes ) {
             var dataType = dataTypes[ iDataType ];
             var dataType = dataTypes[ iDataType ];


             // Move any data marked required in inputData to optional if it not actually required (i.e. doesn't appear
             if( !this[ dataType ] ) {
            // in requiredInputData).
                this[ dataType ] = {
            inputData[ dataType ].optional = inputData[ dataType ].optional.concat( inputData[ dataType ].required.filter( function( index ) {
                    optional: [],
                 return requiredInputData[ dataType ].required.indexOf( index ) === -1;
                    required: []
             } ) ).filter( mw.calculators.uniqueValues );
                 };
             } else {
                // Iterate through the requirement levels (i.e. optional, required) to initialize the structure
                for( var iDataRequirement in dataRequirements ) {
                    var dataRequirement = dataRequirements[ iDataRequirement ];


            inputData[ dataType ].required = requiredInputData[ dataType ].required;
                    // FYI can't check to see if the data actually exists here since it may not be defined yet
                    if( !this[ dataType ].hasOwnProperty( dataRequirement ) ) {
                        this[ dataType ][ dataRequirement ] = [];
                    }
                }
            }
         }
         }
    };


         return inputData;
    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.DrugDosageCalculation.prototype.getCalculationDataValues = function() {
     mw.calculators.objectClasses.CalculationData.prototype.getDataTypes = function() {
         var data = mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues.call( this );
         return [
            'calculations',
            'variables'
        ];
    };


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


        data.indication = data[ this.getVariablePrefix() + 'indication' ] ?
            mw.calculators.getDrugIndication( mw.calculators.getVariable( this.getVariableIds().indication ).value ) :
            null;


        delete data[ this.getVariablePrefix() + 'indication' ];


        data.preparation = data[ this.getVariablePrefix() + 'preparation' ] ?
            this.drug.preparations[ mw.calculators.getVariable( this.getVariableIds().preparation ).value ] :
            null;


         delete data[ this.getVariablePrefix() + 'preparation' ];
    mw.calculators.objectClasses.CalculationData.prototype.merge = function() {
         var mergedData = new mw.calculators.objectClasses.CalculationData();


         data.route = data[ this.getVariablePrefix() + 'route' ] ?
         var data = [ this ].concat( Array.prototype.slice.call( arguments ) );
            mw.calculators.getDrugRoute( mw.calculators.getVariable( this.getVariableIds().route ).value ) :
            null;


         delete data[ this.getVariablePrefix() + 'preparation' ];
         var dataTypes = this.getDataTypes();


         return data;
         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 );


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() {
                mergedData[ dataType ].optional = mergedData[ dataType ].optional
         var labelHtml = this.drug.name;
                    .concat( data[ iData ][ dataType ].optional )
                    .filter( mw.calculators.uniqueValues );
            }
         }


         var $label = $( '<a>', {
         return mergedData;
            href: mw.util.getUrl( this.drug.name ),
    };
            text: labelHtml
        } );


        var highlightColor = this.drug.color.getHighlightColor();


        if( highlightColor ) {
            $label.css( 'background-color', highlightColor );
        }


        labelHtml = $label[ 0 ].outerHTML;


        return labelHtml;
    };


     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getProperties = function() {
     /**
         var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
    * 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 );


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


     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableIds = function() {
     mw.calculators.objectClasses.SimpleCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
        return {
            indication: this.getVariablePrefix() + 'indication',
            preparation: this.getVariablePrefix() + 'preparation',
            route: this.getVariablePrefix() + 'route'
        };
    };


     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariablePrefix = function() {
     mw.calculators.objectClasses.SimpleCalculation.prototype.doRender = function() {
         return this.drug.id + '-';
         var $calculationContainer = $( '.' + this.getContainerId() );
    }


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() {
        if( !$calculationContainer.length ) {
         mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
            return;
         }


         var drug = mw.calculators.getDrug( this.drug );
         // Add all required classes
        $calculationContainer.addClass( 'row no-gutters border ' + this.getContainerClasses() );


         if( !drug ) {
         // Add search phrases
            throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' );
        $calculationContainer.attr( 'data-search', this.getSearchString() );
        }


         this.drug = drug;
         // Get a string version of the calculation's value
        var valueString = this.getValueString();


         var variableIds = this.getVariableIds();
        // We will need to show variable inputs for non-global variable inputs.
        // Global inputs (i.e. those in the header) will claim the DOM id for that variable.
        // Non-global inputs (i.e. specific to a calculation) will only set a class but not the id,
        // and thus will get added to each calculation even if a duplicate.
        // E.g. 2 calculation might use the current hematocrit, but we should show them for both calculations since
        // it wouldn't be obvious the input that only showed the first time would apply to both calculations.
         var inputVariableIds = this.data.variables.required.concat( this.data.variables.optional );
        var missingVariableInputs = [];


         // Create variables for indication, population?, preparation select boxes? Here or m.c.addDosages() or elsewhere?
         for( var iInputVariableId in inputVariableIds ) {
        // Will have to add them to getCalculationData too.
            var variableId = inputVariableIds[ iInputVariableId ];
        var drugVariables = {};


        var indications = this.drug.getIndications();
            if( !$( '#calculator-input-' + variableId ).length ) {
        var indicationOptions = {};
                missingVariableInputs.push( variableId );
         var defaultIndication = 0;
            }
         }


         for( var iIndication in indications ) {
         // Out of 12, uses Bootstrap col- classes in a container
            var indication = indications[ iIndication ];
        var titleColumns = '7';
        var valueColumns = '5';


            defaultIndication = indication.default ? iIndication : defaultIndication;
        // Store this object in a local variable since .each() will reassign this to the DOM object of each
        // calculation container.
        var calculation = this;
        var calculationCount = 0;


             indicationOptions[ indication.id ] = String( indication );
        // Eventually may implement different rendering, so we should regenerate
        }
        // all elements with each iteration of the loop.
        // I.e. might show result in table and inline in 2 different places of article.
        $calculationContainer.each( function() {
            // Initalize the variables for all the elements of the calculation. These need to be in order of placement
             // in the calculation container
            var elementTypes = [
                'title',
                'variables',
                'value',
                'info'
            ];


        drugVariables[ variableIds.indication ] = {
            var elements = {};
            name: 'Indication',
            type: 'string',
            defaultValue: indications.length ? indications[ defaultIndication ].id : null,
            options: indicationOptions
        };


        var preparations = this.drug.getPreparations();
            for( var iElementType in elementTypes ) {
        var preparationOptions = {};
                var elementType = elementTypes[ iElementType ];
        var defaultPreparation = 0;


        for( var iPreparation in preparations ) {
                elements[ elementType ] = {
            var preparation = preparations[ iPreparation ];
                    $container: null,
                    id: calculation.getContainerId() + '-' + elementType
                };


            // We don't want to display any preparations which cannot be directly administered
                if( calculationCount ) {
            if( preparation.dilutionRequired ) {
                    elements[ elementType ].id += '-' + calculationCount;
                 continue;
                 }
             }
             }


             defaultPreparation = preparation.default ? iPreparation : defaultPreparation;
             // Create title element and append to container
            elements.title.$container = $( '<div>', {
                id: elements.title.id
            } );


             preparationOptions[ preparation.id ] = String( preparation );
             elements.title.$container.append( calculation.getTitleHtml() );
        }


        drugVariables[ variableIds.preparation ] = {
            if( calculation.hasInfo() ) {
            name: 'Preparation',
                elements.title.$container.append( calculation.getInfoButton( calculationCount ) );
            type: 'string',
            defaultValue: preparations.length ? preparations[ defaultPreparation ].id : null,
            options: preparationOptions
        };


        var routes = this.drug.getRoutes();
                // Id of the info container should already be set by getInfo()
        var routeOptions = {};
                elements.info.$container = calculation.getInfo();
        var defaultRoute = 0;
            }


        for( var iRoute in routes ) {
            // Create the value element
             var route = routes[ iRoute ];
             elements.value.$container = $( '<div>' ).append( valueString );


             defaultRoute = route.default ? iRoute : defaultRoute;
             if( !missingVariableInputs.length ) {
                // If we have no variable inputs to show, we can put the title and value in one row of the table
                elements.title.$container.addClass( 'col-' + titleColumns + ' border-right' );


            routeOptions[ route.id ] = String( route );
                // Add the id attribute to the value container
        }
                elements.value.$container.attr( 'id', elements.value.id );


        drugVariables[ variableIds.route ] = {
                elements.value.$container.addClass( 'col-' + valueColumns );
            name: 'Route',
            } else {
            type: 'string',
                // If we need to show variable inputs, make the title span the full width of the container,
            defaultValue: routes.length ? routes[ defaultRoute ].id : null,
                // put the variable inputs on a new row, and show the result on a row below that.
            options: routeOptions
                elements.title.$container.addClass( 'col-12 border-bottom' );
        };
                elements.value.$container.addClass( 'col-12' );


        mw.calculators.addVariables( drugVariables );
                // Create a new row for the variable inputs
    };
                elements.variables.$container = $( '<div>', {
                    class: 'row no-gutters border-bottom ' + calculation.getElementClasses( 'variables' ),
                    id: elements.variables.id
                } )
                    .append( $( '<div>', {
                        class: 'col-12'
                    } )
                        .append( mw.calculators.createInputGroup( missingVariableInputs ) ) );


                elements.value.$container = $( '<div>', {
                    class: 'row no-gutters',
                    id: elements.value.id
                } )
                    .append(
                        elements.value.$container
                );
            }


            // Add the title classes after the layout classes
            elements.title.$container.addClass( calculation.getElementClasses( 'title' ) );


            elements.value.$container.addClass( calculation.getElementClasses( 'value' ) );


            // Iterate over elementTypes since it is in order of rendering
            for( var iElementType in elementTypes ) {
                var elementType = elementTypes[ iElementType ];


    /**
                var $existingContainer = $( '#' + elements[ elementType ].id );
    * Class DrugDosageCalculator
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugDosageCalculator}
    * @constructor
    */
    mw.calculators.objectClasses.DrugDosageCalculator = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };


    mw.calculators.objectClasses.DrugDosageCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype );
                if( $existingContainer.length ) {
                    // If an input within this container has focus (i.e. the user changed a variable input which
                    // triggered this rerender), don't rerender the element as this would destroy the focus on
                    // the input.
                    if( !$.contains( $existingContainer[ 0 ], $( ':focus' )[ 0 ] ) ) {
                        $existingContainer.replaceWith( elements[ elementType ].$container );
                    }
                } else {
                    $( this ).append( elements[ elementType ].$container );
                }
            }


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


        if( !$calculatorContainer.length ) {
    mw.calculators.objectClasses.SimpleCalculation.prototype.getClassName = function() {
            return;
        return 'SimpleCalculation';
        }
    };


         $calculatorContainer.addClass( this.getCalculatorClass() );
    mw.calculators.objectClasses.SimpleCalculation.prototype.getProperties = function() {
         var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();


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


        $calculatorContainer.append( $( '<h4>', {
    mw.calculators.objectClasses.SimpleCalculation.prototype.getSearchString = function() {
            text: this.name
        return ( this.id + ' ' + this.abbreviation + ' ' + this.name + ' ' + this.searchData ).trim();
        } ) );
    };


        var $calculationsContainer = $( '<table>', {
    mw.calculators.objectClasses.SimpleCalculation.prototype.getTitleHtml = function() {
            class: 'wikitable'
         var titleHtml = this.getTitleString();
         } ).append( '<tbody>' );


         $calculatorContainer.append( $calculationsContainer );
         if( this.link ) {
            var href = this.link;


        $calculationsContainer
             // Detect internal links (this isn't great)
             .append( $( '<tr>' )
            var matches = href.match( /\[\[(.*?)\]\]/ );
                .append(
                    $( '<th>', {
                        class: this.getCalculatorClass() + '-drug'
                    } ).text( 'Drug' ),
                    $( '<th>', {
                        class: this.getCalculatorClass() + '-indication'
                    }  ).text( 'Indication' ),
                    $( '<th>', {
                        class: this.getCalculatorClass() + '-dose'
                    }  ).text( 'Dose' )
                )
            );


        for( var iCalculationId in this.calculations ) {
            if( matches ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
                href = mw.util.getUrl( matches[ 1 ] );
             var calculationContainerClass = calculation.getContainerClass();
             }


             var $calculationContainer = $( '<tr>', {
             titleHtml = $( '<a>', {
                 class: calculationContainerClass
                 href: href,
             } );
                text: titleHtml
             } )[ 0 ].outerHTML;
        }


            $calculationsContainer.append( $calculationContainer );
        return titleHtml;
    };


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


     mw.calculators.objectClasses.DrugDosageCalculator.prototype.getCalculatorClass = function() {
     mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
         return 'calculator-DrugDosageCalculator';
         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.DrugDosageCalculator.prototype.getProperties = function() {
     mw.calculators.initialize();
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();
 
        return this.mergeProperties( inheritedProperties, {
            required: [],
            optional: []
        } );
    };


}() );
}() );

Latest revision as of 19:39, 5 April 2022

/**
 * @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';

    // Polyfill to convert to roman numerals
    math.roman = function( number ) {
        var romanOrders = {
            M: 1000,
            CM: 900,
            D: 500,
            CD: 400,
            C: 100,
            XC: 90,
            L: 50,
            XL: 40,
            X: 10,
            IX: 9,
            V: 5,
            IV: 4,
            I: 1
        };

        var roman = '';

        for( var iOrder in romanOrders ) {
            var numOfOrder = Math.floor(number / romanOrders[ iOrder ] );
            number -= numOfOrder * romanOrders[ iOrder ];
            roman += iOrder.repeat( numOfOrder );
        }

        return roman;
    };

    // 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 = {
        calculations: {},
        objectClasses: {},
        options: {},
        selectors: {
            calculationCategories: '.calculator-calculationcategory',
            calculations: '.calculator-calculation',
            calculatorOptions: '.calculator-options'
        },
        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();

                mw.calculators.calculations[ calculationId ].update();
            }
        },
        addUnitsBases: function( unitsBaseData ) {
            var unitsBases = mw.calculators.createCalculatorObjects( 'UnitsBase', unitsBaseData );

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

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

                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
                };

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

                mw.calculators.units[ unitsId ] = 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 to set the variable value from the cookie value
                    if( !mw.calculators.variables[ variableId ].setValue( cookieValue ) ) {
                        // 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, global, maxInputsPerRow ) {
            var $form = $( '<form>', {
                novalidate: true
            } );

            var $formRow;

            var inputOptions = {
                global: !!global
            };

            maxInputsPerRow = maxInputsPerRow ?
                maxInputsPerRow :
                mw.calculators.getOptionValue( 'inputgroupmaxinputsperrow' );

            var inputCount = 0;

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

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

                if( inputCount % maxInputsPerRow === 0 ) {
                    if( $formRow ) {
                        $form.append( $formRow );
                    }

                    $formRow = $( '<div>', {
                        class: 'form-row calculator-inputGroup'
                    } );
                }

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

                inputCount++;
            }

            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;
            }
        },
        getOptionValue: function( optionId ) {
            return mw.calculators.options.hasOwnProperty( optionId ) ?
                mw.calculators.options[ optionId ] :
                undefined;
        },
        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 ];

                // Some units are of a given dimension, but have no conversion definition
                // (e.g. 'units' for mass, 'vial' for volume, etc.). These units are added
                // by appending '_abstract' to the baseName of the unit definition. However,
                // the calculator should treat them as the same type of unit
                var baseId = units.unit.base.key.toLowerCase().replace( /_\w+/, '' );

                unitsByBase[ baseId ] = 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( !mw.calculators.isValueMathObject( value ) ) {
                return null;
            }

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

            var absNumber = math.abs( number );

            if( absNumber >= 10 || absNumber === 0 ) {
                if( absNumber < 100 && absNumber !== math.round( absNumber ) && 2 * absNumber === math.round( 2 * absNumber ) ) {
                    // Special case to allow nearly-round decimals (e.g. 12.5)

                    decimals = 1;
                } else {
                    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 ) {
                        value = math.unit( newSIValue.toNumber() + ' ' + value.formatUnits().replace( oldSIUnit, newSIUnit ) );

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

            var valueString = String( valueNumber );

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

            var unitsId = value.formatUnits();

            if( mw.calculators.units.hasOwnProperty( unitsId ) &&
                typeof mw.calculators.units[ unitsId ].formatValue === 'function' ) {
                valueString = mw.calculators.units[ unitsId ].formatValue( valueString );
            }

            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() {
            // Change the menu item from "article" to "calculator"
            $( '#nav-article svg' ).addClass( 'fa-calculator' );
            $( '#nav-article .nav-label' ).html( 'Calculator' );

            // Wrap description in a collapse
            var descriptionCount = 0;

            $( '.calculator-description' ).each( function() {
                var descriptionContainerId = 'calculator-description-info';

                if( descriptionCount ) {
                    descriptionContainerId += '-' + descriptionCount;
                }

                var $descriptionLinkIcon = $( '<i>', {
                    class: 'far fa-question-circle fa-fw'
                } );

                var descriptionLinkString = '';

                descriptionLinkString += $( this ).data( 'title' ) ? $( this ).data( 'title' ) : 'About this calculator';

                var $descriptionLinkLabel = $( '<span>', {
                    html: descriptionLinkString
                } );

                var $descriptionLink = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + descriptionContainerId,
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': descriptionContainerId
                } ).append( $descriptionLinkIcon, $descriptionLinkLabel );

                var $descriptionContainer = $( '<div>', {
                    id: descriptionContainerId,
                    class: 'collapse calculator-description-info',
                    html: $( this ).html()
                } );

                $( this ).empty();

                if( !descriptionCount ) {
                    $descriptionLink.addClass( 'dropdown-item' );
                    $descriptionLinkLabel.addClass( 'nav-label' );

                    $('#menuButton .dropdown-menu').prepend( $descriptionLink );
                } else {
                    $descriptionLink.addClass( 'btn btn-outline-primary btn-sm' );
                    $( this ).append( $descriptionLink );
                }

                $( this ).append( $descriptionContainer );

                descriptionCount++;
            } );

            // Set options
            mw.calculators.setDefaultOptions();

            var $optionsElement = $( mw.calculators.selectors.calculatorOptions );
            if( $optionsElement.length ) {
                $.each( $optionsElement.data(), function( optionId, value ) {
                    mw.calculators.setOptionValue( optionId, value );
                } );
            }

            mw.hook( 'calculators.initialized' ).fire();
        },
        isMobile: function() {
            return window.matchMedia( 'only screen and (max-width: 760px)' ).matches;
        },
        isValueMathObject: function( value ) {
            return value && value.hasOwnProperty( 'value' );
        },
        prepareReferences: function( references ) {
            for( var iReference in references ) {
                var reference = references[ iReference ];

                // http(s)
                reference = reference.replace(
                    /(https?:\/\/[^\s]*)/gmi,
                    '<a href="$1" target="_blank">$1</a>'
                );

                // doi
                reference = reference.replace(
                    /doi: ([\w\d\.\/-]+)((\.\s)|$)/gmi,
                    'doi: <a href="https://doi.org/$1" target="_blank">$1</a>$2'
                );

                // PMCID
                reference = reference.replace(
                    /PMCID: PMC(\d+)/gmi,
                    'PMCID: <a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC$1/" target="_blank">PMC$1</a>'
                );

                // PMID
                reference = reference.replace(
                    /PMID: (\d+)/gmi,
                    'PMID: <a href="https://pubmed.ncbi.nlm.nih.gov/$1" target="_blank">$1</a>'
                );

                references[ iReference ] = reference;
            }

            return references;
        },
        setCookieValue: function( variableId, value ) {
            mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                expires: COOKIE_EXPIRATION
            } );
        },
        setDefaultOptions: function() {
            mw.calculators.setOptionValue( 'inputgroupmaxinputsperrow', 3 );
        },
        setOptionValue: function( optionId, value ) {
            mw.calculators.options[ optionId ] = value;

            return true;
        },
        setValue: function( variableId, value ) {
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return false;
            }

            if( !mw.calculators.variables[ variableId ].setValue( value ) ) {
                return false;
            }

            mw.calculators.setCookieValue( variableId, value );

            return true;
        },
        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',
                'formatValue',
                '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 );
        }

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

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

        this.message = null;
        this.valid = true;

        this.isValueSet = false;
        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.global = inputOptions.hasOwnProperty( 'class' ) ? inputOptions.global : false;
        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;

        // If not creating a global input, assign an iterated id
        if( !inputOptions.global ) {
            var inputIdCount = 0;

            while( $( '#' + inputId + '-' + inputIdCount ).length ) {
                inputIdCount++;
            }

            inputId += '-' + inputIdCount;
        }

        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 );

        // 'this' will be redefined for event handlers
        var variable = this;
        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 form-control-sm 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 custom-select-sm 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;

                            if( !mw.calculators.setValue( variableId, newValue ) ) {
                                if( variable.message ) {
                                    $( this ).parent().parent().parent().find( '.invalid-feedback' ).html( variable.message );
                                }

                                $( this ).parent().parent().addClass( 'is-invalid' );
                            } else {
                                $( this ).parent().parent().removeClass( 'is-invalid' );
                            }
                        } );

                    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();
                    }

                    if( !mw.calculators.setValue( variableId, newValue ) ) {
                        if( variable.message ) {
                            $( this ).parent().parent().find( '.invalid-feedback' ).html( variable.message );
                        }

                        $( this ).parent().addClass( 'is-invalid' );
                    } else {
                        $( this ).parent().removeClass( 'is-invalid' );
                    }
                } );

            // 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 custom-select-sm 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() {
                            if( !mw.calculators.setValue( variableId, $( this ).val() ) ) {
                                if( variable.message ) {
                                    $( this ).parent().parent().find( '.invalid-feedback' ).html( variable.message );
                                }

                                $( this ).parent().addClass( 'is-invalid' );
                            } else {
                                $( this ).parent().removeClass( 'is-invalid' );
                            }

                        } );

                    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 );
                }
            }
        }

        if( $inputContainer.length ) {
            $inputContainer.append( $( '<div>', {
                class: 'invalid-feedback'
            } ) );
        }

        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.valid ) {
            return null;
        } else if( this.value !== null ) {
            return this.value;
        } else if( !this.isValueSet && 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.prepareValue = function( value ) {
        if( value !== null ) {
            if( this.type === TYPE_NUMBER ) {
                if( !mw.calculators.isValueMathObject( value ) ) {
                    value = math.unit( value );
                }
            }
        }

        return value;
    };

    mw.calculators.objectClasses.Variable.prototype.setValue = function( value ) {
        // Set flag to prevent returning defaultValue in getValue()
        this.isValueSet = true;

        var validateResult = this.validateValue( value );

        this.valid = !!validateResult.valid;
        this.message = validateResult.message;

        if( !this.valid ) {
            this.value = null;
            this.valueUpdated();

            return false;
        }

        this.value = this.prepareValue( value );

        this.valueUpdated();

        return true;
    };

    mw.calculators.objectClasses.Variable.prototype.toString = function() {
        return this.getLabelString();
    };

    mw.calculators.objectClasses.Variable.prototype.validateValue = function( value ) {
        // Initialize valid flag to true. Will be set false if an error is found.
        result = {
            message: null,
            valid: true
        };

        // (At least for now) unsetting a variable is always valid
        if( value === null ) {
             return result;
        }

        // Some errors which are plausibly from normal user input we will show as feedback on the input (e.g.
        // a numeric value that is below the minimum value. Errors which are unlikely to be from user input
        // and instead relate to developer issues (e.g. incorrect units in select boxes), only show on the console.
        var consoleWarnPrefix = 'Could not set value "' + value + '" for "' + this.id + '":';

        if( this.type === TYPE_NUMBER ) {
            if( !mw.calculators.isValueMathObject( value ) ) {
                value = math.unit( value );
            }

            var valueUnits;

            if( this.hasUnits() ) {
                valueUnits = value.formatUnits().replace( /\s/g, '' );

                if( !valueUnits ) {
                    // Unlikely to be a user error, so don't set message.
                    result.valid = false;

                    console.warn( consoleWarnPrefix + 'Value must define units' );
                } else if( this.units.indexOf( valueUnits ) === -1 ) {
                    // Unlikely to be a user error, so don't set message.
                    result.valid = false;

                    console.warn( consoleWarnPrefix + 'Units "' + valueUnits + '" are not valid for this variable' );
                }
            }

            if( this.minValue && math.smaller( value, this.minValue ) ) {
                var minValueString = mw.calculators.getValueString( this.minValue );

                if( valueUnits && valueUnits != this.minValue.formatUnits() ) {
                    minValueString += ' (' + mw.calculators.getValueString( this.minValue.to( valueUnits ) ) + ')';
                }

                result.message = String( this ) + ' must be at least ' + minValueString;
                result.valid = false;
            } else if( this.maxValue && math.larger( value, this.maxValue ) ) {
                var maxValueString = mw.calculators.getValueString( this.maxValue );

                if( valueUnits && valueUnits != this.maxValue.formatUnits() ) {
                    maxValueString += ' (' + mw.calculators.getValueString( this.maxValue.to( valueUnits ) ) + ')';
                }

                result.message = String( this ) + ' must be less than ' + maxValueString;
                result.valid = false;
            }
        } else if( this.hasOptions() ) {
            if( !this.options.hasOwnProperty( value ) ) {
                // Unlikely to be a user error, so don't set message
                result.valid = false;

                console.warn( consoleWarnPrefix + 'Value must be one of: ' + Object.keys( this.options ).join( ', ' ) );
            }
        }

        return result;
    };

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

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



    /**
     * 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() {
        throw new Error( 'AbstractCalculation child class "' + this.getClassName() + '" must implement doRender()' );
    };

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

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

        var missingRequiredData = this.getMissingRequiredData();

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

            return false;
        }

        var data = {};

        var calculationId, calculation, variableId, variable;

        var calculations = calculationData.calculations.required.concat( calculationData.calculations.optional );

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

            // We shouldn't use getValue() since that triggers recalculate() which would cause an infinite loop
            data[ calculationId ] = calculation.value;
        }

        var variables = calculationData.variables.required.concat( calculationData.variables.optional );

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

            data[ variableId ] = variable.getValue();
        }

        return data;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getClassName = function() {
        throw new Error( 'AbstractCalculation child class must implement getClassName()' );
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerClasses = function() {
        return this.getElementClasses();
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerId = function() {
        return this.getElementPrefix() + '-' + this.id;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getDescription = function() {
        return this.description;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getElementPrefix = function( useClassName ) {
        var elementPrefix = 'calculator-';

        elementPrefix += useClassName ? this.getClassName() : 'calculation';

        return elementPrefix;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getElementClasses = function( elementId ) {
        elementId = elementId ? '-' + elementId : '';

        return this.getElementPrefix() + elementId + ' ' +
            this.getElementPrefix( true ) + elementId + ' ' +
            this.getContainerId() + elementId;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getFormula = function() {
        return this.formula;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getInfo = function( infoCount ) {
        var infoHtml = '';

        var description = this.getDescription();

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

        var formula = this.getFormula();

        if( formula ) {
            infoHtml += $( '<div>', {
                class: this.getElementClasses( 'formula' )
            } )[ 0 ].outerHTML;
        }

        var references = this.getReferences();

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

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

            infoHtml += $( '<div>', {
                class: this.getElementClasses( 'references' )
            } ).append( $references )[ 0 ].outerHTML;
        }

        var infoContainerId = this.getContainerId() + '-info';

        if( infoCount ) {
            infoContainerId += '-' + infoCount;
        }

        $infoContainer = $( '<div>', {
            id: infoContainerId,
            class: 'collapse row no-gutters border-top ' + this.getElementClasses( 'info' )
        } ).append( infoHtml );

        return $infoContainer;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getInfoButton = function( infoCount ) {
        var infoContainerId = this.getContainerId() + '-info';

        if( infoCount ) {
            infoContainerId += '-' + infoCount;
        }

        return $( '<span>', {
            class: this.getElementClasses( 'infoButton' )
        } )
            .append( $( '<a>', {
                'data-toggle': 'collapse',
                href: '#' + infoContainerId,
                role: 'button',
                'aria-expanded': 'false',
                'aria-controls': infoContainerId
            } )
                .append( $( '<i>', {
                    class: 'far fa-question-circle'
                } ) ) );
    };

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

        var missingRequiredData = [];
        var calculation, variable;

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

            if( !calculation.hasValue() ) {
                missingRequiredData = missingRequiredData.concat( calculation.getMissingRequiredData() );
            }
        }

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

            if( !variable.hasValue() ) {
                missingRequiredData.push( String( variable ) );
            }
        }

        return missingRequiredData.filter( mw.calculators.uniqueValues );
    };

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

    mw.calculators.objectClasses.AbstractCalculation.prototype.getReferences = function() {
        return this.references;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getSearchString = function() {
        var searchString = this.id;

        searchString += this.searchData ? ' ' + this.searchData : '';

        return searchString.trim();
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.getTitleHtml = function() {
        return this.getTitleString();
    };

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

    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 this.getDescription() || this.getFormula() || this.getReferences().length;
    };

    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.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.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];

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

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

        // Remove any placeholder content explicitly set in the markup (used for SEO).
        $( '.' + this.getContainerId() ).empty();
    };

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

    mw.calculators.objectClasses.AbstractCalculation.prototype.parseFormula = function() {
        var formula = this.getFormula();

        if( !formula ) {
            return;
        }

        var api = new mw.Api();

        var containerId = this.getContainerId() + '-formula';

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

    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() {
        // Need to run rendering in setTimeout to allow browser events to remain responsive
        var calculation = this;

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

            calculation.doRender();

            // Send API queries to parse LaTeX formulas
            calculation.parseFormula();

            mw.track( 'mw.calculators.CalculationRendered' );

            if( typeof calculation.onRendered === 'function' ) {
                calculation.onRendered();
            }
        }, 0 );
    };

    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.toString = function() {
        return this.getTitleString();
    };

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

        this.render();
    };

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

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



    /**
     * 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 ];

                    // FYI can't check to see if the data actually exists here since it may not be defined yet
                    if( !this[ dataType ].hasOwnProperty( dataRequirement ) ) {
                        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.doRender = function() {
        var $calculationContainer = $( '.' + this.getContainerId() );

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

        // Add all required classes
        $calculationContainer.addClass( 'row no-gutters border ' + this.getContainerClasses() );

        // Add search phrases
        $calculationContainer.attr( 'data-search', this.getSearchString() );

        // Get a string version of the calculation's value
        var valueString = this.getValueString();

        // We will need to show variable inputs for non-global variable inputs.
        // Global inputs (i.e. those in the header) will claim the DOM id for that variable.
        // Non-global inputs (i.e. specific to a calculation) will only set a class but not the id,
        // and thus will get added to each calculation even if a duplicate.
        // E.g. 2 calculation might use the current hematocrit, but we should show them for both calculations since
        // it wouldn't be obvious the input that only showed the first time would apply to both calculations.
        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 );
            }
        }

        // Out of 12, uses Bootstrap col- classes in a container
        var titleColumns = '7';
        var valueColumns = '5';

        // Store this object in a local variable since .each() will reassign this to the DOM object of each
        // calculation container.
        var calculation = this;
        var calculationCount = 0;

        // Eventually may implement different rendering, so we should regenerate
        // all elements with each iteration of the loop.
        // I.e. might show result in table and inline in 2 different places of article.
        $calculationContainer.each( function() {
            // Initalize the variables for all the elements of the calculation. These need to be in order of placement
            // in the calculation container
            var elementTypes = [
                'title',
                'variables',
                'value',
                'info'
            ];

            var elements = {};

            for( var iElementType in elementTypes ) {
                var elementType = elementTypes[ iElementType ];

                elements[ elementType ] = {
                    $container: null,
                    id: calculation.getContainerId() + '-' + elementType
                };

                if( calculationCount ) {
                    elements[ elementType ].id += '-' + calculationCount;
                }
            }

            // Create title element and append to container
            elements.title.$container = $( '<div>', {
                id: elements.title.id
            } );

            elements.title.$container.append( calculation.getTitleHtml() );

            if( calculation.hasInfo() ) {
                elements.title.$container.append( calculation.getInfoButton( calculationCount ) );

                // Id of the info container should already be set by getInfo()
                elements.info.$container = calculation.getInfo();
            }

            // Create the value element
            elements.value.$container = $( '<div>' ).append( valueString );

            if( !missingVariableInputs.length ) {
                // If we have no variable inputs to show, we can put the title and value in one row of the table
                elements.title.$container.addClass( 'col-' + titleColumns + ' border-right' );

                // Add the id attribute to the value container
                elements.value.$container.attr( 'id', elements.value.id );

                elements.value.$container.addClass( 'col-' + valueColumns );
            } else {
                // If we need to show variable inputs, make the title span the full width of the container,
                // put the variable inputs on a new row, and show the result on a row below that.
                elements.title.$container.addClass( 'col-12 border-bottom' );
                elements.value.$container.addClass( 'col-12' );

                // Create a new row for the variable inputs
                elements.variables.$container = $( '<div>', {
                    class: 'row no-gutters border-bottom ' + calculation.getElementClasses( 'variables' ),
                    id: elements.variables.id
                } )
                    .append( $( '<div>', {
                        class: 'col-12'
                    } )
                        .append( mw.calculators.createInputGroup( missingVariableInputs ) ) );

                elements.value.$container = $( '<div>', {
                    class: 'row no-gutters',
                    id: elements.value.id
                } )
                    .append(
                        elements.value.$container
                );
            }

            // Add the title classes after the layout classes
            elements.title.$container.addClass( calculation.getElementClasses( 'title' ) );

            elements.value.$container.addClass( calculation.getElementClasses( 'value' ) );

            // Iterate over elementTypes since it is in order of rendering
            for( var iElementType in elementTypes ) {
                var elementType = elementTypes[ iElementType ];

                var $existingContainer = $( '#' + elements[ elementType ].id );

                if( $existingContainer.length ) {
                    // If an input within this container has focus (i.e. the user changed a variable input which
                    // triggered this rerender), don't rerender the element as this would destroy the focus on
                    // the input.
                    if( !$.contains( $existingContainer[ 0 ], $( ':focus' )[ 0 ] ) ) {
                        $existingContainer.replaceWith( elements[ elementType ].$container );
                    }
                } else {
                    $( this ).append( elements[ elementType ].$container );
                }
            }

            calculationCount++;
        } );
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getClassName = function() {
        return 'SimpleCalculation';
    };

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

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

    mw.calculators.objectClasses.SimpleCalculation.prototype.getSearchString = function() {
        return ( this.id + ' ' + this.abbreviation + ' ' + this.name + ' ' + this.searchData ).trim();
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getTitleHtml = function() {
        var titleHtml = this.getTitleString();

        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 ] );
            }

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

        return titleHtml;
    };

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

    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.initialize();

}() );