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

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


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


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


     mw.calculators.addUnits( {
        return null;
         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 );
 
            for( var calculationId in calculations ) {
                var calculation = calculations[ calculationId ];
 
                mw.calculators.calculations[ calculationId ] = calculation;
 
                var inputCalculations = calculation.data.calculations.required.concat( calculation.data.calculations.optional );
 
                for( var iInputCalculation in inputCalculations ) {
                    var inputCalculationId = inputCalculations[ iInputCalculation ];
 
                    if( !mw.calculators.calculations.hasOwnProperty( inputCalculationId ) ) {
                        throw new Error('Calculation "' + inputCalculationId + '" does not exist for calculation "' + calculationId + '"');
                    }
 
                    mw.calculators.calculations[ inputCalculationId ].addCalculation( calculationId );
                }
 
                var inputVariables = calculation.data.variables.required.concat( calculation.data.variables.optional );
 
                for( var iInputVariable in inputVariables ) {
                    var inputVariableId = inputVariables[ iInputVariable ];
 
                    if( !mw.calculators.variables.hasOwnProperty( inputVariableId ) ) {
                        throw new Error('Variable "' + inputVariableId + '" does not exist for calculation "' + calculationId + '"');
                    }
 
                    mw.calculators.variables[ inputVariableId ].addCalculation( calculationId );
                }
            }
        },
         addCalculators: function( moduleId, calculatorData, className ) {
             className = className ? className : DEFAULT_CALCULATOR_CLASS;
 
            for( var calculatorId in calculatorData ) {
                calculatorData[ calculatorId ].module = moduleId;
            }
 
            var calculators = mw.calculators.createCalculatorObjects( className, calculatorData );
 
            if( !mw.calculators.calculators.hasOwnProperty( moduleId ) ) {
                mw.calculators.calculators[ moduleId ] = {};
            }
 
            for( var calculatorId in calculators ) {
                mw.calculators.calculators[ moduleId ][ calculatorId ] = calculators[ calculatorId ];
 
                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 ];
            }
        },
        addVariables: function( variableData ) {
            var variables = mw.calculators.createCalculatorObjects( 'Variable', variableData );


            for( var varId in variables ) {
                var variable = variables[ varId ];


    /**
                var cookieValue = mw.calculators.getCookieValue( varId );
    * DrugColor
    */
    mw.calculators.drugColors = {};


    mw.calculators.addDrugColors = function( drugColorData ) {
                if( cookieValue ) {
        var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );
                    variable.setValue( cookieValue );
                }


         for( var drugColorId in drugColors ) {
                mw.calculators.variables[ varId ] = variable;
             mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
            }
        }
        },
    };
         createCalculatorObjects: function( className, objectData ) {
             if( !mw.calculators.objectClasses.hasOwnProperty( className ) ) {
                throw new Error( 'Invalid class name "' + className + '"' );
            }


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


    /**
            for( var objectId in objectData ) {
    * Class DrugColor
                var propertyValues = objectData[ objectId ];
    * @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( typeof objectId === 'string' ) {
                    propertyValues.id = objectId;
                }


        if( !this.primaryColor && !this.parentColor ) {
                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
            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 );
            return objects;
        },
        createInputGroup: function( variableIds ) {
            var $form = $( '<form>', {


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


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


        if( !parentDrugColor ) {
            for( var iVariableId in variableIds ) {
            throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
                var variableId = variableIds[ iVariableId ];
        }


        return parentDrugColor;
                if( !mw.calculators.variables.hasOwnProperty( variableId ) ) {
    };
                    throw new Error( 'Invalid variable name "' + variableId + '"' );
                }


    mw.calculators.objectClasses.DrugColor.getHighlightColor = function() {
                $formRow.append( mw.calculators.variables[ variableId ].createInput() );
        if( this.highlightColor ) {
             }
            return this.highlightColor;
        } else if( this.parentColor ) {
             return this.getParentDrugColor().getHighlightColor();
        }
    };


    mw.calculators.objectClasses.DrugColor.getPrimaryColor = function() {
            return $form.append( $formRow );
         if( this.primaryColor ) {
        },
             return this.primaryColor;
         getCookieKey: function( variableId ) {
         } else if( this.parentColor ) {
             return 'calculators-var-' + variableId;
             return this.getParentDrugColor().getPrimaryColor();
         },
        }
        getCookieValue: function( varId ) {
    };
             var cookieValue = mw.cookie.get( mw.calculators.getCookieKey( varId ) );


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


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


    mw.calculators.drugPopulations = {};
                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;
            }


    mw.calculators.addDrugPopulations = function( drugPopulationData ) {
            if( mw.calculators.variables[ variableId ].setValue( value ) ) {
        var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );
                mw.cookie.set( mw.calculators.getCookieKey( variableId ), value, {
                    expires: COOKIE_EXPIRATION
                } );


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


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


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


        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
                    this[ requiredProperty ] = propertyValues[ requiredProperty ];


        if( this.variables ) {
                     delete propertyValues[ requiredProperty ];
            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 );
            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() {
        return {
            required: [],
            optional: []
        };
    };


    /**
     mw.calculators.objectClasses.CalculatorObject.prototype.mergeProperties = function( inheritedProperties, properties ) {
    * DrugIndication
        var uniqueValues = function( value, index, self ) {
    */
            return self.indexOf( value ) === index;
     mw.calculators.drugIndications = {};
        };


    mw.calculators.addDrugIndications = function( drugIndicationData ) {
        properties.required = inheritedProperties.required.concat( properties.required ).filter( uniqueValues );
         var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );
         properties.optional = inheritedProperties.optional.concat( properties.optional ).filter( uniqueValues );


         for( var drugIndicationId in drugIndications ) {
         return properties;
            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
     * Class UnitsBase
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugIndication}
     * @returns {mw.calculators.objectClasses.UnitsBase}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
     mw.calculators.objectClasses.UnitsBase = function( propertyValues ) {
         var properties = {
         var properties = {
             required: [
             required: [
                 'id',
                 'id'
                'name'
             ],
             ],
             optional: [
             optional: [
                 'abbreviation'
                 'toString'
             ]
             ]
         };
         };
Line 221: Line 344:
     };
     };


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




