Difference between revisions of "MediaWiki:Gadget-calculator-core.js"
From WikiAnesthesia
				| Chris Rishel (talk | contribs) | Chris Rishel (talk | contribs)  | ||
| Line 1: | Line 1: | ||
| ( function() { | ( function() { | ||
|     /** | |||
|      * Class CalculatorVariable | |||
|      * @param varData | |||
|      * @returns object | |||
|      * @constructor | |||
|      */ | |||
|      function CalculatorVariable( varData ) { |      function CalculatorVariable( varData ) { | ||
|          var requiredProperties = [ |          var requiredProperties = [ | ||
| Line 34: | Line 40: | ||
|              } |              } | ||
|          } |          } | ||
|         this.value = null; | |||
|      } |      } | ||
|      CalculatorVariable.prototype.hasUnits  |      CalculatorVariable.prototype = { | ||
|         getValue: function() { | |||
|             return this.value; | |||
|         }, | |||
|         hasUnits: function() { | |||
|             return this.units !== null; | |||
|         }, | |||
|         setValue: function( value ) { | |||
|             if( this.type === 'number' ) { | |||
|                 if( typeof value !== 'object' ) { | |||
|                     value = math.unit( value ); | |||
|                 } | |||
|                 if( this.units ) { | |||
|                 } | |||
|             } | |||
|             this.value = value; | |||
|         } | |||
|      }; |      }; | ||
| Line 54: | Line 80: | ||
|                  varData.name = varName; |                  varData.name = varName; | ||
|                  mw.calculators.variables[ varName ] =  |                  var calculationVariable = new CalculatorVariable( varData ); | ||
|                 if( calculationVariable ) { | |||
|                     mw.calculators.variables[ varName ] = calculationVariable; | |||
|                 } | |||
|              } |              } | ||
|          }, |          }, | ||
| Line 105: | Line 135: | ||
|          getValue: function( varName ) { |          getValue: function( varName ) { | ||
|              if( mw.calculators.variables.hasOwnProperty( varName ) ) { |              if( mw.calculators.variables.hasOwnProperty( varName ) ) { | ||
|                  return mw.calculators.variables[ varName ]. |                  return mw.calculators.variables[ varName ].getValue(); | ||
|              } else { |              } else { | ||
|                  return null; |                  return null; | ||
| Line 118: | Line 148: | ||
|              } |              } | ||
|              return mw.calculators.variables[ varName ].setValue( value ); | |||
|          } |          } | ||
|      }; |      }; | ||
Revision as of 10:01, 16 July 2021
( function() {
    /**
     * Class CalculatorVariable
     * @param varData
     * @returns object
     * @constructor
     */
    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;
            }
        }
        this.value = null;
    }
    CalculatorVariable.prototype = {
        getValue: function() {
            return this.value;
        },
        hasUnits: function() {
            return this.units !== null;
        },
        setValue: function( value ) {
            if( this.type === 'number' ) {
                if( typeof value !== 'object' ) {
                    value = math.unit( value );
                }
                if( this.units ) {
                }
            }
            this.value = value;
        }
    };
    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;
                var calculationVariable = new CalculatorVariable( varData );
                if( calculationVariable ) {
                    mw.calculators.variables[ varName ] = calculationVariable;
                }
            }
        },
        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 ].getValue();
            } else {
                return null;
            }
        },
        init: function() {
            mw.calculators.createUnits();
        },
        setValue: function( varName, value ) {
            if( !mw.calculators.variables.hasOwnProperty( varName ) ) {
                return false;
            }
            return mw.calculators.variables[ varName ].setValue( value );
        }
    };
    mw.calculators.init();
}() );