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

From WikiAnesthesia
Tag: Manual revert
 
(146 intermediate revisions by the same user not shown)
Line 3: Line 3:
  */
  */
( function() {
( function() {
     var DEFAULT_DRUG_COLOR = 'default';
     mw.calculators.setOptionValue( 'defaultDrugColor', 'default' );
     var DEFAULT_DRUG_POPULATION = 'general';
     mw.calculators.setOptionValue( 'defaultDrugPopulation', 'general' );
     var DEFAULT_DRUG_ROUTE = 'iv';
     mw.calculators.setOptionValue( 'defaultDrugRoute', 'iv' );


     mw.calculators.isValueDependent = function( value, variableId ) {
     mw.calculators.isValueDependent = function( value, variableId ) {
Line 22: Line 22:
         concentration: {
         concentration: {
             toString: function( units ) {
             toString: function( units ) {
                 units = units.replace( ' pct', '%' );
                 units = units.replace( 'pct', '%' );
                 units = units.replace( 'ug', 'mcg' );
                 units = units.replace( 'ug', 'mcg' );


Line 38: Line 38:


     mw.calculators.addUnits( {
     mw.calculators.addUnits( {
        Eq: {
            baseName: 'mass_eq',
            prefixes: 'short'
        },
        mcg: {
            baseName: 'mass',
            definition: '1 ug'
        },
        patch: {
            baseName: 'volume_patch'
        },
         pct: {
         pct: {
             baseName: 'concentration',
             baseName: 'concentration',
             definition: '10 mg/mL'
             definition: '10 mg/mL',
            formatValue: function( value ) {
                var pctMatch = value.match( /([\d.]+)\s*?%/ );
 
                if( pctMatch ) {
                    var pctValue = pctMatch[ 1 ];
 
                    value = pctValue + '% (' + 10 * pctValue + ' mg/mL)';
                }
 
                return value;
            }
        },
        pill: {
            baseName: 'volume_pill'
        },
        spray: {
            baseName: 'volume_spray'
        },
        units: {
            baseName: 'mass_units',
            aliases: [
                'unit'
            ]
         },
         },
         vial: {
         vial: {
             baseName: 'volume'
             baseName: 'volume_vial'
         }
         }
     } );
     } );
Line 91: Line 125:
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


         this.parentColor = this.parentColor || this.id === DEFAULT_DRUG_COLOR ? this.parentColor : DEFAULT_DRUG_COLOR;
         this.parentColor = this.parentColor || this.id === mw.calculators.getOptionValue( 'defaultDrugColor' ) ? this.parentColor : mw.calculators.getOptionValue( 'defaultDrugColor' );
     };
     };


Line 271: Line 305:
     */
     */
     mw.calculators.objectClasses.DrugRoute = function( propertyValues ) {
     mw.calculators.objectClasses.DrugRoute = function( propertyValues ) {
         var properties = {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        this.abbreviation = this.abbreviation ? this.abbreviation : this.name;
    };
 
    mw.calculators.objectClasses.DrugRoute.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.DrugRoute.prototype.getProperties = function() {
        return {
             required: [
             required: [
                 'id',
                 'id',
Line 281: Line 323:
             ]
             ]
         };
         };
        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() {
     mw.calculators.objectClasses.DrugRoute.prototype.toString = function() {
Line 325: Line 363:
     */
     */
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
         var properties = {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };
 
    mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.DrugIndication.prototype.getProperties = function() {
        return {
             required: [
             required: [
                 'id',
                 'id',
Line 332: Line 376:
             optional: [
             optional: [
                 'abbreviation',
                 'abbreviation',
                 'default'
                 'default',
                'searchData'
             ]
             ]
         };
         };
    };
    mw.calculators.objectClasses.DrugIndication.prototype.getSearchString = function() {
        var searchString = this.name;


         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         searchString += this.abbreviation ? ' ' + this.abbreviation : '';
        searchString += this.searchData ? ' ' + this.searchData : '';
 
        return searchString.trim();
     };
     };
    mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );


     mw.calculators.objectClasses.DrugIndication.prototype.toString = function() {
     mw.calculators.objectClasses.DrugIndication.prototype.toString = function() {
Line 359: Line 409:
         for( var drugId in drugs ) {
         for( var drugId in drugs ) {
             mw.calculators.drugs[ drugId ] = drugs[ drugId ];
             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();
         }
         }
     };
     };
Line 389: Line 420:


         drug.addDosages( drugDosageData );
         drug.addDosages( drugDosageData );
        // Update calculation dependencies
        var drugDosageCalculation = mw.calculators.getCalculation( mw.calculators.getDrugDosageCalculationId( drugId ) );
        drugDosageCalculation.updateVariables();
        drugDosageCalculation.setDependencies();
     };
     };


Line 417: Line 442:


         if( !this.color ) {
         if( !this.color ) {
             this.color = DEFAULT_DRUG_COLOR;
             this.color = mw.calculators.getOptionValue( 'defaultDrugColor' );
         }
         }


Line 447: Line 472:
             this.dosages = [];
             this.dosages = [];
         }
         }
        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
        this.tradeNames = this.tradeNames ? this.tradeNames : [];
     };
     };


Line 500: Line 528:


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


     mw.calculators.objectClasses.Drug.prototype.getPreparations = function() {
     mw.calculators.objectClasses.Drug.prototype.getPreparations = function( excludeDilutionRequired ) {
         return this.preparations.filter( mw.calculators.uniqueValues );
         var preparations = this.preparations.filter( mw.calculators.uniqueValues );
 
        if( excludeDilutionRequired ) {
            for( var iPreparation in preparations ) {
                if( preparations[ iPreparation ].dilutionRequired ) {
                    delete preparations[ iPreparation ];
                }
            }
        }
 
        return preparations;
     };
     };


Line 521: Line 561:
             optional: [
             optional: [
                 'color',
                 'color',
                'description',
                 'dosages',
                 'dosages',
                 'preparations'
                'formula',
                 'preparations',
                'references',
                'searchData',
                'tradeNames'
             ]
             ]
         };
         };
Line 542: Line 587:


         drug.addPreparations( drugPreparationData );
         drug.addPreparations( drugPreparationData );
        var drugDosageCalculation = mw.calculators.getCalculation( mw.calculators.getDrugDosageCalculationId( drugId ) );
        drugDosageCalculation.recalculate();
     };
     };


Line 610: Line 651:
         this.indication = drugIndication;
         this.indication = drugIndication;


         this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION;
         this.population = this.population ? this.population : mw.calculators.getOptionValue( 'defaultDrugPopulation' );


         var drugPopulation = mw.calculators.getDrugPopulation( this.population );
         var drugPopulation = mw.calculators.getDrugPopulation( this.population );
Line 620: Line 661:
         this.population = drugPopulation;
         this.population = drugPopulation;


         this.route = this.route ? this.route : DEFAULT_DRUG_ROUTE;
         this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
 
        this.routes = this.routes ? this.routes : [ mw.calculators.getOptionValue( 'defaultDrugRoute' ) ];
 
        if( !Array.isArray( this.routes ) ) {
            this.routes = [ this.routes ];
        }
 
        drugRoutes = [];


         var drugRoute = mw.calculators.getDrugRoute( this.route );
         for( var iRoute in this.routes ) {
            var drugRouteId = this.routes[ iRoute ];
            var drugRoute = mw.calculators.getDrugRoute( drugRouteId );
 
            if( !drugRoute ) {
                throw new Error( 'Invalid route "' + drugRouteId + '" for drug dosage' );
            }


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


         this.route = drugRoute;
         this.routes = drugRoutes;


         // Add the dose objects to the drug
         // Add the dose objects to the drug
Line 640: Line 694:


     mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
     mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
         // Each dosage can have one or more associated doses. Ensure this value is an array.
         if( !drugDoseData ) {
        if( !Array.isArray( drugDoseData ) ) {
            return;
        } else if( !Array.isArray( drugDoseData ) ) {
            // Each dosage can have one or more associated doses. Ensure this value is an array.
             drugDoseData = [ drugDoseData ];
             drugDoseData = [ drugDoseData ];
         }
         }
Line 669: Line 725:
         return {
         return {
             required: [
             required: [
                'dose',
                 'id'
                 'id',
                'indication'
             ],
             ],
             optional: [
             optional: [
                 'description',
                 'description',
                'dose',
                'indication',
                 'population',
                 'population',
                 'route'
                 'routes',
                'references'
             ]
             ]
         };
         };
    };
    mw.calculators.objectClasses.DrugDosage.prototype.getRouteString = function() {
        var routeString = '';
        for( var iRoute in this.routes ) {
            routeString += routeString ? '/' : '';
            routeString += this.routes[ iRoute ].abbreviation;
        }
        return routeString;
     };
     };