Line 228: Line 350:


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


    mw.calculators.addDrugs = function( drugData ) {
        mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
        var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );
    };


        for( var drugId in drugs ) {
    mw.calculators.objectClasses.Units.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
            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
     * Class Variable
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.Drug}
     * @returns {mw.calculators.objectClasses.Variable}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.Drug = function( propertyValues ) {
     mw.calculators.objectClasses.Variable = function( propertyValues ) {
         var properties = {
         var properties = {
             required: [
             required: [
                 'id',
                 'id',
                 'name'
                 'name',
                'type'
             ],
             ],
             optional: [
             optional: [
                 'color'
                 'abbreviation',
                'defaultValue',
                'maxLength',
                'options',
                'units'
             ]
             ]
         };
         };
Line 269: Line 401:
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );


         if( !this.color ) {
         if( VALID_TYPES.indexOf( this.type ) === -1 ) {
             this.color = DEFAULT_DRUG_COLOR;
            throw new Error( 'Invalid type "' + this.type + '" for variable "' + this.id + '"' );
        }
 
        this.calculations = [];
 
        if( this.defaultValue ) {
            this.setValue( this.defaultValue );
        } else {
             this.value = null;
         }
         }
    };


         this.dosages = {};
    mw.calculators.objectClasses.Variable.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
         this.preparations = {};
 
    mw.calculators.objectClasses.Variable.prototype.addCalculation = function( calculationId ) {
         if( this.calculations.indexOf( calculationId ) !== -1 ) {
            return;
        }
 
         this.calculations.push( calculationId );
     };
     };


     mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     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;


        // Create the input container
        var $inputContainer = $( '<div>', inputContainerAttribs );


        // Set the input id
        var inputId = 'calculator-input-' + variableId;


    /**
        // Initialize label attributes
    * DrugPreparation
        var labelAttributes = {
    */
            for: inputId,
    mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
            text: this.getLabelString()
        if( !mw.calculators.getDrug( drugId ) ) {
        };
             throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
 
        // 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 );
            }
         }
         }


         for( var drugPreparationId in drugPreparationData ) {
         return $inputContainer;
             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;
         }
         }


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


         for( var drugPreparationId in drugPreparations ) {
    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
             mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
         return this.value && this.value.hasOwnProperty( 'value' );
    };
 
    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 305: Line 640:


     /**
     /**
     * Class DrugPreparation
     * Class AbstractCalculation
     * @param {Object} propertyValues
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.DrugPreparation}
     * @returns {mw.calculators.objectClasses.AbstractCalculation}
     * @constructor
     * @constructor
     */
     */
     mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
     mw.calculators.objectClasses.AbstractCalculation = function( propertyValues ) {
         var properties = {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
 
        this.initialize();
    };
 
    mw.calculators.objectClasses.AbstractCalculation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
 
    mw.calculators.objectClasses.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',
                 'concentration'
                 'calculate'
             ],
             ],
             optional: [
             optional: [
                 'dilutionRequired',
                 'data',
                 'commonDilution'
                 'description',
                'onRender',
                'onRendered',
                'references',
                'type'
             ]
             ]
         };
         };
    };


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


     mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     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 + '"' );
            } else if( !calculation.hasValue() ) {
                if( missingRequiredData ) {
                    missingRequiredData = missingRequiredData + ', ';
                }


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


        for( var iRequiredVariable in this.data.variables.required ) {
            variableId = this.data.variables.required[ iRequiredVariable ];
            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 ) {
            this.message = missingRequiredData + ' required';


    /**
             return false;
    * 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 ) {
         for( var iOptionalVariable in this.data.variables.optional ) {
             drugDosageData[ drugDosageId ].drug = drugId;
             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;
         }
         }


         var drugDosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
         for( var iOptionalCalculation in this.data.calculations.optional ) {
            calculationId = this.data.calculations.optional[ calculationId ];
            calculation = mw.calculators.getVariable( calculationId );
 
            if( !calculation ) {
                throw new Error( 'Invalid optional variable "' + calculationId + '" for calculation "' + this.id + '"' );
            }


        for( var drugDosageId in drugDosages ) {
             data[ calculationId ] = calculation.hasValue() ? calculation.value : null;
             mw.calculators.drugs[ drugId ].dosages[ drugDosageId ] = drugDosages[ drugDosageId ];
         }
         }
        return data;
     };
     };


    mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
        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 = {
    * Class DrugDosage
            optional: [],
    * @param {Object} propertyValues
            required: []
    * @returns {mw.calculators.objectClasses.DrugDosage}
        };
    * @constructor
 
    */
        this.data = this.data ? this.data : {
    mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
             calculations: dataPrototype,
        var properties = {
             variables: dataPrototype
             required: [
                'dose',
                'drug',
                'id',
                'indication'
            ],
             optional: [
                'population'
            ]
         };
         };


         mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
         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.population ) {
         if( !this.data.hasOwnProperty( 'variables' ) ) {
             this.population = DEFAULT_DRUG_POPULATION;
             this.data.variables = dataPrototype;
        } else {
            this.data.variables.optional = this.data.variables.hasOwnProperty( 'optional' ) ? this.data.variables.optional : [];
            this.data.variables.required = this.data.variables.hasOwnProperty( 'required' ) ? this.data.variables.required : [];
         }
         }


         mw.calculators.addDrugDoseCalculations( this.drug, this.id, this.dose );
         this.message = null;
        this.value = null;
     };
     };


     mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
     mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
        return this.value && this.value.hasOwnProperty( 'value' );
    };


    mw.calculators.objectClasses.AbstractCalculation.prototype.recalculate = function() {
        this.message = '';
        this.value = null;


        var data = this.getData();


    /**
         if( data === false ) {
    * DrugDosageCalculation
             return false;
    */
    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 = [
         try {
            'dose',
            var value = this.calculate( data );
            'min',
            'max',
            'absoluteMin',
            'absoluteMax'
        ];


        var calculationData = {};
            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                if( this.units ) {
                    value = value + ' ' + this.units;
                }


        for( var doseId in drugDoseCalculationData ) {
                this.value = math.unit( value );
            drugDoseCalculationData[ doseId ].drug = drugId;
             } else {
            drugDoseCalculationData[ doseId ].calculate = mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate;
                 this.value = value;
 
             }
             var data = {
                calculations: {
                    required: [],
                    optional: []
                },
                variables: {
                    required: [],
                    optional: []
                 }
             };


            // Look at dose properties to identify any variable dependence (e.g. weight-dependence)
             for( var iCalculation in this.calculations ) {
             for( var iDoseProperty in doseProperties ) {
                 calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );
                 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( calculation ) {
                if( dosePropertyValue &&
                     calculation.render();
                    dosePropertyValue.match( /\/\s*?kg/ ) &&
                    data.variables.required.indexOf( 'weight' ) === -1 ) {
                     data.variables.required.push( 'weight' );
                 }
                 }
             }
             }
        } catch( e ) {
            this.message = e.message;
            this.value = null;
        }


            if( drugDoseCalculationData[ doseId ].hasOwnProperty( 'weightCalculation' ) ) {
        return true;
                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;
    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
        this.recalculate();


            calculationData[ drugId + '-' + drugDosageId + '-' + doseId ] = drugDoseCalculationData[ doseId ];
        if( typeof this.onRender === 'function' ) {
            this.onRender();
         }
         }


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




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


