« MediaWiki:Gadget-dosage-calculator.js » : différence entre les versions

De Wikimedica
(Logic)
(Change RadioSelectWidget to RadioSelectInputWidget for change event)
Ligne 119 : Ligne 119 :
// Initialize Unit Radio Input
// Initialize Unit Radio Input
this.unitRadioInput = new OO.ui.RadioSelectInputWidget( {
    options: [
        { data: 'kg', label: 'Kg' },
        { data: 'lbs', label: 'Lbs'}
    ]
} );
/*
this.optionKg = new OO.ui.RadioOptionWidget( {
this.optionKg = new OO.ui.RadioOptionWidget( {
        data: 'kg',
        data: 'kg',
Ligne 130 : Ligne 137 :
        items: [ this.optionKg, this.optionLbs ]
        items: [ this.optionKg, this.optionLbs ]
    } );
    } );
   
// Select 'option Kg' using the RadioSelectWidget's selectItem() method.
// Select 'option Kg' using the RadioSelectWidget's selectItem() method.
this.unitRadioInput.selectItem( this.optionKg );
this.unitRadioInput.selectItem( this.optionKg );
*/
// Set Elements to Horizontal Layout
// Set Elements to Horizontal Layout

Version du 13 janvier 2023 à 00:34

/* 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 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 Medication Label
	this.medicationLabel =  new OO.ui.LabelWidget( { label: 'Amoxiciline 90 mg/kg/j' } ),
	
	// 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.TextInputWidget();
	// Set Weight Input Field
	this.weightInputField = new OO.ui.FieldLayout( this.weightInput, { 
		label: 'Poids du patient:', 
		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'}
	    ]
	} );
	/*
	this.optionKg = new OO.ui.RadioOptionWidget( {
	        data: 'kg',
	        label: 'Kg'
	    } ),
	    this.optionLbs = new OO.ui.RadioOptionWidget( {
	        data: 'lbs',
	        label: 'Lbs'
	    } );
	    this.unitRadioInput = new OO.ui.RadioSelectWidget( {
	        items: [ this.optionKg, this.optionLbs ]
	    } );
	    
	
	// Select 'option Kg' using the RadioSelectWidget's selectItem() method.
	this.unitRadioInput.selectItem( this.optionKg );
	*/
	
	// Set Elements to Horizontal Layout
	this.horizontalLayout.addItems([this.weightInputFieldset, this.unitRadioInput]);
	
	// Initialize Medication Label
	this.resultLabel =  new OO.ui.LabelWidget( { label: 'Amoxiciline 500 mg BID x 7 jours' } ),
	
	// Add Medication Label, Horizontal Input and Result Label to Panel
	this.panel.$element.append( this.medicationLabel.$element );
	this.panel.$element.append( this.horizontalLayout.$element );
	this.panel.$element.append( this.resultLabel.$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' } );
};

// Specify any additional functionality required by the window (disable opening an empty URL, in this case)
DosageDialog.prototype.onUnitRadioInputChange = function ( value ) {
	console.log("CHANGING UNIT INPUT");
};

DosageDialog.prototype.onWeightInputChange = function ( value ) {
	console.log("CHANGING WEIGHT INPUT");
	console.log(this);
	// Get weight input
	var weightInputNumber = parseFloat(this.weightInput.value);
	if (!Number.isNaN(weightInputNumber) && weightInputNumber > 0) {
		var multiplier;
		if (this.unitRadioInput.findSelectedItem().data === 'kg'){
			multiplier = 1.0;
		} else {
			multiplier = 0.453592;
		}
		var dose = Math.round(multiplier * dose_per_kg * weightInputNumber / frequency_to_division[frequency]);
		var resultLabelString = drug + ' ' + dose.toString() + ' ' + dose_unit + ' ' + roa + ' ' + frequency + ' ' + prn + ((prn) ? ' ' : '' ) + '×' + ' ' + duration.toString() + ' ' + duration_unit;
		this.resultLabel.setLabel(resultLabelString);
	} else {
		this.resultLabel.setLabel('');
	}
	
	this.actions.setAbilities( {
		open: !!value.length 
	} );
};


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