Line 782: Line 850:
             'min',
             'min',
             'max',
             'max',
            'absoluteMin',
             'absoluteMax'
             'absoluteMax'
         ];
         ];
Line 793: Line 862:
             optional: [
             optional: [
                 'absoluteMax',
                 'absoluteMax',
                'absoluteMin',
                 'dose',
                 'dose',
                 'duration',
                 'duration',
Line 799: Line 869:
                 'max',
                 'max',
                 'name',
                 'name',
                'text',
                 'weightCalculation'
                 '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();
        // Label column
        var labelAttributes = {
            class: 'calculator-DrugDosageCalculator-drug-value'
        };
        var labelCss = {};
        var $label = $( '<th>', labelAttributes ).css( labelCss );
        $label.append( this.getLabelHtml() );
        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 )
                );
        }
        // Dosage column
        var $dosage = $( '<td>', {
            class: 'calculator-DrugDosageCalculator-dose-value'
        }  );
        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' ) ) {
                $dosage.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 dosageInfoText = mw.calculators.isMobile() ? 'Dosage info' : 'Dosage information';
                var $dosageInfoLink = $( '<a>', {
                    'data-toggle': 'collapse',
                    href: '#' + this.getContainerClass() + '-dose-info-row',
                    role: 'button',
                    'aria-expanded': 'false',
                    'aria-controls': this.getContainerClass() + '-dose-info-row'
                } )
                    .append( dosageInfoText + '&nbsp;' )
                    .append( $( '<i>', {
                        class: 'far fa-question-circle'
                    } ) );
                $doseInfo
                    .append( $( '<div>', {
                        class: 'calculator-DrugDosageCalculator-dose-info-button'
                    } ).append( $dosageInfoLink ) );
                showInfo = true;
            }
            if( showInfo ) {
                $dosage.append( $doseInfo );
            }
            // 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 ];
                var makeList;
                if( dose.name ) {
                    $dosage.append( dose.name + '<br />' );
                    makeList = true;
                }
                if( makeList ) {
                    $dosage.append( '<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' ) ) {
                    massPerWeightHtml += mw.calculators.getValueNumber( doseValue.massPerWeight.min );
                    massPerWeightHtml += dash;
                    massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.max );
                }
                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 + ')';
                    }
                    if( makeList ) {
                        massPerWeightHtml = $( '<li>' ).append( massPerWeightHtml );
                    }
                    $dosage.append( massPerWeightHtml );
                    if( !makeList ) {
                        $dosage.append( '<br />' );
                    }
                }
                var massHtml = '';
                if( doseValue.mass.hasOwnProperty( 'dose' ) ) {
                    massHtml += mw.calculators.getValueString( doseValue.mass.dose );
                } else if( doseValue.mass.hasOwnProperty( 'min' ) &&
                    doseValue.mass.hasOwnProperty( 'max' ) ) {
                    massHtml += mw.calculators.getValueNumber( doseValue.mass.min );
                    massHtml += dash;
                    massHtml += mw.calculators.getValueString( doseValue.mass.max );
                }
                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 + ')';
                    }
                    if( makeList ) {
                        massHtml = $( '<li>' ).append( massHtml );
                    }
                    $dosage.append( massHtml );
                    if( !makeList ) {
                        $dosage.append( '<br />' );
                    }
                }
                var volumeHtml = '';
                if( doseValue.volume.hasOwnProperty( 'dose' ) ) {
                    volumeHtml += mw.calculators.getValueString( doseValue.volume.dose );
                } else if( doseValue.volume.hasOwnProperty( 'min' ) &&
                    doseValue.volume.hasOwnProperty( 'max' ) ) {
                    volumeHtml += mw.calculators.getValueNumber( doseValue.volume.min );
                    volumeHtml += dash;
                    volumeHtml += mw.calculators.getValueString( doseValue.volume.max );
                }
                if( volumeHtml ) {
                    if( administration && ! administrationDisplayed ) {
                        volumeHtml += ' ' + administration;
                        administrationDisplayed = true;
                    }
                    if( makeList ) {
                        volumeHtml = $( '<li>' ).append( volumeHtml );
                    }
                    $dosage.append( volumeHtml );
                    if( !makeList ) {
                        $dosage.append( '<br />' );
                    }
                    hasVolume = true;
                }
                if( makeList ) {
                    $dosage.append( '</ul>' );
                }
            }
        }
        // Options column
        var $options = $( '<td>', {
            class: 'calculator-DrugDosageCalculator-options-value'
        } );
        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(
                $label,
                $dosage,
                $options
            );
        return;
        var calculation = this;
        $calculationContainer.each( function() {
            $( this ).empty();
            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>', {
            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' ) {
            return this.drug.getPreparations();
        } 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 = $( '<table>', {
            class: 'wikitable'
        } ).append( '<tbody>' );
        $calculatorContainer.append( $calculationsContainer );
        $calculationsContainer
            .append( $( '<tr>' )
                .append(
                    $( '<th>', {
                        class: this.getCalculatorClass() + '-drug'
                    } ).text( 'Drug' ),
                    $( '<th>', {
                        class: this.getCalculatorClass() + '-dose'
                    }  ).text( 'Dose' ),
                    $( '<th>', {
                        class: this.getCalculatorClass() + '-options'
                    }  ).text( 'Options' )
                )
            );
        for( var iCalculationId in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
            var calculationContainerClass = calculation.getContainerClass();
            var $calculationContainer = $( '<tr>', {
                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: []
        } );
     };
     };


}() );
}() );

