Difference between revisions of "MediaWiki:Gadget-calculator-drugs-core.js"
From WikiAnesthesia
Chris Rishel (talk | contribs) |
Chris Rishel (talk | contribs) |
||
| Line 3: | Line 3: | ||
*/ | */ | ||
( function() { | ( function() { | ||
var | var DEFAULT_DRUG_COLOR = 'default'; | ||
var DEFAULT_DRUG_POPULATION = 'general'; | |||
mw.calculators.isValueDependent = function( value, variableId ) { | |||
// This may need generalized to support other variables in the future | |||
if( variableId === 'weight' ) { | |||
return value && value.formatUnits().match( /\/[\s(]*?kg/ ); | |||
} else { | |||
throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' ); | |||
} | |||
}; | |||
/** | |||
* Define units | |||
*/ | |||
mw.calculators.addUnitsBases( { | |||
concentration: { | |||
toString: function( units ) { | |||
units = units.replace( ' pct', '%' ); | |||
return units; | |||
return | |||
} | } | ||
} | } | ||
} ); | |||
mw.calculators.addUnits( { | |||
pct: { | |||
baseName: 'concentration', | |||
definition: '10 mg/mL' | |||
mw.calculators | |||
}, | }, | ||
vial: { | |||
baseName: 'VOLUME' | |||
} | |||
} ); | |||
/** | |||
* DrugColor | |||
*/ | |||
mw.calculators.drugColors = {}; | |||
mw.calculators.addDrugColors = function( drugColorData ) { | |||
var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData ); | |||
for( var drugColorId in drugColors ) { | |||
mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ]; | |||
} | |||
}; | |||
mw.calculators.getDrugColor = function( drugColorId ) { | |||
if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) { | |||
return mw.calculators.drugColors[ drugColorId ]; | |||
} | } else { | ||
return null; | |||
} | |||
}; | |||
/** | |||
* Class DrugColor | |||
* @param {Object} propertyValues | |||
} | * @returns {mw.calculators.objectClasses.DrugColor} | ||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugColor = function( propertyValues ) { | |||
var properties = { | |||
required: [ | |||
'id' | |||
], | |||
optional: [ | |||
'parentColor', | |||
'primaryColor', | |||
'highlightColor', | |||
'striped' | |||
] | |||
}; | |||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
this.parentColor = this.parentColor || this.id === DEFAULT_DRUG_COLOR ? this.parentColor : DEFAULT_DRUG_COLOR; | |||
}; | |||
mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | |||
mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() { | |||
if( !this.parentColor ) { | |||
return null; | |||
} | |||
var parentDrugColor = mw.calculators.getDrugColor( this.parentColor ); | |||
if( !parentDrugColor ) { | |||
throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' ); | |||
} | |||
return parentDrugColor; | |||
}; | |||
mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() { | |||
if( this.highlightColor ) { | |||
return this.highlightColor; | |||
} else if( this.parentColor ) { | |||
return this.getParentDrugColor().getHighlightColor(); | |||
} | |||
}; | |||
mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() { | |||
} | if( this.primaryColor ) { | ||
return this.primaryColor; | |||
} else if( this.parentColor ) { | |||
return this.getParentDrugColor().getPrimaryColor(); | |||
} | |||
}; | |||
return | mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() { | ||
} | if( this.striped !== null ) { | ||
return this.striped; | |||
} else if( this.parentColor ) { | |||
return this.getParentDrugColor().isStriped(); | |||
} | |||
}; | |||
/** | |||
* DrugPopulation | |||
*/ | |||
mw.calculators.drugPopulations = {}; | |||
mw.calculators.addDrugPopulations = function( drugPopulationData ) { | |||
var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData ); | |||
for( var drugPopulationId in drugPopulations ) { | |||
mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ]; | |||
} | |||
}; | |||
mw.calculators.getDrugPopulation = function( drugPopulationId ) { | |||
if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) { | |||
return mw.calculators.drugPopulations[ drugPopulationId ]; | |||
} else { | |||
return null; | |||
} | |||
}; | |||
/** | |||
} | * Class DrugPopulation | ||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugPopulation} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) { | |||
var properties = { | |||
required: [ | |||
'id', | |||
'name' | |||
], | |||
optional: [ | |||
'abbreviation', | |||
'variables' | |||
] | |||
}; | |||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
if( this.variables ) { | |||
if( mw.calculators. | for( var variableId in this.variables ) { | ||
if( !mw.calculators.getVariable( variableId ) ) { | |||
throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' ); | |||
} | } | ||
this.variables[ variableId ].min = this.variables[ variableId ].hasOwnProperty( 'min' ) ? | |||
math.unit( this.variables[ variableId ].min ) : null; | |||
this.variables[ variableId ].max = this.variables[ variableId ].hasOwnProperty( 'max' ) ? | |||
math.unit( this.variables[ variableId ].max ) : null; | |||
} | } | ||
} else { | |||
this.variables = {}; | |||
} | |||
}; | |||
mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | |||
mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() { | |||
var inputData = new mw.calculators.objectClasses.CalculationData(); | |||
for( var variableId in this.variables ) { | |||
inputData.variables.required.push( variableId ); | |||
} | |||
return inputData; | |||
}; | |||
mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationDataScore = function( dataValues ) { | |||
// A return value of -1 indicates the data did not match the population definition | |||
for( var variableId in this.variables ) { | |||
if( !dataValues.hasOwnProperty( variableId ) ) { | |||
return -1; | |||
if( | |||
return | |||
} | } | ||
if( this.variables[ variableId ].min && | |||
!math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) { | |||
return -1; | |||
return | |||
} | } | ||
if( | if( this.variables[ variableId ].max && | ||
!math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) { | |||
return -1; | |||
return | |||
} | } | ||
} | |||
// If the data matches the population definition, the score corresponds to the number of variables in the | |||
// population definition. This should roughly correspond to the specificity of the population. | |||
return Object.keys( this.variables ).length; | |||
}; | }; | ||
mw.calculators.objectClasses.DrugPopulation.prototype.toString = function() { | |||
return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name; | |||
} | |||
/** | |||
* DrugIndication | |||
*/ | |||
mw.calculators.drugIndications = {}; | |||
mw.calculators.addDrugIndications = function( drugIndicationData ) { | |||
var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData ); | |||
for( var drugIndicationId in drugIndications ) { | |||
mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ]; | |||
} | } | ||
}; | }; | ||
mw.calculators. | mw.calculators.getDrugIndication = function( drugIndicationId ) { | ||
if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) { | |||
return mw.calculators.drugIndications[ drugIndicationId ]; | |||
} else { | |||
} | return null; | ||
} | |||
}; | }; | ||
/** | /** | ||
* Class | * Class DrugIndication | ||
* @param {Object} propertyValues | * @param {Object} propertyValues | ||
* @returns {mw.calculators.objectClasses. | * @returns {mw.calculators.objectClasses.DrugIndication} | ||
* @constructor | * @constructor | ||
*/ | */ | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugIndication = function( propertyValues ) { | ||
var properties = { | var properties = { | ||
required: [ | required: [ | ||
'id' | 'id', | ||
'name' | |||
], | ], | ||
optional: [ | optional: [ | ||
' | 'abbreviation' | ||
] | ] | ||
}; | }; | ||
| Line 406: | Line 277: | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
mw.calculators.objectClasses.DrugIndication.prototype.toString = function() { | |||
return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name; | |||
}; | |||
| Line 412: | Line 288: | ||
/** | /** | ||
* | * Drug | ||
*/ | */ | ||
mw.calculators. | mw.calculators.drugs = {}; | ||
var | |||
mw.calculators.addDrugs = function( drugData ) { | |||
var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData ); | |||
mw.calculators. | for( var drugId in drugs ) { | ||
mw.calculators.drugs[ drugId ] = drugs[ drugId ]; | |||
} | |||
}; | }; | ||
mw.calculators. | mw.calculators.addDrugDosages = function( drugId, drugDosageData ) { | ||
var drug = mw.calculators.getDrug( drugId ); | |||
if( !drug ) { | |||
throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' ); | |||
} | |||
drug.addDosages( drugDosageData ); | |||
var calculationId = 'drugDosage-' + drugId; | |||
var calculation = mw.calculators.getCalculation( calculationId ); | |||
if( !calculation ) { | |||
var calculationData = {}; | |||
calculationData[ calculationId ] = { | |||
calculate: mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate, | |||
drug: drugId, | |||
type: 'drug' | |||
}; | |||
mw.calculators.addCalculations( calculationData, 'DrugDosageCalculation' ); | |||
calculation = mw.calculators.getCalculation( calculationId ); | |||
} | |||
calculation.setDependencies(); | |||
}; | |||
mw.calculators.getDrug = function( drugId ) { | |||
if( mw.calculators.drugs.hasOwnProperty( drugId ) ) { | |||
return mw.calculators.drugs[ drugId ]; | |||
} else { | |||
return null; | |||
} | |||
}; | |||
/** | /** | ||
* Class | * Class Drug | ||
* @param {Object} propertyValues | * @param {Object} propertyValues | ||
* @returns {mw.calculators.objectClasses. | * @returns {mw.calculators.objectClasses.Drug} | ||
* @constructor | * @constructor | ||
*/ | */ | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.Drug = function( propertyValues ) { | ||
var properties = { | var properties = { | ||
required: [ | required: [ | ||
'id', | 'id', | ||
'name | 'name' | ||
], | ], | ||
optional: [ | optional: [ | ||
' | 'color' | ||
] | ] | ||
}; | }; | ||
| Line 463: | Line 358: | ||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | ||
if( | if( !this.color ) { | ||
this.color = DEFAULT_DRUG_COLOR; | |||
} | } | ||
var color = mw.calculators.getDrugColor( this.color ); | |||
if( !color ) { | |||
throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' ); | |||
this. | |||
} | } | ||
this. | this.color = color; | ||
this.dosages = []; | |||
this.preparations = []; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.Drug.prototype.addDosages = function( drugDosageData ) { | ||
var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData ); | |||
this. | for( var dosageId in dosages ) { | ||
dosages[ dosageId ].id = this.dosages.length; | |||
this.dosages.push( dosages[ dosageId ] ); | |||
} | } | ||
}; | |||
mw.calculators.objectClasses.Drug.prototype.getIndications = function() { | |||
var indications = []; | |||
var | for( var iDosage in this.dosages ) { | ||
if( this.dosages[ iDosage ].indication ) { | |||
indications.push( this.dosages[ iDosage ].indication ); | |||
} | |||
} | } | ||
return indications.filter( mw.calculators.uniqueValues ); | |||
}; | |||
mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) { | |||
var populations = []; | |||
for( var iDosage in this.dosages ) { | |||
if( this.dosages[ iDosage ].population && | |||
if( this. | ( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) { | ||
populations.push( this.dosages[ iDosage ].population ); | |||
} | } | ||
} | |||
return populations.filter( mw.calculators.uniqueValues ); | |||
}; | |||
mw.calculators.objectClasses.Drug.prototype.getPreparations = function() { | |||
return this.preparations.filter( mw.calculators.uniqueValues ); | |||
}; | |||
/** | |||
* DrugPreparation | |||
*/ | |||
mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) { | |||
if( !mw.calculators.getDrug( drugId ) ) { | |||
throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' ); | |||
} | |||
for( var drugPreparationId in drugPreparationData ) { | |||
drugPreparationData[ drugPreparationId ].drug = drugId; | |||
} | |||
var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData ); | |||
for( var drugPreparationId in drugPreparations ) { | |||
mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ]; | |||
} | |||
}; | |||
/** | |||
* Class DrugPreparation | |||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugPreparation} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) { | |||
var properties = { | |||
required: [ | |||
'drug', | |||
'id', | |||
'concentration' | |||
], | |||
optional: [ | |||
'dilutionRequired', | |||
'commonDilution' | |||
] | |||
}; | |||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
this.concentration = this.concentration.replace( 'mcg', 'ug' ); | |||
this.concentration = math.unit( this.concentration ); | |||
}; | |||
mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | |||
mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() { | |||
// The units of concentration will always be of the form "mass / volume" | |||
// The regular expression matches all text leading up to the volume units | |||
return this.concentration.formatUnits().replace( /.*\/\s?/, '' ); | |||
}; | |||
mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() { | |||
return mw.calculators.getValueString( this.concentration ); | |||
}; | |||
/** | |||
* Class DrugDosage | |||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugDosage} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugDosage = function( propertyValues ) { | |||
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | |||
var drugIndication = mw.calculators.getDrugIndication( this.indication ); | |||
if( !drugIndication ) { | |||
throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' ); | |||
} | } | ||
this.indication = drugIndication; | |||
this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION; | |||
var drugPopulation = mw.calculators.getDrugPopulation( this.population ); | |||
if( !drugPopulation ) { | |||
throw new Error( 'Invalid population "' + this.population + '" for drug dosage' ); | |||
( this. | |||
} | } | ||
this.population = drugPopulation; | |||
this.route = this.route ? this.route : 'IV'; | |||
var drugDoseData = this.dose; | |||
this.dose = []; | |||
this.addDoses( drugDoseData ); | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) { | |||
// Each dosage can have one or more associated doses. Ensure this value is an array. | |||
if( !Array.isArray( drugDoseData ) ) { | |||
drugDoseData = [ drugDoseData ]; | |||
} | } | ||
var doses = mw.calculators.createCalculatorObjects( 'DrugDose', drugDoseData ); | |||
for( var | for( var doseId in doses ) { | ||
doses[ doseId ].id = this.dose.length; | |||
this.dose.push( doses[ doseId ] ); | |||
} | } | ||
}; | }; | ||
mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() { | |||
var inputData = new mw.calculators.objectClasses.CalculationData(); | |||
inputData = inputData.merge( this.population.getCalculationData() ); | |||
for( var iDose in this.dose ) { | |||
inputData = inputData.merge( this.dose[ iDose ].getCalculationData() ); | |||
} | } | ||
return inputData; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() { | ||
return { | return { | ||
required: [ | required: [ | ||
'dose', | |||
'id', | 'id', | ||
' | 'indication' | ||
], | ], | ||
optional: [ | optional: [ | ||
'description', | 'description', | ||
' | 'population', | ||
' | 'route' | ||
] | ] | ||
}; | }; | ||
} | } | ||
/** | |||
* Class DrugDose | |||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugDose} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugDose = function( propertyValues ) { | |||
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | |||
var mathProperties = this.getMathProperties(); | |||
for( var iMathProperty in mathProperties ) { | |||
var mathProperty = mathProperties[ iMathProperty ]; | |||
if( this[ mathProperty ] ) { | |||
// TODO consider making a UnitsBase.weight.fromString() | |||
this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' ); | |||
this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' ); | |||
this[ mathProperty ] = math.unit( this[ mathProperty ] ); | |||
} else { | } else { | ||
this[ mathProperty ] = null; | |||
} | } | ||
} | } | ||
if( this.weightCalculation ) { | |||
var weightCalculation = mw.calculators.getCalculation( this.weightCalculation ); | |||
if( ! | if( !weightCalculation ) { | ||
throw new Error( ' | throw new Error( 'Drug dose references weight calculation "' + this.weightCalculation + '" which is not defined' ); | ||
} | |||
this.weightCalculation = weightCalculation; | |||
} | } | ||
}; | |||
mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | |||
mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() { | |||
var calculationData = new mw.calculators.objectClasses.CalculationData(); | |||
var mathProperties = this.getMathProperties(); | |||
// Look at dose properties to identify any variable dependence (e.g. weight-dependence) | |||
for( var iMathProperty in mathProperties ) { | |||
var mathProperty = mathProperties[ iMathProperty ]; | |||
var dosePropertyValue = this[ mathProperty ]; | |||
if( | // For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future | ||
if( mw.calculators.isValueDependent( dosePropertyValue, 'weight' ) && | |||
calculationData.variables.optional.indexOf( 'weight' ) === -1 ) { | |||
calculationData.variables.optional.push( 'weight' ); | |||
} | } | ||
} | |||
if( this.weightCalculation ) { | |||
calculationData.calculations.optional.push( this.weightCalculation.id ); | |||
} | } | ||
return | return calculationData; | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() { | ||
return [ | |||
'dose', | |||
'min', | |||
'max', | |||
'absoluteMin', | |||
'absoluteMax' | |||
]; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() { | ||
return | return { | ||
required: [ | |||
'id' | |||
], | |||
optional: [ | |||
'absoluteMin', | |||
'absoluteMax', | |||
'dose', | |||
'frequency', | |||
'min', | |||
'max', | |||
'name', | |||
'weightCalculation' | |||
] | |||
}; | |||
}; | }; | ||
/** | |||
* Class DrugDosageCalculation | |||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugDosageCalculation} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugDosageCalculation = function( propertyValues ) { | |||
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | |||
this.initialize(); | |||
}; | |||
mw.calculators.objectClasses.DrugDosageCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype ); | |||
mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate = function( data ) { | |||
var value = { | |||
population: null, | |||
preparation: data.preparation, | |||
dose: [] | |||
}; | |||
} | |||
// Determine which dosage to use | |||
var populationScores = []; | |||
for( var iDosage in data.drug.dosages ) { | |||
var drugDosage = data.drug.dosages[ iDosage ]; | |||
// If the indication does not match, set the score to -1 | |||
var populationScore = ( drugDosage.indication.id === data.indication.id ) ? | |||
drugDosage.population.getCalculationDataScore( data ) : -1; | |||
populationScores.push( populationScore ); | |||
} | } | ||
var maxPopulationScore = Math.max.apply( null, populationScores ); | |||
if( | if( maxPopulationScore < 0 ) { | ||
return value; | |||
} | } | ||
// If there is more than one dosage with the same score, take the first. | |||
var | // This allows the data editor to decide which is most important. | ||
var dosageId = populationScores.indexOf( maxPopulationScore ); | |||
var | var dosage = data.drug.dosages[ dosageId ]; | ||
value.population = dosage.population; | |||
// A dosage may contain multiple doses (e.g. induction and maintenance) | |||
for( var iDose in dosage.dose ) { | |||
var dose = dosage.dose[ iDose ]; | |||
var mathProperties = dose.getMathProperties(); | |||
// Initialize value properties for dose | |||
value.dose[ iDose ] = { | |||
massPerWeight: { | |||
decimals: 0 | |||
}, | |||
mass: { | |||
decimals: 0 | |||
}, | |||
name: dose.name, | |||
volume: { | |||
decimals: 0 | |||
}, | |||
weightCalculation: dose.weightCalculation ? dose.weightCalculation : null | |||
}; | |||
var weightValue = dose.weightCalculation ? dose.weightCalculation.value : data.weight; | |||
for( var iMathProperty in mathProperties ) { | |||
var mathProperty = mathProperties[ iMathProperty ]; | |||
var doseValue = dose[ mathProperty ]; | |||
if( doseValue ) { | |||
var doseValueDecimals = mw.calculators.getValueDecimals( doseValue ); | |||
if( mw.calculators.isValueDependent( doseValue, 'weight' ) ) { | |||
value.dose[ iDose ].massPerWeight[ mathProperty ] = doseValue; | |||
// Set the decimals based upon the specified dose | |||
value.dose[ iDose ].massPerWeight.decimals = math.max( | |||
doseValueDecimals, | |||
value.dose[ iDose ].massPerWeight.decimals | |||
); | |||
// For whatever reason math.format will simplify the units, but math.formatUnits will not | |||
// as a hack, we recreate a new unit value with the correct formatting of the result | |||
value.dose[ iDose ].mass[ mathProperty ] = weightValue ? math.unit( math.multiply( doseValue, weightValue ).format() ) : null; | |||
// Mass should use one fewer decimals than massPerWeight (unless already 0) | |||
value.dose[ iDose ].mass.decimals = math.max( | |||
math.max( doseValueDecimals - 1, 0 ), | |||
value.dose[ iDose ].mass.decimals | |||
); | |||
} else { | |||
value.dose[ iDose ].mass[ mathProperty ] = doseValue; | |||
// Set the decimals based upon the specified dose | |||
value.dose[ iDose ].mass.decimals = math.max( | |||
doseValueDecimals, | |||
value.dose[ iDose ].mass.decimals | |||
); | |||
} | |||
if( data.preparation && value.dose[ iDose ].mass[ mathProperty ] ) { | |||
// Same hack as above to get units to simplify correctly | |||
value.dose[ iDose ].volume[ mathProperty ] = math.unit( math.multiply( value.dose[ iDose ].mass[ mathProperty ], math.divide( 1, data.preparation.concentration ) ).format() ); | |||
// value.dose[ iDose ].volume.decimals = 0; | |||
} | |||
} | |||
} | |||
} | } | ||
} | } | ||
return value; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculation.prototype.doRender = function() { | ||
var $calculationContainer = $( '.' + this.getContainerClass() ); | |||
if( !$calculationContainer.length ) { | |||
return; | |||
} | |||
$calculationContainer.empty(); | |||
// Label column | |||
var labelAttributes = {}; | |||
var labelCss = {}; | |||
if( this.drug.color.isStriped() ) { | |||
labelCss[ 'background' ] = 'repeating-linear-gradient(135deg,rgba(0,0,0,0),rgba(0,0,0,0)10px,rgba(255,255,255,1)10px,rgba(255,255,255,1)20px),' + this.drug.color.getPrimaryColor(); | |||
} else { | |||
labelCss[ 'background-color'] = this.drug.color.getPrimaryColor(); | |||
} | |||
var $label = $( '<th>', labelAttributes ).css( labelCss ); | |||
$label.append( this.getLabelHtml() ); | |||
var | var $infoButton = null; | ||
if( this.hasInfo() ) { | |||
$infoButton = $( '<a>', { | |||
'data-toggle': 'collapse', | |||
href: '#' + this.getContainerClass() + '-info', | |||
role: 'button', | |||
'aria-expanded': 'false', | |||
'aria-controls': this.getContainerClass() + '-info' | |||
} ) | |||
.append( $( '<i>', { | |||
class: 'far fa-question-circle' | |||
} ) ); | |||
$label | |||
.append( $( '<span>', { | |||
class: 'calculator-calculation-column-label-info' | |||
} ) | |||
.append( $infoButton ) | |||
. | ); | ||
} | } | ||
// Indication column | |||
var $indication = $( '<td>' ); | |||
var indications = this.drug.getIndications(); | |||
if( indications.length > 1 ) { | |||
$indication.append( mw.calculators.getVariable( this.getVariableIds().indication ).createInput({ | |||
hideLabel: true | |||
} ) ); | |||
} else { | |||
$indication.append( String( indications[ 0 ] ) ); | |||
} | |||
// Dosage column | |||
var $dosage = $( '<td>' ); | |||
var dash = '–'; | |||
var preparations = this.drug.getPreparations(); | |||
var $preparationSelect; | |||
this. | if( preparations.length > 1 ) { | ||
$preparationSelect = mw.calculators.getVariable( this.getVariableIds().preparation ).createInput({ | |||
hideLabel: true, | |||
inline: true, | |||
inputClass: 'calculator-input-preparation' | |||
} ); | |||
} else { | |||
$preparationSelect = String( preparations[ 0 ] ); | |||
} | |||
if( this.value.population && this.value.population.id !== DEFAULT_DRUG_POPULATION ) { | |||
$dosage.append( String( this.value.population ) + '<br />' ); | |||
} | |||
for( var iDose in this.value.dose ) { | |||
var doseValue = this.value.dose[ iDose ]; | |||
if( doseValue.name ) { | |||
$dosage.append( doseValue.name + '<br />' ); | |||
} | |||
var massPerWeightHtml = ''; | |||
if( doseValue.massPerWeight.hasOwnProperty( 'dose' ) ) { | |||
massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.dose, doseValue.massPerWeight.decimals ); | |||
} else if( doseValue.massPerWeight.hasOwnProperty( 'min' ) && | |||
doseValue.massPerWeight.hasOwnProperty( 'max' ) ) { | |||
massPerWeightHtml += mw.calculators.getValueNumber( doseValue.massPerWeight.min, doseValue.massPerWeight.decimals ); | |||
massPerWeightHtml += dash; | |||
massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.max, doseValue.massPerWeight.decimals ); | |||
} | |||
if( massPerWeightHtml ) { | |||
if( doseValue.weightCalculation ) { | |||
massPerWeightHtml += ' (' + doseValue.weightCalculation.getLabelString() + ')'; | |||
} | |||
$dosage.append( massPerWeightHtml + '<br />' ); | |||
} | } | ||
var massHtml = ''; | |||
var massUnits = ''; | |||
} ) | if( doseValue.mass.hasOwnProperty( 'dose' ) ) { | ||
massHtml += mw.calculators.getValueString( doseValue.mass.dose, doseValue.mass.decimals ); | |||
massUnits = mw.calculators.getUnitsString( doseValue.mass.dose ); | |||
} else if( doseValue.mass.hasOwnProperty( 'min' ) && | |||
doseValue.mass.hasOwnProperty( 'max' ) ) { | |||
massHtml += mw.calculators.getValueNumber( doseValue.mass.min, doseValue.mass.decimals ); | |||
massHtml += dash; | |||
massHtml += mw.calculators.getValueString( doseValue.mass.max, doseValue.mass.decimals ); | |||
massUnits = mw.calculators.getUnitsString( doseValue.mass.min ); | |||
} | |||
if( massHtml ) { | |||
$dosage.append( massHtml + '<br />' ); | |||
} | |||
var volumeHtml = ''; | |||
if( this.value.preparation ) { | |||
// Get the volume units of the preparation | |||
var volumeUnits = this.value.preparation.getVolumeUnits(); | |||
if( !$.isEmptyObject( doseValue.mass ) && massUnits ) { | |||
// Extract any remaining additional dependent units (e.g. /min) | |||
var massRemainderUnits = massUnits.replace( /.*\/?/, '' ); | |||
if( massRemainderUnits ) { | |||
volumeUnits = volumeUnits + '/' + massRemainderUnits; | |||
} | |||
} | |||
if( doseValue.volume.hasOwnProperty( 'dose' ) ) { | |||
volumeHtml += mw.calculators.getValueString( doseValue.volume.dose.to( volumeUnits ), doseValue.volume.decimals ); | |||
} else if( doseValue.volume.hasOwnProperty( 'min' ) && | |||
doseValue.volume.hasOwnProperty( 'max' ) ) { | |||
volumeHtml += mw.calculators.getValueNumber( doseValue.volume.min.to( volumeUnits ), doseValue.volume.decimals ); | |||
volumeHtml += dash; | |||
volumeHtml += mw.calculators.getValueString( doseValue.volume.max.to( volumeUnits ), doseValue.volume.decimals ); | |||
} | |||
if( volumeHtml ) { | |||
$dosage.append( volumeHtml + ' of ' ); | |||
$dosage.append( $preparationSelect ); | |||
$dosage.append( '<br />' ); | |||
} | |||
} | } | ||
} | } | ||
$calculationContainer | |||
.append( | |||
$label, | |||
$indication, | |||
$dosage | |||
); | |||
return; | |||
var calculation = this; | var calculation = this; | ||
| Line 1,202: | Line 955: | ||
$( this ).empty(); | $( this ).empty(); | ||
if( calculation.hasInfo() ) { | if( calculation.hasInfo() ) { | ||
| Line 1,281: | Line 998: | ||
} | } | ||
$infoContainer = $( '<tr>', { | |||
id: infoContainerId, | |||
class: 'collapse' | |||
} ) | |||
.append( $( '<td>', { | |||
colspan: 2 | |||
} ).append( infoHtml ) ); | |||
$( this ).after( $infoContainer ); | $( this ).after( $infoContainer ); | ||
} | } | ||
} ); | |||
}; | |||
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationData = function() { | |||
var inputData = new mw.calculators.objectClasses.CalculationData(); | |||
// Add variables created by this calculation | |||
var variableIds = this.getVariableIds(); | |||
for( var variableType in variableIds ) { | |||
inputData.variables.optional.push( variableIds[ variableType ] ); | |||
} | |||
var dataTypes = inputData.getDataTypes(); | |||
// Data is only actually required if it is required by every dosage for the drug. | |||
// Data marked as required by an individual dosage that does not appear in every | |||
// dosage will be converted to optional. | |||
var requiredInputData = new mw.calculators.objectClasses.CalculationData(); | |||
// Need a way to tell the first iteration of the loop to initialize the required variables to a value that | |||
// is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't | |||
// reinitialize). | |||
var initializeRequiredData = true; | |||
// Iterate through each dosage to determine variable dependency | |||
for( var iDosage in this.drug.dosages ) { | |||
var dosageInputData = this.drug.dosages[ iDosage ].getCalculationData(); | |||
inputData = inputData.merge( dosageInputData ); | |||
for( var iDataType in dataTypes ) { | |||
var dataType = dataTypes[ iDataType ]; | |||
if( initializeRequiredData ) { | |||
requiredInputData[ dataType ].required = inputData[ dataType ].required; | |||
} else { | } else { | ||
// Data is only truly required if it is required by all dosage calculations, so use array intersection | |||
requiredInputData[ dataType ].required = requiredInputData[ dataType ].required.filter( function( index ) { | |||
} | return dosageInputData[ dataType ].required.indexOf( index ) !== -1; | ||
} ); | |||
} | } | ||
} | |||
initializeRequiredData = false; | |||
} | |||
for( var iDataType in dataTypes ) { | |||
var dataType = dataTypes[ iDataType ]; | |||
// Move any data marked required in inputData to optional if it not actually required (i.e. doesn't appear | |||
// in requiredInputData). | |||
inputData[ dataType ].optional = inputData[ dataType ].optional.concat( inputData[ dataType ].required.filter( function( index ) { | |||
return requiredInputData[ dataType ].required.indexOf( index ) === -1; | |||
} ) ).filter( mw.calculators.uniqueValues ); | |||
inputData[ dataType ].required = requiredInputData[ dataType ].required; | |||
} | |||
return inputData; | |||
}; | }; | ||
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationDataValues = function() { | |||
var data = mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues.call( this ); | |||
data.drug = this.drug; | |||
data.indication = data[ this.getVariablePrefix() + 'indication' ] ? | |||
mw.calculators.getDrugIndication( mw.calculators.getVariable( this.getVariableIds().indication ).value ) : | |||
null; | |||
delete data[ this.getVariablePrefix() + 'indication' ]; | |||
data.preparation = data[ this.getVariablePrefix() + 'preparation' ] ? | |||
this.drug.preparations[ mw.calculators.getVariable( this.getVariableIds().preparation ).value ] : | |||
null; | |||
delete data[ this.getVariablePrefix() + 'preparation' ]; | |||
return data; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() { | ||
var labelHtml = this.drug.name; | |||
var $label = $( '<a>', { | |||
href: mw.util.getUrl( this.drug.name ), | |||
text: labelHtml | |||
} ); | |||
var highlightColor = this.drug.color.getHighlightColor(); | |||
if( highlightColor ) { | |||
$label.css( 'background-color', highlightColor ); | |||
} | |||
labelHtml = $label[ 0 ].outerHTML; | |||
return labelHtml; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculation.prototype.getProperties = function() { | ||
return | var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties(); | ||
return this.mergeProperties( inheritedProperties, { | |||
required: [ | |||
'drug' | |||
], | |||
optional: [] | |||
} ); | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableIds = function() { | ||
return { | return { | ||
indication: this.getVariablePrefix() + 'indication', | |||
preparation: this.getVariablePrefix() + 'preparation' | |||
}; | }; | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariablePrefix = function() { | ||
if( | return this.drug.id + '-'; | ||
} | |||
mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() { | |||
mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this ); | |||
var drug = mw.calculators.getDrug( this.drug ); | |||
if( !drug ) { | |||
throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' ); | |||
} | |||
this.drug = drug; | |||
var variableIds = this.getVariableIds(); | |||
// Create variables for indication, population?, preparation select boxes? Here or m.c.addDosages() or elsewhere? | |||
// Will have to add them to getCalculationData too. | |||
var drugVariables = {}; | |||
var indications = this.drug.getIndications(); | |||
var indicationOptions = {}; | |||
for( var iIndication in indications ) { | |||
var indication = indications[ iIndication ]; | |||
indicationOptions[ indication.id ] = String( indication ); | |||
} | } | ||
this. | drugVariables[ variableIds.indication ] = { | ||
name: 'Indication', | |||
type: 'string', | |||
defaultValue: indications.length ? indications[ 0 ].id : null, | |||
options: indicationOptions | |||
}; | |||
var preparations = this.drug.getPreparations(); | |||
var preparationOptions = {}; | |||
for( var iPreparation in preparations ) { | |||
var preparation = preparations[ iPreparation ]; | |||
preparationOptions[ preparation.id ] = String( preparation ); | |||
} | } | ||
drugVariables[ variableIds.preparation ] = { | |||
name: 'Preparation', | |||
type: 'string', | |||
defaultValue: preparations.length ? preparations[ 0 ].id : null, | |||
options: preparationOptions | |||
}; | |||
mw.calculators.addVariables( drugVariables ); | |||
}; | |||
| Line 1,381: | Line 1,190: | ||
/** | /** | ||
* Class | * Class DrugDosageCalculator | ||
* @param {Object} propertyValues | * @param {Object} propertyValues | ||
* @returns {mw.calculators.objectClasses. | * @returns {mw.calculators.objectClasses.DrugDosageCalculator} | ||
* @constructor | * @constructor | ||
*/ | */ | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculator = function( propertyValues ) { | ||
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype ); | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculator.prototype.doRender = function() { | ||
var $calculatorContainer = $( '.' + this.getContainerClass() ); | var $calculatorContainer = $( '.' + this.getContainerClass() ); | ||
| Line 1,400: | Line 1,209: | ||
$calculatorContainer.addClass( this.getCalculatorClass() ); | $calculatorContainer.addClass( this.getCalculatorClass() ); | ||
$calculatorContainer.empty(); | $calculatorContainer.empty(); | ||
| Line 1,411: | Line 1,216: | ||
} ) ); | } ) ); | ||
var $calculationsContainer; | var $calculationsContainer = $( '<table>', { | ||
class: 'wikitable' | |||
} ).append( '<tbody>' ); | |||
$calculatorContainer.append( $calculationsContainer ); | |||
$calculationsContainer | |||
.append( $( '<tr>' ) | |||
.append( | |||
$( '<th>', { | |||
class: this.getCalculatorClass() + '-drug' | |||
} ).text( 'Drug' ), | |||
$( '<th>', { | |||
class: this.getCalculatorClass() + '-indication' | |||
} ).text( 'Indication' ), | |||
$( '<th>', { | |||
class: this.getCalculatorClass() + '-dose' | |||
} ).text( 'Dose' ) | |||
) | |||
); | |||
for( var iCalculationId in this.calculations ) { | for( var iCalculationId in this.calculations ) { | ||
| Line 1,439: | Line 1,241: | ||
var calculationContainerClass = calculation.getContainerClass(); | var calculationContainerClass = calculation.getContainerClass(); | ||
var $calculationContainer = $( ' | var $calculationContainer = $( '<tr>', { | ||
class: calculationContainerClass | |||
} ); | |||
$calculationsContainer.append( $calculationContainer ); | |||
calculation.render(); | calculation.render(); | ||
| Line 1,460: | Line 1,251: | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosageCalculator.prototype.getCalculatorClass = function() { | ||
return 'calculator- | return 'calculator-DrugDosageCalculator'; | ||
}; | }; | ||
mw.calculators.objectClasses.DrugDosageCalculator.prototype.getProperties = function() { | |||
mw.calculators.objectClasses. | |||
var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties(); | var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties(); | ||
return this.mergeProperties( inheritedProperties, { | return this.mergeProperties( inheritedProperties, { | ||
required: [], | required: [], | ||
optional: [ | optional: [] | ||
} ); | } ); | ||
}; | }; | ||
}() ); | }() ); | ||
Revision as of 21:51, 11 August 2021
/**
* @author Chris Rishel
*/
( function() {
var DEFAULT_DRUG_COLOR = 'default';
var DEFAULT_DRUG_POPULATION = 'general';
mw.calculators.isValueDependent = function( value, variableId ) {
// This may need generalized to support other variables in the future
if( variableId === 'weight' ) {
return value && value.formatUnits().match( /\/[\s(]*?kg/ );
} else {
throw new Error( 'Dependence "' + variableId + '" not supported by isValueDependent' );
}
};
/**
* Define units
*/
mw.calculators.addUnitsBases( {
concentration: {
toString: function( units ) {
units = units.replace( ' pct', '%' );
return units;
}
}
} );
mw.calculators.addUnits( {
pct: {
baseName: 'concentration',
definition: '10 mg/mL'
},
vial: {
baseName: 'VOLUME'
}
} );
/**
* DrugColor
*/
mw.calculators.drugColors = {};
mw.calculators.addDrugColors = function( drugColorData ) {
var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );
for( var drugColorId in drugColors ) {
mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
}
};
mw.calculators.getDrugColor = function( drugColorId ) {
if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
return mw.calculators.drugColors[ drugColorId ];
} else {
return null;
}
};
/**
* Class DrugColor
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugColor}
* @constructor
*/
mw.calculators.objectClasses.DrugColor = function( propertyValues ) {
var properties = {
required: [
'id'
],
optional: [
'parentColor',
'primaryColor',
'highlightColor',
'striped'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
this.parentColor = this.parentColor || this.id === DEFAULT_DRUG_COLOR ? this.parentColor : DEFAULT_DRUG_COLOR;
};
mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugColor.prototype.getParentDrugColor = function() {
if( !this.parentColor ) {
return null;
}
var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );
if( !parentDrugColor ) {
throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
}
return parentDrugColor;
};
mw.calculators.objectClasses.DrugColor.prototype.getHighlightColor = function() {
if( this.highlightColor ) {
return this.highlightColor;
} else if( this.parentColor ) {
return this.getParentDrugColor().getHighlightColor();
}
};
mw.calculators.objectClasses.DrugColor.prototype.getPrimaryColor = function() {
if( this.primaryColor ) {
return this.primaryColor;
} else if( this.parentColor ) {
return this.getParentDrugColor().getPrimaryColor();
}
};
mw.calculators.objectClasses.DrugColor.prototype.isStriped = function() {
if( this.striped !== null ) {
return this.striped;
} else if( this.parentColor ) {
return this.getParentDrugColor().isStriped();
}
};
/**
* DrugPopulation
*/
mw.calculators.drugPopulations = {};
mw.calculators.addDrugPopulations = function( drugPopulationData ) {
var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );
for( var drugPopulationId in drugPopulations ) {
mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
}
};
mw.calculators.getDrugPopulation = function( drugPopulationId ) {
if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
return mw.calculators.drugPopulations[ drugPopulationId ];
} else {
return null;
}
};
/**
* Class DrugPopulation
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugPopulation}
* @constructor
*/
mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) {
var properties = {
required: [
'id',
'name'
],
optional: [
'abbreviation',
'variables'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
if( this.variables ) {
for( var variableId in this.variables ) {
if( !mw.calculators.getVariable( variableId ) ) {
throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
}
this.variables[ variableId ].min = this.variables[ variableId ].hasOwnProperty( 'min' ) ?
math.unit( this.variables[ variableId ].min ) : null;
this.variables[ variableId ].max = this.variables[ variableId ].hasOwnProperty( 'max' ) ?
math.unit( this.variables[ variableId ].max ) : null;
}
} else {
this.variables = {};
}
};
mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationData = function() {
var inputData = new mw.calculators.objectClasses.CalculationData();
for( var variableId in this.variables ) {
inputData.variables.required.push( variableId );
}
return inputData;
};
mw.calculators.objectClasses.DrugPopulation.prototype.getCalculationDataScore = function( dataValues ) {
// A return value of -1 indicates the data did not match the population definition
for( var variableId in this.variables ) {
if( !dataValues.hasOwnProperty( variableId ) ) {
return -1;
}
if( this.variables[ variableId ].min &&
!math.largerEq( dataValues[ variableId ], this.variables[ variableId ].min ) ) {
return -1;
}
if( this.variables[ variableId ].max &&
!math.smallerEq( dataValues[ variableId ], this.variables[ variableId ].max ) ) {
return -1;
}
}
// If the data matches the population definition, the score corresponds to the number of variables in the
// population definition. This should roughly correspond to the specificity of the population.
return Object.keys( this.variables ).length;
};
mw.calculators.objectClasses.DrugPopulation.prototype.toString = function() {
return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
}
/**
* DrugIndication
*/
mw.calculators.drugIndications = {};
mw.calculators.addDrugIndications = function( drugIndicationData ) {
var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );
for( var drugIndicationId in drugIndications ) {
mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
}
};
mw.calculators.getDrugIndication = function( drugIndicationId ) {
if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
return mw.calculators.drugIndications[ drugIndicationId ];
} else {
return null;
}
};
/**
* Class DrugIndication
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugIndication}
* @constructor
*/
mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
var properties = {
required: [
'id',
'name'
],
optional: [
'abbreviation'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
};
mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugIndication.prototype.toString = function() {
return mw.calculators.isMobile() && this.abbreviation ? this.abbreviation : this.name;
};
/**
* Drug
*/
mw.calculators.drugs = {};
mw.calculators.addDrugs = function( drugData ) {
var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );
for( var drugId in drugs ) {
mw.calculators.drugs[ drugId ] = drugs[ drugId ];
}
};
mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
var drug = mw.calculators.getDrug( drugId );
if( !drug ) {
throw new Error( 'DrugDosage references drug "' + drugId + '" which is not defined' );
}
drug.addDosages( drugDosageData );
var calculationId = 'drugDosage-' + drugId;
var calculation = mw.calculators.getCalculation( calculationId );
if( !calculation ) {
var calculationData = {};
calculationData[ calculationId ] = {
calculate: mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate,
drug: drugId,
type: 'drug'
};
mw.calculators.addCalculations( calculationData, 'DrugDosageCalculation' );
calculation = mw.calculators.getCalculation( calculationId );
}
calculation.setDependencies();
};
mw.calculators.getDrug = function( drugId ) {
if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
return mw.calculators.drugs[ drugId ];
} else {
return null;
}
};
/**
* Class Drug
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.Drug}
* @constructor
*/
mw.calculators.objectClasses.Drug = function( propertyValues ) {
var properties = {
required: [
'id',
'name'
],
optional: [
'color'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
if( !this.color ) {
this.color = DEFAULT_DRUG_COLOR;
}
var color = mw.calculators.getDrugColor( this.color );
if( !color ) {
throw new Error( 'Invalid drug color "' + this.color + '" for drug "' + this.id + '"' );
}
this.color = color;
this.dosages = [];
this.preparations = [];
};
mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.Drug.prototype.addDosages = function( drugDosageData ) {
var dosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
for( var dosageId in dosages ) {
dosages[ dosageId ].id = this.dosages.length;
this.dosages.push( dosages[ dosageId ] );
}
};
mw.calculators.objectClasses.Drug.prototype.getIndications = function() {
var indications = [];
for( var iDosage in this.dosages ) {
if( this.dosages[ iDosage ].indication ) {
indications.push( this.dosages[ iDosage ].indication );
}
}
return indications.filter( mw.calculators.uniqueValues );
};
mw.calculators.objectClasses.Drug.prototype.getPopulations = function( indicationId ) {
var populations = [];
for( var iDosage in this.dosages ) {
if( this.dosages[ iDosage ].population &&
( !indicationId || ( this.dosages[ iDosage ].indication && this.dosages[ iDosage ].indication.id === indicationId ) ) ) {
populations.push( this.dosages[ iDosage ].population );
}
}
return populations.filter( mw.calculators.uniqueValues );
};
mw.calculators.objectClasses.Drug.prototype.getPreparations = function() {
return this.preparations.filter( mw.calculators.uniqueValues );
};
/**
* DrugPreparation
*/
mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
if( !mw.calculators.getDrug( drugId ) ) {
throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
}
for( var drugPreparationId in drugPreparationData ) {
drugPreparationData[ drugPreparationId ].drug = drugId;
}
var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData );
for( var drugPreparationId in drugPreparations ) {
mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
}
};
/**
* Class DrugPreparation
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugPreparation}
* @constructor
*/
mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
var properties = {
required: [
'drug',
'id',
'concentration'
],
optional: [
'dilutionRequired',
'commonDilution'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
this.concentration = this.concentration.replace( 'mcg', 'ug' );
this.concentration = math.unit( this.concentration );
};
mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugPreparation.prototype.getVolumeUnits = function() {
// The units of concentration will always be of the form "mass / volume"
// The regular expression matches all text leading up to the volume units
return this.concentration.formatUnits().replace( /.*\/\s?/, '' );
};
mw.calculators.objectClasses.DrugPreparation.prototype.toString = function() {
return mw.calculators.getValueString( this.concentration );
};
/**
* Class DrugDosage
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugDosage}
* @constructor
*/
mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
var drugIndication = mw.calculators.getDrugIndication( this.indication );
if( !drugIndication ) {
throw new Error( 'Invalid indication "' + this.indication + '" for drug dosage' );
}
this.indication = drugIndication;
this.population = this.population ? this.population : DEFAULT_DRUG_POPULATION;
var drugPopulation = mw.calculators.getDrugPopulation( this.population );
if( !drugPopulation ) {
throw new Error( 'Invalid population "' + this.population + '" for drug dosage' );
}
this.population = drugPopulation;
this.route = this.route ? this.route : 'IV';
var drugDoseData = this.dose;
this.dose = [];
this.addDoses( drugDoseData );
};
mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugDosage.prototype.addDoses = function( drugDoseData ) {
// Each dosage can have one or more associated doses. Ensure this value is an array.
if( !Array.isArray( drugDoseData ) ) {
drugDoseData = [ drugDoseData ];
}
var doses = mw.calculators.createCalculatorObjects( 'DrugDose', drugDoseData );
for( var doseId in doses ) {
doses[ doseId ].id = this.dose.length;
this.dose.push( doses[ doseId ] );
}
};
mw.calculators.objectClasses.DrugDosage.prototype.getCalculationData = function() {
var inputData = new mw.calculators.objectClasses.CalculationData();
inputData = inputData.merge( this.population.getCalculationData() );
for( var iDose in this.dose ) {
inputData = inputData.merge( this.dose[ iDose ].getCalculationData() );
}
return inputData;
};
mw.calculators.objectClasses.DrugDosage.prototype.getProperties = function() {
return {
required: [
'dose',
'id',
'indication'
],
optional: [
'description',
'population',
'route'
]
};
}
/**
* Class DrugDose
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugDose}
* @constructor
*/
mw.calculators.objectClasses.DrugDose = function( propertyValues ) {
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
var mathProperties = this.getMathProperties();
for( var iMathProperty in mathProperties ) {
var mathProperty = mathProperties[ iMathProperty ];
if( this[ mathProperty ] ) {
// TODO consider making a UnitsBase.weight.fromString()
this[ mathProperty ] = this[ mathProperty ].replace( 'kg', 'kgwt' );
this[ mathProperty ] = this[ mathProperty ].replace( 'mcg', 'ug' );
this[ mathProperty ] = math.unit( this[ mathProperty ] );
} else {
this[ mathProperty ] = null;
}
}
if( this.weightCalculation ) {
var weightCalculation = mw.calculators.getCalculation( this.weightCalculation );
if( !weightCalculation ) {
throw new Error( 'Drug dose references weight calculation "' + this.weightCalculation + '" which is not defined' );
}
this.weightCalculation = weightCalculation;
}
};
mw.calculators.objectClasses.DrugDose.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugDose.prototype.getCalculationData = function() {
var calculationData = new mw.calculators.objectClasses.CalculationData();
var mathProperties = this.getMathProperties();
// Look at dose properties to identify any variable dependence (e.g. weight-dependence)
for( var iMathProperty in mathProperties ) {
var mathProperty = mathProperties[ iMathProperty ];
var dosePropertyValue = this[ mathProperty ];
// For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
if( mw.calculators.isValueDependent( dosePropertyValue, 'weight' ) &&
calculationData.variables.optional.indexOf( 'weight' ) === -1 ) {
calculationData.variables.optional.push( 'weight' );
}
}
if( this.weightCalculation ) {
calculationData.calculations.optional.push( this.weightCalculation.id );
}
return calculationData;
};
mw.calculators.objectClasses.DrugDose.prototype.getMathProperties = function() {
return [
'dose',
'min',
'max',
'absoluteMin',
'absoluteMax'
];
};
mw.calculators.objectClasses.DrugDose.prototype.getProperties = function() {
return {
required: [
'id'
],
optional: [
'absoluteMin',
'absoluteMax',
'dose',
'frequency',
'min',
'max',
'name',
'weightCalculation'
]
};
};
/**
* Class DrugDosageCalculation
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugDosageCalculation}
* @constructor
*/
mw.calculators.objectClasses.DrugDosageCalculation = function( propertyValues ) {
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
this.initialize();
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
mw.calculators.objectClasses.DrugDosageCalculation.prototype.calculate = function( data ) {
var value = {
population: null,
preparation: data.preparation,
dose: []
};
// Determine which dosage to use
var populationScores = [];
for( var iDosage in data.drug.dosages ) {
var drugDosage = data.drug.dosages[ iDosage ];
// If the indication does not match, set the score to -1
var populationScore = ( drugDosage.indication.id === data.indication.id ) ?
drugDosage.population.getCalculationDataScore( data ) : -1;
populationScores.push( populationScore );
}
var maxPopulationScore = Math.max.apply( null, populationScores );
if( maxPopulationScore < 0 ) {
return value;
}
// If there is more than one dosage with the same score, take the first.
// This allows the data editor to decide which is most important.
var dosageId = populationScores.indexOf( maxPopulationScore );
var dosage = data.drug.dosages[ dosageId ];
value.population = dosage.population;
// A dosage may contain multiple doses (e.g. induction and maintenance)
for( var iDose in dosage.dose ) {
var dose = dosage.dose[ iDose ];
var mathProperties = dose.getMathProperties();
// Initialize value properties for dose
value.dose[ iDose ] = {
massPerWeight: {
decimals: 0
},
mass: {
decimals: 0
},
name: dose.name,
volume: {
decimals: 0
},
weightCalculation: dose.weightCalculation ? dose.weightCalculation : null
};
var weightValue = dose.weightCalculation ? dose.weightCalculation.value : data.weight;
for( var iMathProperty in mathProperties ) {
var mathProperty = mathProperties[ iMathProperty ];
var doseValue = dose[ mathProperty ];
if( doseValue ) {
var doseValueDecimals = mw.calculators.getValueDecimals( doseValue );
if( mw.calculators.isValueDependent( doseValue, 'weight' ) ) {
value.dose[ iDose ].massPerWeight[ mathProperty ] = doseValue;
// Set the decimals based upon the specified dose
value.dose[ iDose ].massPerWeight.decimals = math.max(
doseValueDecimals,
value.dose[ iDose ].massPerWeight.decimals
);
// For whatever reason math.format will simplify the units, but math.formatUnits will not
// as a hack, we recreate a new unit value with the correct formatting of the result
value.dose[ iDose ].mass[ mathProperty ] = weightValue ? math.unit( math.multiply( doseValue, weightValue ).format() ) : null;
// Mass should use one fewer decimals than massPerWeight (unless already 0)
value.dose[ iDose ].mass.decimals = math.max(
math.max( doseValueDecimals - 1, 0 ),
value.dose[ iDose ].mass.decimals
);
} else {
value.dose[ iDose ].mass[ mathProperty ] = doseValue;
// Set the decimals based upon the specified dose
value.dose[ iDose ].mass.decimals = math.max(
doseValueDecimals,
value.dose[ iDose ].mass.decimals
);
}
if( data.preparation && value.dose[ iDose ].mass[ mathProperty ] ) {
// Same hack as above to get units to simplify correctly
value.dose[ iDose ].volume[ mathProperty ] = math.unit( math.multiply( value.dose[ iDose ].mass[ mathProperty ], math.divide( 1, data.preparation.concentration ) ).format() );
// value.dose[ iDose ].volume.decimals = 0;
}
}
}
}
return value;
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.doRender = function() {
var $calculationContainer = $( '.' + this.getContainerClass() );
if( !$calculationContainer.length ) {
return;
}
$calculationContainer.empty();
// Label column
var labelAttributes = {};
var labelCss = {};
if( this.drug.color.isStriped() ) {
labelCss[ 'background' ] = 'repeating-linear-gradient(135deg,rgba(0,0,0,0),rgba(0,0,0,0)10px,rgba(255,255,255,1)10px,rgba(255,255,255,1)20px),' + this.drug.color.getPrimaryColor();
} else {
labelCss[ 'background-color'] = this.drug.color.getPrimaryColor();
}
var $label = $( '<th>', labelAttributes ).css( labelCss );
$label.append( this.getLabelHtml() );
var $infoButton = null;
if( this.hasInfo() ) {
$infoButton = $( '<a>', {
'data-toggle': 'collapse',
href: '#' + this.getContainerClass() + '-info',
role: 'button',
'aria-expanded': 'false',
'aria-controls': this.getContainerClass() + '-info'
} )
.append( $( '<i>', {
class: 'far fa-question-circle'
} ) );
$label
.append( $( '<span>', {
class: 'calculator-calculation-column-label-info'
} )
.append( $infoButton )
);
}
// Indication column
var $indication = $( '<td>' );
var indications = this.drug.getIndications();
if( indications.length > 1 ) {
$indication.append( mw.calculators.getVariable( this.getVariableIds().indication ).createInput({
hideLabel: true
} ) );
} else {
$indication.append( String( indications[ 0 ] ) );
}
// Dosage column
var $dosage = $( '<td>' );
var dash = '–';
var preparations = this.drug.getPreparations();
var $preparationSelect;
if( preparations.length > 1 ) {
$preparationSelect = mw.calculators.getVariable( this.getVariableIds().preparation ).createInput({
hideLabel: true,
inline: true,
inputClass: 'calculator-input-preparation'
} );
} else {
$preparationSelect = String( preparations[ 0 ] );
}
if( this.value.population && this.value.population.id !== DEFAULT_DRUG_POPULATION ) {
$dosage.append( String( this.value.population ) + '<br />' );
}
for( var iDose in this.value.dose ) {
var doseValue = this.value.dose[ iDose ];
if( doseValue.name ) {
$dosage.append( doseValue.name + '<br />' );
}
var massPerWeightHtml = '';
if( doseValue.massPerWeight.hasOwnProperty( 'dose' ) ) {
massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.dose, doseValue.massPerWeight.decimals );
} else if( doseValue.massPerWeight.hasOwnProperty( 'min' ) &&
doseValue.massPerWeight.hasOwnProperty( 'max' ) ) {
massPerWeightHtml += mw.calculators.getValueNumber( doseValue.massPerWeight.min, doseValue.massPerWeight.decimals );
massPerWeightHtml += dash;
massPerWeightHtml += mw.calculators.getValueString( doseValue.massPerWeight.max, doseValue.massPerWeight.decimals );
}
if( massPerWeightHtml ) {
if( doseValue.weightCalculation ) {
massPerWeightHtml += ' (' + doseValue.weightCalculation.getLabelString() + ')';
}
$dosage.append( massPerWeightHtml + '<br />' );
}
var massHtml = '';
var massUnits = '';
if( doseValue.mass.hasOwnProperty( 'dose' ) ) {
massHtml += mw.calculators.getValueString( doseValue.mass.dose, doseValue.mass.decimals );
massUnits = mw.calculators.getUnitsString( doseValue.mass.dose );
} else if( doseValue.mass.hasOwnProperty( 'min' ) &&
doseValue.mass.hasOwnProperty( 'max' ) ) {
massHtml += mw.calculators.getValueNumber( doseValue.mass.min, doseValue.mass.decimals );
massHtml += dash;
massHtml += mw.calculators.getValueString( doseValue.mass.max, doseValue.mass.decimals );
massUnits = mw.calculators.getUnitsString( doseValue.mass.min );
}
if( massHtml ) {
$dosage.append( massHtml + '<br />' );
}
var volumeHtml = '';
if( this.value.preparation ) {
// Get the volume units of the preparation
var volumeUnits = this.value.preparation.getVolumeUnits();
if( !$.isEmptyObject( doseValue.mass ) && massUnits ) {
// Extract any remaining additional dependent units (e.g. /min)
var massRemainderUnits = massUnits.replace( /.*\/?/, '' );
if( massRemainderUnits ) {
volumeUnits = volumeUnits + '/' + massRemainderUnits;
}
}
if( doseValue.volume.hasOwnProperty( 'dose' ) ) {
volumeHtml += mw.calculators.getValueString( doseValue.volume.dose.to( volumeUnits ), doseValue.volume.decimals );
} else if( doseValue.volume.hasOwnProperty( 'min' ) &&
doseValue.volume.hasOwnProperty( 'max' ) ) {
volumeHtml += mw.calculators.getValueNumber( doseValue.volume.min.to( volumeUnits ), doseValue.volume.decimals );
volumeHtml += dash;
volumeHtml += mw.calculators.getValueString( doseValue.volume.max.to( volumeUnits ), doseValue.volume.decimals );
}
if( volumeHtml ) {
$dosage.append( volumeHtml + ' of ' );
$dosage.append( $preparationSelect );
$dosage.append( '<br />' );
}
}
}
$calculationContainer
.append(
$label,
$indication,
$dosage
);
return;
var calculation = this;
$calculationContainer.each( function() {
$( this ).empty();
if( calculation.hasInfo() ) {
var infoHtml = '';
if( calculation.description ) {
infoHtml += $( '<p>', {
html: calculation.description
} )[ 0 ].outerHTML;
}
if( calculation.formula ) {
infoHtml += $( '<span>', {
class: calculation.getContainerClass() + '-formula'
} )[ 0 ].outerHTML;
var api = new mw.Api();
api.parse( calculation.formula ).then( function( result ) {
$( '.' + calculation.getContainerClass() + '-formula' ).html( result );
} );
}
if( calculation.references.length ) {
var $references = $( '<ol>' );
for( var iReference in calculation.references ) {
$references.append( $( '<li>', {
text: calculation.references[ iReference ]
} ) );
}
infoHtml += $references[ 0 ].outerHTML;
}
var infoContainerId = calculation.getContainerClass() + '-info';
var $infoContainer = $( '#' + infoContainerId );
if( $infoContainer.length ) {
$infoContainer.empty();
}
$infoContainer = $( '<tr>', {
id: infoContainerId,
class: 'collapse'
} )
.append( $( '<td>', {
colspan: 2
} ).append( infoHtml ) );
$( this ).after( $infoContainer );
}
} );
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationData = function() {
var inputData = new mw.calculators.objectClasses.CalculationData();
// Add variables created by this calculation
var variableIds = this.getVariableIds();
for( var variableType in variableIds ) {
inputData.variables.optional.push( variableIds[ variableType ] );
}
var dataTypes = inputData.getDataTypes();
// Data is only actually required if it is required by every dosage for the drug.
// Data marked as required by an individual dosage that does not appear in every
// dosage will be converted to optional.
var requiredInputData = new mw.calculators.objectClasses.CalculationData();
// Need a way to tell the first iteration of the loop to initialize the required variables to a value that
// is distinct from the empty array (populated across loop using array intersect, so could become [] and shouldn't
// reinitialize).
var initializeRequiredData = true;
// Iterate through each dosage to determine variable dependency
for( var iDosage in this.drug.dosages ) {
var dosageInputData = this.drug.dosages[ iDosage ].getCalculationData();
inputData = inputData.merge( dosageInputData );
for( var iDataType in dataTypes ) {
var dataType = dataTypes[ iDataType ];
if( initializeRequiredData ) {
requiredInputData[ dataType ].required = inputData[ dataType ].required;
} else {
// Data is only truly required if it is required by all dosage calculations, so use array intersection
requiredInputData[ dataType ].required = requiredInputData[ dataType ].required.filter( function( index ) {
return dosageInputData[ dataType ].required.indexOf( index ) !== -1;
} );
}
}
initializeRequiredData = false;
}
for( var iDataType in dataTypes ) {
var dataType = dataTypes[ iDataType ];
// Move any data marked required in inputData to optional if it not actually required (i.e. doesn't appear
// in requiredInputData).
inputData[ dataType ].optional = inputData[ dataType ].optional.concat( inputData[ dataType ].required.filter( function( index ) {
return requiredInputData[ dataType ].required.indexOf( index ) === -1;
} ) ).filter( mw.calculators.uniqueValues );
inputData[ dataType ].required = requiredInputData[ dataType ].required;
}
return inputData;
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationDataValues = function() {
var data = mw.calculators.objectClasses.AbstractCalculation.prototype.getCalculationDataValues.call( this );
data.drug = this.drug;
data.indication = data[ this.getVariablePrefix() + 'indication' ] ?
mw.calculators.getDrugIndication( mw.calculators.getVariable( this.getVariableIds().indication ).value ) :
null;
delete data[ this.getVariablePrefix() + 'indication' ];
data.preparation = data[ this.getVariablePrefix() + 'preparation' ] ?
this.drug.preparations[ mw.calculators.getVariable( this.getVariableIds().preparation ).value ] :
null;
delete data[ this.getVariablePrefix() + 'preparation' ];
return data;
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() {
var labelHtml = this.drug.name;
var $label = $( '<a>', {
href: mw.util.getUrl( this.drug.name ),
text: labelHtml
} );
var highlightColor = this.drug.color.getHighlightColor();
if( highlightColor ) {
$label.css( 'background-color', highlightColor );
}
labelHtml = $label[ 0 ].outerHTML;
return labelHtml;
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getProperties = function() {
var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
return this.mergeProperties( inheritedProperties, {
required: [
'drug'
],
optional: []
} );
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariableIds = function() {
return {
indication: this.getVariablePrefix() + 'indication',
preparation: this.getVariablePrefix() + 'preparation'
};
};
mw.calculators.objectClasses.DrugDosageCalculation.prototype.getVariablePrefix = function() {
return this.drug.id + '-';
}
mw.calculators.objectClasses.DrugDosageCalculation.prototype.initialize = function() {
mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
var drug = mw.calculators.getDrug( this.drug );
if( !drug ) {
throw new Error( 'DrugDosage references drug "' + this.drug + '" which is not defined' );
}
this.drug = drug;
var variableIds = this.getVariableIds();
// Create variables for indication, population?, preparation select boxes? Here or m.c.addDosages() or elsewhere?
// Will have to add them to getCalculationData too.
var drugVariables = {};
var indications = this.drug.getIndications();
var indicationOptions = {};
for( var iIndication in indications ) {
var indication = indications[ iIndication ];
indicationOptions[ indication.id ] = String( indication );
}
drugVariables[ variableIds.indication ] = {
name: 'Indication',
type: 'string',
defaultValue: indications.length ? indications[ 0 ].id : null,
options: indicationOptions
};
var preparations = this.drug.getPreparations();
var preparationOptions = {};
for( var iPreparation in preparations ) {
var preparation = preparations[ iPreparation ];
preparationOptions[ preparation.id ] = String( preparation );
}
drugVariables[ variableIds.preparation ] = {
name: 'Preparation',
type: 'string',
defaultValue: preparations.length ? preparations[ 0 ].id : null,
options: preparationOptions
};
mw.calculators.addVariables( drugVariables );
};
/**
* Class DrugDosageCalculator
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugDosageCalculator}
* @constructor
*/
mw.calculators.objectClasses.DrugDosageCalculator = function( propertyValues ) {
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
};
mw.calculators.objectClasses.DrugDosageCalculator.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculator.prototype );
mw.calculators.objectClasses.DrugDosageCalculator.prototype.doRender = function() {
var $calculatorContainer = $( '.' + this.getContainerClass() );
if( !$calculatorContainer.length ) {
return;
}
$calculatorContainer.addClass( this.getCalculatorClass() );
$calculatorContainer.empty();
$calculatorContainer.append( $( '<h4>', {
text: this.name
} ) );
var $calculationsContainer = $( '<table>', {
class: 'wikitable'
} ).append( '<tbody>' );
$calculatorContainer.append( $calculationsContainer );
$calculationsContainer
.append( $( '<tr>' )
.append(
$( '<th>', {
class: this.getCalculatorClass() + '-drug'
} ).text( 'Drug' ),
$( '<th>', {
class: this.getCalculatorClass() + '-indication'
} ).text( 'Indication' ),
$( '<th>', {
class: this.getCalculatorClass() + '-dose'
} ).text( 'Dose' )
)
);
for( var iCalculationId in this.calculations ) {
var calculation = mw.calculators.getCalculation( this.calculations[ iCalculationId ] );
var calculationContainerClass = calculation.getContainerClass();
var $calculationContainer = $( '<tr>', {
class: calculationContainerClass
} );
$calculationsContainer.append( $calculationContainer );
calculation.render();
}
};
mw.calculators.objectClasses.DrugDosageCalculator.prototype.getCalculatorClass = function() {
return 'calculator-DrugDosageCalculator';
};
mw.calculators.objectClasses.DrugDosageCalculator.prototype.getProperties = function() {
var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties();
return this.mergeProperties( inheritedProperties, {
required: [],
optional: []
} );
};
}() );