MediaWiki:Gadget-calculator-core.js

From WikiAnesthesia
Revision as of 09:46, 16 July 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.
( function() {
    function CalculatorVariable( varData ) {
        var requiredProperties = [
            'name',
            'type'
        ];

        var optionalProperties = [
            'defaultValue',
            'options',
            'units'
        ];

        for( var iRequiredProperty in requiredProperties ) {
            var requiredProperty = requiredProperties[ iRequiredProperty ];

            if( !varData.hasOwnProperty( requiredProperty ) ) {
                console.error( 'Calculator variable missing required property' );
                console.log( varData );

                return null;
            }

            this[ requiredProperty ] = varData[ requiredProperty ];
        }

        for( var iOptionalProperty in optionalProperties ) {
            var optionalProperty = optionalProperties[ iOptionalProperty ];

            if( varData.hasOwnProperty( optionalProperty ) ) {
                this[ optionalProperty ] = varData[ optionalProperty ];
            } else {
                this[ optionalProperty ] = null;
            }
        }
    }

    CalculatorVariable.prototype.hasUnits = function() {
        return this.units !== null;
    };


    mw.calculators = {
        variables: {},
        addVariables: function( variables ) {
            for( var varName in variables ) {
                if( mw.calculators.variables.hasOwnProperty( varName ) ) {
                    console.warn( 'Calculation variable "' + varName + '" already defined.' );
                    continue;
                }

                var varData = variables[ varName ];

                varData.name = varName;

                mw.calculators.variables[ varName ] = new CalculatorVariable( varData );
            }
        },
        createUnits: function() {
            // Create aliases for abbreviations of existing units
            math.createUnit( 'dy', {
                definition: '1 day'
            } );

            math.createUnit( 'mo', {
                definition: '1 month'
            } );

            math.createUnit( 'yr', {
                definition: '1 year'
            } );

            // Body weight needs to be treated as a different fundamental unit type compared to mass for stoichiometry
            // Gram-weight, which uses short SI prefixes (e.g. 1 kgwt = 1000 gwt)
            math.createUnit( 'gwt', {
                prefixes: 'short'
            } );

            // Pound-weight, which uses no prefixes
            math.createUnit( 'lbwt', {
                definition: '453.59237 gwt'
            } );

            math.createUnit( 'puff' );

            math.createUnit( 'unit', {
                aliases: [ 'units' ]
            } );

            math.createUnit( 'vial', {
                aliases: [ 'vials' ]
            } );

        },
        getCookieKey: function( varName ) {
            return 'calculators-var-' + varName;
        },
        getVariable: function( varName ) {
            if( mw.calculators.variables.hasOwnProperty( varName ) ) {
                return mw.calculators.variables[ varName ];
            } else {
                return null;
            }
        },
        getValue: function( varName ) {
            if( mw.calculators.variables.hasOwnProperty( varName ) ) {
                return mw.calculators.variables[ varName ].value;
            } else {
                return null;
            }
        },
        init: function() {
            mw.calculators.createUnits();
        },
        setValue: function( varName, value ) {
            if( !mw.calculators.variables.hasOwnProperty( varName ) ) {
                return false;
            }

            var varData = mw.calculators.variables[ varName ];

            if( varData.hasOwnProperty( 'type' ) &&
                varData.type === 'number' &&
                typeof value !== 'object'
            ) {
                value = math.unit( value );

                if( varData.hasOwnProperty( 'units' ) ) {

                }
            }

            mw.calculators.variables[ varName ].value = value;

            return true;
        }
    };

    mw.calculators.init();

}() );