MediaWiki:Gadget-calculator-drugs-core.js
From WikiAnesthesia
Revision as of 19:18, 10 August 2021 by Chris Rishel (talk | contribs)
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/** * @author Chris Rishel */ ( function() { var DEFAULT_DRUG_COLOR = 'default'; var DEFAULT_DRUG_POPULATION = 'general'; /** * Define units */ mw.calculators.addUnitsBases( { concentration: { toString: function( units ) { units = units.replace( ' pct', '%' ); return units; } } } ); mw.calculators.addUnits( { mcg: { definition: '1 ug' }, 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' ); } } } 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; }; /** * 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.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; } 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 ); }; mw.calculators.objectClasses.DrugPreparation.prototype = Object.create( mw.calculators.objectClasses.CalculatorObject.prototype ); /** * Class DrugDosage * @param {Object} propertyValues * @returns {mw.calculators.objectClasses.DrugDosage} * @constructor */ mw.calculators.objectClasses.DrugDosage = function( propertyValues ) { var properties = { required: [ 'dose', 'id', 'indication' ], optional: [ 'population' ] }; mw.calculators.objectClasses.CalculatorObject.call( this, properties, 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; 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; }; /** * 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 ]; this[ mathProperty ] = this[ mathProperty ] ? math.unit( 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( dosePropertyValue && dosePropertyValue.formatUnits().match( /\/[\s(]*?kg/ ) && 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', 'min', 'max', 'name', 'route', '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 = { mass: null, volume: null }; var dosageId = 0; for( var iDosage in data.drug.dosages ) { var dosage = data.drug.dosages[ iDosage ]; console.log( dosage ); } console.log( data ); }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.doRender = function() { var $calculationContainer = $( '.' + this.getContainerClass() ); if( !$calculationContainer.length ) { return; } }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.getCalculationData = function() { var inputData = new mw.calculators.objectClasses.CalculationData(); 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; 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.preparation = mw.calculators.getVariable( this.getVariableIds().preparation ).value; return data; }; mw.calculators.objectClasses.DrugDosageCalculation.prototype.getLabelHtml = function() { var labelHtml = this.name; labelHtml = $( '<a>', { href: mw.util.getUrl( this.name ), text: labelHtml } )[ 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() { var variablePrefix = this.drug.id + '-'; return { indication: variablePrefix + 'indication', population: variablePrefix + 'population', preparation: variablePrefix + 'preparation' }; }; 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(); drugVariables[ variableIds.indication ] = { name: 'Indication', type: 'string', defaultValue: indications.length ? indications[ 0 ].id : null, options: indications.map( function( indication ) { return indication.id; } ) }; var populations = this.drug.getPopulations( indications[ 0 ].id ); drugVariables[ variableIds.population ] = { name: 'Population', type: 'string', defaultValue: populations.length ? populations[ 0 ].id : null, options: populations.map( function( population ) { return population.id; } ) }; var preparations = this.drug.getPreparations(); drugVariables[ variableIds.preparation ] = { name: 'Preparation', type: 'string', defaultValue: preparations.length ? preparations[ 0 ].id : null, options: preparations.map( function( preparation ) { return preparation.id; } ) }; 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.empty(); var $calculationsContainer = $( '<table>', { class: 'wikitable' } ).append( '<tbody>' ); $calculatorContainer.append( $calculationsContainer ); 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.getProperties = function() { var inheritedProperties = mw.calculators.objectClasses.AbstractCalculator.prototype.getProperties(); return this.mergeProperties( inheritedProperties, { required: [], optional: [] } ); }; }() );