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

From WikiAnesthesia
Line 3: Line 3:
  */
  */
( function() {
( function() {
     var COOKIE_EXPIRATION = 12 * 60 * 60;
     var DEFAULT_DRUG_COLOR = 'default';
    var DEFAULT_DRUG_POPULATION = 'general';
    var DEFAULT_DRUG_ROUTE = 'iv';


     var TYPE_NUMBER = 'number';
     mw.calculators.isValueDependent = function( value, variableId ) {
    var TYPE_STRING = 'string';
         // This may need generalized to support other variables in the future
 
         if( variableId === 'weight' ) {
    var VALID_TYPES = [
             return value && value.formatUnits().match( /\/[\s(]*?kg/ );
        TYPE_NUMBER,
        } else {
         TYPE_STRING
            throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' );
    ];
 
    var DEFAULT_CALCULATION_CLASS = 'SimpleCalculation';
    var DEFAULT_CALCULATOR_CLASS = 'SimpleCalculator';
 
    // Polyfill to fetch unit's base. This may become unnecessary in a future version of math.js
    math.Unit.prototype.getBase = function() {
         for( var iBase in math.Unit.BASE_UNITS ) {
             if( this.equalBase( math.Unit.BASE_UNITS[ iBase ] ) ) {
                return iBase;
            }
         }
         }
        return null;
     };
     };


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


    mw.calculators = {
                return units;
        calculators: {},
        calculations: {},
        objectClasses: {},
        units: {},
        unitsBases: {},
        variables: {},
        addCalculations: function( calculationData, className ) {
            className = className ? className : DEFAULT_CALCULATION_CLASS;
 
            var calculations = mw.calculators.createCalculatorObjects( className, calculationData );
 
            for( var calculationId in calculations ) {
                var calculation = calculations[ calculationId ];
 
                mw.calculators.calculations[ calculationId ] = calculation;
 
                mw.calculators.calculations[ calculationId ].setDependencies();
             }
             }
         },
         },
         addCalculators: function( moduleId, calculatorData, className ) {
         mass: {
             className = className ? className : DEFAULT_CALCULATOR_CLASS;
             toString: function( units ) {
 
                 units = units.replace( 'ug', 'mcg' );
            for( var calculatorId in calculatorData ) {
                 calculatorData[ calculatorId ].module = moduleId;
 
                // Make sure the calculations have been defined
                for( var iCalculation in calculatorData[ calculatorId ].calculations ) {
                    var calculationId = calculatorData[ calculatorId ].calculations[ iCalculation ];


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


            var calculators = mw.calculators.createCalculatorObjects( className, calculatorData );
    mw.calculators.addUnits( {
 
        mcg: {
            // Initalize the calculators property for the module
             baseName: 'mass',
            if( !mw.calculators.calculators.hasOwnProperty( moduleId ) ) {
             definition: '1 ug'
                mw.calculators.calculators[ moduleId ] = {};
             }
 
            // Store the calculators
            for( var calculatorId in calculators ) {
                mw.calculators.calculators[ moduleId ][ calculatorId ] = calculators[ calculatorId ];
 
                mw.calculators.calculators[ moduleId ][ calculatorId ].render();
             }
         },
         },
         addUnitsBases: function( unitsBaseData ) {
         pct: {
             var unitsBases = mw.calculators.createCalculatorObjects( 'UnitsBase', unitsBaseData );
             baseName: 'concentration',
 
             definition: '10 mg/mL'
             for( var unitsBaseId in unitsBases ) {
                mw.calculators.unitsBases[ unitsBaseId ] = unitsBases[ unitsBaseId ];
            }
         },
         },
         addUnits: function( unitsData ) {
         vial: {
             var units = mw.calculators.createCalculatorObjects( 'Units', unitsData );
             baseName: 'volume'
        }
    } );


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


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


                    math.createUnit( unitsId, unitData );
    /**
                } catch( e ) {
    * DrugColor
                    console.warn( e.message );
    */
                }
    mw.calculators.drugColors = {};


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


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


                var cookieValue = mw.calculators.getCookieValue( variableId );
    mw.calculators.getDrugColor = function( drugColorId ) {
        if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
            return mw.calculators.drugColors[ drugColorId ];
        } else {
            return null;
        }
    };


                if( cookieValue ) {
    /**
                    try {
    * Class DrugColor
                        // isValueValid will throw an error if invalid, so the catch clause is our else condition
    * @param {Object} propertyValues
                        if( mw.calculators.variables[ variableId ].isValueValid( cookieValue ) ) {
    * @returns {mw.calculators.objectClasses.DrugColor}
                            mw.calculators.variables[ variableId ].setValue( cookieValue );
    * @constructor
                        }
    */
                    } catch( e ) {
    mw.calculators.objectClasses.DrugColor = function( propertyValues ) {
                        // Unset the cookie value since for whatever reason it's no longer valid.
        var properties = {
                        mw.calculators.setCookieValue( variableId, null );
            required: [
                    }
                 'id'
                 }
             ],
             }
            optional: [
        },
                'parentColor',
        createCalculatorObjects: function( className, objectData ) {
                'primaryColor',
            if( !mw.calculators.objectClasses.hasOwnProperty( className ) ) {
                 'highlightColor',
                 throw new Error( 'Invalid class name "' + className + '"' );
                'striped'
             }
             ]
        };


            var objects = {};
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


            for( var objectId in objectData ) {
        this.parentColor = this.parentColor || this.id === DEFAULT_DRUG_COLOR ? this.parentColor : DEFAULT_DRUG_COLOR;
                var propertyValues = objectData[ objectId ];
    };


                // Id can either be specified using the 'id' property, or as the property name in objectData
    mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                if( propertyValues.hasOwnProperty( 'id' ) ) {
                    objectId = propertyValues.id;
                }
                else {
                    propertyValues.id = objectId;
                }


                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
    mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() {
            }
        if( !this.parentColor ) {
            return null;
        }


            return objects;
         var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );
         },
        createInputGroup: function( variableIds ) {
            var $form = $( '<form>', {


             } );
        if( !parentDrugColor ) {
             throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
        }


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


             for( var iVariableId in variableIds ) {
    mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
                var variableId = variableIds[ iVariableId ];
        if( this.highlightColor ) {
             return this.highlightColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getHighlightColor();
        }
    };


                if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
    mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
                    throw new Error( 'Invalid variable name "' + variableId + '"' );
        if( this.primaryColor ) {
                }
            return this.primaryColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getPrimaryColor();
        }
    };


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


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


            if( !cookieValue ) {
                return null;
            }


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


            var unitsByBase = {};


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


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


            return unitsByBase;
    mw.calculators.addDrugPopulations = function( drugPopulationData ) {
        },
        var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );
        getUnitsString: function( value ) {
            if( typeof value !== 'object' ) {
                return null;
            }


             var unitsString = value.formatUnits();
        for( var drugPopulationId in drugPopulations ) {
             mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
        }
    };


            var reDenominator = /\/\s?\((.*)\)/;
    mw.calculators.getDrugPopulation = function( drugPopulationId ) {
             var denominatorMatches = unitsString.match( reDenominator );
        if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
            return mw.calculators.drugPopulations[ drugPopulationId ];
        } else {
             return null;
        }
    };


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


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


             unitsString = unitsString
    /**
                 .replace( /\s/g, '' )
    * Class DrugPopulation
                 .replace( /(\^(\d+))/g, '<sup>$2</sup>' );
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugPopulation}
    * @constructor
    */
    mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) {
        var properties = {
             required: [
                 'id',
                'name'
            ],
            optional: [
                 'abbreviation',
                'variables'
            ]
        };


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


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


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


            // Convert the number to a string, reverse, and count the number of characters up to the period.
    mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
            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.
    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
            decimals = decimals > 0 ? decimals : 0;
        var inputData = new mw.calculators.objectClasses.CalculationData();


            return decimals;
         for( var variableId in this.variables ) {
         },
             inputData.variables.required.push( variableId );
        getValueNumber: function( value, decimals ) {
        }
             if( typeof value !== 'object' ) {
                return null;
            }


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


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


            if( absNumber >= 10 ) {
        for( var variableId in this.variables ) {
                decimals = 0;
             if( !dataValues.hasOwnProperty( variableId ) ) {
             } else {
                return -1;
                decimals = -math.floor( math.log10( absNumber ) ) + 1;
             }
             }


             return math.round( number, decimals );
             if( this.variables[ variableId ].min &&
        },
                ( !dataValues[ variableId ] ||
        getValueString: function( value, decimals ) {
                    !math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) ) {
            if( !mw.calculators.isValueMathObject( value ) ) {
                 return -1;
                 return null;
             }
             }


             var valueNumber = mw.calculators.getValueNumber( value, decimals );
             if( this.variables[ variableId ].max &&
             var valueUnits = mw.calculators.getUnitsString( value );
                ( !dataValues[ variableId ] ||
                    !math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) ) {
                return -1;
             }
        }


            if( math.abs( math.log10( valueNumber ) ) > 3 ) {
        // If the data matches the population definition, the score corresponds to the number of variables in the
                var valueUnitsByBase = mw.calculators.getUnitsByBase( value );
        // population definition. This should roughly correspond to the specificity of the population.
        return Object.keys( this.variables ).length;
    };


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


                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();
    * DrugRoute
    */
    mw.calculators.drugRoutes = {};


                    if( newSIUnit !== oldSIUnit ) {
    mw.calculators.addDrugRoutes = function( drugRouteData ) {
                        var newValue = math.unit( newSIValue.toNumber() + ' ' + value.formatUnits().replace( oldSIUnit, newSIUnit ) );
        var drugRoutes = mw.calculators.createCalculatorObjects( 'DrugRoute', drugRouteData );


                        valueNumber = mw.calculators.getValueNumber( newValue, decimals );
        for( var drugRouteId in drugRoutes ) {
                        valueUnits = mw.calculators.getUnitsString( newValue );
            mw.calculators.drugRoutes[ drugRouteId ] = drugRoutes[ drugRouteId ];
                    }
        }
                }
    };
            }


            var valueString = String( valueNumber );
    mw.calculators.getDrugRoute = function( drugRouteId ) {
 
         if( mw.calculators.drugRoutes.hasOwnProperty( drugRouteId ) ) {
            if( valueUnits ) {
            return mw.calculators.drugRoutes[ drugRouteId ];
                valueString += ' ' + valueUnits;
         } else {
            }
             return null;
 
            return valueString;
        },
        getVariable: function( variableId ) {
            if( mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return mw.calculators.variables[ variableId ];
            } else {
                return null;
            }
        },
        hasData: function( dataType, dataId ) {
            if( mw.calculators.hasOwnProperty( dataType ) &&
                mw.calculators[ dataType ].hasOwnProperty( dataId ) ) {
                return true;
            } else {
                return false;
            }
        },
        initialize: function() {
            $( '.calculator' ).each( function() {
                var gadgetModule = 'ext.gadget.calculator-' + $( this ).attr( 'data-module' );
 
                if( gadgetModule && mw.loader.getState( gadgetModule ) === 'registered' ) {
                    mw.loader.load( gadgetModule );
                }
            } );
        },
        isMobile: function() {
            return window.matchMedia( 'only screen and (max-width: 760px)' ).matches;
         },
        isValueMathObject: function( value ) {
            return value && value.hasOwnProperty( 'value' );
        },
        setCookieValue: function( variableId, value ) {
            mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                expires: COOKIE_EXPIRATION
            } );
        },
        setValue: function( variableId, value ) {
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return false;
            }
 
            if( mw.calculators.variables[ variableId ].setValue( value ) ) {
                mw.calculators.setCookieValue( variableId, value );
 
                return true;
            }
 
            return false;
         },
        uniqueValues: function( value, index, self ) {
             return self.indexOf( value ) === index;
         }
         }
     };
     };


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


         if( properties ) {
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            if( properties.hasOwnProperty( 'required' ) ) {
    };
                for( var iRequiredProperty in properties.required ) {
 
                    var requiredProperty = properties.required[ iRequiredProperty ];
    mw.calculators.objectClasses.DrugRoute.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.DrugRoute.prototype.toString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };


                    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' ) {
    * DrugIndication
                        this[ optionalProperty ] = null;
    */
                    }
    mw.calculators.drugIndications = {};
                }
            }


            var invalidProperties = Object.keys( propertyValues );
    mw.calculators.addDrugIndications = function( drugIndicationData ) {
        var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );


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


     mw.calculators.objectClasses.CalculatorObject.prototype.getProperties = function() {
     mw.calculators.getDrugIndication = function( drugIndicationId ) {
         return {
         if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
             required: [],
             return mw.calculators.drugIndications[ drugIndicationId ];
             optional: []
        } else {
         };
             return null;
         }
     };
     };
    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
     * Class DrugIndication
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.UnitsBase}
     * @returns {mw.calculators.objectClasses.DrugIndication}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.UnitsBase = function( propertyValues ) {
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
         var properties = {
         var properties = {
             required: [
             required: [
                 'id'
                 'id',
                'name'
             ],
             ],
             optional: [
             optional: [
                 'toString'
                 'abbreviation',
                'default'
             ]
             ]
         };
         };
Line 478: Line 343:
     };
     };


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