Latest revision as of 20:55, 29 March 2022

/**
 * @author Chris Rishel
 */
( function() {
    mw.calculators.setOptionValue( 'defaultDrugColor', 'default' );
    mw.calculators.setOptionValue( 'defaultDrugPopulation', 'general' );
    mw.calculators.setOptionValue( 'defaultDrugRoute', '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( {
        Eq: {
            baseName: 'mass_eq',
            prefixes: 'short'
        },
        mcg: {
            baseName: 'mass',
            definition: '1 ug'
        },
        patch: {
            baseName: 'volume_patch'
        },
        pct: {
            baseName: 'concentration',
            definition: '10 mg/mL',
            formatValue: function( value ) {
                var pctMatch = value.match( /([\d.]+)\s*?%/ );

                if( pctMatch ) {
                    var pctValue = pctMatch[ 1 ];

                    value = pctValue + '% (' + 10 * pctValue + ' mg/mL)';
                }

                return value;
            }
        },
        pill: {
            baseName: 'volume_pill'
        },
        spray: {
            baseName: 'volume_spray'
        },
        units: {
            baseName: 'mass_units',
            aliases: [
                'unit'
            ]
        },
        vial: {
            baseName: 'volume_vial'
        }
    } );



    /**
     * 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 === mw.calculators.getOptionValue( 'defaultDrugColor' ) ? this.parentColor : mw.calculators.getOptionValue( 'defaultDrugColor' );
    };

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

        this.abbreviation = this.abbreviation ? this.abbreviation : this.name;
    };

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

    mw.calculators.objectClasses.DrugRoute.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default'
            ]
        };
    };

    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 ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
    };

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

    mw.calculators.objectClasses.DrugIndication.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation',
                'default',
                'searchData'
            ]
        };
    };

    mw.calculators.objectClasses.DrugIndication.prototype.getSearchString = function() {
        var searchString = this.name;

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

        return searchString.trim();
    };

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

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

    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 = mw.calculators.getOptionValue( 'defaultDrugColor' );
        }

        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 = [];
        }

        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];
        this.tradeNames = this.tradeNames ? this.tradeNames : [];
    };

    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 ].routes.length &&
                ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
                for( var iRoute in this.dosages[ iDosage ].routes ) {
                    routes.push( this.dosages[ iDosage ].routes[ iRoute ] );
                }
            }
        }

        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',
                'description',
                'dosages',
                'formula',
                'preparations',
                'references',
                'searchData',
                'tradeNames'
            ]
        };
    };





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



    /**
     * 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 : mw.calculators.getOptionValue( 'defaultDrugPopulation' );

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

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

        this.population = drugPopulation;

        this.references = this.references ? mw.calculators.prepareReferences( this.references ) : [];

        this.routes = this.routes ? this.routes : [ mw.calculators.getOptionValue( 'defaultDrugRoute' ) ];

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

        drugRoutes = [];

        for( var iRoute in this.routes ) {
            var drugRouteId = this.routes[ iRoute ];
            var drugRoute = mw.calculators.getDrugRoute( drugRouteId );

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

            drugRoutes[ iRoute ] = drugRoute;
        }

        this.routes = drugRoutes;

        // 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 ) {
        if( !drugDoseData ) {
            return;
        } else if( !Array.isArray( drugDoseData ) ) {
            // Each dosage can have one or more associated doses. Ensure this value is an array.
            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: [
                'id'
            ],
            optional: [
                'description',
                'dose',
                'indication',
                'population',
                'routes',
                'references'
            ]
        };
    };

    mw.calculators.objectClasses.DrugDosage.prototype.getRouteString = function() {
        var routeString = '';

        for( var iRoute in this.routes ) {
            routeString += routeString ? '/' : '';
            routeString += this.routes[ iRoute ].abbreviation;
        }

        return routeString;
    };

    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',
            'absoluteMin',
            'absoluteMax'
        ];
    };

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

}() );