MediaWiki:Gadget-calculator-drugs-core.js

From WikiAnesthesia
Revision as of 18:54, 7 August 2021 by Chris Rishel (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * @author Chris Rishel
 */
( function() {
    var DEFAULT_DRUG_COLOR = 'default';
    var DEFAULT_DRUG_POPULATION = 'general';

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

                return units;
            }
        }
    } );

    mw.calculators.addUnits( {
        pct: {
            baseName: 'concentration',
            definition: '10 mg/mL'
        }
    } );



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

        if( !this.primaryColor && !this.parentColor ) {
            throw new Error( 'Drug color "' + this.id + '" must define either a primary color or a parent color.' );
        }
    };

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

    mw.calculators.objectClasses.DrugColor.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.getHighlightColor = function() {
        if( this.highlightColor ) {
            return this.highlightColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getHighlightColor();
        }
    };

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

    mw.calculators.objectClasses.DrugColor.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' );
                }
            }
        }
    };

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





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

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

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

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



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

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

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





    /**
     * 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.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 ) {
        var properties = {
            required: [
                'id',
                'name'
            ],
            optional: [
                'color'
            ]
        };

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

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

        this.dosages = {};
        this.preparations = {};
    };

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





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

        for( var drugPreparationId in drugPreparationData ) {
            drugPreparationData[ drugPreparationId ].drug = drugId;
        }

        var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData );

        for( var drugPreparationId in drugPreparations ) {
            mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
        }
    };



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

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

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





    /**
     * DrugDosage
     */
    mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
        if( !mw.calculators.getDrug( drugId ) ) {
            throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
        }

        for( var drugDosageId in drugDosageData ) {
            drugDosageData[ drugDosageId ].drug = drugId;
        }

        var drugDosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );

        for( var drugDosageId in drugDosages ) {
            mw.calculators.drugs[ drugId ].dosages[ drugDosageId ] = drugDosages[ drugDosageId ];
        }
    };



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

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

        if( !this.population ) {
            this.population = DEFAULT_DRUG_POPULATION;
        }
    };

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










    /*******
     * BEGIN DRUG DATA
     *******/



    /**
     * 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'
        },
        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( {
        generalAnesthesia: {
            name: 'General anesthesia',
            abbreviation: 'GA'
        }
    } );



    /**
     * Drug data
     */


    /**
     * Cefazolin
     */
    mw.calculators.addDrugs( {
        cefazolin: {
            name: 'Cefazolin'
        }
    } );


    /**
     * Ketamine
     */
    mw.calculators.addDrugs( {
        ketamine: {
            name: 'Ketamine',
            color: 'sedativeHypnotic'
        }
    } );

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


    /**
     * Lidocaine
     */
    mw.calculators.addDrugs( {
        lidocaine: {
            name: 'Lidocaine',
            color: 'localAnesthetic'
        }
    } );

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


    /**
     * Propofol
     */
    mw.calculators.addDrugs( {
        propofol: {
            name: 'Propofol',
            color: 'sedativeHypnotic'
        }
    } );

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

    mw.calculators.addDrugDosages( 'propofol', [
        {
            indication: 'generalAnesthesia',
            population: 'general',
            dose: [
                {
                    name: 'Induction',
                    min: '1 mg/kg',
                    max: '2.5 mg/kg',
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '100 mcg/kg/min',
                    max: '200 mcg/kg/min',
                    weightCalculation: 'tbw',
                    route: 'IV'
                }
            ]
        }, {
            indication: 'generalAnesthesia',
            population: 'pediatric',
            dose: [
                {
                    name: 'Induction',
                    min: '2.5 mg/kg',
                    max: '3.5 mg/kg',
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '125 mcg/kg/min',
                    max: '300 mcg/kg/min',
                    weightCalculation: 'tbw'
                }
            ]
        }, {
            indication: 'generalAnesthesia',
            population: 'elderly',
            dose: [
                {
                    name: 'Induction',
                    min: '1 mg/kg',
                    max: '1.5 mg/kg',
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '50 mcg/kg/min',
                    max: '100 mcg/kg/min',
                    weightCalculation: 'tbw'
                }
            ]
        }, {
            indication: 'mac',
            population: 'general',
            dose: [
                {
                    min: '25 mcg/kg/min',
                    max: '75 mcg/kg/min'
                }
            ]
        }
    ] );




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

}() );