Line 463: Line 890:
     };
     };


     mw.calculators.objectClasses.DrugDoseCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
     mw.calculators.objectClasses.SimpleCalculation.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();


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


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


         return labelHtml;
         return labelHtml;
     };
     };


     mw.calculators.objectClasses.DrugDoseCalculation.prototype.getProperties = function() {
     mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelString = 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: [
                 'drug'
                 'name'
             ],
             ],
             optional: [
             optional: [
                 'absoluteMin',
                 'abbreviation',
                 'absoluteMax',
                 'digits',
                 'dose',
                 'formula',
                 'min',
                 'link',
                 'max',
                 'units'
                'route',
                'weightCalculation'
             ]
             ]
         } );
         } );
     };
     };


     mw.calculators.objectClasses.DrugDoseCalculation.prototype.initialize = function() {
     mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
         mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
         if( this.message ) {
            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;
 
            var valueString = String( number.toFixed( digits ) );
 
            if( units ) {
                valueString = valueString + ' ' + mw.calculators.getUnitsString( value );
            }


         this.route = this.route ? this.route : 'IV';
            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;


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


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


            var $infoButton = null;


            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();
    * BEGIN DRUG DATA
    *******/


            if( isTable ) {
                if( calculation.hasInfo() ) {
                    labelHtml += $( '<span>', {
                        class: 'calculator-calculation-column-label-info'
                    } ).append( $infoButton )[ 0 ].outerHTML;
                }


                $( 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() ) {
    * DrugColor data
                var infoHtml = '';
    */
 
    mw.calculators.addDrugColors( {
                if( calculation.description ) {
        anticholinergic: {
                    infoHtml += $( '<p>', {
            primaryColor: '#00ac8c'
                        html: calculation.description
        },
                    } )[ 0 ].outerHTML;
        benzodiazepine: {
                }
            primaryColor: '#ff6c2f'
 
        },
                if( calculation.formula ) {
        benzodiazepineReversal: {
                    infoHtml += $( '<span>', {
            parentColor: 'benzodiazepine',
                        class: calculation.getContainerClass() + '-formula'
            striped: true
                    } )[ 0 ].outerHTML;
        },
 
        cardiovascularAgonist: {
                    var api = new mw.Api();
            primaryColor: '#ba93df'
 
        },
                    api.parse( calculation.formula ).then( function( result ) {
        cardiovascularAntagonist: {
                        $( '.' + calculation.getContainerClass() + '-formula' ).html( result );
            parentColor: 'cardiovascularAgonist',
                    } );
            striped: true
                }
        },
 
        default: {
                if( calculation.references.length ) {
            primaryColor: '#fff'
                    var $references = $( '<ol>' );
        },
 
        desflurane: {
                    for( var iReference in calculation.references ) {
            primaryColor: '#0ab8fd'
                        $references.append( $( '<li>', {
        },
                            text: calculation.references[ iReference ]
        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'
        }
    } );


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


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


    /**
                if( $infoContainer.length ) {
    * DrugPopulation data
                    $infoContainer.empty();
    */
    mw.calculators.addDrugPopulations( {
        general: {
            name: 'General',
            abbreviation: 'Gen.'
        },
        neonatal: {
            name: 'Neonatal',
            abbreviation: 'Neo.',
            variables: {
                age: {
                    max: '0 yo'
                 }
                 }
            }
 
        },
                if( isTable ) {
        pediatric: {
                    $infoContainer = $( '<tr>', {
            name: 'Pediatric',
                        id: infoContainerId,
            abbreviation: 'Ped.',
                        class: 'collapse'
            variables: {
                    } )
                 age: {
                        .append( $( '<td>', {
                     min: '0 yo',
                            colspan: 2
                    max: '17.9 yo'
                        } ).append( infoHtml ) );
                 } else {
                     $infoContainer = $( '<div>', {
                        id: infoContainerId,
                        class: 'collapse'
                    } ).append( infoHtml );
                 }
                 }
                $( this ).after( $infoContainer );
             }
             }
        },
 
        elderly: {
            if( missingVariableInputs.length ) {
            name: 'Elderly',
                var variablesContainerClass = 'calculator-calculation-variables ' + calculation.getContainerClass() + '-variables';
            abbreviation: 'Eld.',
                var inputGroup = mw.calculators.createInputGroup( missingVariableInputs );
            variables: {
 
                 age: {
                if( isTable ) {
                     min: '65 yo'
                    $variablesContainer =  $( '<tr>' )
                        .append( $( '<td>', {
                            class: variablesContainerClass,
                            colspan: 2
                        } ).append( inputGroup ) );
                 } else {
                     $variablesContainer = $( '<div>', {
                        class: variablesContainerClass
                    } ).append( inputGroup );
                 }
                 }
                $( this ).after( $variablesContainer );
                missingVariableInputs = [];
             }
             }
         }
         } );
     } );
     };
 
 






     /**
     /**
     * DrugIndication data
     * Class AbstractCalculator
    * @param {Object} propertyValues
    * @returns {mw.calculators.objectClasses.AbstractCalculator}
    * @constructor
     */
     */
     mw.calculators.addDrugIndications( {
     mw.calculators.objectClasses.AbstractCalculator = function( propertyValues ) {
         generalAnesthesia: {
         mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
             name: 'General anesthesia',
    };
             abbreviation: 'GA'
 
    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;
    };
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties = function() {
        return {
             required: [
                'id',
                'module',
                'name',
                'calculations'
            ],
            optional: [
                'onRender',
                'onRendered'
             ]
        };
    };
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.render = function() {
        if( typeof this.onRender === 'function' ) {
            this.onRender();
        }
 
        this.doRender();
 
        if( typeof this.onRendered === 'function' ) {
            this.onRendered();
         }
         }
     } );
     };
 
 
    mw.calculators.objectClasses.AbstractCalculator.prototype.doRender = function() {};






    /**
    * Drug data
    */




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




    /**
     mw.calculators.objectClasses.SimpleCalculator.prototype.getProperties = function() {
    * Ketamine
         var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();
    */
     mw.calculators.addDrugs( {
         ketamine: {
            name: 'Ketamine',
            color: 'sedativeHypnotic'
        }
    } );


    mw.calculators.addDrugPreparations( 'ketamine', [
        return this.mergeProperties( inheritedProperties, {
        {
             required: [],
             concentration: '10 mg/mL'
             optional: [
        }, {
                'css',
             concentration: '50 mg/mL'
                'table'
        }, {
            ]
            concentration: '100 mg/mL'
         } );
         }
     };
     ] );


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


    /**
        if( !$calculatorContainer.length ) {
    * Lidocaine
             return;
    */
    mw.calculators.addDrugs( {
        lidocaine: {
             name: 'Lidocaine',
            color: 'localAnesthetic'
         }
         }
    } );


    mw.calculators.addDrugPreparations( 'lidocaine', [
        if( this.css ) {
        {
             $calculatorContainer.css( this.css );
             concentration: '1 pct'
        }, {
            concentration: '2 pct'
         }
         }
    ] );


        $calculatorContainer.empty();


    /**
        $calculatorContainer.append( $( '<h4>', {
    * Propofol
             text: this.name
    */
         } ) );
    mw.calculators.addDrugs( {
        propofol: {
            name: 'Propofol',
             color: 'sedativeHypnotic'
         }
    } );


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


    mw.calculators.addDrugDosages( 'propofol', [
        if( this.table ) {
        {
             $calculationsContainer = $( '<table>', {
            indication: 'generalAnesthesia',
                 class: 'wikitable'
            population: 'general',
             } ).append( '<tbody>' );
            dose: [
         } else {
                {
             $calculationsContainer = $( '<div>' );
                    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'
                }
            ]
         }
         }
    ] );


        $calculatorContainer.append( $calculationsContainer );


        for( var iCalculationId in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
            var calculationContainerClass = calculation.getContainerClass();


            var $calculationContainer = $( '.' + calculationContainerClass );


    /**
            // If a container doesn't exist yet, add it
    * DrugDosage
            if( !$calculationContainer.length ) {
    *
                if( this.table ) {
    * Structure is:
                    $calculationContainer = $( '<tr>', {
    *
                        class: calculationContainerClass
    * drugId: {
                    } );
    *    indicationId: {
                } else {
    *        doseId: {
                    $calculationContainer = $( '<div>', {
    *
                        class: calculationContainerClass
    *        }
                    } );
    *    }
                }
    * }
    */


    var drugDosages = {
                 $calculationsContainer.append( $calculationContainer );
        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'
                    }
                }
             }
             }
            calculation.render();
         }
         }
     };
     };
    mw.calculators.init();


}() );
}() );

