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

From WikiAnesthesia
Line 3: Line 3:
  */
  */
( function() {
( function() {
    var DEFAULT_DRUG_COLOR = 'default';
    var DEFAULT_DRUG_POPULATION = 'general';
    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
     * DrugColor data
     */
     */
     mw.calculators.addUnitsBases( {
     mw.calculators.addDrugColors( {
         concentration: {
         anticholinergic: {
             toString: function( units ) {
             primaryColor: '#00ac8c'
                units = units.replace( ' pct', '%' );
        },
 
        benzodiazepine: {
                return units;
            primaryColor: '#ff6c2f'
             }
        },
         }
        benzodiazepineReversal: {
    } );
            parentColor: 'benzodiazepine',
 
            striped: true
    mw.calculators.addUnits( {
        },
         pct: {
        cardiovascularAgonist: {
             baseName: 'concentration',
            primaryColor: '#ba93df'
             definition: '10 mg/mL'
        },
        cardiovascularAntagonist: {
            parentColor: 'cardiovascularAgonist',
            striped: true
        },
        default: {
            primaryColor: '#fff',
            highlightColor: '#fff'
        },
        desflurane: {
            primaryColor: '#0ab8fd'
        },
        enflurane: {
            primaryColor: '#f58733'
        },
        epinephrine: {
            parentColor: 'cardiovascularAntagonist',
            highlightColor: '#000'
        },
        halothane: {
            primaryColor: '#b20107'
        },
        isoflurane: {
            primaryColor: '#ca7fc0'
        },
        localAnesthetic: {
            primaryColor: '#dad9d6'
        },
        neuromuscularBlocker: {
             primaryColor: '#fe5442'
        },
        neuromuscularBlockerReversal: {
            parentColor: 'neuromuscularBlocker',
            striped: true
         },
        nitrousOxide: {
            primaryColor: '#2d549f'
        },
        opioid: {
            primaryColor: '#6cd1ef'
        },
        opioidReversal: {
            parentColor: 'opioid',
            striped: true
        },
         sedativeHypnotic: {
             primaryColor: '#ffe800'
        },
        sevoflurane: {
             primaryColor: '#f8da00'
         },
         },
         vial: {
         succinylcholine: {
             baseName: 'VOLUME'
             parentColor: 'neuromuscularBlocker',
            highlightColor: '#000'
         }
         }
     } );
     } );
Line 41: Line 79:


     /**
     /**
     * DrugColor
     * DrugPopulation data
     */
     */
     mw.calculators.drugColors = {};
     mw.calculators.addDrugPopulations( {
 
         general: {
    mw.calculators.addDrugColors = function( drugColorData ) {
             name: 'General',
         var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );
             abbreviation: 'Gen.'
 
         },
        for( var drugColorId in drugColors ) {
         neonatal: {
             mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
             name: 'Neonatal',
        }
             abbreviation: 'Neo.',
    };
             variables: {
 
                 age: {
    mw.calculators.getDrugColor = function( drugColorId ) {
                     max: '0 yo'
        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 = {};
         pediatric: {
         }
            name: 'Pediatric',
    };
            abbreviation: 'Ped.',
 
             variables: {
    mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                age: {
 
                    min: '0 yo',
    mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
                    max: '17.9 yo'
        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 &&
        elderly: {
                !math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) {
             name: 'Elderly',
                return -1;
            abbreviation: 'Eld.',
            }
             variables: {
 
                 age: {
             if( this.variables[ variableId ].max &&
                    min: '65 yo'
                 !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;
    }
 
 






     /**
     /**
     * DrugIndication
     * DrugIndication data
     */
     */
     mw.calculators.drugIndications = {};
     mw.calculators.addDrugIndications( {
 
        abxProphylaxis: {
    mw.calculators.addDrugIndications = function( drugIndicationData ) {
            name: 'Antimicrobial prophylaxis',
         var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );
            abbreviation: 'Abx.'
 
        },
         for( var drugIndicationId in drugIndications ) {
        anxiolysis: {
             mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
            name: 'Anxiolysis',
            abbreviation: 'Anxiety'
        },
        generalAnesthesiaInduction: {
            name: 'General anesthesia (induction)',
            abbreviation: 'GA ind.'
        },
        generalAnesthesiaMaintenance: {
            name: 'General anesthesia (maintenance)',
            abbreviation: 'GA maint.'
        },
        neuromuscularBlockade: {
            name: 'Neuromuscular blockade (non-RSI)',
            abbreviation: 'NMB (non-RSI)'
        },
        neuromuscularBlockadeRsi: {
            name: 'Neuromuscular blockade (rapid sequence induction)',
            abbreviation: 'NMB (RSI)'
        },
        neuromuscularBlockadeReversal: {
            name: 'Neuromuscular blockade reversal',
            abbreviation: 'NMB reversal'
        },
        mac: {
            name: 'Monitored anesthesia care',
            abbreviation: 'MAC'
         },
        ponv: {
            name: 'Postoperative nausea & vomiting',
            abbreviation: 'PONV'
        },
        tivaLoad: {
            name: 'Total intravenous anesthesia (loading dose)',
            abbreviation: 'TIVA load'
        },
         tivaMaintenance: {
            name: 'Total intravenous anesthesia (maintenance)',
             abbreviation: 'TIVA maint.'
         }
         }
     };
     } );
 
    mw.calculators.getDrugIndication = function( drugIndicationId ) {
        if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
            return mw.calculators.drugIndications[ drugIndicationId ];
        } else {
            return null;
        }
    };






     /**
     /**
     * Class DrugIndication
     * Drug data
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.DrugIndication}
    * @constructor
     */
     */
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
     var drugId;
        var properties = {
            required: [
                'id',
                'name'
            ],
            optional: [
                'abbreviation'
            ]
        };
 
        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
     * Cefazolin
     */
     */
     mw.calculators.drugs = {};
     drugId = 'cefazolin';


     mw.calculators.addDrugs = function( drugData ) {
     mw.calculators.addDrugs( [
         var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );
         {
 
            id: drugId,
        for( var drugId in drugs ) {
             name: 'Cefazolin'
             mw.calculators.drugs[ drugId ] = drugs[ drugId ];
         }
         }
     };
     ] );


     mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
     mw.calculators.addDrugDosages( drugId, [
         var drug = mw.calculators.getDrug( drugId );
         {
 
            indication: 'abxProphylaxis',
        if( !drug ) {
            population: 'general',
            throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
            dose: {
                dose: '2 g',
                frequency: 'q4h'
            }
         }
         }
    ] );


        drug.addDosages( drugDosageData );
    mw.calculators.addDrugPreparations( drugId, [
 
         {
        var calculationId = 'drugDosage-' + drugId;
             concentration: '1 g/vial'
        var calculation = mw.calculators.getCalculation( calculationId );
 
         if( !calculation ) {
             var calculationData = {};
 
            calculationData[ calculationId ] = {
                calculate: mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate,
                drug: drugId,
                type: 'drug'
            };
 
            mw.calculators.addCalculations( calculationData, 'DrugDosageCalculation' );
 
            calculation = mw.calculators.getCalculation( calculationId );
        }
 
        calculation.setDependencies();
    };
 
    mw.calculators.getDrug = function( drugId ) {
        if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
            return mw.calculators.drugs[ drugId ];
        } else {
            return null;
         }
         }
     };
     ] );






     /**
     /**
     * Class Drug
     * Ketamine
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.Drug}
    * @constructor
     */
     */
     mw.calculators.objectClasses.Drug = function( propertyValues ) {
     drugId = 'ketamine';
        var properties = {
            required: [
                'id',
                'name'
            ],
            optional: [
                'color'
            ]
        };
 
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


         if( !this.color ) {
    mw.calculators.addDrugs( [
             this.color = DEFAULT_DRUG_COLOR;
         {
             id: drugId,
            name: 'Ketamine',
            color: 'sedativeHypnotic'
         }
         }
    ] );


        var color = mw.calculators.getDrugColor( this.color );
    mw.calculators.addDrugPreparations( drugId, [
 
         {
         if( !color ) {
             concentration: '10 mg/mL'
             throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' );
        }, {
            concentration: '50 mg/mL'
        }, {
            concentration: '100 mg/mL'
         }
         }
    ] );


        this.color = color;
     mw.calculators.addDrugDosages( drugId, [
 
        {
        this.dosages = [];
            indication: 'generalAnesthesiaInduction',
        this.preparations = [];
             population: 'general',
    };
             dose: {
 
                min: '1 mg/kg',
     mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                max: '2 mg/kg',
 
                 weightCalculation: 'lbw'
    mw.calculators.objectClasses.Drug.prototype.addDosages = function( drugDosageData ) {
        var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
 
        for( var dosageId in dosages ) {
             dosages[ dosageId ].id = this.dosages.length;
 
             this.dosages.push( dosages[ dosageId ] );
        }
    };
 
    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 );
             }
             }
         }
         }, {
 
            indication: 'generalAnesthesiaInduction',
        return indications.filter( mw.calculators.uniqueValues );
            population: 'general',
    };
            route: 'im',
 
             dose: {
    mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) {
                 min: '4 mg/kg',
        var populations = [];
                 max: '6 mg/kg'
 
        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.getPreparations = function() {
        return this.preparations.filter( mw.calculators.uniqueValues );
    };
 
 






     /**
     /**
     * DrugPreparation
     * Lidocaine
     */
     */
     mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
     drugId = 'lidocaine';
        if( !mw.calculators.getDrug( drugId ) ) {
            throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
        }


         for( var drugPreparationId in drugPreparationData ) {
    mw.calculators.addDrugs( [
             drugPreparationData[ drugPreparationId ].drug = drugId;
         {
             id: drugId,
            name: 'Lidocaine',
            color: 'localAnesthetic'
         }
         }
    ] );


        var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData );
    mw.calculators.addDrugPreparations( drugId, [
 
        {
         for( var drugPreparationId in drugPreparations ) {
            concentration: '1 pct'
             mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
         }, {
             concentration: '2 pct'
         }
         }
     };
     ] );






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


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
    mw.calculators.addDrugs( [
 
        {
 
            id: drugId,
        this.concentration = this.concentration.replace( 'mcg', 'ug' );
            name: 'Midazolam',
 
            color: 'benzodiazepine'
         this.concentration = math.unit( this.concentration );
         }
    };
     ] );
 
     mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );


    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'anxiolysis',
            population: 'general',
            dose: {
                min: '0.01 mg/kg',
                max: '0.03 mg/kg'
            }
        }, {
            indication: 'anxiolysis',
            population: 'general',
            route: 'im',
            dose: {
                min: '0.07 mg/kg',
                max: '0.08 mg/kg'
            }
        }, {
            indication: 'anxiolysis',
            population: 'general',
            route: 'po',
            dose: {
                dose: '0.5 mg/kg',
                absoluteMax: '20 mg'
            }
        }, {
            indication: 'generalAnesthesiaInduction',
            population: 'general',
            dose: {
                min: '0.1 mg/kg',
                max: '0.3 mg/kg',
                weightCalculation: 'lbw'
            }
        }
    ] );


    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '1 mg/mL'
        }, {
            concentration: '5 mg/mL'
        }
    ] );






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


        var drugIndication = mw.calculators.getDrugIndication( this.indication );
    mw.calculators.addDrugs( [
 
         {
         if( !drugIndication ) {
             id: drugId,
             throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
            name: 'Neostigmine',
            color: 'neuromuscularBlockerReversal'
         }
         }
    ] );


         this.indication = drugIndication;
    mw.calculators.addDrugDosages( drugId, [
 
         {
        this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION;
            indication: 'neuromuscularBlockadeReversal',
 
            population: 'general',
        var drugPopulation = mw.calculators.getDrugPopulation( this.population );
            dose: {
 
                min: '0.03 mg/kg',
        if( !drugPopulation ) {
                max: '0.07 mg/kg',
             throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
                absoluteMax: '5 mg'
            },
             description: 'For each 1 mg of neostigmine, give 0.2 mg of glycopyrrolate to avoid bradycardia'
         }
         }
    ] );


        this.population = drugPopulation;
     mw.calculators.addDrugPreparations( drugId, [
 
         {
        this.route = this.route ? this.route : 'IV';
             concentration: '0.5 mg/mL'
 
         }, {
        var drugDoseData = this.dose;
             concentration: '1 mg/mL'
        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'
            ]
        };
    }
 
 






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


        var mathProperties = this.getMathProperties();
    mw.calculators.addDrugs( [
 
         {
         for( var iMathProperty in mathProperties ) {
             id: drugId,
             var mathProperty = mathProperties[ iMathProperty ];
             name: 'Ondansetron'
 
             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 ] );
            } else {
                this[ mathProperty ] = null;
            }
         }
         }
    ] );


        if( this.weightCalculation ) {
    mw.calculators.addDrugDosages( drugId, [
            var weightCalculation = mw.calculators.getCalculation( this.weightCalculation );
        {
 
            indication: 'ponv',
             if( !weightCalculation ) {
            population: 'general',
                 throw new Error( 'Drug dose references weight calculation "' + this.weightCalculation + '" which is not defined' );
             dose: {
                 dose: '4 mg'
             }
             }
 
         }, {
            this.weightCalculation = weightCalculation;
             indication: 'ponv',
         }
             population: 'pediatric',
    };
             dose: {
 
                 dose: '0.1 mg/kg',
    mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
                 absoluteMax: '4 mg'
 
    mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
        var calculationData = new mw.calculators.objectClasses.CalculationData();
 
        var mathProperties = this.getMathProperties();
 
        // Look at dose properties to identify any variable dependence (e.g. weight-dependence)
        for( var iMathProperty in mathProperties ) {
             var mathProperty = mathProperties[ iMathProperty ];
 
             var dosePropertyValue = this[ mathProperty ];
 
            // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
             if( mw.calculators.isValueDependent( dosePropertyValue, 'weight' ) &&
                 calculationData.variables.optional.indexOf( 'weight' ) === -1 ) {
                 calculationData.variables.optional.push( 'weight' );
             }
             }
         }
         }
    ] );


         if( this.weightCalculation ) {
    mw.calculators.addDrugPreparations( drugId, [
             calculationData.calculations.optional.push( this.weightCalculation.id );
         {
             concentration: '2 mg/mL'
         }
         }
 
     ] );
        return calculationData;
    };
 
     mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
        return [
            'dose',
            'min',
            'max',
            'absoluteMin',
            'absoluteMax',
            'frequency'
        ];
    };
 
    mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
        return {
            required: [
                'id'
            ],
            optional: [
                'absoluteMin',
                'absoluteMax',
                'dose',
                'min',
                'max',
                'name',
                'weightCalculation'
            ]
        };
    };
 






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


        this.initialize();
     mw.calculators.addDrugs( [
    };
         {
 
             id: drugId,
     mw.calculators.objectClasses.DrugDosageCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
             name: 'Propofol',
 
             color: 'sedativeHypnotic'
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate = function( data ) {
         var value = {
             population: null,
             dose: []
        };
 
        // Determine which dosage to use
        var populationScores = [];
 
        for( var iDosage in data.drug.dosages ) {
            var drugDosage = data.drug.dosages[ iDosage ];
 
            // If the indication does not match, set the score to -1
             var populationScore = ( drugDosage.indication.id === data.indication.id ) ?
                drugDosage.population.getCalculationDataScore( data ) : -1;
 
            populationScores.push( populationScore );
         }
         }
    ] );


        var maxPopulationScore = Math.max.apply( null, populationScores );
    mw.calculators.addDrugPreparations( drugId, [
 
         {
         if( maxPopulationScore < 0 ) {
             concentration: '10 mg/mL'
             return value;
         }
         }
    ] );


        // If there is more than one dosage with the same score, take the first.
    mw.calculators.addDrugDosages( drugId, [
        // This allows the data editor to decide which is most important.
         {
        var dosageId = populationScores.indexOf( maxPopulationScore );
             indication: 'generalAnesthesiaInduction',
 
             population: 'general',
        var dosage = data.drug.dosages[ dosageId ];
             dose: {
 
                 min: '1 mg/kg',
        value.population = dosage.population;
                 max: '2.5 mg/kg',
 
                 weightCalculation: 'lbw'
         // A dosage may contain multiple doses (e.g. induction and maintenance)
             }
        for( var iDose in dosage.dose ) {
        }, {
             var dose = dosage.dose[ iDose ];
             indication: 'generalAnesthesiaInduction',
             var mathProperties = dose.getMathProperties();
            population: 'pediatric',
 
             dose: {
            // Initialize value properties for dose
                 min: '2.5 mg/kg',
             value.dose[ iDose ] = {
                max: '3.5 mg/kg',
                massPerWeight: {},
                 weightCalculation: 'lbw'
                 mass: {},
                 name: dose.name,
                volume: {},
                 weightCalculation: dose.weightCalculation ? dose.weightCalculation : null
             };
 
             var weightValue = dose.weightCalculation ? dose.weightCalculation.value : data.weight;
 
             for( var iMathProperty in mathProperties ) {
                 var mathProperty = mathProperties[ iMathProperty ];
 
                var doseValue = dose[ mathProperty ];
 
                if( doseValue ) {
                    if( mw.calculators.isValueDependent( doseValue, 'weight' ) ) {
                        value.dose[ iDose ].massPerWeight[ mathProperty ] = doseValue;
 
                        // 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 ] = weightValue ? math.unit( math.multiply( doseValue, weightValue ).format() ) : null;
                    } 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
                        value.dose[ iDose ].volume[ mathProperty ] = math.unit( math.multiply( value.dose[ iDose ].mass[ mathProperty ], math.divide( 1, data.preparation.concentration ) ).format() );
                    }
                 }
             }
             }
         }
         }, {
 
             indication: 'generalAnesthesiaInduction',
        return value;
             population: 'elderly',
    };
            dose: {
 
                 min: '1 mg/kg',
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.doRender = function() {
                 max: '1.5 mg/kg',
        var $calculationContainer = $( '.' + this.getContainerClass() );
                 weightCalculation: 'lbw'
 
        if( !$calculationContainer.length ) {
            return;
        }
 
        $calculationContainer.empty();
 
        // Label column
        var labelAttributes = {};
        var labelCss = {};
 
        if( this.drug.color.isStriped() ) {
            labelCss[ 'background' ] = 'repeating-linear-gradient(135deg,rgba(0,0,0,0),rgba(0,0,0,0)10px,rgba(255,255,255,1)10px,rgba(255,255,255,1)20px),' + this.drug.color.getPrimaryColor();
        } else {
             labelCss[ 'background-color'] = this.drug.color.getPrimaryColor();
        }
 
        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 )
                );
        }
 
        // Indication column
        var $indication = $( '<td>' );
 
        var indications = this.drug.getIndications();
 
        if( indications.length > 1 ) {
            $indication.append( mw.calculators.getVariable( this.getVariableIds().indication  ).createInput( true ) );
        } else {
            $indication.append( String( indications[ 0 ] ) );
        }
 
        // Dosage column
        var dosageHtml = '';
 
        var dash = '&ndash;';
 
        if( this.value.population && this.value.population.id !== DEFAULT_DRUG_POPULATION ) {
            dosageHtml += String( this.value.population ) + '<br />';
        }
 
        for( var iDose in this.value.dose ) {
            var doseValue = this.value.dose[ iDose ];
 
            if( doseValue.name ) {
                dosageHtml += doseValue.name + '<br />';
             }
             }
 
        }, {
             if( !$.isEmptyObject( doseValue.massPerWeight ) ) {
             indication: 'generalAnesthesiaMaintenance',
                if( doseValue.massPerWeight.hasOwnProperty( 'dose' ) ) {
            population: 'general',
                    dosageHtml += mw.calculators.getValueString( doseValue.massPerWeight.dose );
            dose: {
                 } else if( doseValue.massPerWeight.hasOwnProperty( 'min' ) &&
                 min: '100 mcg/kg/min',
                    doseValue.massPerWeight.hasOwnProperty( 'max' ) ) {
                 max: '200 mcg/kg/min'
                    dosageHtml += mw.calculators.getValueString( doseValue.massPerWeight.min );
                    dosageHtml += dash;
                    dosageHtml += mw.calculators.getValueString( doseValue.massPerWeight.max );
                }
 
                if( doseValue.weightCalculation ) {
                    dosageHtml += ' (' + doseValue.weightCalculation.getLabelString() + ')';
                 }
 
                dosageHtml += '<br />';
             }
             }
 
        }, {
             if( !$.isEmptyObject( doseValue.mass ) ) {
             indication: 'generalAnesthesiaMaintenance',
                if( doseValue.mass.hasOwnProperty( 'dose' ) ) {
            population: 'pediatric',
                    dosageHtml += mw.calculators.getValueString( doseValue.mass.dose );
            dose: {
                 } else if( doseValue.mass.hasOwnProperty( 'min' ) &&
                 min: '125 mcg/kg/min',
                    doseValue.mass.hasOwnProperty( 'max' ) ) {
                max: '300 mcg/kg/min'
                    dosageHtml += mw.calculators.getValueString( doseValue.mass.min );
                    dosageHtml += dash;
                    dosageHtml += mw.calculators.getValueString( doseValue.mass.max );
                }
 
                dosageHtml += '<br />';
             }
             }
 
        }, {
             if( !$.isEmptyObject( doseValue.volume ) ) {
             indication: 'generalAnesthesiaMaintenance',
                if( doseValue.volume.hasOwnProperty( 'dose' ) ) {
            population: 'elderly',
                    dosageHtml += mw.calculators.getValueString( doseValue.volume.dose );
            dose: {
                 } else if( doseValue.volume.hasOwnProperty( 'min' ) &&
                 min: '50 mcg/kg/min',
                    doseValue.volume.hasOwnProperty( 'max' ) ) {
                max: '100 mcg/kg/min'
                    dosageHtml += mw.calculators.getValueString( doseValue.volume.min );
                    dosageHtml += dash;
                    dosageHtml += mw.calculators.getValueString( doseValue.volume.max );
                }
 
                dosageHtml += '<br />';
             }
             }
 
         }, {
            dosageHtml += '<br />';
             indication: 'mac',
         }
            population: 'general',
 
            dose: {
 
                 min: '25 mcg/kg/min',
        $calculationContainer
                 max: '75 mcg/kg/min'
            .append(
                $label,
                $indication,
                $( '<td>' ).html( dosageHtml )
            );
 
 
        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
    * Rocuronium
        // reinitialize).
    */
        var initializeRequiredData = true;
    drugId = 'rocuronium';


        // Iterate through each dosage to determine variable dependency
    mw.calculators.addDrugs( [
        for( var iDosage in this.drug.dosages ) {
        {
            var dosageInputData = this.drug.dosages[ iDosage ].getCalculationData();
            id: drugId,
 
             name: 'Rocuronium',
            inputData = inputData.merge( dosageInputData );
             color: 'neuromuscularBlocker'
 
            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 ) {
    mw.calculators.addDrugPreparations( drugId, [
            var dataType = dataTypes[ iDataType ];
        {
 
             concentration: '10 mg/mL'
             // 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.addDrugDosages( drugId, [
    };
         {
 
            indication: 'neuromuscularBlockade',
     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationDataValues = function() {
             population: 'general',
         var data = mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues.call( this );
             dose: {
 
                dose: '0.6 mg/kg'
        data.drug = this.drug;
            }
 
         }, {
        data.indication = data[ this.getVariablePrefix() + 'indication' ] ?
            indication: 'neuromuscularBlockadeRsi',
             mw.calculators.getDrugIndication( mw.calculators.getVariable( this.getVariableIds().indication ).value ) :
             population: 'general',
            null;
             dose: {
 
                dose: '1.2 mg/kg'
        delete data[ this.getVariablePrefix() + 'indication' ];
             }
 
        data.preparation = data[ this.getVariablePrefix() + 'preparation' ] ?
             this.drug.preparations[ mw.calculators.getVariable( this.getVariableIds().preparation ).value ] :
            null;
 
        delete data[ this.getVariablePrefix() + 'preparation' ];
 
         return data;
    };
 
 
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() {
        var labelHtml = this.drug.name;
 
        var $label = $( '<a>', {
             href: mw.util.getUrl( this.drug.name ),
             text: labelHtml
        } );
 
        var highlightColor = this.drug.color.getHighlightColor();
 
        if( highlightColor ) {
             $label.css( 'background-color', highlightColor );
         }
         }
    ] );


        labelHtml = $label[ 0 ].outerHTML;


        return labelHtml;
    };


     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getProperties = function() {
     /**
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
    * Sufentanil
    */
    drugId = 'sufentanil';


        return this.mergeProperties( inheritedProperties, {
     mw.calculators.addDrugs( [
            required: [
         {
                'drug'
             id: drugId,
            ],
             name: 'Sufentanil',
            optional: []
             color: 'opioid'
        } );
    };
 
     mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableIds = function() {
         return {
             indication: this.getVariablePrefix() + 'indication',
             preparation: this.getVariablePrefix() + 'preparation'
        };
    };
 
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariablePrefix = function() {
        return this.drug.id + '-';
    }
 
    mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() {
        mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
 
        var drug = mw.calculators.getDrug( this.drug );
 
        if( !drug ) {
             throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' );
         }
         }
    ] );


        this.drug = drug;
    mw.calculators.addDrugPreparations( drugId, [
 
         {
        var variableIds = this.getVariableIds();
            concentration: '5 mcg/mL'
 
         },
        // Create variables for indication, population?, preparation select boxes? Here or m.c.addDosages() or elsewhere?
         {
        // Will have to add them to getCalculationData too.
             concentration: '50 mcg/mL'
         var drugVariables = {};
 
         var indications = this.drug.getIndications();
        var indicationOptions = {};
 
         for( var iIndication in indications ) {
             var indication = indications[ iIndication ];
 
            indicationOptions[ indication.id ] = String( indication );
         }
         }
    ] );


         drugVariables[ variableIds.indication ] = {
    mw.calculators.addDrugDosages( drugId, [
             name: 'Indication',
         {
             type: 'string',
             indication: 'tivaLoad',
             defaultValue: indications.length ? indications[ 0 ].id : null,
             population: 'general',
             options: indicationOptions
             dose: {
         };
                min: '0.25 mcg/kg',
 
                max: '2 mcg/kg',
        var preparations = this.drug.getPreparations();
                weightCalculation: 'lbw'
        var preparationOptions = {};
             }
 
         }, {
        for( var iPreparation in preparations ) {
            indication: 'tivaMaintenance',
            var preparation = preparations[ iPreparation ];
            population: 'general',
 
            dose: {
             preparationOptions[ preparation.id ] = String( preparation );
                name: 'Maintenance',
                min: '0.5 mcg/kg/hr',
                max: '1.5 mcg/kg/hr'
             }
         }
         }
 
    ] );
        drugVariables[ variableIds.preparation ] = {
            name: 'Preparation',
            type: 'string',
            defaultValue: preparations.length ? preparations[ 0 ].id : null,
            options: preparationOptions
        };
 
        mw.calculators.addVariables( drugVariables );
    };
 
 






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


     mw.calculators.objectClasses.DrugDosageCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype );
     mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Sugammadex',
            color: 'neuromuscularBlockerReversal'
        }
    ] );


     mw.calculators.objectClasses.DrugDosageCalculator.prototype.doRender = function() {
     mw.calculators.addDrugDosages( drugId, [
        var $calculatorContainer = $( '.' + this.getContainerClass() );
        {
            indication: 'neuromuscularBlockadeReversal',
            population: 'general',
            dose: {
                min: '2 mg/kg',
                max: '4 mg/kg',
                absoluteMax: '16 mg/kg'
            }
        }
    ] );


         if( !$calculatorContainer.length ) {
    mw.calculators.addDrugPreparations( drugId, [
             return;
         {
             concentration: '100 mg/mL'
         }
         }
    ] );


        $calculatorContainer.empty();


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


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


        $calculatorContainer.append( $calculationsContainer );


        $calculationsContainer
            .append( $( '<tr>' )
                .append(
                    $( '<th>' ).text( 'Drug' ),
                    $( '<th>' ).text( 'Indication' ),
                    $( '<th>' ).text( 'Dose' )
                )
            );


        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 );
    /**
    * DrugDosage
    *
    * Structure is:
    *
    * drugId: {
    *    indicationId: {
    *        doseId: {
    *
    *        }
    *    }
    * }
    */


             calculation.render();
    var drugDosages = {
        cefazolin: {
             abxProphylaxis: {
                general: {
                    population: 'general',
                    dose: '2 g'
                },
                general120kg: {
                    population: {
                        id: 'general',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                    },
                    dose: '3 g'
                },
                pediatric: {
                    dose: {
                        absoluteMax: '2 g',
                        dose: '30 mg/kg'
                    }
                },
                pediatric120kg: {
                    population: {
                        id: 'pediatric',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                    },
                    dose: {
                        dose: '3 g'
                    }
                }
            }
         }
         }
    };
    mw.calculators.objectClasses.DrugDosageCalculator.prototype.getProperties = function() {
        var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();
        return this.mergeProperties( inheritedProperties, {
            required: [],
            optional: []
        } );
     };
     };


}() );
}() );