Line 484: Line 354:


     /**
     /**
     * Class Units
     * Drug
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.Units}
    * @constructor
     */
     */
     mw.calculators.objectClasses.Units = function( propertyValues ) {
     mw.calculators.drugs = {};
         var properties = {
 
             required: [
    mw.calculators.addDrugs = function( drugData ) {
                 'id'
         var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );
            ],
 
            optional: [
        for( var drugId in drugs ) {
                'aliases',
             mw.calculators.drugs[ drugId ] = drugs[ drugId ];
                 'baseName',
 
                 'definition',
            var drugDosageCalculationId = mw.calculators.getDrugDosageCalculationId( drugId );
                'offset',
            var drugDosageCalculation = mw.calculators.getCalculation( drugDosageCalculationId );
                 'prefixes'
 
             ]
            if( !drugDosageCalculation ) {
        };
                 var calculationData = {};
 
                calculationData[ drugDosageCalculationId ] = {
                    calculate: mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate,
                    drug: drugId,
                    type: 'drug'
                 };
 
                 mw.calculators.addCalculations( calculationData, 'DrugDosageCalculation' );
 
                 drugDosageCalculation = mw.calculators.getCalculation( drugDosageCalculationId );
             }


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


     mw.calculators.objectClasses.Units.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
        var drug = mw.calculators.getDrug( drugId );


        if( !drug ) {
            throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
        }
        drug.addDosages( drugDosageData );
        // Update calculation dependencies
        var drugDosageCalculation = mw.calculators.getCalculation( mw.calculators.getDrugDosageCalculationId( drugId ) );
        drugDosageCalculation.updateVariables();
        drugDosageCalculation.setDependencies();
    };
    mw.calculators.getDrug = function( drugId ) {
        if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
            return mw.calculators.drugs[ drugId ];
        } else {
            return null;
        }
    };






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


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


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


             for( var iOption in this.options ) {
        if( !color ) {
                var option = this.options[ iOption ];
             throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' );
        }


                options[ option ] = option;
        this.color = color;
             }
 
        if( this.preparations ) {
            var preparationData = this.preparations;
 
             this.preparations = [];


             this.options = options;
             this.addPreparations( preparationData );
        } else {
            this.preparations = [];
         }
         }


         this.calculations = [];
         if( this.dosages ) {
            var dosageData = this.dosages;


        if( this.defaultValue ) {
            this.dosages = [];
             this.defaultValue = this.prepareValue( this.defaultValue );
 
            this.addDosages( dosageData );
        } else {
             this.dosages = [];
         }
         }
    };
    mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
    mw.calculators.objectClasses.Drug.prototype.addDosages = function( dosageData ) {
        var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', dosageData );
        for( var dosageId in dosages ) {
            dosages[ dosageId ].id = this.dosages.length;


        this.value = null;
            this.dosages.push( dosages[ dosageId ] );
        }
     };
     };


     mw.calculators.objectClasses.Variable.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     mw.calculators.objectClasses.Drug.prototype.addPreparations = function( preparationData ) {
        var preparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', preparationData );


    mw.calculators.objectClasses.Variable.prototype.addCalculation = function( calculationId ) {
        for( var preparationId in preparations ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            preparations[ preparationId ].id = this.preparations.length;
            return;
 
            this.preparations.push( preparations[ preparationId ] );
         }
         }
    };


        this.calculations.push( calculationId );
    mw.calculators.objectClasses.Drug.prototype.getIndications = function() {
    };
        var indications = [];


    mw.calculators.objectClasses.Variable.prototype.createInput = function( inputOptions ) {
        for( var iDosage in this.dosages ) {
        if( !inputOptions ) {
            if( this.dosages[ iDosage ].indication ) {
             inputOptions = {};
                indications.push( this.dosages[ iDosage ].indication );
             }
         }
         }


         inputOptions.class = inputOptions.hasOwnProperty( 'class' ) ? inputOptions.class : '';
         return indications.filter( mw.calculators.uniqueValues );
        inputOptions.hideLabel = inputOptions.hasOwnProperty( 'hideLabel' ) ? inputOptions.hideLabel : false;
    };
        inputOptions.hideLabelMobile = inputOptions.hasOwnProperty( 'hideLabelMobile' ) ? inputOptions.hideLabelMobile : false;
 
        inputOptions.inline = inputOptions.hasOwnProperty( 'inline' ) ? inputOptions.inline : false;
    mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) {
         inputOptions.inputClass = inputOptions.hasOwnProperty( 'inputClass' ) ? inputOptions.inputClass : '';
         var populations = [];


         var variableId = this.id;
         for( var iDosage in this.dosages ) {
         var inputId = 'calculator-input-' + variableId;
            if( this.dosages[ iDosage ].population &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                populations.push( this.dosages[ iDosage ].population );
            }
         }


         var inputContainerTag = inputOptions.inline ? '<span>' : '<div>';
         return populations.filter( mw.calculators.uniqueValues );
    };


        var inputContainerAttributes = {
    mw.calculators.objectClasses.Drug.prototype.getRoutes = function( indicationId ) {
            class: 'form-group mb-0 calculator-container-input'
         var routes = [];
         };


         inputContainerAttributes.class += inputOptions.class ? ' ' + inputOptions.class : '';
         for( var iDosage in this.dosages ) {
         inputContainerAttributes.class += ' calculator-container-input-' + variableId;
            if( this.dosages[ iDosage ].route &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                routes.push( this.dosages[ iDosage ].route );
            }
         }


         var inputContainerCss = {};
         return routes.filter( mw.calculators.uniqueValues );
    };


        // Initialize label attributes
    mw.calculators.objectClasses.Drug.prototype.getPreparations = function( excludeDilutionRequired ) {
         var labelAttributes = {
         var preparations = this.preparations.filter( mw.calculators.uniqueValues );
            for: inputId,
            html: this.getLabelString()
        };


         if( inputOptions.hideLabel || ( inputOptions.hideLabelMobile && mw.calculators.isMobile() ) ) {
         if( excludeDilutionRequired ) {
             labelAttributes.class = 'sr-only';
            for( var iPreparation in preparations ) {
                if( preparations[ iPreparation ].dilutionRequired ) {
                    delete preparations[ iPreparation ];
                }
             }
         }
         }


         var labelCss = {};
         return preparations;
    };


        if( inputOptions.inline ) {
    mw.calculators.objectClasses.Drug.prototype.getProperties = function() {
             inputContainerTag = '<span>';
        return {
            required: [
                'id',
                'name'
             ],
            optional: [
                'color',
                'dosages',
                'preparations'
            ]
        };
    };


            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 );
    /**
    * DrugPreparation
    */
    mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
         var drug = mw.calculators.getDrug( drugId );


         var value = this.getValue();
         if( !drug ) {
            throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
        }


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


            var inputValue = '';
        var drugDosageCalculation = mw.calculators.getCalculation( mw.calculators.getDrugDosageCalculationId( drugId ) );


            if( mw.calculators.isValueMathObject( value ) ) {
        drugDosageCalculation.recalculate();
                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 ) {
    * Class DrugPreparation
                 inputAttributes.maxlength = this.maxLength;
    * @param {Object} propertyValues
             }
    * @returns {mw.calculators.objectClasses.DrugPreparation}
    * @constructor
    */
    mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
        var properties = {
            required: [
                'id',
                 'concentration'
            ],
            optional: [
                'default',
                'dilutionRequired',
                'commonDilution'
             ]
        };


            // Add any additional classes to the input
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            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
        this.concentration = this.concentration.replace( 'mcg', 'ug' );
            if( this.hasUnits() ) {
                // Set the units id
                unitsId = inputId + '-units';


                var unitsValue = mw.calculators.isValueMathObject( value ) ? value.formatUnits() : null;
        this.concentration = math.unit( this.concentration );
    };


                var unitsInputAttributes = {
    mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                    id: unitsId
                };


                // Create the units container
    mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() {
                $unitsContainer = $( '<div>', {
        // The units of concentration will always be of the form "mass / volume"
                    class: 'input-group-append'
        // The regular expression matches all text leading up to the volume units
                } ).css( 'align-items', 'center' );
        return mw.calculators.getUnitsByBase( this.concentration ).volume;
    };


                if( this.units.length === 1 ) {
    mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() {
                    unitsInputAttributes.type = 'hidden';
        return mw.calculators.getValueString( this.concentration );
                    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;
    /**
    * Class DrugDosage
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugDosage}
    * @constructor
    */
    mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );


                            mw.calculators.setValue( variableId, newValue );
        var drugIndication = mw.calculators.getDrugIndication( this.indication );
                        } );


                    for( var iUnits in this.units ) {
        if( !drugIndication ) {
                        var units = this.units[ iUnits ];
            throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
        }


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


                        if( units === unitsValue ) {
        this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION;
                            unitsOptionAttributes.selected = true;
                        }


                        $unitsInput.append( $( '<option>', unitsOptionAttributes ) );
        var drugPopulation = mw.calculators.getDrugPopulation( this.population );
                    }


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


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


                    var newValue = numberValue ? numberValue : null;
        this.route = this.route ? this.route : DEFAULT_DRUG_ROUTE;


                    if( newValue && unitsId ) {
        var drugRoute = mw.calculators.getDrugRoute( this.route );
                        newValue = newValue + ' ' + $( '#' + unitsId ).val();
                    }


                    mw.calculators.setValue( variableId, newValue );
        if( !drugRoute ) {
                } );
            throw new Error( 'Invalid route "' + this.route + '" for drug dosage' );
        }


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


            if( $unitsContainer ) {
        // Add the dose objects to the drug
                $inputGroup.append( $unitsContainer );
        var drugDoseData = this.dose;
            }
        this.dose = [];


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


                if( optionKeys.length === 1 ) {
    mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                    $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
    mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
                    selectAttributes.class += inputOptions.inputClass ? ' ' + inputOptions.inputClass : '';
        // Each dosage can have one or more associated doses. Ensure this value is an array.
        if( !Array.isArray( drugDoseData ) ) {
            drugDoseData = [ drugDoseData ];
        }


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


                    for( var optionId in this.options ) {
        for( var doseId in doses ) {
                        var displayText = this.options[ optionId ];
            doses[ doseId ].id = this.dose.length;


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


                        if( optionId === value ) {
    mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() {
                            optionAttributes.selected = true;
        var inputData = new mw.calculators.objectClasses.CalculationData();
                        }


                        $select.append( $( '<option>', optionAttributes ) );
        inputData = inputData.merge( this.population.getCalculationData() );
                    }


                    $inputContainer.append( $select );
        for( var iDose in this.dose ) {
                }
            inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
            }
         }
         }


         return $inputContainer;
         return inputData;
     };
     };


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


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


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


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


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


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


        if( value === null ||
    /**
            ( mw.calculators.isValueMathObject( value ) && !value.toNumber() ) ) {
    * Class DrugDose
            return false;
    * @param {Object} propertyValues
        }
    * @returns {mw.calculators.objectClasses.DrugDose}
    * @constructor
    */
    mw.calculators.objectClasses.DrugDose = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );


         return true;
         if( this.weightCalculation ) {
    };
            var weightCalculationIds = this.weightCalculation;


    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
            // weightCalculation property will contain references to the actual objects, so reinitialize
        return mw.calculators.isValueMathObject( this.value );
            this.weightCalculation = [];
    };


    mw.calculators.objectClasses.Variable.prototype.isValueValid = function( value ) {
             if( !Array.isArray( weightCalculationIds ) ) {
        if( value === null ) {
                 weightCalculationIds = [ weightCalculationIds ];
             return true;
        }
 
        if( this.type === TYPE_NUMBER ) {
            if( typeof value !== 'object' ) {
                 value = math.unit( value );
             }
             }


             if( this.hasUnits() ) {
             for( var iWeightCalculation in weightCalculationIds ) {
                 var valueUnits = value.formatUnits();
                 var weightCalculationId = weightCalculationIds[ iWeightCalculation ];
                var weightCalculation = mw.calculators.getCalculation( weightCalculationId );


                 if( !valueUnits ) {
                 if( !weightCalculation ) {
                     throw new Error( 'Could not set value for "' + this.id + '": Value must define units' );
                     throw new Error( 'Drug dose references weight calculation "' + weightCalculationId + '" which is not defined' );
                } else if( this.units.indexOf( valueUnits ) === -1 ) {
                    throw new Error( 'Could not set value for "' + this.id + '": Units "' + valueUnits + '" are not valid for this variable' );
                 }
                 }
                this.weightCalculation.push( weightCalculation );
             }
             }
         } else if( this.hasOptions() ) {
         } else {
             if( !this.options.hasOwnProperty( value ) ) {
             this.weightCalculation = [];
                throw new Error( 'Could not set value "' + value + '" for "' + this.id + '": Value must define be one of: ' + Object.keys( this.options ).join( ', ' ) );
            }
         }
         }


         return true;
         var mathProperties = this.getMathProperties();
    };
        var isWeightDependent = false;
 
        for( var iMathProperty in mathProperties ) {
            var mathProperty = mathProperties[ iMathProperty ];
 
            if( this[ mathProperty ] ) {
                // TODO consider making a UnitsBase.weight.fromString()
                this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' );
                this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' );


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


        if( value !== null ) {
                if( mw.calculators.isValueDependent( this[ mathProperty ], 'weight' ) ) {
            if( this.type === TYPE_NUMBER ) {
                     isWeightDependent = true;
                if( typeof value !== 'object' ) {
                     value = math.unit( value );
                 }
                 }
            } else {
                this[ mathProperty ] = null;
             }
             }
         }
         }


         return value;
         if( isWeightDependent ) {
            // Default is tbw
            this.weightCalculation.push( mw.calculators.getCalculation( 'tbw' ) );
        }
     };
     };


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


        this.valueUpdated();
    mw.calculators.objectClasses.DrugDose.prototype.getAdministration = function() {
        var administration = '';


         return true;
         if( this.frequency ) {
    };
            administration += administration ? ' ' : '';
            administration += this.frequency;
        }


    mw.calculators.objectClasses.Variable.prototype.valueUpdated = function() {
         if( this.duration ) {
         for( var iCalculation in this.calculations ) {
             administration += administration ? ' ' : '';
             var calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
             administration += 'over ' + this.duration;
 
             if( calculation ) {
                calculation.render();
            }
         }
         }
    }


 
         return administration;
    /**
    * 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.DrugDose.prototype.getCalculationData = function() {
        var calculationData = new mw.calculators.objectClasses.CalculationData();


    mw.calculators.objectClasses.AbstractCalculation.prototype.addCalculation = function( calculationId ) {
        for( var iWeightCalculation in this.weightCalculation ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            calculationData.calculations.optional.push( this.weightCalculation[ iWeightCalculation ].id );
            return;
         }
         }


         this.calculations.push( calculationId );
         return calculationData;
     };
     };


     mw.calculators.objectClasses.AbstractCalculation.prototype.doRender = function() {};
     mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
 
         return [
    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerClass = function() {
            'dose',
         return 'calculator-calculation-' + this.id;
            'min',
    };
            'max',
 
            'absoluteMax'
    mw.calculators.objectClasses.AbstractCalculation.prototype.getLabelString = function() {
         ];
         return this.id;
     };
     };


     mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties = function() {
     mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
         return {
         return {
             required: [
             required: [
                 'id',
                 'id'
                'calculate'
             ],
             ],
             optional: [
             optional: [
                 'data',
                 'absoluteMax',
                 'description',
                 'dose',
                 'onRender',
                 'duration',
                 'onRendered',
                 'frequency',
                 'references',
                 'min',
                 'type'
                 'max',
                'name',
                'weightCalculation'
             ]
             ]
         };
         };
     };
     };


    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.getDrugDosageCalculationId = function( drugId ) {
         return 'drugDosages-' + drugId;
     };
     };


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


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


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


    mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationData = function() {
        if( !data.drug.dosages.length ) {
        return this.data;
            value.message = 'No dose data';
    };


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


         var data = {};
         // Determine which dosage to use
         var missingRequiredData = '';
         var populationScores = [];
        var calculationId, calculation, variableId, variable;


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


             if( !calculation ) {
             // If the indication and route do not match, set the score to -1
                 throw new Error( 'Invalid required calculation "' + calculationId + '" for calculation "' + this.id + '"' );
            var populationScore =
            } else if( !calculation.hasValue() ) {
                 drugDosage.indication.id === data.indication.id && drugDosage.route.id === data.route.id ?
                 if( missingRequiredData ) {
                 drugDosage.population.getCalculationDataScore( data ) : -1;
                    missingRequiredData = missingRequiredData + ', ';
                }


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


         for( var iRequiredVariable in calculationData.variables.required ) {
         var maxPopulationScore = Math.max.apply( null, populationScores );
            variableId = calculationData.variables.required[ iRequiredVariable ];
            variable = mw.calculators.getVariable( variableId );


            if( !variable ) {
        if( maxPopulationScore < 0 ) {
                throw new Error( 'Invalid required variable "' + variableId + '" for calculation "' + this.id + '"' );
            value.message = 'No dose data for indication "' + String( data.indication ) + '" and route "' + String( data.route ) + '"';
            } else if( !variable.hasValue() ) {
                if( missingRequiredData ) {
                    missingRequiredData = missingRequiredData + ', ';
                }


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


         if( missingRequiredData ) {
         // If there is more than one dosage with the same score, take the first.
            this.message = missingRequiredData + ' required';
        // This allows the data editor to decide which is most important.
        value.dosageId = populationScores.indexOf( maxPopulationScore );


             return false;
        var dosage = data.drug.dosages[ value.dosageId ];
        }
 
        // A dosage may contain multiple doses (e.g. induction and maintenance)
        for( var iDose in dosage.dose ) {
            var dose = dosage.dose[ iDose ];
             var mathProperties = dose.getMathProperties();
 
            var weightCalculation = null;
            var weightValue = null;


        for( var iOptionalCalculation in calculationData.calculations.optional ) {
            // data.weightCalculation should be in order of preference, so take the first non-null value
            calculationId = calculationData.calculations.optional[ iOptionalCalculation ];
            for( var iWeightCalculation in dose.weightCalculation ) {
            calculation = mw.calculators.getCalculation( calculationId );
                if( dose.weightCalculation[ iWeightCalculation ].value !== null ) {
                    weightCalculation = dose.weightCalculation[ iWeightCalculation ];
                    weightValue = dose.weightCalculation[ iWeightCalculation ].value;


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


             data[ calculationId ] = calculation.hasValue() ? calculation.value : null;
             // Initialize value properties for dose
        }
            value.dose[ iDose ] = {
                massPerWeight: {},
                mass: {},
                volume: {},
                weightCalculation: weightCalculation ? weightCalculation : null
            };
 
            var massUnits;
            var volumeUnits;
 
            for( var iMathProperty in mathProperties ) {
                var mathProperty = mathProperties[ iMathProperty ];


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


            if( !variable ) {
                if( doseValue ) {
                throw new Error( 'Invalid optional variable "' + variableId + '" for calculation "' + this.id + '"' );
                    var doseUnitsByBase = mw.calculators.getUnitsByBase( doseValue );
            }


            data[ variableId ] = variable.hasValue() ? variable.getValue() : null;
                    if( doseUnitsByBase.hasOwnProperty( 'weight' ) ) {
        }
                        value.dose[ iDose ].massPerWeight[ mathProperty ] = doseValue;


        return data;
                        if( weightValue ) {
    };
                            massUnits = doseUnitsByBase.mass;


    mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
                            if( doseUnitsByBase.hasOwnProperty( 'time' ) ) {
        if( typeof this.calculate !== 'function' ) {
                                massUnits += '/' + doseUnitsByBase.time;
            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
                            // For whatever reason math.format will simplify the units, but math.formatUnits will not
        this.calculations = [];
                            // as a hack, we recreate a new unit value with the correct formatting of the result
                            value.dose[ iDose ].mass[ mathProperty ] = math.unit( math.multiply( doseValue, weightValue ).format() ).to( massUnits );
                        }
                    } else {
                        value.dose[ iDose ].mass[ mathProperty ] = doseValue;
                    }


        this.data = new mw.calculators.objectClasses.CalculationData( this.getCalculationData() );
                    if( data.preparation && value.dose[ iDose ].mass[ mathProperty ] ) {
                        // Same hack as above to get units to simplify correctly
                        var preparationUnitsByBase = mw.calculators.getUnitsByBase( data.preparation.concentration );


        this.type = this.type ? this.type : TYPE_NUMBER;
                        volumeUnits = preparationUnitsByBase.volume;


        this.message = null;
                        if( doseUnitsByBase.hasOwnProperty( 'time' ) ) {
        this.value = null;
                            volumeUnits += '/' + doseUnitsByBase.time;
    };
                        }


    mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
                        value.dose[ iDose ].volume[ mathProperty ] = math.unit( math.multiply( value.dose[ iDose ].mass[ mathProperty ], math.divide( 1, data.preparation.concentration ) ).format() ).to( volumeUnits );
        return mw.calculators.isValueMathObject( this.value );
                    }
    };
                }
            }


    mw.calculators.objectClasses.AbstractCalculation.prototype.recalculate = function() {
            if( value.dose[ iDose ].mass.hasOwnProperty( 'absoluteMax' ) ) {
        this.message = '';
                if( value.dose[ iDose ].mass.hasOwnProperty( 'min' ) && math.smaller( value.dose[ iDose ].mass.absoluteMax, value.dose[ iDose ].mass.min ) ) {
        this.value = null;
                    // Both min and max are larger than the absolute max dose, so just convert to single dose.
                    value.dose[ iDose ].mass.dose = value.dose[ iDose ].mass.absoluteMax;


        var data = this.getCalculationDataValues();
                    delete value.dose[ iDose ].mass.min;
                    delete value.dose[ iDose ].mass.max;


        if( data === false ) {
                    if( value.dose[ iDose ].volume.hasOwnProperty( 'absoluteMax' ) ) {
            this.valueUpdated();
                        value.dose[ iDose ].volume.dose = value.dose[ iDose ].volume.absoluteMax;


            return false;
                        delete value.dose[ iDose ].volume.min;
        }
                        delete value.dose[ iDose ].volume.max;
                    }
                } else if( value.dose[ iDose ].mass.hasOwnProperty( 'max' ) && math.smaller( value.dose[ iDose ].mass.absoluteMax, value.dose[ iDose ].mass.max ) ) {
                    value.dose[ iDose ].mass.max = value.dose[ iDose ].mass.absoluteMax;


        try {
                    if( value.dose[ iDose ].volume.hasOwnProperty( 'absoluteMax' ) ) {
            var value = this.calculate( data );
                        value.dose[ iDose ].volume.max = value.dose[ iDose ].volume.absoluteMax;
                    }
                } else if( value.dose[ iDose ].mass.hasOwnProperty( 'dose' ) && math.smaller( value.dose[ iDose ].mass.absoluteMax, value.dose[ iDose ].mass.dose ) ) {
                    value.dose[ iDose ].mass.dose = value.dose[ iDose ].mass.absoluteMax;


            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                    if( value.dose[ iDose ].volume.hasOwnProperty( 'absoluteMax' ) ) {
                if( this.units ) {
                        value.dose[ iDose ].volume.dose = value.dose[ iDose ].volume.absoluteMax;
                    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;
         return value;
     };
     };


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.doRender = function() {
        var $calculationContainer = $( '.' + this.getContainerClass() );
        if( !$calculationContainer.length ) {
            return;
        }


        $calculationContainer.empty();


    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
        // Drug label
         this.recalculate();
        var drugLabelAttributes = {
            class: 'calculator-DrugDosageCalculator-drug-cell'
         };


         if( typeof this.onRender === 'function' ) {
         var $drugLabel = $( '<div>', drugLabelAttributes );
            this.onRender();
        }


         this.doRender();
         $drugLabel.append( this.getLabelHtml() );


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


    mw.calculators.objectClasses.AbstractCalculation.prototype.setDependencies = function() {
        // Dose column
         this.data = this.getCalculationData();
        var $dose = $( '<div>', {
            class: 'col-8 calculator-DrugDosageCalculator-dose-cell'
         );


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


         for( var iCalculationId in calculationIds ) {
         // The options column should only show the preparation if there is a calculated volume
            var calculationId = calculationIds[ iCalculationId ];
        var hasVolume;


             if( !mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
        if( !this.value || this.value.dosageId === null ) {
                 throw new Error('Calculation "' + calculationId + '" does not exist for calculation "' + this.id + '"');
             if( this.value && this.value.hasOwnProperty( 'message' ) ) {
                 $dose.append( $( '<i>' ).append( this.value.message ) );
             }
             }
        } else {
            var dosage = this.drug.dosages[ this.value.dosageId ];


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


        var variableIds = this.data.variables.required.concat( this.data.variables.optional );
            var $doseInfo = $( '<div>', {
                class: 'calculator-DrugDosageCalculator-dose-info'
            } );


        for( var iVariableId in variableIds ) {
            if( dosage.population && dosage.population.id !== DEFAULT_DRUG_POPULATION ) {
            var variableId = variableIds[ iVariableId ];
                $doseInfo
                    .append( $( '<div>', {
                        class: 'calculator-DrugDosageCalculator-dose-info-population'
                    } ).append( String( dosage.population ) + ' dosing' ) );


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


             mw.calculators.variables[ variableId ].addCalculation( this.id );
             if( dosage.hasInfo() ) {
        }
                var doseInfoText = mw.calculators.isMobile() ? 'Dosage info' : 'Dosage information';
 
                var $doseInfoLink = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + this.getContainerClass() + '-dose-info-row',
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': this.getContainerClass() + '-dose-info-row'
                } )
                    .append( doseInfoText + '&nbsp;' )
                    .append( $( '<i>', {
                        class: 'far fa-question-circle'
                    } ) );


        this.recalculate();
                $doseInfo
    };
                    .append( $( '<div>', {
                        class: 'calculator-DrugDosageCalculator-dose-info-button'
                    } ).append( $doseInfoLink ) );


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


             if( calculation ) {
             if( showInfo ) {
                 calculation.render();
                 $dose.append( $doseInfo );
             }
             }
        }
    };


            var $doseData = $( '<div>', {
                class: 'calculator-DrugDosageCalculator-dose-data'
            } );
            // This will iterate through the calculated doses. iDose should exactly correspond to doses within dosage
            // to allow referencing other properties of the dose.
            for( var iDose in this.value.dose ) {
                var dose = dosage.dose[ iDose ];
                var doseValue = this.value.dose[ iDose ];
                if( dose.name ) {
                    $doseData.append( dose.name + '<br />' );
                }


                var $doseList = $( '<ul>' );


    /**
                var administration = dose.getAdministration();
    * Class CalculationData
                var administrationDisplayed = false;
    * @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 massPerWeightHtml = '';
        var dataRequirements = this.getDataRequirements();


        // Iterate through the supported data types (e.g. calculation, variable) to initialize the structure
                if( doseValue.massPerWeight.hasOwnProperty( 'dose' ) ) {
        for( var iDataType in dataTypes ) {
                    massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.dose );
            var dataType = dataTypes[ iDataType ];
                } else if( doseValue.massPerWeight.hasOwnProperty( 'min' ) &&
                    doseValue.massPerWeight.hasOwnProperty( 'max' ) ) {


            if( !this[ dataType ] ) {
                     // getValueString will simplify the value and may adjust the units
                this[ dataType ] = {
                    var massPerWeightMinValue = math.unit( mw.calculators.getValueString( doseValue.massPerWeight.min ) );
                     optional: [],
                     var massPerWeightMaxValue = math.unit( mw.calculators.getValueString( doseValue.massPerWeight.max ) );
                    required: []
                };
            } else {
                // Iterate through the requirement levels (i.e. optional, required) to initialize the structure
                for( var iDataRequirement in dataRequirements ) {
                     var dataRequirement = dataRequirements[ iDataRequirement ];


                     if( this[ dataType ].hasOwnProperty( dataRequirement ) ) {
                     if( massPerWeightMinValue.formatUnits() !== massPerWeightMaxValue.formatUnits() ) {
                         for( var iDataId in this[ dataType ][ dataRequirement ] ) {
                         // If the units between min and max don't match, show both
                            var dataId = this[ dataType ][ dataRequirement ][ iDataId ];
                        massPerWeightHtml += mw.calculators.getValueString( massPerWeightMinValue );
                        }
                     } else {
                     } else {
                         this[ dataType ][ dataRequirement ] = [];
                         massPerWeightHtml += mw.calculators.getValueNumber( massPerWeightMinValue );
                     }
                     }
                    massPerWeightHtml += dash;
                    massPerWeightHtml += mw.calculators.getValueString( massPerWeightMaxValue );
                 }
                 }
            }
        }
    };


    mw.calculators.objectClasses.CalculationData.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                if( massPerWeightHtml ) {
                    if( administration && ! administrationDisplayed ) {
                        massPerWeightHtml += ' ' + administration;
                        administrationDisplayed = true;
                    }
 
                    var massPerWeightNotesHtml = '';
 
                    if( doseValue.mass.hasOwnProperty( 'absoluteMax' ) ) {
                        massPerWeightNotesHtml += 'Max: ' + mw.calculators.getValueString( doseValue.mass.absoluteMax );
                    }


    mw.calculators.objectClasses.CalculationData.prototype.getDataRequirements = function() {
                    if( dose.weightCalculation && dose.weightCalculation[ 0 ].id !== 'tbw' ) {
        return [
                        if( massPerWeightNotesHtml ) {
            'optional',
                            massPerWeightNotesHtml += ', ';
            'required'
                        }
        ];
    };


    mw.calculators.objectClasses.CalculationData.prototype.getDataTypes = function() {
                        massPerWeightNotesHtml += dose.weightCalculation[ 0 ].getLabelString();
        return [
                    }
            'calculations',
            'variables'
        ];
    };


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


                    massPerWeightHtml = $( '<li>' ).append( massPerWeightHtml );


                    $doseList.append( massPerWeightHtml );
                }


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


        var data = [ this ].concat( Array.prototype.slice.call( arguments ) );
                if( doseValue.mass.hasOwnProperty( 'dose' ) ) {
                    massHtml += mw.calculators.getValueString( doseValue.mass.dose );
                } else if( doseValue.mass.hasOwnProperty( 'min' ) &&
                    doseValue.mass.hasOwnProperty( 'max' ) ) {


        var dataTypes = this.getDataTypes();
                    // getValueString will simplify the value and may adjust the units
                    var massMinValue = math.unit( mw.calculators.getValueString( doseValue.mass.min ) );
                    var massMaxValue = math.unit( mw.calculators.getValueString( doseValue.mass.max ) );


        for( var iData in data ) {
                    if( massMinValue.formatUnits() !== massMaxValue.formatUnits() ) {
            for( var iDataType in dataTypes ) {
                        // If the units between min and max don't match, show both
                var dataType = dataTypes[ iDataType ];
                        massHtml += mw.calculators.getValueString( massMinValue );
                    } else {
                        massHtml += mw.calculators.getValueNumber( massMinValue );
                    }


                mergedData[ dataType ].required = mergedData[ dataType ].required
                    massHtml += dash;
                     .concat( data[ iData ][ dataType ].required )
                     massHtml += mw.calculators.getValueString( massMaxValue );
                    .filter( mw.calculators.uniqueValues );
                }


                 mergedData[ dataType ].optional = mergedData[ dataType ].optional
                 if( massHtml ) {
                    .concat( data[ iData ][ dataType ].optional )
                     if( administration && ! administrationDisplayed ) {
                     .filter( mw.calculators.uniqueValues );
                        massHtml += ' ' + administration;
            }
                        administrationDisplayed = true;
        }
                    }


        return mergedData;
                    if( dose.weightCalculation.length && doseValue.weightCalculation.id !== dose.weightCalculation[ 0 ].id ) {
    };
                        var weightCalculationLabel = doseValue.weightCalculation.getLabelString();


                        massHtml += '&nbsp; (' + weightCalculationLabel + '&nbsp;' + $( '<i>', {
                            class: 'far fa-question-circle'
                        } )[ 0 ].outerHTML + ')';
                    }


                    massHtml = $( '<li>' ).append( massHtml );


                    $doseList.append( massHtml );
                }


                var volumeHtml = '';


    /**
                if( doseValue.volume.hasOwnProperty( 'dose' ) ) {
    * Class SimpleCalculation
                    volumeHtml += mw.calculators.getValueString( doseValue.volume.dose );
    * @param {Object} propertyValues
                } else if( doseValue.volume.hasOwnProperty( 'min' ) &&
    * @returns {mw.calculators.objectClasses.SimpleCalculation}
                    doseValue.volume.hasOwnProperty( 'max' ) ) {
    * @constructor
    */
    mw.calculators.objectClasses.SimpleCalculation = function( propertyValues ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );


        this.initialize();
                    // getValueString will simplify the value and may adjust the units
    };
                    var volumeMinValue = math.unit( mw.calculators.getValueString( doseValue.volume.min ) );
                    var volumeMaxValue = math.unit( mw.calculators.getValueString( doseValue.volume.max ) );


    mw.calculators.objectClasses.SimpleCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
                    if( volumeMinValue.formatUnits() !== volumeMaxValue.formatUnits() ) {
                        // If the units between min and max don't match, show both
                        volumeHtml += mw.calculators.getValueString( volumeMinValue );
                    } else {
                        volumeHtml += mw.calculators.getValueNumber( volumeMinValue );
                    }


                    volumeHtml += dash;
                    volumeHtml += mw.calculators.getValueString( doseValue.volume.max );
                }


    mw.calculators.objectClasses.SimpleCalculation.prototype.hasInfo = function() {
                if( volumeHtml ) {
        return this.description || this.formula || this.references.length;
                    if( administration && ! administrationDisplayed ) {
    };
                        volumeHtml += ' ' + administration;
                        administrationDisplayed = true;
                    }


    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelHtml = function() {
                    volumeHtml = $( '<li>' ).append( volumeHtml );
        var labelHtml = this.getLabelString();


        if( this.link ) {
                    $doseList.append( volumeHtml );
            var href = this.link;


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


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


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


        return labelHtml;
    };


    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelString = function() {
        // Options column
         return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
        var $options = $( '<div>', {
    };
            class: 'col-4 calculator-DrugDosageCalculator-options-cell'
        } );
 
         var indications = this.drug.getIndications();


    mw.calculators.objectClasses.SimpleCalculation.prototype.getProperties = function() {
        if( indications.length ) {
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
            $options.append( mw.calculators.getVariable( this.getVariableIds().indication ).createInput({
                class: 'calculator-container-input-DrugDosageCalculator-options',
                hideLabelMobile: true,
                inline: true
            } ) );
        }


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


    mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
         if( routes.length ) {
         if( this.message ) {
             $options.append( mw.calculators.getVariable( this.getVariableIds().route  ).createInput({
             return this.message;
                class: 'calculator-container-input-DrugDosageCalculator-options',
        } else if( typeof this.value === 'object' && this.value.hasOwnProperty( 'value' ) ) {
                hideLabelMobile: true,
            return mw.calculators.getValueString( this.value );
                inline: true
        } else {
             } ) );
             return String( this.value );
         }
         }
    };


    mw.calculators.objectClasses.SimpleCalculation.prototype.doRender = function() {
        // Don't show preparations if there isn't a dose with volume
        var $calculationContainer = $( '.' + this.getContainerClass() );
        if( hasVolume ) {
            var preparations = this.drug.getPreparations();


        if( !$calculationContainer.length ) {
            if( preparations.length ) {
             return;
                $options.append( mw.calculators.getVariable( this.getVariableIds().preparation  ).createInput({
                    class: 'calculator-container-input-DrugDosageCalculator-options',
                    hideLabelMobile: true,
                    inline: true
                } ) );
             }
         }
         }


         var valueString = this.getValueString();
         $calculationContainer
            .append( $( '<div>', {
                    class: 'col-12 border'
                } ).append(
                    $drugLabel,
                    $( '<div>', {
                        class: 'row calculator-DrugDosageCalculator-dosage-row'
                    } )
                        .append(
                            $dose,
                            $options
                        )
                )
            );


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


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


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


         var calculation = this;
         var calculation = this;
Line 1,355: Line 1,279:
             $( this ).empty();
             $( this ).empty();


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


             var $infoButton = null;
             var $infoButton = null;


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


            var labelHtml = calculation.getLabelHtml();
                 $label
 
                     .append( $( '<span>', {
            if( isTable ) {
                            class: 'calculator-calculation-column-label-info'
                if( calculation.hasInfo() ) {
                         } )
                    labelHtml += $( '<span>', {
                            .append( $infoButton )
                        class: 'calculator-SimpleCalculator-info'
                     );
                    } ).append( $infoButton )[ 0 ].outerHTML;
                }
 
                 $( this )
                     .append( $( '<th>', {
                        class: 'calculator-SimpleCalculator-calculation-cell',
                         html: labelHtml
                    } ) )
                    .append( $( '<td>', {
                        class: 'calculator-SimpleCalculator-value-cell',
                        html: valueString
                    } ) );
            } else {
                $( this )
                     .append( labelHtml + $infoButton[ 0 ].outerHTML + ': ' + valueString );
             }
             }


Line 1,435: Line 1,344:
                 }
                 }


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


                 $( this ).after( $infoContainer );
                 $( this ).after( $infoContainer );
             }
             }
        } );
    };
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();
        // Add variables created by this calculation
        var variableIds = this.getVariableIds();
        for( var variableType in variableIds ) {
            inputData.variables.optional.push( variableIds[ variableType ] );
        }
        var dataTypes = inputData.getDataTypes();
        // Data is only actually required if it is required by every dosage for the drug.
        // Data marked as required by an individual dosage that does not appear in every
        // 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
        // is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't
        // reinitialize).
        var initializeRequiredData = true;
        // Iterate through each dosage to determine variable dependency
        for( var iDosage in this.drug.dosages ) {
            var dosageInputData = this.drug.dosages[ iDosage ].getCalculationData();


             if( missingVariableInputs.length ) {
             inputData = inputData.merge( dosageInputData );
                var variablesContainerClass = 'calculator-SimpleCalculator-variables ' + calculation.getContainerClass() + '-variables';
 
                 var inputGroup = mw.calculators.createInputGroup( missingVariableInputs );
            for( var iDataType in dataTypes ) {
                 var dataType = dataTypes[ iDataType ];


                 if( isTable ) {
                 if( initializeRequiredData ) {
                     $variablesContainer = $( '<tr>' )
                     requiredInputData[ dataType ].required = inputData[ dataType ].required;
                        .append( $( '<td>', {
                            class: variablesContainerClass,
                            colspan: 2
                        } ).append( inputGroup ) );
                 } else {
                 } else {
                     $variablesContainer = $( '<div>', {
                     // Data is only truly required if it is required by all dosage calculations, so use array intersection
                         class: variablesContainerClass
                    requiredInputData[ dataType ].required = requiredInputData[ dataType ].required.filter( function( index ) {
                     } ).append( inputGroup );
                         return dosageInputData[ dataType ].required.indexOf( index ) !== -1;
                     } );
                 }
                 }
            }


                $( this ).after( $variablesContainer );
            initializeRequiredData = false;
        }


                 missingVariableInputs = [];
        for( var iDataType in dataTypes ) {
             }
            var dataType = dataTypes[ iDataType ];
         } );
 
            // Move any data marked required in inputData to optional if it not actually required (i.e. doesn't appear
            // in requiredInputData).
            inputData[ dataType ].optional = inputData[ dataType ].optional.concat( inputData[ dataType ].required.filter( function( index ) {
                 return requiredInputData[ dataType ].required.indexOf( index ) === -1;
             } ) ).filter( mw.calculators.uniqueValues );
 
            inputData[ dataType ].required = requiredInputData[ dataType ].required;
         }
 
        return inputData;
     };
     };


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationDataValues = function() {
        var data = mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues.call( this );
        data.drug = this.drug;
        data.indication = data[ this.getVariablePrefix() + 'indication' ] !== null ?
            mw.calculators.getDrugIndication( mw.calculators.getVariable( this.getVariableIds().indication ).getValue() ) :
            null;


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


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


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


    /**
        data.route = data[ this.getVariablePrefix() + 'route' ] !== null ?
    * Class AbstractCalculator
            mw.calculators.getDrugRoute( mw.calculators.getVariable( this.getVariableIds().route ).getValue() ) :
    * @param {Object} propertyValues
            null;
    * @returns {mw.calculators.objectClasses.AbstractCalculator}
 
    * @constructor
        delete data[ this.getVariablePrefix() + 'route' ];
    */
 
    mw.calculators.objectClasses.AbstractCalculator = function( propertyValues ) {
        return data;
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
     };
     };


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


     mw.calculators.objectClasses.AbstractCalculator.prototype.getCalculatorClass = function() {
     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() {
         return '';
         var $label = $( '<a>', {
            class: 'calculator-DrugDosageCalculator-drug-name',
            href: mw.util.getUrl( this.drug.name ),
            text: this.drug.name
        } ).css( 'background-color', '#fff' );
 
        var highlightColor = this.drug.color.getHighlightColor();
 
        if( highlightColor ) {
            var highlightContainerAttributes = {
                class: 'calculator-DrugDosageCalculator-drug-highlight'
            };
 
            var highlightContainerCss = {};
 
            highlightContainerCss[ 'background' ] = highlightColor;
 
            $label = $( '<span>', highlightContainerAttributes ).append( $label ).css( highlightContainerCss );
        }
 
        var primaryColor = this.drug.color.getPrimaryColor();
 
        if( primaryColor ) {
            var backgroundContainerAttributes = {
                class: 'calculator-DrugDosageCalculator-drug-background'
            };
 
            var backgroundContainerCss = {};
 
            if( this.drug.color.isStriped() ) {
                backgroundContainerCss[ '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),' + primaryColor;
            } else {
                backgroundContainerCss[ 'background'] = primaryColor;
            }
 
            $label = $( '<span>', backgroundContainerAttributes ).append( $label ).css( backgroundContainerCss );
        }
 
        return $label;
     };
     };


     mw.calculators.objectClasses.AbstractCalculator.prototype.getContainerClass = function() {
     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getProperties = function() {
         return 'calculator-' + this.module + '-' + this.id;
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
 
         return this.mergeProperties( inheritedProperties, {
            required: [
                'drug'
            ],
            optional: []
        } );
     };
     };


     mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties = function() {
     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableIds = function() {
         return {
         return {
             required: [
             indication: this.getVariablePrefix() + 'indication',
                'id',
            preparation: this.getVariablePrefix() + 'preparation',
                'module',
             route: this.getVariablePrefix() + 'route'
                'name',
                'calculations'
            ],
             optional: [
                'onRender',
                'onRendered'
            ]
         };
         };
     };
     };


     mw.calculators.objectClasses.AbstractCalculator.prototype.render = function() {
     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableOptions = function( variableId ) {
         if( typeof this.onRender === 'function' ) {
         if( variableId === this.getVariablePrefix() + 'indication' ) {
             this.onRender();
            return this.drug.getIndications();
        } else if( variableId === this.getVariablePrefix() + 'preparation' ) {
            // Exclude preparations which require dilution
            return this.drug.getPreparations( true );
        } else if( variableId === this.getVariablePrefix() + 'route' ) {
             return this.drug.getRoutes();
         }
         }
    };
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariablePrefix = function() {
        return this.drug.id + '-';
    };
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() {
        if( typeof this.drug === 'string' ) {
            var drug = mw.calculators.getDrug( this.drug );


        this.doRender();
            if( !drug ) {
                throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' );
            }


        if( typeof this.onRendered === 'function' ) {
            this.drug = drug;
            this.onRendered();
         }
         }
        this.updateVariables();
        mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
     };
     };


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.updateVariables = function() {
        var variableIds = this.getVariableIds();


    mw.calculators.objectClasses.AbstractCalculator.prototype.doRender = function() {};
        for( var variableType in variableIds ) {
            var variableId = variableIds[ variableType ];
            var variableOptions = this.getVariableOptions( variableId );
            var variableOptionValues = {};
            var defaultOption = 0;


            for( var iVariableOption in variableOptions ) {
                var variableOption = variableOptions[ iVariableOption ];


                defaultOption = variableOption.default ? iVariableOption : defaultOption;
                variableOptionValues[ variableOption.id ] = String( variableOption );
            }
            var defaultValue = variableOptions.length ? variableOptions[ defaultOption ].id : null;
            var variable = mw.calculators.getVariable( variableId );
            if( !variable ) {
                var newVariable = {};
                newVariable[ variableId ] = {
                    name: variableType.charAt(0).toUpperCase() + variableType.slice(1),
                    type: 'string',
                    defaultValue: defaultValue,
                    options: variableOptionValues
                };
                mw.calculators.addVariables( newVariable );
            } else {
                // Probably not ideal to reach into the variable to change these things directly
                // Perhaps add helper functions to variable class
                mw.calculators.variables[ variableId ].defaultValue = defaultValue;
                mw.calculators.variables[ variableId ].options = variableOptionValues;
            }
        }
    };
    mw.calculators.addDrugCalculators = function( moduleId, drugCalculatorData, className ) {
        className = className ? className : 'DrugDosageCalculator';
        for( var drugCalculatorId in drugCalculatorData ) {
            drugCalculatorData[ drugCalculatorId ].module = moduleId;
            for( var iCalculation in drugCalculatorData[ drugCalculatorId].calculations ) {
                drugCalculatorData[ drugCalculatorId].calculations[ iCalculation ] = moduleId + '-' +
                    drugCalculatorData[ drugCalculatorId].calculations[ iCalculation ];
            }
        }
        mw.calculators.addCalculators( moduleId, drugCalculatorData, className );
    };






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


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


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


Line 1,554: Line 1,619:


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


         $calculatorContainer.empty();
         $calculatorContainer.empty();
Line 1,565: Line 1,626:
         } ) );
         } ) );


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


         $calculatorContainer.append( $calculationsContainer );
         $calculatorContainer.append( $calculationsContainer );