Revision as of 13:47, 9 August 2021

/**
 * @author Chris Rishel
 */
( function() {
    var COOKIE_EXPIRATION = 12 * 60 * 60;

    var TYPE_NUMBER = 'number';
    var TYPE_STRING = 'string';

    var VALID_TYPES = [
        TYPE_NUMBER,
        TYPE_STRING
    ];

    var DEFAULT_CALCULATION_CLASS = 'SimpleCalculation';
    var DEFAULT_CALCULATOR_CLASS = 'SimpleCalculator';

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

        return null;
    };


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

            for( var calculationId in calculations ) {
                var calculation = calculations[ calculationId ];

                mw.calculators.calculations[ calculationId ] = calculation;

                var inputCalculations = calculation.data.calculations.required.concat( calculation.data.calculations.optional );

                for( var iInputCalculation in inputCalculations ) {
                    var inputCalculationId = inputCalculations[ iInputCalculation ];

                    if( !mw.calculators.calculations.hasOwnProperty( inputCalculationId ) ) {
                        throw new Error('Calculation "' + inputCalculationId + '" does not exist for calculation "' + calculationId + '"');
                    }

                    mw.calculators.calculations[ inputCalculationId ].addCalculation( calculationId );
                }

                var inputVariables = calculation.data.variables.required.concat( calculation.data.variables.optional );

                for( var iInputVariable in inputVariables ) {
                    var inputVariableId = inputVariables[ iInputVariable ];

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

                    mw.calculators.variables[ inputVariableId ].addCalculation( calculationId );
                }
            }
        },
        addCalculators: function( moduleId, calculatorData, className ) {
            className = className ? className : DEFAULT_CALCULATOR_CLASS;

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

            var calculators = mw.calculators.createCalculatorObjects( className, calculatorData );

            if( !mw.calculators.calculators.hasOwnProperty( moduleId ) ) {
                mw.calculators.calculators[ moduleId ] = {};
            }

            for( var calculatorId in calculators ) {
                mw.calculators.calculators[ moduleId ][ calculatorId ] = calculators[ calculatorId ];

                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 ];
            }
        },
        addVariables: function( variableData ) {
            var variables = mw.calculators.createCalculatorObjects( 'Variable', variableData );

            for( var varId in variables ) {
                var variable = variables[ varId ];

                var cookieValue = mw.calculators.getCookieValue( varId );

                if( cookieValue ) {
                    variable.setValue( cookieValue );
                }

                mw.calculators.variables[ varId ] = variable;
            }
        },
        createCalculatorObjects: function( className, objectData ) {
            if( !mw.calculators.objectClasses.hasOwnProperty( className ) ) {
                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;
                }

                objects[ objectId ] = new mw.calculators.objectClasses[ className ]( propertyValues );
            }

            return objects;
        },
        createInputGroup: function( variableIds ) {
            var $form = $( '<form>', {

            } );

            var $formRow = $( '<div>', {
                class: 'form-row'
            } ).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;
        },
        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
     *
     * @param {Object} properties
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.CalculatorObject}
     * @constructor
     */
    mw.calculators.objectClasses.CalculatorObject = function( properties, propertyValues ) {
        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 ];

                    delete propertyValues[ requiredProperty ];
                }
            }

            if( properties.hasOwnProperty( 'optional' ) ) {
                for( var iOptionalProperty in properties.optional ) {
                    var optionalProperty = properties.optional[ iOptionalProperty ];

                    if( propertyValues && propertyValues.hasOwnProperty( optionalProperty ) ) {
                        this[ optionalProperty ] = propertyValues[ optionalProperty ];

                        delete propertyValues[ optionalProperty ];
                    } else if( typeof this[ optionalProperty ] === 'undefined' ) {
                        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() {
        return {
            required: [],
            optional: []
        };
    };

    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
     * @param {Object} propertyValues
     * @returns {mw.calculators.objectClasses.UnitsBase}
     * @constructor
     */
    mw.calculators.objectClasses.UnitsBase = function( propertyValues ) {
        var properties = {
            required: [
                'id'
            ],
            optional: [
                'toString'
            ]
        };

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

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




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

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

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




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

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

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

        this.calculations = [];

        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.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;

        // Create the input container
        var $inputContainer = $( '<div>', inputContainerAttribs );

        // Set the input id
        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;
    };

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

    mw.calculators.objectClasses.Variable.prototype.isValueMathObject = function() {
        return this.value && this.value.hasOwnProperty( 'value' );
    };

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



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

        this.initialize();
    };

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

    mw.calculators.objectClasses.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: [
                'id',
                'calculate'
            ],
            optional: [
                'data',
                'description',
                'onRender',
                'onRendered',
                'references',
                'type'
            ]
        };
    };

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

        return this.value;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.hasInfo = function() {};

    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 + '"' );
            } else if( !calculation.hasValue() ) {
                if( missingRequiredData ) {
                    missingRequiredData = missingRequiredData + ', ';
                }

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

        for( var iRequiredVariable in this.data.variables.required ) {
            variableId = this.data.variables.required[ iRequiredVariable ];
            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 ) {
            this.message = missingRequiredData + ' required';

            return false;
        }

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

        for( var iOptionalCalculation in this.data.calculations.optional ) {
            calculationId = this.data.calculations.optional[ calculationId ];
            calculation = mw.calculators.getVariable( calculationId );

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

            data[ calculationId ] = calculation.hasValue() ? calculation.value : null;
        }

        return data;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.initialize = function() {
        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: [],
            required: []
        };

        this.data = this.data ? this.data : {
            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' ) ) {
            this.data.variables = dataPrototype;
        } else {
            this.data.variables.optional = this.data.variables.hasOwnProperty( 'optional' ) ? this.data.variables.optional : [];
            this.data.variables.required = this.data.variables.hasOwnProperty( 'required' ) ? this.data.variables.required : [];
        }

        this.message = null;
        this.value = null;
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.isValueMathObject = function() {
        return this.value && this.value.hasOwnProperty( 'value' );
    };

    mw.calculators.objectClasses.AbstractCalculation.prototype.recalculate = function() {
        this.message = '';
        this.value = null;

        var data = this.getData();

        if( data === false ) {
            return false;
        }

        try {
            var value = this.calculate( data );

            if( this.type === TYPE_NUMBER && !isNaN( value ) ) {
                if( this.units ) {
                    value = value + ' ' + this.units;
                }

                this.value = math.unit( value );
            } else {
                this.value = value;
            }

            for( var iCalculation in this.calculations ) {
                calculation = mw.calculators.getCalculation( this.calculations[ iCalculation ] );

                if( calculation ) {
                    calculation.render();
                }
            }
        } catch( e ) {
            this.message = e.message;
            this.value = null;
        }

        return true;
    };



    mw.calculators.objectClasses.AbstractCalculation.prototype.render = function() {
        this.recalculate();

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

        this.doRender();

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



    mw.calculators.objectClasses.AbstractCalculation.prototype.doRender = function() {};




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

        this.initialize();
    };

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


    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 ) {
            var href = this.link;

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

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

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

        return labelHtml;
    };

    mw.calculators.objectClasses.SimpleCalculation.prototype.getLabelString = 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();

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

    mw.calculators.objectClasses.SimpleCalculation.prototype.getValueString = function() {
        if( this.message ) {
            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;

            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;

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

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

            var $infoButton = null;

            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() ) {
                    labelHtml += $( '<span>', {
                        class: 'calculator-calculation-column-label-info'
                    } ).append( $infoButton )[ 0 ].outerHTML;
                }

                $( 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() ) {
                var infoHtml = '';

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

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

                    var api = new mw.Api();

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

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

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

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

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

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

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

                missingVariableInputs = [];
            }
        } );
    };





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

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

    mw.calculators.objectClasses.AbstractCalculator.prototype.getContainerClass = function() {
        return 'calculator-' + this.module + '-' + this.id;
    };

    mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties = function() {
        return {
            required: [
                'id',
                'module',
                'name',
                'calculations'
            ],
            optional: [
                'onRender',
                'onRendered'
            ]
        };
    };

    mw.calculators.objectClasses.AbstractCalculator.prototype.render = function() {
        if( typeof this.onRender === 'function' ) {
            this.onRender();
        }

        this.doRender();

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


    mw.calculators.objectClasses.AbstractCalculator.prototype.doRender = function() {};





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

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


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

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

    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;

        if( this.table ) {
            $calculationsContainer = $( '<table>', {
                class: 'wikitable'
            } ).append( '<tbody>' );
        } else {
            $calculationsContainer = $( '<div>' );
        }

        $calculatorContainer.append( $calculationsContainer );

        for( var iCalculationId in this.calculations ) {
            var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
            var calculationContainerClass = calculation.getContainerClass();

            var $calculationContainer = $( '.' + calculationContainerClass );

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

                $calculationsContainer.append( $calculationContainer );
            }

            calculation.render();
        }
    };

    mw.calculators.init();

}() );