Difference between revisions of "MediaWiki:Gadget-calculator-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'; | |||
/** | |||
* 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' | |||
} | |||
} ); | |||
/** | |||
* DrugColor | |||
*/ | |||
mw.calculators.drugColors = {}; | |||
mw.calculators.addDrugColors = function( drugColorData ) { | |||
var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData ); | |||
for( var drugColorId in drugColors ) { | |||
mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ]; | |||
} | |||
}; | |||
mw.calculators.getDrugColor = function( drugColorId ) { | |||
if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) { | |||
return mw.calculators.drugColors[ drugColorId ]; | |||
} else { | |||
return null; | |||
} | |||
}; | |||
/** | |||
* Class DrugColor | |||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugColor} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugColor = function( propertyValues ) { | |||
var properties = { | |||
required: [ | |||
'id' | |||
], | |||
optional: [ | |||
'parentColor', | |||
'primaryColor', | |||
'highlightColor', | |||
'striped' | |||
] | |||
}; | |||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
if( !this.primaryColor && !this.parentColor ) { | |||
throw new Error( 'Drug color "' + this.id + '" must define either a primary color or a parent color.' ); | |||
} | |||
}; | |||
mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | |||
mw.calculators.objectClasses.DrugColor.getParentDrugColor = function() { | |||
if( !this.parentColor ) { | |||
return null; | |||
} | |||
var parentDrugColor = mw.calculators.getDrugColor( this.parentColor ); | |||
if( !parentDrugColor ) { | |||
throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' ); | |||
} | |||
} | |||
return parentDrugColor; | |||
}; | |||
mw.calculators.objectClasses.DrugColor.getHighlightColor = function() { | |||
if( this.highlightColor ) { | |||
return this.highlightColor; | |||
} else if( this.parentColor ) { | |||
return this.getParentDrugColor().getHighlightColor(); | |||
} | |||
}; | |||
mw.calculators.objectClasses.DrugColor.getPrimaryColor = function() { | |||
if( this.primaryColor ) { | |||
return this.primaryColor; | |||
} else if( this.parentColor ) { | |||
return this.getParentDrugColor().getPrimaryColor(); | |||
} | |||
}; | |||
mw.calculators.objectClasses.DrugColor.isStriped = function() { | |||
if( this.striped !== null ) { | |||
return this.striped; | |||
} else if( this.parentColor ) { | |||
return this.getParentDrugColor().isStriped(); | |||
} | |||
}; | |||
/** | |||
* DrugPopulation | |||
*/ | |||
mw.calculators.drugPopulations = {}; | |||
mw.calculators.addDrugPopulations = function( drugPopulationData ) { | |||
var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData ); | |||
for( var drugPopulationId in drugPopulations ) { | |||
mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ]; | |||
} | |||
}; | |||
mw.calculators.getDrugPopulation = function( drugPopulationId ) { | |||
if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) { | |||
return mw.calculators.drugPopulations[ drugPopulationId ]; | |||
} else { | |||
return null; | |||
} | |||
} | }; | ||
/** | |||
* Class DrugPopulation | |||
} | * @param {Object} propertyValues | ||
* @returns {mw.calculators.objectClasses.DrugPopulation} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) { | |||
var properties = { | |||
required: [ | |||
'id', | |||
'name' | |||
], | |||
optional: [ | |||
'abbreviation', | |||
'variables' | |||
] | |||
}; | |||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
if( this.variables ) { | |||
for( var variableId in this.variables ) { | |||
if( !mw.calculators.getVariable( variableId ) ) { | |||
throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' ); | |||
for( var | |||
if( !mw.calculators. | |||
throw new Error( ' | |||
} | } | ||
} | } | ||
} | |||
}; | |||
mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | |||
/** | /** | ||
* | * DrugIndication | ||
*/ | */ | ||
mw.calculators. | 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; | |||
} | |||
return | |||
} | |||
}; | }; | ||
/** | /** | ||
* 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 344: | Line 221: | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
| Line 350: | Line 228: | ||
/** | /** | ||
* | * 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.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 401: | Line 269: | ||
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; | |||
} | } | ||
this. | this.dosages = {}; | ||
this.preparations = {}; | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
/** | |||
* DrugPreparation | |||
*/ | |||
mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) { | |||
if( !mw.calculators.getDrug( drugId ) ) { | |||
throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' ); | |||
} | } | ||
for( var drugPreparationId in drugPreparationData ) { | |||
drugPreparationData[ drugPreparationId ].drug = drugId; | |||
} | } | ||
var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData ); | |||
for( var drugPreparationId in drugPreparations ) { | |||
mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ]; | |||
} | } | ||
}; | }; | ||
| Line 640: | Line 305: | ||
/** | /** | ||
* Class | * Class DrugPreparation | ||
* @param {Object} propertyValues | * @param {Object} propertyValues | ||
* @returns {mw.calculators.objectClasses. | * @returns {mw.calculators.objectClasses.DrugPreparation} | ||
* @constructor | * @constructor | ||
*/ | */ | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) { | ||
var properties = { | |||
required: [ | required: [ | ||
'drug', | |||
'id', | 'id', | ||
' | 'concentration' | ||
], | ], | ||
optional: [ | optional: [ | ||
' | 'dilutionRequired', | ||
' | 'commonDilution' | ||
] | ] | ||
}; | }; | ||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
/** | |||
* DrugDosage | |||
*/ | |||
mw.calculators.addDrugDosages = function( drugId, drugDosageData ) { | |||
if( !mw.calculators.getDrug( drugId ) ) { | |||
throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' ); | |||
} | } | ||
for( var | for( var drugDosageId in drugDosageData ) { | ||
drugDosageData[ drugDosageId ].drug = drugId; | |||
} | } | ||
var drugDosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData ); | |||
for( var drugDosageId in drugDosages ) { | |||
mw.calculators.drugs[ drugId ].dosages[ drugDosageId ] = drugDosages[ drugDosageId ]; | |||
} | } | ||
}; | |||
/** | |||
} | * Class DrugDosage | ||
* @param {Object} propertyValues | |||
* @returns {mw.calculators.objectClasses.DrugDosage} | |||
* @constructor | |||
*/ | |||
mw.calculators.objectClasses.DrugDosage = function( propertyValues ) { | |||
var properties = { | |||
required: [ | |||
'dose', | |||
'drug', | |||
'id', | |||
'indication' | |||
], | |||
optional: [ | |||
'population' | |||
] | |||
}; | |||
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues ); | |||
if( !this.population ) { | |||
this.population = DEFAULT_DRUG_POPULATION; | |||
} | } | ||
mw.calculators.addDrugDoseCalculations( this.drug, this.id, this.dose ); | |||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); | ||
/** | |||
* DrugDosageCalculation | |||
*/ | |||
mw.calculators.addDrugDoseCalculations = function( drugId, drugDosageId, drugDoseCalculationData ) { | |||
if( !mw.calculators.getDrug( drugId ) ) { | |||
throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' ); | |||
if( ! | |||
} | } | ||
var doseProperties = [ | |||
'dose', | |||
'min', | |||
'max', | |||
'absoluteMin', | |||
'absoluteMax' | |||
]; | |||
var calculationData = {}; | |||
for( var doseId in drugDoseCalculationData ) { | |||
drugDoseCalculationData[ doseId ].drug = drugId; | |||
drugDoseCalculationData[ doseId ].calculate = mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate; | |||
var data = { | |||
calculations: { | |||
required: [], | |||
optional: [] | |||
}, | |||
variables: { | |||
required: [], | |||
optional: [] | |||
} | |||
}; | |||
// Look at dose properties to identify any variable dependence (e.g. weight-dependence) | |||
for( var iDoseProperty in doseProperties ) { | |||
var dosePropertyValue = drugDoseCalculationData[ doseId ][ doseProperties[ iDoseProperty ] ]; | |||
// For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future | |||
if( dosePropertyValue && | |||
dosePropertyValue.match( /\/\s*?kg/ ) && | |||
data.variables.required.indexOf( 'weight' ) === -1 ) { | |||
data.variables.required.push( 'weight' ); | |||
} | } | ||
} | } | ||
if( drugDoseCalculationData[ doseId ].hasOwnProperty( 'weightCalculation' ) ) { | |||
var weightCalculationId = drugDoseCalculationData[ doseId ].weightCalculation; | |||
var weightCalculation = mw.calculators.getCalculation( weightCalculationId ); | |||
if( | if( !weightCalculation ) { | ||
throw new Error( 'Drug "' + drugId + '" dose ' + drugDosageId + '-' + doseId + ': weightCalculation "' + weightCalculationId + '" which is not defined' ); | |||
} | } | ||
data.calculations.optional.push( weightCalculationId ); | |||
data.variables.optional = data.variables.optional.concat( weightCalculation.data.variables.required.concat( weightCalculation.data.variables.optional ) ); | |||
} | } | ||
drugDoseCalculationData[ doseId ].data = data; | |||
calculationData[ drugId + '-' + drugDosageId + '-' + doseId ] = drugDoseCalculationData[ doseId ]; | |||
} | } | ||
mw.calculators.addCalculations( calculationData, 'DrugDoseCalculation' ); | |||
}; | }; | ||
/** | /** | ||
* Class | * Class DrugDoseCalculation | ||
* @param {Object} propertyValues | * @param {Object} propertyValues | ||
* @returns {mw.calculators.objectClasses. | * @returns {mw.calculators.objectClasses.DrugDoseCalculation} | ||
* @constructor | * @constructor | ||
*/ | */ | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDoseCalculation = function( propertyValues ) { | ||
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues ); | ||
| Line 890: | Line 463: | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDoseCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype ); | ||
mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate = function() { | |||
}; | }; | ||
mw.calculators.objectClasses.DrugDoseCalculation.prototype.getLabelHtml = function() { | |||
var labelHtml = this.name; | |||
labelHtml = $( '<a>', { | |||
href: mw.util.getUrl( this.name ), | |||
text: labelHtml | |||
} )[ 0 ].outerHTML; | |||
return labelHtml; | return labelHtml; | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDoseCalculation.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' | ||
], | ], | ||
optional: [ | optional: [ | ||
' | 'absoluteMin', | ||
' | 'absoluteMax', | ||
' | 'dose', | ||
' | 'min', | ||
' | 'max', | ||
'route', | |||
'weightCalculation' | |||
] | ] | ||
} ); | } ); | ||
}; | }; | ||
mw.calculators.objectClasses. | mw.calculators.objectClasses.DrugDoseCalculation.prototype.initialize = function() { | ||
mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this ); | |||
this.route = this.route ? this.route : 'IV'; | |||
}; | |||
/******* | |||
* BEGIN DRUG DATA | |||
*******/ | |||
/** | |||
* DrugColor data | |||
*/ | |||
mw.calculators.addDrugColors( { | |||
anticholinergic: { | |||
primaryColor: '#00ac8c' | |||
}, | |||
benzodiazepine: { | |||
primaryColor: '#ff6c2f' | |||
}, | |||
benzodiazepineReversal: { | |||
parentColor: 'benzodiazepine', | |||
striped: true | |||
}, | |||
cardiovascularAgonist: { | |||
primaryColor: '#ba93df' | |||
}, | |||
cardiovascularAntagonist: { | |||
parentColor: 'cardiovascularAgonist', | |||
striped: true | |||
}, | |||
default: { | |||
primaryColor: '#fff' | |||
}, | |||
desflurane: { | |||
primaryColor: '#0ab8fd' | |||
}, | |||
enflurane: { | |||
primaryColor: '#f58733' | |||
}, | |||
epinephrine: { | |||
parentColor: 'cardiovascularAntagonist', | |||
highlightColor: '#000' | |||
}, | |||
halothane: { | |||
primaryColor: '#b20107' | |||
}, | |||
isoflurane: { | |||
primaryColor: '#ca7fc0' | |||
}, | |||
localAnesthetic: { | |||
primaryColor: '#dad9d6' | |||
}, | |||
neuromuscularBlocker: { | |||
primaryColor: '#fe5442' | |||
}, | |||
neuromuscularBlockerReversal: { | |||
parentColor: 'neuromuscularBlocker', | |||
striped: true | |||
}, | |||
nitrousOxide: { | |||
primaryColor: '#2d549f' | |||
}, | |||
opioid: { | |||
primaryColor: '#6cd1ef' | |||
}, | |||
opioidReversal: { | |||
parentColor: 'opioid', | |||
striped: true | |||
}, | |||
sedativeHypnotic: { | |||
primaryColor: '#ffe800' | |||
}, | |||
sevoflurane: { | |||
primaryColor: '#f8da00' | |||
}, | |||
succinylcholine: { | |||
parentColor: 'neuromuscularBlocker', | |||
highlightColor: '#000' | |||
} | |||
} ); | |||
/** | |||
* DrugPopulation data | |||
*/ | |||
mw.calculators.addDrugPopulations( { | |||
general: { | |||
name: 'General', | |||
abbreviation: 'Gen.' | |||
}, | |||
neonatal: { | |||
name: 'Neonatal', | |||
abbreviation: 'Neo.', | |||
variables: { | |||
age: { | |||
max: '0 yo' | |||
} | } | ||
} | } | ||
}, | |||
pediatric: { | |||
name: 'Pediatric', | |||
abbreviation: 'Ped.', | |||
variables: { | |||
age: { | |||
min: '0 yo', | |||
max: '17.9 yo' | |||
} | } | ||
} | |||
}, | |||
elderly: { | |||
name: 'Elderly', | |||
abbreviation: 'Eld.', | |||
variables: { | |||
age: { | |||
min: '65 yo' | |||
} | } | ||
} | } | ||
} | |||
} ); | |||
/** | |||
* DrugIndication data | |||
*/ | |||
mw.calculators.addDrugIndications( { | |||
} | generalAnesthesia: { | ||
}; | name: 'General anesthesia', | ||
abbreviation: 'GA' | |||
} | |||
} ); | |||
/** | |||
* Drug data | |||
*/ | |||
/** | /** | ||
* | * Cefazolin | ||
*/ | */ | ||
mw.calculators. | mw.calculators.addDrugs( { | ||
cefazolin: { | |||
}; | name: 'Cefazolin' | ||
} | |||
} ); | |||
/** | |||
* Ketamine | |||
*/ | |||
mw.calculators.addDrugs( { | |||
mw.calculators. | ketamine: { | ||
name: 'Ketamine', | |||
color: 'sedativeHypnotic' | |||
} | |||
} ); | |||
} | |||
}; | |||
mw.calculators. | mw.calculators.addDrugPreparations( 'ketamine', [ | ||
{ | |||
concentration: '10 mg/mL' | |||
}, { | |||
concentration: '50 mg/mL' | |||
}, { | |||
concentration: '100 mg/mL' | |||
} | } | ||
] ); | |||
/** | |||
* Lidocaine | |||
*/ | |||
mw.calculators.addDrugs( { | |||
lidocaine: { | |||
name: 'Lidocaine', | |||
color: 'localAnesthetic' | |||
} | } | ||
} | } ); | ||
mw.calculators.addDrugPreparations( 'lidocaine', [ | |||
{ | |||
concentration: '1 pct' | |||
}, { | |||
concentration: '2 pct' | |||
} | |||
] ); | |||
/** | /** | ||
* | * Propofol | ||
*/ | */ | ||
mw.calculators. | mw.calculators.addDrugs( { | ||
propofol: { | |||
}; | name: 'Propofol', | ||
color: 'sedativeHypnotic' | |||
} | |||
} ); | |||
mw.calculators. | mw.calculators.addDrugPreparations( 'propofol', [ | ||
{ | |||
concentration: '10 mg/mL' | |||
} | |||
] ); | |||
mw.calculators.addDrugDosages( 'propofol', [ | |||
mw.calculators. | { | ||
indication: 'generalAnesthesia', | |||
population: 'general', | |||
dose: [ | |||
{ | |||
name: 'Induction', | |||
' | min: '1 mg/kg', | ||
max: '2.5 mg/kg', | |||
weightCalculation: 'lbw' | |||
}, { | |||
name: 'Maintenance', | |||
min: '100 mcg/kg/min', | |||
max: '200 mcg/kg/min', | |||
route: 'IV' | |||
} | |||
] | |||
}, { | |||
indication: 'generalAnesthesia', | |||
population: 'pediatric', | |||
dose: [ | |||
{ | |||
name: 'Induction', | |||
min: '2.5 mg/kg', | |||
max: '3.5 mg/kg', | |||
weightCalculation: 'lbw' | |||
}, { | |||
name: 'Maintenance', | |||
min: '125 mcg/kg/min', | |||
max: '300 mcg/kg/min' | |||
} | |||
] | |||
}, { | |||
indication: 'generalAnesthesia', | |||
population: 'elderly', | |||
dose: [ | |||
{ | |||
name: 'Induction', | |||
min: '1 mg/kg', | |||
max: '1.5 mg/kg', | |||
weightCalculation: 'lbw' | |||
}, { | |||
name: 'Maintenance', | |||
min: '50 mcg/kg/min', | |||
max: '100 mcg/kg/min' | |||
} | |||
] | |||
}, { | |||
indication: 'mac', | |||
population: 'general', | |||
dose: [ | |||
{ | |||
min: '25 mcg/kg/min', | |||
max: '75 mcg/kg/min' | |||
} | |||
] | ] | ||
} | } | ||
] ); | |||
/** | |||
* DrugDosage | |||
* | |||
* Structure is: | |||
* | |||
* drugId: { | |||
* indicationId: { | |||
* doseId: { | |||
* | |||
* } | |||
* } | |||
* } | |||
*/ | |||
var drugDosages = { | |||
cefazolin: { | |||
abxProphylaxis: { | |||
general: { | |||
population: 'general', | |||
dose: '2 g' | |||
}, | |||
general120kg: { | |||
population: { | |||
id: 'general', | |||
variables: { | |||
weight: { | |||
min: '120 kg' | |||
} | |||
} | |||
}, | |||
dose: '3 g' | |||
}, | |||
pediatric: { | |||
dose: { | |||
absoluteMax: '2 g', | |||
dose: '30 mg/kg' | |||
} | |||
}, | |||
pediatric120kg: { | |||
} | population: { | ||
id: 'pediatric', | |||
variables: { | |||
weight: { | |||
min: '120 kg' | |||
} | |||
} | |||
}, | |||
dose: { | |||
dose: '3 g' | |||
} | |||
} | } | ||
} | } | ||
} | } | ||
}; | }; | ||
}() ); | }() ); | ||
Revision as of 13:47, 9 August 2021
/**
* @author Chris Rishel
*/
( function() {
var DEFAULT_DRUG_COLOR = 'default';
var DEFAULT_DRUG_POPULATION = 'general';
/**
* Define units
*/
mw.calculators.addUnitsBases( {
concentration: {
toString: function( units ) {
units = units.replace( ' pct', '%' );
return units;
}
}
} );
mw.calculators.addUnits( {
pct: {
baseName: 'concentration',
definition: '10 mg/mL'
}
} );
/**
* DrugColor
*/
mw.calculators.drugColors = {};
mw.calculators.addDrugColors = function( drugColorData ) {
var drugColors = mw.calculators.createCalculatorObjects( 'DrugColor', drugColorData );
for( var drugColorId in drugColors ) {
mw.calculators.drugColors[ drugColorId ] = drugColors[ drugColorId ];
}
};
mw.calculators.getDrugColor = function( drugColorId ) {
if( mw.calculators.drugColors.hasOwnProperty( drugColorId ) ) {
return mw.calculators.drugColors[ drugColorId ];
} else {
return null;
}
};
/**
* Class DrugColor
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugColor}
* @constructor
*/
mw.calculators.objectClasses.DrugColor = function( propertyValues ) {
var properties = {
required: [
'id'
],
optional: [
'parentColor',
'primaryColor',
'highlightColor',
'striped'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
if( !this.primaryColor && !this.parentColor ) {
throw new Error( 'Drug color "' + this.id + '" must define either a primary color or a parent color.' );
}
};
mw.calculators.objectClasses.DrugColor.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
mw.calculators.objectClasses.DrugColor.getParentDrugColor = function() {
if( !this.parentColor ) {
return null;
}
var parentDrugColor = mw.calculators.getDrugColor( this.parentColor );
if( !parentDrugColor ) {
throw new Error( 'Parent drug color "' + this.parentColor + '" not found for drug color "' + this.id + '"' );
}
return parentDrugColor;
};
mw.calculators.objectClasses.DrugColor.getHighlightColor = function() {
if( this.highlightColor ) {
return this.highlightColor;
} else if( this.parentColor ) {
return this.getParentDrugColor().getHighlightColor();
}
};
mw.calculators.objectClasses.DrugColor.getPrimaryColor = function() {
if( this.primaryColor ) {
return this.primaryColor;
} else if( this.parentColor ) {
return this.getParentDrugColor().getPrimaryColor();
}
};
mw.calculators.objectClasses.DrugColor.isStriped = function() {
if( this.striped !== null ) {
return this.striped;
} else if( this.parentColor ) {
return this.getParentDrugColor().isStriped();
}
};
/**
* DrugPopulation
*/
mw.calculators.drugPopulations = {};
mw.calculators.addDrugPopulations = function( drugPopulationData ) {
var drugPopulations = mw.calculators.createCalculatorObjects( 'DrugPopulation', drugPopulationData );
for( var drugPopulationId in drugPopulations ) {
mw.calculators.drugPopulations[ drugPopulationId ] = drugPopulations[ drugPopulationId ];
}
};
mw.calculators.getDrugPopulation = function( drugPopulationId ) {
if( mw.calculators.drugPopulations.hasOwnProperty( drugPopulationId ) ) {
return mw.calculators.drugPopulations[ drugPopulationId ];
} else {
return null;
}
};
/**
* Class DrugPopulation
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugPopulation}
* @constructor
*/
mw.calculators.objectClasses.DrugPopulation = function( propertyValues ) {
var properties = {
required: [
'id',
'name'
],
optional: [
'abbreviation',
'variables'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
if( this.variables ) {
for( var variableId in this.variables ) {
if( !mw.calculators.getVariable( variableId ) ) {
throw new Error( 'DrugPopulation variable "' + variableId + '" not defined' );
}
}
}
};
mw.calculators.objectClasses.DrugPopulation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
/**
* DrugIndication
*/
mw.calculators.drugIndications = {};
mw.calculators.addDrugIndications = function( drugIndicationData ) {
var drugIndications = mw.calculators.createCalculatorObjects( 'DrugIndication', drugIndicationData );
for( var drugIndicationId in drugIndications ) {
mw.calculators.drugIndications[ drugIndicationId ] = drugIndications[ drugIndicationId ];
}
};
mw.calculators.getDrugIndication = function( drugIndicationId ) {
if( mw.calculators.drugIndications.hasOwnProperty( drugIndicationId ) ) {
return mw.calculators.drugIndications[ drugIndicationId ];
} else {
return null;
}
};
/**
* Class DrugIndication
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugIndication}
* @constructor
*/
mw.calculators.objectClasses.DrugIndication = function( propertyValues ) {
var properties = {
required: [
'id',
'name'
],
optional: [
'abbreviation'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
};
mw.calculators.objectClasses.DrugIndication.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
/**
* Drug
*/
mw.calculators.drugs = {};
mw.calculators.addDrugs = function( drugData ) {
var drugs = mw.calculators.createCalculatorObjects( 'Drug', drugData );
for( var drugId in drugs ) {
mw.calculators.drugs[ drugId ] = drugs[ drugId ];
}
};
mw.calculators.getDrug = function( drugId ) {
if( mw.calculators.drugs.hasOwnProperty( drugId ) ) {
return mw.calculators.drugs[ drugId ];
} else {
return null;
}
};
/**
* Class Drug
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.Drug}
* @constructor
*/
mw.calculators.objectClasses.Drug = function( propertyValues ) {
var properties = {
required: [
'id',
'name'
],
optional: [
'color'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
if( !this.color ) {
this.color = DEFAULT_DRUG_COLOR;
}
this.dosages = {};
this.preparations = {};
};
mw.calculators.objectClasses.Drug.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
/**
* DrugPreparation
*/
mw.calculators.addDrugPreparations = function( drugId, drugPreparationData ) {
if( !mw.calculators.getDrug( drugId ) ) {
throw new Error( 'DrugPreparation references drug "' + drugId + '" which is not defined' );
}
for( var drugPreparationId in drugPreparationData ) {
drugPreparationData[ drugPreparationId ].drug = drugId;
}
var drugPreparations = mw.calculators.createCalculatorObjects( 'DrugPreparation', drugPreparationData );
for( var drugPreparationId in drugPreparations ) {
mw.calculators.drugs[ drugId ].preparations[ drugPreparationId ] = drugPreparations[ drugPreparationId ];
}
};
/**
* Class DrugPreparation
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugPreparation}
* @constructor
*/
mw.calculators.objectClasses.DrugPreparation = function( propertyValues ) {
var properties = {
required: [
'drug',
'id',
'concentration'
],
optional: [
'dilutionRequired',
'commonDilution'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
};
mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
/**
* DrugDosage
*/
mw.calculators.addDrugDosages = function( drugId, drugDosageData ) {
if( !mw.calculators.getDrug( drugId ) ) {
throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
}
for( var drugDosageId in drugDosageData ) {
drugDosageData[ drugDosageId ].drug = drugId;
}
var drugDosages = mw.calculators.createCalculatorObjects( 'DrugDosage', drugDosageData );
for( var drugDosageId in drugDosages ) {
mw.calculators.drugs[ drugId ].dosages[ drugDosageId ] = drugDosages[ drugDosageId ];
}
};
/**
* Class DrugDosage
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugDosage}
* @constructor
*/
mw.calculators.objectClasses.DrugDosage = function( propertyValues ) {
var properties = {
required: [
'dose',
'drug',
'id',
'indication'
],
optional: [
'population'
]
};
mw.calculators.objectClasses.CalculatorObject.call( this, properties, propertyValues );
if( !this.population ) {
this.population = DEFAULT_DRUG_POPULATION;
}
mw.calculators.addDrugDoseCalculations( this.drug, this.id, this.dose );
};
mw.calculators.objectClasses.DrugDosage.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype );
/**
* DrugDosageCalculation
*/
mw.calculators.addDrugDoseCalculations = function( drugId, drugDosageId, drugDoseCalculationData ) {
if( !mw.calculators.getDrug( drugId ) ) {
throw new Error( 'DrugDosase references drug "' + drugId + '" which is not defined' );
}
var doseProperties = [
'dose',
'min',
'max',
'absoluteMin',
'absoluteMax'
];
var calculationData = {};
for( var doseId in drugDoseCalculationData ) {
drugDoseCalculationData[ doseId ].drug = drugId;
drugDoseCalculationData[ doseId ].calculate = mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate;
var data = {
calculations: {
required: [],
optional: []
},
variables: {
required: [],
optional: []
}
};
// Look at dose properties to identify any variable dependence (e.g. weight-dependence)
for( var iDoseProperty in doseProperties ) {
var dosePropertyValue = drugDoseCalculationData[ doseId ][ doseProperties[ iDoseProperty ] ];
// For now, this only supports weight dependence, unclear if it will need to be more generalizable in the future
if( dosePropertyValue &&
dosePropertyValue.match( /\/\s*?kg/ ) &&
data.variables.required.indexOf( 'weight' ) === -1 ) {
data.variables.required.push( 'weight' );
}
}
if( drugDoseCalculationData[ doseId ].hasOwnProperty( 'weightCalculation' ) ) {
var weightCalculationId = drugDoseCalculationData[ doseId ].weightCalculation;
var weightCalculation = mw.calculators.getCalculation( weightCalculationId );
if( !weightCalculation ) {
throw new Error( 'Drug "' + drugId + '" dose ' + drugDosageId + '-' + doseId + ': weightCalculation "' + weightCalculationId + '" which is not defined' );
}
data.calculations.optional.push( weightCalculationId );
data.variables.optional = data.variables.optional.concat( weightCalculation.data.variables.required.concat( weightCalculation.data.variables.optional ) );
}
drugDoseCalculationData[ doseId ].data = data;
calculationData[ drugId + '-' + drugDosageId + '-' + doseId ] = drugDoseCalculationData[ doseId ];
}
mw.calculators.addCalculations( calculationData, 'DrugDoseCalculation' );
};
/**
* Class DrugDoseCalculation
* @param {Object} propertyValues
* @returns {mw.calculators.objectClasses.DrugDoseCalculation}
* @constructor
*/
mw.calculators.objectClasses.DrugDoseCalculation = function( propertyValues ) {
mw.calculators.objectClasses.CalculatorObject.call( this, this.getProperties(), propertyValues );
this.initialize();
};
mw.calculators.objectClasses.DrugDoseCalculation.prototype = Object.create( mw.calculators.objectClasses.AbstractCalculation.prototype );
mw.calculators.objectClasses.DrugDoseCalculation.prototype.calculate = function() {
};
mw.calculators.objectClasses.DrugDoseCalculation.prototype.getLabelHtml = function() {
var labelHtml = this.name;
labelHtml = $( '<a>', {
href: mw.util.getUrl( this.name ),
text: labelHtml
} )[ 0 ].outerHTML;
return labelHtml;
};
mw.calculators.objectClasses.DrugDoseCalculation.prototype.getProperties = function() {
var inheritedProperties = mw.calculators.objectClasses.AbstractCalculation.prototype.getProperties();
return this.mergeProperties( inheritedProperties, {
required: [
'drug'
],
optional: [
'absoluteMin',
'absoluteMax',
'dose',
'min',
'max',
'route',
'weightCalculation'
]
} );
};
mw.calculators.objectClasses.DrugDoseCalculation.prototype.initialize = function() {
mw.calculators.objectClasses.AbstractCalculation.prototype.initialize.call( this );
this.route = this.route ? this.route : 'IV';
};
/*******
* BEGIN DRUG DATA
*******/
/**
* DrugColor data
*/
mw.calculators.addDrugColors( {
anticholinergic: {
primaryColor: '#00ac8c'
},
benzodiazepine: {
primaryColor: '#ff6c2f'
},
benzodiazepineReversal: {
parentColor: 'benzodiazepine',
striped: true
},
cardiovascularAgonist: {
primaryColor: '#ba93df'
},
cardiovascularAntagonist: {
parentColor: 'cardiovascularAgonist',
striped: true
},
default: {
primaryColor: '#fff'
},
desflurane: {
primaryColor: '#0ab8fd'
},
enflurane: {
primaryColor: '#f58733'
},
epinephrine: {
parentColor: 'cardiovascularAntagonist',
highlightColor: '#000'
},
halothane: {
primaryColor: '#b20107'
},
isoflurane: {
primaryColor: '#ca7fc0'
},
localAnesthetic: {
primaryColor: '#dad9d6'
},
neuromuscularBlocker: {
primaryColor: '#fe5442'
},
neuromuscularBlockerReversal: {
parentColor: 'neuromuscularBlocker',
striped: true
},
nitrousOxide: {
primaryColor: '#2d549f'
},
opioid: {
primaryColor: '#6cd1ef'
},
opioidReversal: {
parentColor: 'opioid',
striped: true
},
sedativeHypnotic: {
primaryColor: '#ffe800'
},
sevoflurane: {
primaryColor: '#f8da00'
},
succinylcholine: {
parentColor: 'neuromuscularBlocker',
highlightColor: '#000'
}
} );
/**
* DrugPopulation data
*/
mw.calculators.addDrugPopulations( {
general: {
name: 'General',
abbreviation: 'Gen.'
},
neonatal: {
name: 'Neonatal',
abbreviation: 'Neo.',
variables: {
age: {
max: '0 yo'
}
}
},
pediatric: {
name: 'Pediatric',
abbreviation: 'Ped.',
variables: {
age: {
min: '0 yo',
max: '17.9 yo'
}
}
},
elderly: {
name: 'Elderly',
abbreviation: 'Eld.',
variables: {
age: {
min: '65 yo'
}
}
}
} );
/**
* DrugIndication data
*/
mw.calculators.addDrugIndications( {
generalAnesthesia: {
name: 'General anesthesia',
abbreviation: 'GA'
}
} );
/**
* Drug data
*/
/**
* Cefazolin
*/
mw.calculators.addDrugs( {
cefazolin: {
name: 'Cefazolin'
}
} );
/**
* Ketamine
*/
mw.calculators.addDrugs( {
ketamine: {
name: 'Ketamine',
color: 'sedativeHypnotic'
}
} );
mw.calculators.addDrugPreparations( 'ketamine', [
{
concentration: '10 mg/mL'
}, {
concentration: '50 mg/mL'
}, {
concentration: '100 mg/mL'
}
] );
/**
* Lidocaine
*/
mw.calculators.addDrugs( {
lidocaine: {
name: 'Lidocaine',
color: 'localAnesthetic'
}
} );
mw.calculators.addDrugPreparations( 'lidocaine', [
{
concentration: '1 pct'
}, {
concentration: '2 pct'
}
] );
/**
* Propofol
*/
mw.calculators.addDrugs( {
propofol: {
name: 'Propofol',
color: 'sedativeHypnotic'
}
} );
mw.calculators.addDrugPreparations( 'propofol', [
{
concentration: '10 mg/mL'
}
] );
mw.calculators.addDrugDosages( 'propofol', [
{
indication: 'generalAnesthesia',
population: 'general',
dose: [
{
name: 'Induction',
min: '1 mg/kg',
max: '2.5 mg/kg',
weightCalculation: 'lbw'
}, {
name: 'Maintenance',
min: '100 mcg/kg/min',
max: '200 mcg/kg/min',
route: 'IV'
}
]
}, {
indication: 'generalAnesthesia',
population: 'pediatric',
dose: [
{
name: 'Induction',
min: '2.5 mg/kg',
max: '3.5 mg/kg',
weightCalculation: 'lbw'
}, {
name: 'Maintenance',
min: '125 mcg/kg/min',
max: '300 mcg/kg/min'
}
]
}, {
indication: 'generalAnesthesia',
population: 'elderly',
dose: [
{
name: 'Induction',
min: '1 mg/kg',
max: '1.5 mg/kg',
weightCalculation: 'lbw'
}, {
name: 'Maintenance',
min: '50 mcg/kg/min',
max: '100 mcg/kg/min'
}
]
}, {
indication: 'mac',
population: 'general',
dose: [
{
min: '25 mcg/kg/min',
max: '75 mcg/kg/min'
}
]
}
] );
/**
* DrugDosage
*
* Structure is:
*
* drugId: {
* indicationId: {
* doseId: {
*
* }
* }
* }
*/
var drugDosages = {
cefazolin: {
abxProphylaxis: {
general: {
population: 'general',
dose: '2 g'
},
general120kg: {
population: {
id: 'general',
variables: {
weight: {
min: '120 kg'
}
}
},
dose: '3 g'
},
pediatric: {
dose: {
absoluteMax: '2 g',
dose: '30 mg/kg'
}
},
pediatric120kg: {
population: {
id: 'pediatric',
variables: {
weight: {
min: '120 kg'
}
}
},
dose: {
dose: '3 g'
}
}
}
}
};
}() );