MediaWiki:Gadget-dosage-calculator.js

De Wikimedica

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

/* This modal dialog displays a tool for drug dosage calculations. */

console.log("Loading dosage calculator ...");

/*
	@param string dose (ex: 90, 1,5)
	@param string unit (ex: mg, mcg)
	@param string frequency (ex: DIE, BID, q12h)
*/
drugKgDosageCalculator = function(dose, unit, frequency) 
{
	// Lazy load the following librairies
	$.when( mw.loader.using( [ 'mediawiki.api', 'oojs-ui-core', 'oojs-ui-windows' ] )).then( function() {
		
		var dialog = new OO.ui.MessageDialog();
		
		// Create and append a window manager.
		var windowManager = new OO.ui.WindowManager();
		$( 'body' ).append( windowManager.$element );
		
		// Add the dialog to the window manager.
		windowManager.addWindows( [ dialog ] );
		
		dialog.getActionProcess = function(action) // Set the cookie and close the dialog when the user clicks the button.
		{ 
			document.cookie = "wikimedica-skip-welcome-message=1; expires=Thu, 18 Dec 2030 12:00:00 UTC; path=/"; 
			this.close();
			return OO.ui.MessageDialog.super.prototype.getActionProcess.call( this, action );
		}
		
		// Configure the message dialog when it is opened with the window manager's openWindow() method.
		windowManager.openWindow( dialog, {
		  title: new OO.ui.HtmlSnippet('Calculateur de dose'),
		  message: new OO.ui.HtmlSnippet('Amoxiciline 90 mg/kg/j'),
		  verbose: true,
		  size: 'medium',
		  actions: [
		    {
		      action: 'accept',
		      label: "Fermer le calculateur",
		      flags: 'primary'
		    }
		  ]
		});
	});
};















/********************************** TEST **********************************/
let frequency_to_division = {'DIE': 1, 'BID': 2, 'TID': 3, 'QID': 4, 'q 2h': 12, 'q 3h': 8, 'q 4h': 6, 'q 5h': 4.8, 'q 6h': 4, 'q 8h': 3, 'q 10h': 2.4, 'q 12h': 2, 'q 24h': 1};

var drug = 'Amoxiciline';
var dose_per_kg = 90;
var max_daily_dose = 3000;
var dose_unit = 'mg';
var roa = 'PO';
var frequency = 'BID';
var prn = '';
var duration = 7;
var duration_unit = 'jours';


// Subclass DosageDialog.
function DosageDialog( config ) {
	DosageDialog.super.call( this, config );
}
OO.inheritClass( DosageDialog, OO.ui.ProcessDialog );

// Specify a name for .addWindows()
DosageDialog.static.name = 'dosageDialog';
// Specify a static title and actions.
DosageDialog.static.title = 'Calculateur de dose';
DosageDialog.static.actions = [
	{
		action: 'save',
		label: 'Fermer',
		flags: 'primary'
	}
];

// Initialize main panel widgets and set up event handlers.
DosageDialog.prototype.initialize = function () {
	DosageDialog.super.prototype.initialize.call( this );
	this.panel = new OO.ui.PanelLayout( { 
		padded: true, 
		expanded: true 
	} );
	
	// Initialize Horizontal Layout
	this.horizontalLayout = new OO.ui.HorizontalLayout( {
	} );
	
	// Initialize Weight Input Field Set
	this.weightInputFieldset = new OO.ui.FieldsetLayout();
	// Initialize Weight Input
	this.weightInput = new OO.ui.NumberInputWidget({
		min: 0,
		max: 500,
		step: 0.001,
		buttonStep: 1,
		placeholder: 'Poids',
		icon: 'userAvatar'
	});
	// Set Weight Input Field
	this.weightInputField = new OO.ui.FieldLayout( this.weightInput, { 
		label: drug + ' ' + dose_per_kg.toString() + ' ' + dose_unit + '/kg/j', 
		align: 'left',
	} );
	// Add Weight Input Field to Weight Input Field Set
	this.weightInputFieldset.addItems( [ this.weightInputField ] );
	
	// Initialize Unit Radio Input
	this.unitRadioInput = new OO.ui.RadioSelectInputWidget( {
	    options: [
	        { data: 'kg', label: 'Kg' },
	        { data: 'lbs', label: 'Lbs'}
	    ]
	} );
	
	// Set Elements to Horizontal Layout
	this.horizontalLayout.addItems([this.weightInputFieldset, this.unitRadioInput]);
	
	// Initialize Dropdown Broselow Input
	this.dropdownBroselowInput = new OO.ui.DropdownInputWidget( {
		options: [
			{
				icon: 'map',
				data: "0",
				label: 'Échelle de Broselow'
			},
			{
				optgroup: '6-7 kg, 3-5 mois'
			},
			{
				icon: 'stop',
				data: "6",
				label: 'Rose'
			},
			{
				optgroup: '8-9 kg, 6-11 mois'
			},
			{
				icon: 'stop',
				data: "8",
				label: 'Rouge'
			},
			{
				optgroup: '10-11 kg, 12-24 mois'
			},
			{
				icon: 'stop',
				data: "10",
				label: 'Mauve'
			},
			{
				optgroup: '12-14 kg, 2 ans mois'
			},
			{
				icon: 'stop',
				data: "13",
				label: 'Jaune'
			},
			{
				optgroup: '15-18 kg, 3-4 ans'
			},
			{
				icon: 'stop',
				data: "16",
				label: 'Blanc'
			},
			{
				optgroup: '19-23 kg, 5-6 ans'
			},
			{
				icon: 'stop',
				data: "21",
				label: 'Bleu'
			},
			{
				optgroup: '24-29 kg, 7-9 ans'
			},
			{
				icon: 'stop',
				data: "26",
				label: 'Orange'
			},
			{
				optgroup: '30-36 kg, 10-11 ans'
			},
			{
				icon: 'stop',
				data: "33",
				label: 'Vert'
			},
		],
		value: ''
	} );
	
	// Initialize Dosage Result Label
	this.dosageResultLabel =  new OO.ui.LabelWidget( { label: '' } ),
	
	// Add Horizontal Input, Dropdown Broselow Input and Dosage Result Label to Panel
	this.panel.$element.append( this.horizontalLayout.$element );
	this.panel.$element.append( this.dropdownBroselowInput.$element );
	this.panel.$element.append( this.dosageResultLabel.$element );
	// Add Panel to Body
	this.$body.append( this.panel.$element );

	// Add Event Handler to Weight Input and Unit Radio Input
	this.weightInput.connect( this, { 'change': 'onWeightInputChange' } );
	this.unitRadioInput.connect( this, { 'change': 'onUnitRadioInputChange' } );
	this.dropdownBroselowInput.connect( this, { 'change': 'onDropdownBroselowInputChange' } );
};