Line 1,591: Line 1,634:
         for( var iCalculationId in this.calculations ) {
         for( var iCalculationId in this.calculations ) {
             var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
             var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
             var calculationContainerClass = calculation.getContainerClass();
             var calculationContainerClass = 'row no-gutters ' + calculation.getContainerClass();


             var $calculationContainer = $( '.' + calculationContainerClass );
             var $calculationContainer = $( '<div>', {
                class: calculationContainerClass
            } );


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


             calculation.render();
             calculation.render();
Line 1,614: Line 1,646:
     };
     };


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


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


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


}() );
}() );

Revision as of 23:13, 21 August 2021

/**
 * @author Chris Rishel
 */
( function() {
    var DEFAULT_DRUG_COLOR = 'default';
    var DEFAULT_DRUG_POPULATION = 'general';
    var DEFAULT_DRUG_ROUTE = 'iv';

    mw.calculators.isValueDependent = function( value, variableId ) {
        // This may need generalized to support other variables in the future
        if( variableId === 'weight' ) {
            return value && value.formatUnits().match( /\/[\s(]*?kg/ );
        } else {
            throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' );
        }
    };

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

                return units;
            }
        },
        mass: {
            toString: function( units ) {
                units = units.replace( 'ug', 'mcg' );

                return units;
            }
        }
    } );

    mw.calculators.addUnits( {
        mcg: {
            baseName: 'mass',
            definition: '1 ug'
        },
        pct: {
            baseName: 'concentration',
            definition: '10 mg/mL'
        },
        vial: {
            baseName: 'volume'
        }
    } );



    /**
     * DrugColor
     */
    mw.calculators.drugColors = {};

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

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

    mw.calculators.getDrugColor = function( drugColorId ) {
        if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
            return mw.calculators.drugColors[ drugColorId ];
        } else {
            return null;
        }
    };

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

        this.parentColor = this.parentColor || this.id === DEFAULT_DRUG_COLOR ? this.parentColor : DEFAULT_DRUG_COLOR;
    };

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

    mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() {
        if( !this.parentColor ) {
            return null;
        }

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

        if( !parentDrugColor ) {
            throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
        }

        return parentDrugColor;
    };

    mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
        if( this.highlightColor ) {
            return this.highlightColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getHighlightColor();
        }
    };

    mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
        if( this.primaryColor ) {
            return this.primaryColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getPrimaryColor();
        }
    };

    mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() {
        if( this.striped !== null ) {
            return this.striped;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().isStriped();
        }
    };





    /**
     * DrugPopulation
     */

    mw.calculators.drugPopulations = {};

    mw.calculators.addDrugPopulations = function( drugPopulationData ) {
        var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );

        for( var drugPopulationId in drugPopulations ) {
            mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
        }
    };

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



    /**
     * 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( this.variables ) {
            for( var variableId in this.variables ) {
                if( !mw.calculators.getVariable( variableId ) ) {
                    throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
                }

                this.variables[ variableId ].min = this.variables[ variableId ].hasOwnProperty( 'min' ) ?
                    math.unit( this.variables[ variableId ].min ) : null;

                this.variables[ variableId ].max = this.variables[ variableId ].hasOwnProperty( 'max' ) ?
                    math.unit( this.variables[ variableId ].max ) : null;
            }
        } else {
            this.variables = {};
        }
    };

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

    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();

        for( var variableId in this.variables ) {
            inputData.variables.required.push( variableId );
        }

        return inputData;
    };

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

        for( var variableId in this.variables ) {
            if( !dataValues.hasOwnProperty( variableId ) ) {
                return -1;
            }

            if( this.variables[ variableId ].min &&
                ( !dataValues[ variableId ] ||
                    !math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) ) {
                return -1;
            }

            if( this.variables[ variableId ].max &&
                ( !dataValues[ variableId ] ||
                    !math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) ) {
                return -1;
            }
        }

        // If the data matches the population definition, the score corresponds to the number of variables in the
        // 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() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };



    /**
     * DrugRoute
     */
    mw.calculators.drugRoutes = {};

    mw.calculators.addDrugRoutes = function( drugRouteData ) {
        var drugRoutes = mw.calculators.createCalculatorObjects( 'DrugRoute', drugRouteData );

        for( var drugRouteId in drugRoutes ) {
            mw.calculators.drugRoutes[ drugRouteId ] = drugRoutes[ drugRouteId ];
        }
    };

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

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

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

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







    /**
     * DrugIndication
     */
    mw.calculators.drugIndications = {};

    mw.calculators.addDrugIndications = function( drugIndicationData ) {
        var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );

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

    mw.calculators.getDrugIndication = function( drugIndicationId ) {
        if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
            return mw.calculators.drugIndications[ drugIndicationId ];
        } else {
            return null;
        }
    };

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

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

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

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





    /**
     * Drug
     */
    mw.calculators.drugs = {};

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

        for( var drugId in drugs ) {
            mw.calculators.drugs[ drugId ] = drugs[ drugId ];

            var drugDosageCalculationId = mw.calculators.getDrugDosageCalculationId( drugId );
            var drugDosageCalculation = mw.calculators.getCalculation( drugDosageCalculationId );

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

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

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

                drugDosageCalculation = mw.calculators.getCalculation( drugDosageCalculationId );
            }

            drugDosageCalculation.setDependencies();
        }
    };

    mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
        var drug = mw.calculators.getDrug( drugId );

        if( !drug ) {
            throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
        }

        drug.addDosages( drugDosageData );

        // Update calculation dependencies
        var drugDosageCalculation = mw.calculators.getCalculation( mw.calculators.getDrugDosageCalculationId( drugId ) );

        drugDosageCalculation.updateVariables();
        drugDosageCalculation.setDependencies();
    };

    mw.calculators.getDrug = function( drugId ) {
        if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
            return mw.calculators.drugs[ drugId ];
        } else {
            return null;
        }
    };



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

        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 + '"' );
        }

        this.color = color;

        if( this.preparations ) {
            var preparationData = this.preparations;

            this.preparations = [];

            this.addPreparations( preparationData );
        } else {
            this.preparations = [];
        }

        if( this.dosages ) {
            var dosageData = this.dosages;

            this.dosages = [];

            this.addDosages( dosageData );
        } else {
            this.dosages = [];
        }
    };

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

    mw.calculators.objectClasses.Drug.prototype.addDosages = function( dosageData ) {
        var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', dosageData );

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

            this.dosages.push( dosages[ dosageId ] );
        }
    };

    mw.calculators.objectClasses.Drug.prototype.addPreparations = function( preparationData ) {
        var preparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', preparationData );

        for( var preparationId in preparations ) {
            preparations[ preparationId ].id = this.preparations.length;

            this.preparations.push( preparations[ preparationId ] );
        }
    };

    mw.calculators.objectClasses.Drug.prototype.getIndications = function() {
        var indications = [];

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

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

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

        for( var iDosage in this.dosages ) {
            if( this.dosages[ iDosage ].population &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                populations.push( this.dosages[ iDosage ].population );
            }
        }

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

    mw.calculators.objectClasses.Drug.prototype.getRoutes = function( indicationId ) {
        var routes = [];

        for( var iDosage in this.dosages ) {
            if( this.dosages[ iDosage ].route &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                routes.push( this.dosages[ iDosage ].route );
            }
        }

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

    mw.calculators.objectClasses.Drug.prototype.getPreparations = function( excludeDilutionRequired ) {
        var preparations = this.preparations.filter( mw.calculators.uniqueValues );

        if( excludeDilutionRequired ) {
            for( var iPreparation in preparations ) {
                if( preparations[ iPreparation ].dilutionRequired ) {
                    delete preparations[ iPreparation ];
                }
            }
        }

        return preparations;
    };

    mw.calculators.objectClasses.Drug.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'color',
                'dosages',
                'preparations'
            ]
        };
    };





    /**
     * DrugPreparation
     */
    mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
        var drug = mw.calculators.getDrug( drugId );

        if( !drug ) {
            throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
        }

        drug.addPreparations( drugPreparationData );

        var drugDosageCalculation = mw.calculators.getCalculation( mw.calculators.getDrugDosageCalculationId( drugId ) );

        drugDosageCalculation.recalculate();
    };



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

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


        this.concentration = this.concentration.replace( 'mcg', 'ug' );

        this.concentration = math.unit( this.concentration );
    };

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

    mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() {
        // The units of concentration will always be of the form "mass / volume"
        // The regular expression matches all text leading up to the volume units
        return mw.calculators.getUnitsByBase( this.concentration ).volume;
    };

    mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() {
        return mw.calculators.getValueString( this.concentration );
    };





    /**
     * Class DrugDosage
     * @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 );

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

        this.indication = drugIndication;

        this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION;

        var drugPopulation = mw.calculators.getDrugPopulation( this.population );

        if( !drugPopulation ) {
            throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
        }

        this.population = drugPopulation;

        this.route = this.route ? this.route : DEFAULT_DRUG_ROUTE;

        var drugRoute = mw.calculators.getDrugRoute( this.route );

        if( !drugRoute ) {
            throw new Error( 'Invalid route "' + this.route + '" for drug dosage' );
        }

        this.route = drugRoute;

        // Add the dose objects to the drug
        var drugDoseData = this.dose;
        this.dose = [];

        this.addDoses( drugDoseData );
    };

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

    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 doseId in doses ) {
            doses[ doseId ].id = this.dose.length;

            this.dose.push( doses[ doseId ] );
        }
    };

    mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();

        inputData = inputData.merge( this.population.getCalculationData() );

        for( var iDose in this.dose ) {
            inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
        }

        return inputData;
    };

    mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() {
        return {
            required: [
                'dose',
                'id',
                'indication'
            ],
            optional: [
                'description',
                'population',
                'route'
            ]
        };
    };

    mw.calculators.objectClasses.DrugDosage.prototype.hasInfo = function() {
        return this.description;
    };





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

        if( this.weightCalculation ) {
            var weightCalculationIds = this.weightCalculation;

            // weightCalculation property will contain references to the actual objects, so reinitialize
            this.weightCalculation = [];

            if( !Array.isArray( weightCalculationIds ) ) {
                weightCalculationIds = [ weightCalculationIds ];
            }

            for( var iWeightCalculation in weightCalculationIds ) {
                var weightCalculationId = weightCalculationIds[ iWeightCalculation ];
                var weightCalculation = mw.calculators.getCalculation( weightCalculationId );

                if( !weightCalculation ) {
                    throw new Error( 'Drug dose references weight calculation "' + weightCalculationId + '" which is not defined' );
                }

                this.weightCalculation.push( weightCalculation );
            }
        } else {
            this.weightCalculation = [];
        }

        var mathProperties = this.getMathProperties();
        var isWeightDependent = false;

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

            if( this[ mathProperty ] ) {
                // TODO consider making a UnitsBase.weight.fromString()
                this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' );
                this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' );

                this[ mathProperty ] = math.unit( this[ mathProperty ] );

                if( mw.calculators.isValueDependent( this[ mathProperty ], 'weight' ) ) {
                    isWeightDependent = true;
                }
            } else {
                this[ mathProperty ] = null;
            }
        }

        if( isWeightDependent ) {
            // Default is tbw
            this.weightCalculation.push( mw.calculators.getCalculation( 'tbw' ) );
        }
    };

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

    mw.calculators.objectClasses.DrugDose.prototype.getAdministration = function() {
        var administration = '';

        if( this.frequency ) {
            administration += administration ? ' ' : '';
            administration += this.frequency;
        }

        if( this.duration ) {
            administration += administration ? ' ' : '';
            administration += 'over ' + this.duration;
        }

        return administration;
    };

    mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
        var calculationData = new mw.calculators.objectClasses.CalculationData();

        for( var iWeightCalculation in this.weightCalculation ) {
            calculationData.calculations.optional.push( this.weightCalculation[ iWeightCalculation ].id );
        }

        return calculationData;
    };

    mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
        return [
            'dose',
            'min',
            'max',
            'absoluteMax'
        ];
    };

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




    mw.calculators.getDrugDosageCalculationId = function( drugId ) {
        return 'drugDosages-' + drugId;
    };

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

        this.initialize();
    };

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

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

        if( !data.drug.dosages.length ) {
            value.message = 'No dose data';

            return value;
        }

        // Determine which dosage to use
        var populationScores = [];

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

            // If the indication and route do not match, set the score to -1
            var populationScore =
                drugDosage.indication.id === data.indication.id && drugDosage.route.id === data.route.id ?
                drugDosage.population.getCalculationDataScore( data ) : -1;

            populationScores.push( populationScore );
        }

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

        if( maxPopulationScore < 0 ) {
            value.message = 'No dose data for indication "' + String( data.indication ) + '" and route "' + String( data.route ) + '"';

            return value;
        }

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

        var dosage = data.drug.dosages[ value.dosageId ];

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

            var weightCalculation = null;
            var weightValue = null;

            // data.weightCalculation should be in order of preference, so take the first non-null value
            for( var iWeightCalculation in dose.weightCalculation ) {
                if( dose.weightCalculation[ iWeightCalculation ].value !== null ) {
                    weightCalculation = dose.weightCalculation[ iWeightCalculation ];
                    weightValue = dose.weightCalculation[ iWeightCalculation ].value;

                    break;
                }
            }

            // Initialize value properties for dose
            value.dose[ iDose ] = {
                massPerWeight: {},
                mass: {},
                volume: {},
                weightCalculation: weightCalculation ? weightCalculation : null
            };

            var massUnits;
            var volumeUnits;

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

                var doseValue = dose[ mathProperty ];

                if( doseValue ) {
                    var doseUnitsByBase = mw.calculators.getUnitsByBase( doseValue );

                    if( doseUnitsByBase.hasOwnProperty( 'weight' ) ) {
                        value.dose[ iDose ].massPerWeight[ mathProperty ] = doseValue;

                        if( weightValue ) {
                            massUnits = doseUnitsByBase.mass;

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

                            // For whatever reason math.format will simplify the units, but math.formatUnits will not
                            // as a hack, we recreate a new unit value with the correct formatting of the result
                            value.dose[ iDose ].mass[ mathProperty ] = math.unit( math.multiply( doseValue, weightValue ).format() ).to( massUnits );
                        }
                    } else {
                        value.dose[ iDose ].mass[ mathProperty ] = doseValue;
                    }

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

                        volumeUnits = preparationUnitsByBase.volume;

                        if( doseUnitsByBase.hasOwnProperty( 'time' ) ) {
                            volumeUnits += '/' + doseUnitsByBase.time;
                        }

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

            if( value.dose[ iDose ].mass.hasOwnProperty( 'absoluteMax' ) ) {
                if( value.dose[ iDose ].mass.hasOwnProperty( 'min' ) && math.smaller( value.dose[ iDose ].mass.absoluteMax, value.dose[ iDose ].mass.min ) ) {
                    // Both min and max are larger than the absolute max dose, so just convert to single dose.
                    value.dose[ iDose ].mass.dose = value.dose[ iDose ].mass.absoluteMax;

                    delete value.dose[ iDose ].mass.min;
                    delete value.dose[ iDose ].mass.max;

                    if( value.dose[ iDose ].volume.hasOwnProperty( 'absoluteMax' ) ) {
                        value.dose[ iDose ].volume.dose = value.dose[ iDose ].volume.absoluteMax;

                        delete value.dose[ iDose ].volume.min;
                        delete value.dose[ iDose ].volume.max;
                    }
                } else if( value.dose[ iDose ].mass.hasOwnProperty( 'max' ) && math.smaller( value.dose[ iDose ].mass.absoluteMax, value.dose[ iDose ].mass.max ) ) {
                    value.dose[ iDose ].mass.max = value.dose[ iDose ].mass.absoluteMax;

                    if( value.dose[ iDose ].volume.hasOwnProperty( 'absoluteMax' ) ) {
                        value.dose[ iDose ].volume.max = value.dose[ iDose ].volume.absoluteMax;
                    }
                } else if( value.dose[ iDose ].mass.hasOwnProperty( 'dose' ) && math.smaller( value.dose[ iDose ].mass.absoluteMax, value.dose[ iDose ].mass.dose ) ) {
                    value.dose[ iDose ].mass.dose = value.dose[ iDose ].mass.absoluteMax;

                    if( value.dose[ iDose ].volume.hasOwnProperty( 'absoluteMax' ) ) {
                        value.dose[ iDose ].volume.dose = value.dose[ iDose ].volume.absoluteMax;
                    }
                }
            }
        }

        return value;
    };

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

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

        $calculationContainer.empty();

        // Drug label
        var drugLabelAttributes = {
            class: 'calculator-DrugDosageCalculator-drug-cell'
        };

        var $drugLabel = $( '<div>', drugLabelAttributes );

        $drugLabel.append( this.getLabelHtml() );


        // Dose column
        var $dose = $( '<div>', {
            class: 'col-8 calculator-DrugDosageCalculator-dose-cell'
        }  );

        var dash = '-';

        // The options column should only show the preparation if there is a calculated volume
        var hasVolume;

        if( !this.value || this.value.dosageId === null ) {
            if( this.value && this.value.hasOwnProperty( 'message' ) ) {
                $dose.append( $( '<i>' ).append( this.value.message ) );
            }
        } else {
            var dosage = this.drug.dosages[ this.value.dosageId ];

            var showInfo;

            var $doseInfo = $( '<div>', {
                class: 'calculator-DrugDosageCalculator-dose-info'
            } );

            if( dosage.population && dosage.population.id !== DEFAULT_DRUG_POPULATION ) {
                $doseInfo
                    .append( $( '<div>', {
                        class: 'calculator-DrugDosageCalculator-dose-info-population'
                    } ).append( String( dosage.population ) + ' dosing' ) );

                showInfo = true;
            }

            if( dosage.hasInfo() ) {
                var doseInfoText = mw.calculators.isMobile() ? 'Dosage info' : 'Dosage information';

                var $doseInfoLink = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + this.getContainerClass() + '-dose-info-row',
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': this.getContainerClass() + '-dose-info-row'
                } )
                    .append( doseInfoText + '&nbsp;' )
                    .append( $( '<i>', {
                        class: 'far fa-question-circle'
                    } ) );

                $doseInfo
                    .append( $( '<div>', {
                        class: 'calculator-DrugDosageCalculator-dose-info-button'
                    } ).append( $doseInfoLink ) );

                showInfo = true;
            }

            if( showInfo ) {
                $dose.append( $doseInfo );
            }

            var $doseData = $( '<div>', {
                class: 'calculator-DrugDosageCalculator-dose-data'
            } );

            // This will iterate through the calculated doses. iDose should exactly correspond to doses within dosage
            // to allow referencing other properties of the dose.
            for( var iDose in this.value.dose ) {
                var dose = dosage.dose[ iDose ];
                var doseValue = this.value.dose[ iDose ];

                if( dose.name ) {
                    $doseData.append( dose.name + '<br />' );
                }

                var $doseList = $( '<ul>' );

                var administration = dose.getAdministration();
                var administrationDisplayed = false;

                var massPerWeightHtml = '';

                if( doseValue.massPerWeight.hasOwnProperty( 'dose' ) ) {
                    massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.dose );
                } else if( doseValue.massPerWeight.hasOwnProperty( 'min' ) &&
                    doseValue.massPerWeight.hasOwnProperty( 'max' ) ) {

                    // getValueString will simplify the value and may adjust the units
                    var massPerWeightMinValue = math.unit( mw.calculators.getValueString( doseValue.massPerWeight.min ) );
                    var massPerWeightMaxValue = math.unit( mw.calculators.getValueString( doseValue.massPerWeight.max ) );

                    if( massPerWeightMinValue.formatUnits() !== massPerWeightMaxValue.formatUnits() ) {
                        // If the units between min and max don't match, show both
                        massPerWeightHtml += mw.calculators.getValueString( massPerWeightMinValue );
                    } else {
                        massPerWeightHtml += mw.calculators.getValueNumber( massPerWeightMinValue );
                    }

                    massPerWeightHtml += dash;
                    massPerWeightHtml += mw.calculators.getValueString( massPerWeightMaxValue );
                }

                if( massPerWeightHtml ) {
                    if( administration && ! administrationDisplayed ) {
                        massPerWeightHtml += ' ' + administration;
                        administrationDisplayed = true;
                    }

                    var massPerWeightNotesHtml = '';

                    if( doseValue.mass.hasOwnProperty( 'absoluteMax' ) ) {
                        massPerWeightNotesHtml += 'Max: ' + mw.calculators.getValueString( doseValue.mass.absoluteMax );
                    }

                    if( dose.weightCalculation && dose.weightCalculation[ 0 ].id !== 'tbw' ) {
                        if( massPerWeightNotesHtml ) {
                            massPerWeightNotesHtml += ', ';
                        }

                        massPerWeightNotesHtml += dose.weightCalculation[ 0 ].getLabelString();
                    }

                    if( massPerWeightNotesHtml ) {
                        massPerWeightHtml += ' (' + massPerWeightNotesHtml + ')';
                    }

                    massPerWeightHtml = $( '<li>' ).append( massPerWeightHtml );

                    $doseList.append( massPerWeightHtml );
                }

                var massHtml = '';

                if( doseValue.mass.hasOwnProperty( 'dose' ) ) {
                    massHtml += mw.calculators.getValueString( doseValue.mass.dose );
                } else if( doseValue.mass.hasOwnProperty( 'min' ) &&
                    doseValue.mass.hasOwnProperty( 'max' ) ) {

                    // getValueString will simplify the value and may adjust the units
                    var massMinValue = math.unit( mw.calculators.getValueString( doseValue.mass.min ) );
                    var massMaxValue = math.unit( mw.calculators.getValueString( doseValue.mass.max ) );

                    if( massMinValue.formatUnits() !== massMaxValue.formatUnits() ) {
                        // If the units between min and max don't match, show both
                        massHtml += mw.calculators.getValueString( massMinValue );
                    } else {
                        massHtml += mw.calculators.getValueNumber( massMinValue );
                    }

                    massHtml += dash;
                    massHtml += mw.calculators.getValueString( massMaxValue );
                }

                if( massHtml ) {
                    if( administration && ! administrationDisplayed ) {
                        massHtml += ' ' + administration;
                        administrationDisplayed = true;
                    }

                    if( dose.weightCalculation.length && doseValue.weightCalculation.id !== dose.weightCalculation[ 0 ].id ) {
                        var weightCalculationLabel = doseValue.weightCalculation.getLabelString();

                        massHtml += '&nbsp; (' + weightCalculationLabel + '&nbsp;' + $( '<i>', {
                            class: 'far fa-question-circle'
                        } )[ 0 ].outerHTML + ')';
                    }

                    massHtml = $( '<li>' ).append( massHtml );

                    $doseList.append( massHtml );
                }

                var volumeHtml = '';

                if( doseValue.volume.hasOwnProperty( 'dose' ) ) {
                    volumeHtml += mw.calculators.getValueString( doseValue.volume.dose );
                } else if( doseValue.volume.hasOwnProperty( 'min' ) &&
                    doseValue.volume.hasOwnProperty( 'max' ) ) {

                    // getValueString will simplify the value and may adjust the units
                    var volumeMinValue = math.unit( mw.calculators.getValueString( doseValue.volume.min ) );
                    var volumeMaxValue = math.unit( mw.calculators.getValueString( doseValue.volume.max ) );

                    if( volumeMinValue.formatUnits() !== volumeMaxValue.formatUnits() ) {
                        // If the units between min and max don't match, show both
                        volumeHtml += mw.calculators.getValueString( volumeMinValue );
                    } else {
                        volumeHtml += mw.calculators.getValueNumber( volumeMinValue );
                    }

                    volumeHtml += dash;
                    volumeHtml += mw.calculators.getValueString( doseValue.volume.max );
                }

                if( volumeHtml ) {
                    if( administration && ! administrationDisplayed ) {
                        volumeHtml += ' ' + administration;
                        administrationDisplayed = true;
                    }

                    volumeHtml = $( '<li>' ).append( volumeHtml );

                    $doseList.append( volumeHtml );

                    hasVolume = true;
                }

                $doseData.append( $doseList );
            }

            $dose.append( $doseData );
        }


        // Options column
        var $options = $( '<div>', {
            class: 'col-4 calculator-DrugDosageCalculator-options-cell'
        } );

        var indications = this.drug.getIndications();

        if( indications.length ) {
            $options.append( mw.calculators.getVariable( this.getVariableIds().indication ).createInput({
                class: 'calculator-container-input-DrugDosageCalculator-options',
                hideLabelMobile: true,
                inline: true
            } ) );
        }

        var routes = this.drug.getRoutes();

        if( routes.length ) {
            $options.append( mw.calculators.getVariable( this.getVariableIds().route  ).createInput({
                class: 'calculator-container-input-DrugDosageCalculator-options',
                hideLabelMobile: true,
                inline: true
            } ) );
        }

        // Don't show preparations if there isn't a dose with volume
        if( hasVolume ) {
            var preparations = this.drug.getPreparations();

            if( preparations.length ) {
                $options.append( mw.calculators.getVariable( this.getVariableIds().preparation  ).createInput({
                    class: 'calculator-container-input-DrugDosageCalculator-options',
                    hideLabelMobile: true,
                    inline: true
                } ) );
            }
        }

        $calculationContainer
            .append( $( '<div>', {
                    class: 'col-12 border'
                } ).append(
                    $drugLabel,
                    $( '<div>', {
                        class: 'row calculator-DrugDosageCalculator-dosage-row'
                    } )
                        .append(
                            $dose,
                            $options
                        )
                )
            );


        return;



        var calculation = this;

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




            var $infoButton = null;

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

                $label
                    .append( $( '<span>', {
                            class: 'calculator-calculation-column-label-info'
                        } )
                            .append( $infoButton )
                    );
            }

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

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

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

                    var api = new mw.Api();

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

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

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

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

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

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

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


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

    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationData = function() {
        var inputData = new mw.calculators.objectClasses.CalculationData();

        // Add variables created by this calculation
        var variableIds = this.getVariableIds();

        for( var variableType in variableIds ) {
            inputData.variables.optional.push( variableIds[ variableType ] );
        }

        var dataTypes = inputData.getDataTypes();

        // Data is only actually required if it is required by every dosage for the drug.
        // Data marked as required by an individual dosage that does not appear in every
        // 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
        // is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't
        // reinitialize).
        var initializeRequiredData = true;

        // Iterate through each dosage to determine variable dependency
        for( var iDosage in this.drug.dosages ) {
            var dosageInputData = this.drug.dosages[ iDosage ].getCalculationData();

            inputData = inputData.merge( dosageInputData );

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

                if( initializeRequiredData ) {
                    requiredInputData[ dataType ].required = inputData[ dataType ].required;
                } 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;
        }

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

            // Move any data marked required in inputData to optional if it not actually required (i.e. doesn't appear
            // in requiredInputData).
            inputData[ dataType ].optional = inputData[ dataType ].optional.concat( inputData[ dataType ].required.filter( function( index ) {
                return requiredInputData[ dataType ].required.indexOf( index ) === -1;
            } ) ).filter( mw.calculators.uniqueValues );

            inputData[ dataType ].required = requiredInputData[ dataType ].required;
        }

        return inputData;
    };

    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationDataValues = function() {
        var data = mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues.call( this );

        data.drug = this.drug;

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

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

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

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

        data.route = data[ this.getVariablePrefix() + 'route' ] !== null ?
            mw.calculators.getDrugRoute( mw.calculators.getVariable( this.getVariableIds().route ).getValue() ) :
            null;

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

        return data;
    };


    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() {
        var $label = $( '<a>', {
            class: 'calculator-DrugDosageCalculator-drug-name',
            href: mw.util.getUrl( this.drug.name ),
            text: this.drug.name
        } ).css( 'background-color', '#fff' );

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

        if( highlightColor ) {
            var highlightContainerAttributes = {
                class: 'calculator-DrugDosageCalculator-drug-highlight'
            };

            var highlightContainerCss = {};

            highlightContainerCss[ 'background' ] = highlightColor;

            $label = $( '<span>', highlightContainerAttributes ).append( $label ).css( highlightContainerCss );
        }

        var primaryColor = this.drug.color.getPrimaryColor();

        if( primaryColor ) {
            var backgroundContainerAttributes = {
                class: 'calculator-DrugDosageCalculator-drug-background'
            };

            var backgroundContainerCss = {};

            if( this.drug.color.isStriped() ) {
                backgroundContainerCss[ '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),' + primaryColor;
            } else {
                backgroundContainerCss[ 'background'] = primaryColor;
            }

            $label = $( '<span>', backgroundContainerAttributes ).append( $label ).css( backgroundContainerCss );
        }

        return $label;
    };

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

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

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

    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableOptions = function( variableId ) {
        if( variableId === this.getVariablePrefix() + 'indication' ) {
            return this.drug.getIndications();
        } else if( variableId === this.getVariablePrefix() + 'preparation' ) {
            // Exclude preparations which require dilution
            return this.drug.getPreparations( true );
        } else if( variableId === this.getVariablePrefix() + 'route' ) {
            return this.drug.getRoutes();
        }
    };

    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariablePrefix = function() {
        return this.drug.id + '-';
    };

    mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() {
        if( typeof this.drug === 'string' ) {
            var drug = mw.calculators.getDrug( this.drug );

            if( !drug ) {
                throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' );
            }

            this.drug = drug;
        }

        this.updateVariables();

        mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
    };

    mw.calculators.objectClasses.DrugDosageCalculation.prototype.updateVariables = function() {
        var variableIds = this.getVariableIds();

        for( var variableType in variableIds ) {
            var variableId = variableIds[ variableType ];
            var variableOptions = this.getVariableOptions( variableId );
            var variableOptionValues = {};
            var defaultOption = 0;

            for( var iVariableOption in variableOptions ) {
                var variableOption = variableOptions[ iVariableOption ];

                defaultOption = variableOption.default ? iVariableOption : defaultOption;

                variableOptionValues[ variableOption.id ] = String( variableOption );
            }

            var defaultValue = variableOptions.length ? variableOptions[ defaultOption ].id : null;

            var variable = mw.calculators.getVariable( variableId );

            if( !variable ) {
                var newVariable = {};

                newVariable[ variableId ] = {
                    name: variableType.charAt(0).toUpperCase() + variableType.slice(1),
                    type: 'string',
                    defaultValue: defaultValue,
                    options: variableOptionValues
                };

                mw.calculators.addVariables( newVariable );
            } else {
                // Probably not ideal to reach into the variable to change these things directly
                // Perhaps add helper functions to variable class
                mw.calculators.variables[ variableId ].defaultValue = defaultValue;
                mw.calculators.variables[ variableId ].options = variableOptionValues;
            }
        }
    };






    mw.calculators.addDrugCalculators = function( moduleId, drugCalculatorData, className ) {
        className = className ? className : 'DrugDosageCalculator';

        for( var drugCalculatorId in drugCalculatorData ) {
            drugCalculatorData[ drugCalculatorId ].module = moduleId;

            for( var iCalculation in drugCalculatorData[ drugCalculatorId].calculations ) {
                drugCalculatorData[ drugCalculatorId].calculations[ iCalculation ] = moduleId + '-' +
                    drugCalculatorData[ drugCalculatorId].calculations[ iCalculation ];
            }
        }

        mw.calculators.addCalculators( moduleId, drugCalculatorData, className );
    };



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

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

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

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

        $calculatorContainer.empty();

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

        var $calculationsContainer = $( '<div>', {
            class: 'container-fluid'
        } );

        $calculatorContainer.append( $calculationsContainer );

        for( var iCalculationId in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
            var calculationContainerClass = 'row no-gutters ' + calculation.getContainerClass();

            var $calculationContainer = $( '<div>', {
                class: calculationContainerClass
            } );

            $calculationsContainer.append( $calculationContainer );

            calculation.render();
        }
    };

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

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

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

}() );