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

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


     var TYPE_NUMBER = 'number';
     /**
    var TYPE_STRING = 'string';
    * Define units
 
    */
     var VALID_TYPES = [
     mw.calculators.addUnitsBases( {
        TYPE_NUMBER,
         concentration: {
         TYPE_STRING
            toString: function( units ) {
    ];
                units = units.replace( ' pct', '%' );
 
    var DEFAULT_CALCULATION_CLASS = 'SimpleCalculation';
    var DEFAULT_CALCULATOR_CLASS = 'SimpleCalculator';


    // Polyfill to fetch unit's base. This may become unnecessary in a future version of math.js
                 return units;
    math.Unit.prototype.getBase = function() {
        for( var iBase in math.Unit.BASE_UNITS ) {
            if( this.equalBase( math.Unit.BASE_UNITS[ iBase ] ) ) {
                 return iBase;
             }
             }
         }
         }
    } );


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




    mw.calculators = {
        calculators: {},
        calculations: {},
        objectClasses: {},
        units: {},
        unitsBases: {},
        variables: {},
        addCalculations: function( calculationData, className ) {
            className = className ? className : DEFAULT_CALCULATION_CLASS;


            var calculations = mw.calculators.createCalculatorObjects( className, calculationData );
    /**
    * DrugColor
    */
    mw.calculators.drugColors = {};


            for( var calculationId in calculations ) {
    mw.calculators.addDrugColors = function( drugColorData ) {
                var calculation = calculations[ calculationId ];
        var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );


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


                var inputCalculations = calculation.data.calculations.required.concat( calculation.data.calculations.optional );
    mw.calculators.getDrugColor = function( drugColorId ) {
        if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
            return mw.calculators.drugColors[ drugColorId ];
        } else {
            return null;
        }
    };


                for( var iInputCalculation in inputCalculations ) {
    /**
                    var inputCalculationId = inputCalculations[ iInputCalculation ];
    * 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'
            ]
        };


                    if( !mw.calculators.calculations.hasOwnProperty( inputCalculationId ) ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
                        throw new Error('Calculation "' + inputCalculationId + '" does not exist for calculation "' + calculationId + '"');
                    }


                    mw.calculators.calculations[ inputCalculationId ].addCalculation( calculationId );
        if( !this.primaryColor && !this.parentColor ) {
                }
            throw new Error( 'Drug color "' + this.id + '" must define either a primary color or a parent color.' );
        }
    };


                var inputVariables = calculation.data.variables.required.concat( calculation.data.variables.optional );
    mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );


                for( var iInputVariable in inputVariables ) {
    mw.calculators.objectClasses.DrugColor.getParentDrugColor = function() {
                    var inputVariableId = inputVariables[ iInputVariable ];
        if( !this.parentColor ) {
            return null;
        }


                    if( !mw.calculators.variables.hasOwnProperty( inputVariableId ) ) {
        var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );
                        throw new Error('Variable "' + inputVariableId + '" does not exist for calculation "' + calculationId + '"');
                    }


                    mw.calculators.variables[ inputVariableId ].addCalculation( calculationId );
        if( !parentDrugColor ) {
                }
            throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
            }
         }
         },
        addCalculators: function( moduleId, calculatorData, className ) {
            className = className ? className : DEFAULT_CALCULATOR_CLASS;


            for( var calculatorId in calculatorData ) {
        return parentDrugColor;
                calculatorData[ calculatorId ].module = moduleId;
    };
            }


            var calculators = mw.calculators.createCalculatorObjects( className, calculatorData );
    mw.calculators.objectClasses.DrugColor.getHighlightColor = function() {
        if( this.highlightColor ) {
            return this.highlightColor;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().getHighlightColor();
        }
    };


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


             for( var calculatorId in calculators ) {
    mw.calculators.objectClasses.DrugColor.isStriped = function() {
                mw.calculators.calculators[ moduleId ][ calculatorId ] = calculators[ calculatorId ];
        if( this.striped !== null ) {
             return this.striped;
        } else if( this.parentColor ) {
            return this.getParentDrugColor().isStriped();
        }
    };


                mw.calculators.calculators[ moduleId ][ calculatorId ].render();
            }
        },
        addUnitsBases: function( unitsBaseData ) {
            var unitsBases = mw.calculators.createCalculatorObjects( 'UnitsBase', unitsBaseData );


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


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


                try {
                    math.createUnit( unitsId, {
                        aliases: units[ unitsId ].aliases,
                        baseName: units[ unitsId ].baseName,
                        definition: units[ unitsId ].definition,
                        prefixes: units[ unitsId ].prefixes,
                        offset: units[ unitsId ].offset,
                    } );
                } catch( e ) {
                    console.warn( e.message );
                }


                mw.calculators.units[ units ] = units[ unitsId ];
    /**
            }
    * DrugPopulation
        },
    */
        addVariables: function( variableData ) {
            var variables = mw.calculators.createCalculatorObjects( 'Variable', variableData );


            for( var varId in variables ) {
    mw.calculators.drugPopulations = {};
                var variable = variables[ varId ];


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


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


                mw.calculators.variables[ varId ] = variable;
    mw.calculators.getDrugPopulation = function( drugPopulationId ) {
            }
        if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
        },
            return mw.calculators.drugPopulations[ drugPopulationId ];
        createCalculatorObjects: function( className, objectData ) {
        } else {
            if( !mw.calculators.objectClasses.hasOwnProperty( className ) ) {
             return null;
                throw new Error( 'Invalid class name "' + className + '"' );
        }
             }
    };


            var objects = {};


            for( var objectId in objectData ) {
                var propertyValues = objectData[ objectId ];


                if( typeof objectId === 'string' ) {
    /**
                    propertyValues.id = objectId;
    * 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'
            ]
        };


                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            }
 
            return objects;
        },
        createInputGroup: function( variableIds ) {
            var $form = $( '<form>', {


            } );
        if( this.variables ) {
 
             for( var variableId in this.variables ) {
            var $formRow = $( '<div>', {
                 if( !mw.calculators.getVariable( variableId ) ) {
                class: 'form-row'
                     throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
            } ).css( 'flex-wrap', 'nowrap' );
 
             for( var iVariableId in variableIds ) {
                var variableId = variableIds[ iVariableId ];
 
                 if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                     throw new Error( 'Invalid variable name "' + variableId + '"' );
                 }
                 }
                $formRow.append( mw.calculators.variables[ variableId ].createInput() );
            }
            return $form.append( $formRow );
        },
        getCookieKey: function( variableId ) {
            return 'calculators-var-' + variableId;
        },
        getCookieValue: function( varId ) {
            var cookieValue = mw.cookie.get( mw.calculators.getCookieKey( varId ) );
            if( !cookieValue ) {
                return null;
            }
            return cookieValue;
        },
        getCalculation: function( calculationId ) {
            if( mw.calculators.calculations.hasOwnProperty( calculationId ) ) {
                return mw.calculators.calculations[ calculationId ];
            } else {
                return null;
            }
        },
        getCalculator: function( moduleId, calculatorId ) {
            if( mw.calculators.calculators.hasOwnProperty( moduleId ) &&
                mw.calculators.calculators[ moduleId ].hasOwnProperty( calculatorId ) ) {
                return mw.calculators.calculators[ moduleId ][ calculatorId ];
            } else {
                return null;
            }
        },
        getUnitsString: function( value ) {
            if( typeof value !== 'object' ) {
                return null;
            }
            var units = value.formatUnits()
                .replace( /\s/g, '' )
                .replace( /(\^(\d+))/g, '<sup>$2</sup>' );
            var unitsBase = value.getBase();
            if( mw.calculators.unitsBases.hasOwnProperty( unitsBase ) &&
                typeof mw.calculators.unitsBases[ unitsBase ].toString === 'function' ) {
                units = mw.calculators.unitsBases[ unitsBase ].toString( units );
             }
             }
        }
    };


            return units;
    mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
        },
        getVariable: function( variableId ) {
            if( mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return mw.calculators.variables[ variableId ];
            } else {
                return null;
            }
        },
        init: function() {
            $( '.calculator' ).each( function() {
                var gadgetModule = 'ext.gadget.calculator-' + $( this ).attr( 'data-module' );


                if( gadgetModule && mw.loader.getState( gadgetModule ) === 'registered' ) {
                    mw.loader.load( gadgetModule );
                }
            } );
        },
        isMobile: function() {
            return window.matchMedia( 'only screen and (max-width: 760px)' ).matches;
        },
        setValue: function( variableId, value ) {
            if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
                return false;
            }


            if( mw.calculators.variables[ variableId ].setValue( value ) ) {
                mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                    expires: COOKIE_EXPIRATION
                } );


                return true;
            }


            return false;
        }
    };


     /**
     /**
     * Class CalculatorObject
     * DrugIndication
    *
    * @param {Object} properties
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.CalculatorObject}
    * @constructor
     */
     */
     mw.calculators.objectClasses.CalculatorObject = function( properties, propertyValues ) {
     mw.calculators.drugIndications = {};
        if( properties ) {
            if( properties.hasOwnProperty( 'required' ) ) {
                for( var iRequiredProperty in properties.required ) {
                    var requiredProperty = properties.required[ iRequiredProperty ];
 
                    if( !propertyValues || !propertyValues.hasOwnProperty( requiredProperty ) ) {
                        console.error( 'Missing required property "' + requiredProperty + '"' );
                        console.log( propertyValues );
 
                        return null;
                    }


                    this[ requiredProperty ] = propertyValues[ requiredProperty ];
    mw.calculators.addDrugIndications = function( drugIndicationData ) {
        var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );


                    delete propertyValues[ requiredProperty ];
        for( var drugIndicationId in drugIndications ) {
                }
            mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
            }
 
            if( properties.hasOwnProperty( 'optional' ) ) {
                for( var iOptionalProperty in properties.optional ) {
                    var optionalProperty = properties.optional[ iOptionalProperty ];
 
                    if( propertyValues && propertyValues.hasOwnProperty( optionalProperty ) ) {
                        this[ optionalProperty ] = propertyValues[ optionalProperty ];
 
                        delete propertyValues[ optionalProperty ];
                    } else if( typeof this[ optionalProperty ] === 'undefined' ) {
                        this[ optionalProperty ] = null;
                    }
                }
            }
 
            var invalidProperties = Object.keys( propertyValues );
 
            if( invalidProperties.length ) {
                console.warn( 'Unsupported properties defined for ' + typeof this + ' with id "' + this.id + '": ' + invalidProperties.join( ', ' ) );
            }
         }
         }
     };
     };


     mw.calculators.objectClasses.CalculatorObject.prototype.getProperties = function() {
     mw.calculators.getDrugIndication = function( drugIndicationId ) {
         return {
         if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
            required: [],
             return mw.calculators.drugIndications[ drugIndicationId ];
            optional: []
         } else {
        };
            return null;
    };
         }
 
    mw.calculators.objectClasses.CalculatorObject.prototype.mergeProperties = function( inheritedProperties, properties ) {
        var uniqueValues = function( value, index, self ) {
             return self.indexOf( value ) === index;
         };
 
        properties.required = inheritedProperties.required.concat( properties.required ).filter( uniqueValues );
         properties.optional = inheritedProperties.optional.concat( properties.optional ).filter( uniqueValues );
 
        return properties;
     };
     };






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


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




Line 350: Line 228:


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


         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         for( var drugId in drugs ) {
            mw.calculators.drugs[ drugId ] = drugs[ drugId ];
        }
     };
     };


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






     /**
     /**
     * Class Variable
     * Class Drug
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.Variable}
     * @returns {mw.calculators.objectClasses.Drug}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.Variable = function( propertyValues ) {
     mw.calculators.objectClasses.Drug = function( propertyValues ) {
         var properties = {
         var properties = {
             required: [
             required: [
                 'id',
                 'id',
                 'name',
                 'name'
                'type'
             ],
             ],
             optional: [
             optional: [
                 'abbreviation',
                 'color'
                'defaultValue',
                'maxLength',
                'options',
                'units'
             ]
             ]
         };
         };
Line 401: Line 269:
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


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


         this.calculations = [];
         this.dosages = {};
 
         this.preparations = {};
         if( this.defaultValue ) {
            this.setValue( this.defaultValue );
        } else {
            this.value = null;
        }
     };
     };


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


    mw.calculators.objectClasses.Variable.prototype.addCalculation = function( calculationId ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            return;
        }


        this.calculations.push( calculationId );
    };


    mw.calculators.objectClasses.Variable.prototype.createInput = function() {
        var variableId = this.id;


        var inputContainerAttribs = {
            class: 'form-group calculator-container-input'
        };


        inputContainerAttribs.class = inputContainerAttribs.class + ' calculator-container-input-' + variableId;
    /**
 
    * DrugPreparation
        // Create the input container
    */
        var $inputContainer = $( '<div>', inputContainerAttribs );
    mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
 
        if( !mw.calculators.getDrug( drugId ) ) {
        // Set the input id
             throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
        var inputId = 'calculator-input-' + variableId;
 
        // Initialize label attributes
        var labelAttributes = {
            for: inputId,
            text: this.getLabelString()
        };
 
        // Create the input label and append to the container
        $inputContainer.append( $( '<label>', labelAttributes ) );
 
        if( this.type === TYPE_NUMBER ) {
            // Initialize the primary units variables (needed for handlers, even if doesn't have units)
            var unitsId = null;
            var $unitsContainer = null;
 
            // Initialize input options
            var inputAttributes = {
                id: inputId,
                class: 'form-control calculator-input-text',
                type: 'text',
                autocomplete: 'off',
                inputmode: 'decimal',
                value: this.isValueMathObject() ? this.value.toNumber() : this.value
            };
 
            // Configure additional options
            if( this.maxLength ) {
                inputAttributes.maxlength = this.maxLength;
            }
 
            // Add the input id to the list of classes
            inputAttributes.class = inputAttributes.class + ' ' + inputId;
 
            // If the variable has units, create the units input
            if( this.hasUnits() ) {
                // Set the units id
                unitsId = inputId + '-units';
 
                var unitsValue = this.isValueMathObject() ? this.value.formatUnits() : null;
 
                // Create the units container
                $unitsContainer = $( '<div>', {
                    class: 'input-group-append'
                } );
 
                // Initialize the units input options
                var unitsInputAttributes = {
                    id: unitsId,
                    class: 'custom-select calculator-input-select'
                };
 
                unitsInputAttributes.class = unitsInputAttributes.class + ' ' + unitsId;
 
                var $unitsInput = $( '<select>', unitsInputAttributes )
                    .on( 'change', function() {
                        var newValue = $( '#' + inputId ).val() + ' ' + $( this ).val();
 
                        mw.calculators.setValue( variableId, newValue );
                    } );
 
                for( var iUnits in this.units ) {
                    var units = this.units[ iUnits ];
 
                    var unitsOptionAttributes = {
                        text: mw.calculators.getUnitsString( math.unit( '0 ' + units ) ),
                        value: units
                    };
 
                    if( units === unitsValue ) {
                        unitsOptionAttributes.selected = true;
                    }
 
                    $unitsInput.append( $( '<option>', unitsOptionAttributes ) );
                }
 
                $unitsContainer.append( $unitsInput );
            }
 
             // Create the input and add handlers
            var $input = $( '<input>', inputAttributes )
                .on( 'input', function() {
                    var newValue = $( this ).val();
 
                    if( unitsId ) {
                        newValue = newValue + ' ' + $( '#' + unitsId ).val();
                    }
 
                    mw.calculators.setValue( variableId, newValue );
                } );
 
            // Create the input group
            var $inputGroup = $( '<div>', {
                class: 'input-group'
            } ).append( $input );
 
            if( $unitsContainer ) {
                $inputGroup.append( $unitsContainer );
            }
 
            $inputContainer.append( $inputGroup );
        } else if( this.type === TYPE_STRING ) {
            if( this.hasOptions() ) {
                var varOptions = this.options;
 
                var selectAttributes = {
                    id: inputId,
                    class: 'custom-select calculator-input-select'
                };
 
                var $select = $( '<select>', selectAttributes )
                    .on( 'change', function() {
                        mw.calculators.setValue( variableId, $( this ).val() );
                    } );
 
                for( var iVarOption in varOptions ) {
                    var varOption = varOptions[ iVarOption ];
 
                    var optionAttributes = {
                        value: varOption,
                        text: varOption
                    };
 
                    if( varOption === this.value ) {
                        optionAttributes.selected = true;
                    }
 
                    $select.append( $( '<option>', optionAttributes ) );
                }
 
                $inputContainer.append( $select );
            }
         }
         }


         return $inputContainer;
         for( var drugPreparationId in drugPreparationData ) {
    };
            drugPreparationData[ drugPreparationId ].drug = drugId;
 
    mw.calculators.objectClasses.Variable.prototype.getLabelString = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };
 
    mw.calculators.objectClasses.Variable.prototype.getValueString = function() {
        return String( this.value );
    };
 
    mw.calculators.objectClasses.Variable.prototype.hasOptions = function() {
        return this.options !== null;
    };
 
    mw.calculators.objectClasses.Variable.prototype.hasUnits = function() {
        return this.units !== null;
    };
 
    mw.calculators.objectClasses.Variable.prototype.hasValue = function() {
        if( !this.value ||
            ( this.isValueMathObject() && !this.value.toNumber() ) ) {
            return false;
         }
         }


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


    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
         for( var drugPreparationId in drugPreparations ) {
         return this.value && this.value.hasOwnProperty( 'value' );
             mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
    };
 
    mw.calculators.objectClasses.Variable.prototype.setValue = function( value ) {
        if( this.type === TYPE_NUMBER ) {
            if( typeof value !== 'object' ) {
                value = math.unit( value );
            }
 
            if( this.hasUnits() ) {
                var valueUnits = value.formatUnits();
 
                if( !valueUnits ) {
                    throw new Error( 'Could not set value for "' + this.id + '": Value must define units' );
                } else if( this.units.indexOf( valueUnits ) === -1 ) {
                    throw new Error( 'Could not set value for "' + this.id + '": Units "' + valueUnits + '" are not valid for this variable' );
                }
             }
        } else if( this.hasOptions() ) {
            if( this.options.indexOf( value ) === -1 ) {
                throw new Error( 'Could not set value "' + value + '" for "' + this.id + '": Value must define be one of: ' + this.options.join( ', ' ) );
            }
         }
         }
        this.value = value;
        for( var iCalculation in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
            if( calculation ) {
                calculation.render();
            }
        }
        return true;
     };
     };