// Specify any additional functionality required by the window (disable opening an empty URL, in this case)
function updateDosageResult(dosageDialog){
	// Get weight input
	var weightInputNumber = parseFloat(dosageDialog.weightInput.value);
	if (!Number.isNaN(weightInputNumber) && weightInputNumber > 0) {
		var multiplier;
		console.log(dosageDialog.unitRadioInput.value);
		if (dosageDialog.unitRadioInput.value === 'kg'){
			multiplier = 1.0;
		} else {
			multiplier = 0.453592;
		}
		var daily_dose = Math.round(multiplier * dose_per_kg * weightInputNumber / frequency_to_division[frequency]);
		var dose;
		if (max_daily_dose !== 0) {
			dose = Math.round((Math.min(max_daily_dose, daily_dose)) / frequency_to_division[frequency]);
		} else {
			dose = Math.round(daily_dose / frequency_to_division[frequency]);
		}
		var dosageResultLabelString = drug + ' ' + dose.toString() + ' ' + dose_unit + ' ' + roa + ' ' + frequency + ' ' + prn + ((prn) ? ' ' : '' ) + '×' + ' ' + duration.toString() + ' ' + duration_unit;
		dosageDialog.dosageResultLabel.setLabel(dosageResultLabelString);
	} else {
		dosageDialog.dosageResultLabel.setLabel('');
	}
}


DosageDialog.prototype.onWeightInputChange = function ( value ) {
	console.log("CHANGING WEIGHT INPUT");
	console.log(this);
	updateDosageResult(this);
};

DosageDialog.prototype.onUnitRadioInputChange = function ( value ) {
	console.log("CHANGING UNIT INPUT");
	updateDosageResult(this);
};

DosageDialog.prototype.onDropdownBroselowInputChange = function ( value ) {
	console.log("CHANGING BROSELOW INPUT");
	this.weightInput.setValue(parseFloat(this.dropdownBroselowInput.getValue()));
	this.unitRadioInput.setValue('kg')
};

// Use the getActionProcess() method to specify a process to handle the
// actions (for the 'save' action, in this example).
DosageDialog.prototype.getActionProcess = function ( action ) {
	var dialog = this;
	if ( action ) {
		return new OO.ui.Process( function () {
			dialog.close( {
				action: action
			} );
		} );
	}
// Fallback to parent handler.
	return DosageDialog.super.prototype.getActionProcess.call( this, action );
};

/*
// Get dialog height.
DosageDialog.prototype.getBodyHeight = function () {
	return this.content.$element.outerHeight( true );
};
*/

// Create and append the window manager.
var windowManager = new OO.ui.WindowManager();
$( document.body ).append( windowManager.$element );

// Create a new dialog window.
var dosageDialog = new DosageDialog({
	size: 'big'
});

// Add windows to window manager using the addWindows() method.
windowManager.addWindows( [ dosageDialog ] );

// Open the window.
windowManager.openWindow( dosageDialog );