Revision as of 19:21, 11 August 2021

/**
 * @author Chris Rishel
 */
( function() {
    /**
     * DrugColor data
     */
    mw.calculators.addDrugColors( {
        anticholinergic: {
            primaryColor: '#00ac8c'
        },
        benzodiazepine: {
            primaryColor: '#ff6c2f'
        },
        benzodiazepineReversal: {
            parentColor: 'benzodiazepine',
            striped: true
        },
        cardiovascularAgonist: {
            primaryColor: '#ba93df'
        },
        cardiovascularAntagonist: {
            parentColor: 'cardiovascularAgonist',
            striped: true
        },
        default: {
            primaryColor: '#fff',
            highlightColor: '#fff'
        },
        desflurane: {
            primaryColor: '#0ab8fd'
        },
        enflurane: {
            primaryColor: '#f58733'
        },
        epinephrine: {
            parentColor: 'cardiovascularAntagonist',
            highlightColor: '#000'
        },
        halothane: {
            primaryColor: '#b20107'
        },
        isoflurane: {
            primaryColor: '#ca7fc0'
        },
        localAnesthetic: {
            primaryColor: '#dad9d6'
        },
        neuromuscularBlocker: {
            primaryColor: '#fe5442'
        },
        neuromuscularBlockerReversal: {
            parentColor: 'neuromuscularBlocker',
            striped: true
        },
        nitrousOxide: {
            primaryColor: '#2d549f'
        },
        opioid: {
            primaryColor: '#6cd1ef'
        },
        opioidReversal: {
            parentColor: 'opioid',
            striped: true
        },
        sedativeHypnotic: {
            primaryColor: '#ffe800'
        },
        sevoflurane: {
            primaryColor: '#f8da00'
        },
        succinylcholine: {
            parentColor: 'neuromuscularBlocker',
            highlightColor: '#000'
        }
    } );



    /**
     * DrugPopulation data
     */
    mw.calculators.addDrugPopulations( {
        general: {
            name: 'General',
            abbreviation: 'Gen.'
        },
        neonatal: {
            name: 'Neonatal',
            abbreviation: 'Neo.',
            variables: {
                age: {
                    max: '0 yo'
                }
            }
        },
        pediatric: {
            name: 'Pediatric',
            abbreviation: 'Ped.',
            variables: {
                age: {
                    min: '0 yo',
                    max: '17.9 yo'
                }
            }
        },
        elderly: {
            name: 'Elderly',
            abbreviation: 'Eld.',
            variables: {
                age: {
                    min: '65 yo'
                }
            }
        }
    } );



    /**
     * DrugIndication data
     */
    mw.calculators.addDrugIndications( {
        abxProphylaxis: {
            name: 'Antimicrobial prophylaxis',
            abbreviation: 'Abx.'
        },
        anxiolysis: {
            name: 'Anxiolysis',
            abbreviation: 'Anxiety'
        },
        generalAnesthesiaInduction: {
            name: 'General anesthesia (induction)',
            abbreviation: 'GA ind.'
        },
        generalAnesthesiaMaintenance: {
            name: 'General anesthesia (maintenance)',
            abbreviation: 'GA maint.'
        },
        neuromuscularBlockade: {
            name: 'Neuromuscular blockade (non-RSI)',
            abbreviation: 'NMB (non-RSI)'
        },
        neuromuscularBlockadeRsi: {
            name: 'Neuromuscular blockade (rapid sequence induction)',
            abbreviation: 'NMB (RSI)'
        },
        neuromuscularBlockadeReversal: {
            name: 'Neuromuscular blockade reversal',
            abbreviation: 'NMB reversal'
        },
        mac: {
            name: 'Monitored anesthesia care',
            abbreviation: 'MAC'
        },
        ponv: {
            name: 'Postoperative nausea & vomiting',
            abbreviation: 'PONV'
        },
        tivaLoad: {
            name: 'Total intravenous anesthesia (loading dose)',
            abbreviation: 'TIVA load'
        },
        tivaMaintenance: {
            name: 'Total intravenous anesthesia (maintenance)',
            abbreviation: 'TIVA maint.'
        }
    } );



    /**
     * Drug data
     */
    var drugId;



    /**
     * Cefazolin
     */
    drugId = 'cefazolin';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Cefazolin'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'abxProphylaxis',
            population: 'general',
            dose: {
                dose: '2 g',
                frequency: 'q4h'
            }
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '1 g/vial'
        }
    ] );



    /**
     * Ketamine
     */
    drugId = 'ketamine';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Ketamine',
            color: 'sedativeHypnotic'
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '10 mg/mL'
        }, {
            concentration: '50 mg/mL'
        }, {
            concentration: '100 mg/mL'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'generalAnesthesiaInduction',
            population: 'general',
            dose: {
                min: '1 mg/kg',
                max: '2 mg/kg',
                weightCalculation: 'lbw'
            }
        }, {
            indication: 'generalAnesthesiaInduction',
            population: 'general',
            route: 'im',
            dose: {
                min: '4 mg/kg',
                max: '6 mg/kg'
            }
        }
    ] );



    /**
     * Lidocaine
     */
    drugId = 'lidocaine';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Lidocaine',
            color: 'localAnesthetic'
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '1 pct'
        }, {
            concentration: '2 pct'
        }
    ] );



    /**
     * Midazolam
     */
    drugId = 'midazolam';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Midazolam',
            color: 'benzodiazepine'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'anxiolysis',
            population: 'general',
            dose: {
                min: '0.01 mg/kg',
                max: '0.03 mg/kg'
            }
        }, {
            indication: 'anxiolysis',
            population: 'general',
            route: 'im',
            dose: {
                min: '0.07 mg/kg',
                max: '0.08 mg/kg'
            }
        }, {
            indication: 'anxiolysis',
            population: 'general',
            route: 'po',
            dose: {
                dose: '0.5 mg/kg',
                absoluteMax: '20 mg'
            }
        }, {
            indication: 'generalAnesthesiaInduction',
            population: 'general',
            dose: {
                min: '0.1 mg/kg',
                max: '0.3 mg/kg',
                weightCalculation: 'lbw'
            }
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '1 mg/mL'
        }, {
            concentration: '5 mg/mL'
        }
    ] );



    /**
     * Neostigmine
     */
    drugId = 'neostigmine';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Neostigmine',
            color: 'neuromuscularBlockerReversal'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'neuromuscularBlockadeReversal',
            population: 'general',
            dose: {
                min: '0.03 mg/kg',
                max: '0.07 mg/kg',
                absoluteMax: '5 mg'
            },
            description: 'For each 1 mg of neostigmine, give 0.2 mg of glycopyrrolate to avoid bradycardia'
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '0.5 mg/mL'
        }, {
            concentration: '1 mg/mL'
        }
    ] );



    /**
     * Ondansetron
     */
    drugId = 'ondansetron';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Ondansetron'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'ponv',
            population: 'general',
            dose: {
                dose: '4 mg'
            }
        }, {
            indication: 'ponv',
            population: 'pediatric',
            dose: {
                dose: '0.1 mg/kg',
                absoluteMax: '4 mg'
            }
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '2 mg/mL'
        }
    ] );



    /**
     * Propofol
     */
    drugId = 'propofol';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Propofol',
            color: 'sedativeHypnotic'
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '10 mg/mL'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'generalAnesthesiaInduction',
            population: 'general',
            dose: {
                min: '1 mg/kg',
                max: '2.5 mg/kg',
                weightCalculation: 'lbw'
            }
        }, {
            indication: 'generalAnesthesiaInduction',
            population: 'pediatric',
            dose: {
                min: '2.5 mg/kg',
                max: '3.5 mg/kg',
                weightCalculation: 'lbw'
            }
        }, {
            indication: 'generalAnesthesiaInduction',
            population: 'elderly',
            dose: {
                min: '1 mg/kg',
                max: '1.5 mg/kg',
                weightCalculation: 'lbw'
            }
        }, {
            indication: 'generalAnesthesiaMaintenance',
            population: 'general',
            dose: {
                min: '100 mcg/kg/min',
                max: '200 mcg/kg/min'
            }
        }, {
            indication: 'generalAnesthesiaMaintenance',
            population: 'pediatric',
            dose: {
                min: '125 mcg/kg/min',
                max: '300 mcg/kg/min'
            }
        }, {
            indication: 'generalAnesthesiaMaintenance',
            population: 'elderly',
            dose: {
                min: '50 mcg/kg/min',
                max: '100 mcg/kg/min'
            }
        }, {
            indication: 'mac',
            population: 'general',
            dose: {
                min: '25 mcg/kg/min',
                max: '75 mcg/kg/min'
            }
        }
    ] );



    /**
     * Rocuronium
     */
    drugId = 'rocuronium';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Rocuronium',
            color: 'neuromuscularBlocker'
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '10 mg/mL'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'neuromuscularBlockade',
            population: 'general',
            dose: {
                dose: '0.6 mg/kg'
            }
        }, {
            indication: 'neuromuscularBlockadeRsi',
            population: 'general',
            dose: {
                dose: '1.2 mg/kg'
            }
        }
    ] );



    /**
     * Sufentanil
     */
    drugId = 'sufentanil';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Sufentanil',
            color: 'opioid'
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '5 mcg/mL'
        },
        {
            concentration: '50 mcg/mL'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'tivaLoad',
            population: 'general',
            dose: {
                min: '0.25 mcg/kg',
                max: '2 mcg/kg',
                weightCalculation: 'lbw'
            }
        }, {
            indication: 'tivaMaintenance',
            population: 'general',
            dose: {
                name: 'Maintenance',
                min: '0.5 mcg/kg/hr',
                max: '1.5 mcg/kg/hr'
            }
        }
    ] );



    /**
     * Sugammadex
     */
    drugId = 'sugammadex';

    mw.calculators.addDrugs( [
        {
            id: drugId,
            name: 'Sugammadex',
            color: 'neuromuscularBlockerReversal'
        }
    ] );

    mw.calculators.addDrugDosages( drugId, [
        {
            indication: 'neuromuscularBlockadeReversal',
            population: 'general',
            dose: {
                min: '2 mg/kg',
                max: '4 mg/kg',
                absoluteMax: '16 mg/kg'
            }
        }
    ] );

    mw.calculators.addDrugPreparations( drugId, [
        {
            concentration: '100 mg/mL'
        }
    ] );








    /**
     * DrugDosage
     *
     * Structure is:
     *
     * drugId: {
     *     indicationId: {
     *         doseId: {
     *
     *         }
     *     }
     * }
     */

    var drugDosages = {
        cefazolin: {
            abxProphylaxis: {
                general: {
                    population: 'general',
                    dose: '2 g'
                },
                general120kg: {
                    population: {
                        id: 'general',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                    },
                    dose: '3 g'
                },
                pediatric: {
                    dose: {
                        absoluteMax: '2 g',
                        dose: '30 mg/kg'
                    }
                },
                pediatric120kg: {
                    population: {
                        id: 'pediatric',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                    },
                    dose: {
                        dose: '3 g'
                    }
                }
            }
        }
    };

}() );