Line 640: Line 305:


     /**
     /**
     * Class AbstractCalculation
     * Class DrugPreparation
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.AbstractCalculation}
     * @returns {mw.calculators.objectClasses.DrugPreparation}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.AbstractCalculation = function( propertyValues ) {
     mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
         var properties = {
 
        this.initialize();
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.addCalculation = function( calculationId ) {
        if( this.calculations.indexOf( calculationId ) !== -1 ) {
            return;
        }
 
        this.calculations.push( calculationId );
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getContainerClass = function() {
        return 'calculator-calculation-' + this.id;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getLabelString = function() {
        return this.id;
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties = function() {
        return {
             required: [
             required: [
                'drug',
                 'id',
                 'id',
                 'calculate'
                 'concentration'
             ],
             ],
             optional: [
             optional: [
                 'data',
                 'dilutionRequired',
                 'description',
                 'commonDilution'
                'onRender',
                'onRendered',
                'references',
                'type'
             ]
             ]
         };
         };
    };


    mw.calculators.objectClasses.AbstractCalculation.prototype.getValue = function() {
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
        // For now, we always need to recalculate, since the calculation may not be rendered but still required by
        // other calculations (i.e. drug dosages using lean body weight).
        this.recalculate();
 
        return this.value;
     };
     };


     mw.calculators.objectClasses.AbstractCalculation.prototype.hasInfo = function() {};
     mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );


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


        return true;
    };


    mw.calculators.objectClasses.AbstractCalculation.prototype.getData = function() {
        var data = {};
        var missingRequiredData = '';
        var calculationId, calculation, variableId, variable;


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


            if( !calculation ) {
    /**
                throw new Error( 'Invalid required calculation "' + calculationId + '" for calculation "' + this.id + '"' );
    * DrugDosage
            } else if( !calculation.hasValue() ) {
    */
                if( missingRequiredData ) {
    mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
                    missingRequiredData = missingRequiredData + ', ';
        if( !mw.calculators.getDrug( drugId ) ) {
                }
            throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
 
                missingRequiredData = missingRequiredData + calculation.getLabelString();
            } else {
                data[ calculationId ] = calculation.value;
            }
         }
         }


         for( var iRequiredVariable in this.data.variables.required ) {
         for( var drugDosageId in drugDosageData ) {
             variableId = this.data.variables.required[ iRequiredVariable ];
             drugDosageData[ drugDosageId ].drug = drugId;
            variable = mw.calculators.getVariable( variableId );
 
            if( !variable ) {
                throw new Error( 'Invalid required variable "' + variableId + '" for calculation "' + this.id + '"' );
            } else if( !variable.hasValue() ) {
                if( missingRequiredData ) {
                    missingRequiredData = missingRequiredData + ', ';
                }
 
                missingRequiredData = missingRequiredData + variable.getLabelString();
            } else {
                data[ variableId ] = variable.value;
            }
         }
         }


         if( missingRequiredData ) {
         var drugDosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
            this.message = missingRequiredData + ' required';


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


        for( var iOptionalVariable in this.data.variables.optional ) {
            variableId = this.data.variables.optional[ iOptionalVariable ];
            variable = mw.calculators.getVariable( variableId );


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


             data[ variableId ] = variable.hasValue() ? variable.value : null;
    /**
         }
    * 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'
            ]
         };


         for( var iOptionalCalculation in this.data.calculations.optional ) {
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
            calculationId = this.data.calculations.optional[ calculationId ];
            calculation = mw.calculators.getVariable( calculationId );


            if( !calculation ) {
        if( !this.population ) {
                throw new Error( 'Invalid optional variable "' + calculationId + '" for calculation "' + this.id + '"' );
            this.population = DEFAULT_DRUG_POPULATION;
            }
 
            data[ calculationId ] = calculation.hasValue() ? calculation.value : null;
         }
         }


         return data;
         mw.calculators.addDrugDoseCalculations( this.drug, this.id, this.dose );
     };
     };


     mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
     mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
        if( typeof this.calculate !== 'function' ) {
            throw new Error( 'calculate() must be a function for Calculation "' + this.id + '"' );
        }


        // Initialize array to store calculation ids which depend on this calculation's value
        this.calculations = [];


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


        dataPrototype = {
    /**
            optional: [],
    * DrugDosageCalculation
            required: []
    */
        };
    mw.calculators.addDrugDoseCalculations = function( drugId, drugDosageId, drugDoseCalculationData ) {
 
         if( !mw.calculators.getDrug( drugId ) ) {
        this.data = this.data ? this.data : {
             throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
            calculations: dataPrototype,
            variables: dataPrototype
        };
 
         if( !this.data.hasOwnProperty( 'calculations' ) ) {
             this.data.calculations = dataPrototype;
        } else {
            this.data.calculations.optional = this.data.calculations.hasOwnProperty( 'optional' ) ? this.data.calculations.optional : [];
            this.data.calculations.required = this.data.calculations.hasOwnProperty( 'required' ) ? this.data.calculations.required : [];
         }
         }


         if( !this.data.hasOwnProperty( 'variables' ) ) {
         var doseProperties = [
             this.data.variables = dataPrototype;
            'dose',
        } else {
             'min',
             this.data.variables.optional = this.data.variables.hasOwnProperty( 'optional' ) ? this.data.variables.optional : [];
            'max',
             this.data.variables.required = this.data.variables.hasOwnProperty( 'required' ) ? this.data.variables.required : [];
             'absoluteMin',
        }
             'absoluteMax'
        ];


         this.message = null;
         var calculationData = {};
        this.value = null;
    };


    mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
        for( var doseId in drugDoseCalculationData ) {
        return this.value && this.value.hasOwnProperty( 'value' );
            drugDoseCalculationData[ doseId ].drug = drugId;
    };
            drugDoseCalculationData[ doseId ].calculate = mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate;


    mw.calculators.objectClasses.AbstractCalculation.prototype.recalculate = function() {
            var data = {
        this.message = '';
                calculations: {
        this.value = null;
                    required: [],
 
                    optional: []
        var data = this.getData();
                },
 
                variables: {
        if( data === false ) {
                    required: [],
             return false;
                    optional: []
        }
                }
             };


        try {
            // Look at dose properties to identify any variable dependence (e.g. weight-dependence)
            var value = this.calculate( data );
            for( var iDoseProperty in doseProperties ) {
                var dosePropertyValue = drugDoseCalculationData[ doseId ][ doseProperties[ iDoseProperty ] ];


            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
                if( this.units ) {
                if( dosePropertyValue &&
                     value = value + ' ' + this.units;
                    dosePropertyValue.match( /\/\s*?kg/ ) &&
                    data.variables.required.indexOf( 'weight' ) === -1 ) {
                     data.variables.required.push( 'weight' );
                 }
                 }
                this.value = math.unit( value );
            } else {
                this.value = value;
             }
             }


             for( var iCalculation in this.calculations ) {
             if( drugDoseCalculationData[ doseId ].hasOwnProperty( 'weightCalculation' ) ) {
                 calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
                 var weightCalculationId = drugDoseCalculationData[ doseId ].weightCalculation;
                var weightCalculation = mw.calculators.getCalculation( weightCalculationId );


                 if( calculation ) {
                 if( !weightCalculation ) {
                     calculation.render();
                     throw new Error( 'Drug "' + drugId + '" dose ' + drugDosageId + '-' + doseId + ': weightCalculation "' + weightCalculationId + '" which is not defined' );
                 }
                 }
                data.calculations.optional.push( weightCalculationId );
                data.variables.optional = data.variables.optional.concat( weightCalculation.data.variables.required.concat( weightCalculation.data.variables.optional ) );
             }
             }
        } catch( e ) {
            this.message = e.message;
            this.value = null;
        }
        return true;
    };


            drugDoseCalculationData[ doseId ].data = data;


    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
            calculationData[ drugId + '-' + drugDosageId + '-' + doseId ] = drugDoseCalculationData[ doseId ];
        this.recalculate();
 
        if( typeof this.onRender === 'function' ) {
            this.onRender();
         }
         }


         this.doRender();
         mw.calculators.addCalculations( calculationData, 'DrugDoseCalculation' );
 
        if( typeof this.onRendered === 'function' ) {
            this.onRendered();
        }
     };
     };
    mw.calculators.objectClasses.AbstractCalculation.prototype.doRender = function() {};




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


