Difference between revisions of "MediaWiki:Gadget-calculator-core.js"
From WikiAnesthesia
				| Chris Rishel (talk | contribs)  (Created page with "( function( mw, $ ){      console.log( 'calculator-core loaded' ); }( jQuery, mediaWiki ) );") | Chris Rishel (talk | contribs)  | ||
| Line 1: | Line 1: | ||
| ( function( mw, $ ){   | ( function( mw, $ ){ | ||
|      console. |      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 ]; | |||
|                 mw.calculators.variables[ varName ] = 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(); | |||
|         } | |||
|     }; | |||
|     mw.calculators.init(); | |||
| }( jQuery, mediaWiki ) ); | }( jQuery, mediaWiki ) ); | ||
Revision as of 08:37, 16 July 2021
( function( mw, $ ){
    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 ];
                mw.calculators.variables[ varName ] = 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();
        }
    };
    mw.calculators.init();
}( jQuery, mediaWiki ) );