Line 890: Line 463:
     };
     };


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


    mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate = function() {


    mw.calculators.objectClasses.SimpleCalculation.prototype.hasInfo = function() {
        return this.description || this.formula || this.references.length;
     };
     };


    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelHtml = function() {
        var labelHtml = this.getLabelString();


        if( this.link ) {
    mw.calculators.objectClasses.DrugDoseCalculation.prototype.getLabelHtml = function() {
            var href = this.link;
        var labelHtml = this.name;
 
            // Detect internal links (this isn't great)
            var matches = href.match(/\[\[(.*?)\]\]/);
 
            if( matches ) {
                href = mw.util.getUrl( matches[ 1 ] );
            }


            labelHtml = $( '<a>', {
        labelHtml = $( '<a>', {
                href: href,
            href: mw.util.getUrl( this.name ),
                text: labelHtml
            text: labelHtml
            } )[ 0 ].outerHTML;
        } )[ 0 ].outerHTML;
        }


         return labelHtml;
         return labelHtml;
     };
     };


     mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelString = function() {
     mw.calculators.objectClasses.DrugDoseCalculation.prototype.getProperties = function() {
        return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
    };
 
    mw.calculators.objectClasses.SimpleCalculation.prototype.getProperties = function() {
         var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
         var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();


         return this.mergeProperties( inheritedProperties, {
         return this.mergeProperties( inheritedProperties, {
             required: [
             required: [
                 'name'
                 'drug'
             ],
             ],
             optional: [
             optional: [
                 'abbreviation',
                 'absoluteMin',
                 'digits',
                 'absoluteMax',
                 'formula',
                 'dose',
                 'link',
                 'min',
                 'units'
                 'max',
                'route',
                'weightCalculation'
             ]
             ]
         } );
         } );
     };
     };


     mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
     mw.calculators.objectClasses.DrugDoseCalculation.prototype.initialize = function() {
         if( this.message ) {
         mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
            return this.message;
        } else if( typeof this.value === 'object' && this.value.hasOwnProperty( 'value' ) ) {
            // format() will convert the value to the most visually appealing units (e.g. 5200 mL becomes 5.2 L)
            // We then want to turn that back into a math object.
            var value = math.unit( this.value.format() );
            var units = value.formatUnits();
            var number = value.toNumber();


            var digits = ( this.value.formatUnits() === units && this.digits !== null ) ? this.digits : 1;
        this.route = this.route ? this.route : 'IV';
    };


            var valueString = String( number.toFixed( digits ) );


            if( units ) {
                valueString = valueString + ' ' + mw.calculators.getUnitsString( value );
            }


            return valueString;
        } else {
            return String( this.value );
        }
    };


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


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


        var valueString = this.getValueString();


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


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


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


        var calculation = this;
    /*******
    * BEGIN DRUG DATA
    *******/


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


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


             var $infoButton = null;
    /**
    * 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'
        }
    } );


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


            var labelHtml = calculation.getLabelHtml();


            if( isTable ) {
    /**
                if( calculation.hasInfo() ) {
    * DrugPopulation data
                    labelHtml += $( '<span>', {
    */
                        class: 'calculator-calculation-column-label-info'
    mw.calculators.addDrugPopulations( {
                     } ).append( $infoButton )[ 0 ].outerHTML;
        general: {
            name: 'General',
            abbreviation: 'Gen.'
        },
        neonatal: {
            name: 'Neonatal',
            abbreviation: 'Neo.',
            variables: {
                age: {
                     max: '0 yo'
                 }
                 }
                $( this )
                    .append( $( '<th>', {
                        html: labelHtml
                    } ) )
                    .append( $( '<td>', {
                        class: 'calculator-calculation-column-value',
                        html: valueString
                    } ) );
            } else {
                $( this )
                    .append( labelHtml + $infoButton[ 0 ].outerHTML + ': ' + valueString );
             }
             }
 
        },
             if( calculation.hasInfo() ) {
        pediatric: {
                var infoHtml = '';
             name: 'Pediatric',
 
            abbreviation: 'Ped.',
                 if( calculation.description ) {
            variables: {
                     infoHtml += $( '<p>', {
                 age: {
                        html: calculation.description
                     min: '0 yo',
                    } )[ 0 ].outerHTML;
                    max: '17.9 yo'
                 }
                 }
 
            }
                if( calculation.formula ) {
        },
                    infoHtml += $( '<span>', {
        elderly: {
                        class: calculation.getContainerClass() + '-formula'
            name: 'Elderly',
                    } )[ 0 ].outerHTML;
            abbreviation: 'Eld.',
 
            variables: {
                    var api = new mw.Api();
                 age: {
 
                     min: '65 yo'
                    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();
                }
 
                if( isTable ) {
                    $infoContainer = $( '<tr>', {
                        id: infoContainerId,
                        class: 'collapse'
                    } )
                        .append( $( '<td>', {
                            colspan: 2
                        } ).append( infoHtml ) );
                 } else {
                     $infoContainer = $( '<div>', {
                        id: infoContainerId,
                        class: 'collapse'
                    } ).append( infoHtml );
                 }
                 }
                $( this ).after( $infoContainer );
             }
             }
        }
    } );


            if( missingVariableInputs.length ) {
                var variablesContainerClass = 'calculator-calculation-variables ' + calculation.getContainerClass() + '-variables';
                var inputGroup = mw.calculators.createInputGroup( missingVariableInputs );


                if( isTable ) {
                    $variablesContainer =  $( '<tr>' )
                        .append( $( '<td>', {
                            class: variablesContainerClass,
                            colspan: 2
                        } ).append( inputGroup ) );
                } else {
                    $variablesContainer = $( '<div>', {
                        class: variablesContainerClass
                    } ).append( inputGroup );
                }


                $( this ).after( $variablesContainer );
    /**
 
    * DrugIndication data
                missingVariableInputs = [];
    */
             }
    mw.calculators.addDrugIndications( {
         } );
        generalAnesthesia: {
     };
            name: 'General anesthesia',
             abbreviation: 'GA'
         }
     } );






    /**
    * Drug data
    */




     /**
     /**
     * Class AbstractCalculator
     * Cefazolin
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.AbstractCalculator}
    * @constructor
     */
     */
     mw.calculators.objectClasses.AbstractCalculator = function( propertyValues ) {
     mw.calculators.addDrugs( {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
         cefazolin: {
     };
            name: 'Cefazolin'
        }
     } );


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


     mw.calculators.objectClasses.AbstractCalculator.prototype.getContainerClass = function() {
     /**
        return 'calculator-' + this.module + '-' + this.id;
    * Ketamine
    };
    */
 
     mw.calculators.addDrugs( {
     mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties = function() {
         ketamine: {
         return {
             name: 'Ketamine',
             required: [
             color: 'sedativeHypnotic'
                'id',
         }
                'module',
     } );
                'name',
                'calculations'
            ],
             optional: [
                'onRender',
                'onRendered'
            ]
         };
     };


     mw.calculators.objectClasses.AbstractCalculator.prototype.render = function() {
     mw.calculators.addDrugPreparations( 'ketamine', [
         if( typeof this.onRender === 'function' ) {
        {
             this.onRender();
            concentration: '10 mg/mL'
         }, {
            concentration: '50 mg/mL'
        }, {
             concentration: '100 mg/mL'
         }
         }
    ] );


        this.doRender();


         if( typeof this.onRendered === 'function' ) {
    /**
             this.onRendered();
    * Lidocaine
    */
    mw.calculators.addDrugs( {
         lidocaine: {
            name: 'Lidocaine',
             color: 'localAnesthetic'
         }
         }
     };
     } );
 
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.doRender = function() {};
 
 


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




     /**
     /**
     * Class SimpleCalculator
     * Propofol
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.SimpleCalculator}
    * @constructor
     */
     */
     mw.calculators.objectClasses.SimpleCalculator = function( propertyValues ) {
     mw.calculators.addDrugs( {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
         propofol: {
     };
            name: 'Propofol',
            color: 'sedativeHypnotic'
        }
     } );


     mw.calculators.objectClasses.SimpleCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype );
     mw.calculators.addDrugPreparations( 'propofol', [
        {
            concentration: '10 mg/mL'
        }
    ] );


 
     mw.calculators.addDrugDosages( 'propofol', [
     mw.calculators.objectClasses.SimpleCalculator.prototype.getProperties = function() {
        {
         var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();
            indication: 'generalAnesthesia',
 
            population: 'general',
         return this.mergeProperties( inheritedProperties, {
            dose: [
             required: [],
                {
             optional: [
                    name: 'Induction',
                 'css',
                    min: '1 mg/kg',
                'table'
                    max: '2.5 mg/kg',
                    weightCalculation: 'lbw'
                }, {
                    name: 'Maintenance',
                    min: '100 mcg/kg/min',
                    max: '200 mcg/kg/min',
                    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'
                }
            ]
         }, {
             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'
                }
            ]
        }, {
            indication: 'mac',
            population: 'general',
             dose: [
                 {
                    min: '25 mcg/kg/min',
                    max: '75 mcg/kg/min'
                }
             ]
             ]
        } );
    };
    mw.calculators.objectClasses.SimpleCalculator.prototype.doRender = function() {
        var $calculatorContainer = $( '.' + this.getContainerClass() );
        if( !$calculatorContainer.length ) {
            return;
         }
         }
    ] );


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


        $calculatorContainer.empty();


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


         var $calculationsContainer;
    /**
    * DrugDosage
    *
    * Structure is:
    *
    * drugId: {
    *    indicationId: {
    *         doseId: {
    *
    *        }
    *    }
    * }
    */


         if( this.table ) {
    var drugDosages = {
             $calculationsContainer = $( '<table>', {
         cefazolin: {
                class: 'wikitable'
             abxProphylaxis: {
            } ).append( '<tbody>' );
                general: {
        } else {
                    population: 'general',
            $calculationsContainer = $( '<div>' );
                    dose: '2 g'
        }
                },
 
                general120kg: {
        $calculatorContainer.append( $calculationsContainer );
                    population: {
 
                        id: 'general',
        for( var iCalculationId in this.calculations ) {
                        variables: {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
                            weight: {
            var calculationContainerClass = calculation.getContainerClass();
                                min: '120 kg'
 
                            }
            var $calculationContainer = $( '.' + calculationContainerClass );
                        }
 
                    },
            // If a container doesn't exist yet, add it
                    dose: '3 g'
            if( !$calculationContainer.length ) {
                },
                if( this.table ) {
                pediatric: {
                    $calculationContainer = $( '<tr>', {
                    dose: {
                        class: calculationContainerClass
                        absoluteMax: '2 g',
                    } );
                        dose: '30 mg/kg'
                } else {
                    }
                     $calculationContainer = $( '<div>', {
                },
                         class: calculationContainerClass
                pediatric120kg: {
                     } );
                    population: {
                        id: 'pediatric',
                        variables: {
                            weight: {
                                min: '120 kg'
                            }
                        }
                     },
                    dose: {
                         dose: '3 g'
                     }
                 }
                 }
                $calculationsContainer.append( $calculationContainer );
             }
             }
            calculation.render();
         }
         }
     };
     };
    mw.calculators.init();


}() );
}() );

Revision as of 13:47, 9 August 2021

/**
 * @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.addDrugDoseCalculations( this.drug, this.id, this.dose );
    };

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



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

        var doseProperties = [
            'dose',
            'min',
            'max',
            'absoluteMin',
            'absoluteMax'
        ];

        var calculationData = {};

        for( var doseId in drugDoseCalculationData ) {
            drugDoseCalculationData[ doseId ].drug = drugId;
            drugDoseCalculationData[ doseId ].calculate = mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate;

            var data = {
                calculations: {
                    required: [],
                    optional: []
                },
                variables: {
                    required: [],
                    optional: []
                }
            };

            // Look at dose properties to identify any variable dependence (e.g. weight-dependence)
            for( var iDoseProperty in doseProperties ) {
                var dosePropertyValue = drugDoseCalculationData[ doseId ][ doseProperties[ iDoseProperty ] ];

                // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
                if( dosePropertyValue &&
                    dosePropertyValue.match( /\/\s*?kg/ ) &&
                    data.variables.required.indexOf( 'weight' ) === -1 ) {
                    data.variables.required.push( 'weight' );
                }
            }

            if( drugDoseCalculationData[ doseId ].hasOwnProperty( 'weightCalculation' ) ) {
                var weightCalculationId = drugDoseCalculationData[ doseId ].weightCalculation;
                var weightCalculation = mw.calculators.getCalculation( weightCalculationId );

                if( !weightCalculation ) {
                    throw new Error( 'Drug "' + drugId + '" dose ' + drugDosageId + '-' + doseId + ': weightCalculation "' + weightCalculationId + '" which is not defined' );
                }

                data.calculations.optional.push( weightCalculationId );
                data.variables.optional = data.variables.optional.concat( weightCalculation.data.variables.required.concat( weightCalculation.data.variables.optional ) );
            }

            drugDoseCalculationData[ doseId ].data = data;

            calculationData[ drugId + '-' + drugDosageId + '-' + doseId ] = drugDoseCalculationData[ doseId ];
        }

        mw.calculators.addCalculations( calculationData, 'DrugDoseCalculation' );
    };


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

        this.initialize();
    };

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

    mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate = function() {

    };


    mw.calculators.objectClasses.DrugDoseCalculation.prototype.getLabelHtml = function() {
        var labelHtml = this.name;

        labelHtml = $( '<a>', {
            href: mw.util.getUrl( this.name ),
            text: labelHtml
        } )[ 0 ].outerHTML;

        return labelHtml;
    };

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

        return this.mergeProperties( inheritedProperties, {
            required: [
                'drug'
            ],
            optional: [
                'absoluteMin',
                'absoluteMax',
                'dose',
                'min',
                'max',
                'route',
                'weightCalculation'
            ]
        } );
    };

    mw.calculators.objectClasses.DrugDoseCalculation.prototype.initialize = function() {
        mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );

        this.route = this.route ? this.route : 'IV';
    };










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

}() );