API Reference

Weight & Height Blocks

Overview

The weight and height input blocks provide automatic unit conversion between metric and imperial systems, storing values in all measurement units and providing comprehensive callback data for advanced customization.

Features

  • Automatic Unit Conversion: Real-time conversion between metric and imperial units
  • Dual Unit Storage: Saves values in both measurement systems regardless of user input
  • System Preference Tracking: Automatically sets user_units_system based on user selection
  • Rich Callback Data: Provides comprehensive conversion data to callback functions
  • BMI Calculation: Built-in BMI calculation utilities

Weight Block

Properties saved to user_properties

// User inputs 70 kg
{
  "screen_name_kg": "70",           // Original kg input
  "screen_name_lbs": "154",         // Auto-converted lbs value
  "selected-dimension": "kg",       // Active unit tab
  "user_units_system": "metric"     // System preference
}

// User inputs 154 lbs  
{
  "screen_name_kg": "70",           // Auto-converted kg value
  "screen_name_lbs": "154",         // Original lbs input
  "selected-dimension": "lbs",      // Active unit tab
  "user_units_system": "imperial"   // System preference
}

Callback Object

When a weight field changes, the callback function, defined in "JS onchange callback function" receives:

// Metric input (kg field)
{
  value: '70',                // Current field value
  name: 'screen_27_kg',       // Field name
  kg: '70',                   // Kg value (original)
  lbs: 154,                   // Converted lbs value
  unit_system: 'metric'       // Current system
}

// Imperial input (lbs field)
{
  value: '154',               // Current field value
  name: 'screen_27_lbs',      // Field name
  kg: 70,                     // Converted kg value
  lbs: '154',                 // Lbs value (original)
  unit_system: 'imperial'     // Current system
}

Height Block

Properties saved to user_properties

// User inputs 175 cm
{
  "screen_name_cm": "175",          // Original cm input
  "screen_name_ft": "5",            // Auto-converted feet
  "screen_name_in": "9",            // Auto-converted inches
  "selected-dimension": "cm",       // Active unit tab
  "user_units_system": "metric"     // System preference
}

// User inputs 5'9"
{
  "screen_name_cm": "175",          // Auto-converted cm value
  "screen_name_ft": "5",            // Original feet input
  "screen_name_in": "9",            // Original inches input
  "selected-dimension": "ft",       // Active unit tab
  "user_units_system": "imperial"   // System preference
}

Callback Object

When a height field changes, the callback receives:

// Metric input (cm field)
{
  value: '175',               // Current field value
  name: 'welcome_cm',         // Field name
  cm: '175',                  // Cm value (original)
  ft: 5,                      // Converted feet
  in: 9,                      // Converted inches
  unit_system: 'metric'       // Current system
}

// Imperial input (ft/in fields)
{
  value: '5',                 // Current field value
  name: 'welcome_ft',         // Field name
  cm: 175,                    // Converted cm value
  ft: 5,                      // Feet value
  in: 9,                      // Inches value
  unit_system: 'imperial'     // Current system
}

Unit Conversion Functions

Access conversion utilities through w2w.units:

Weight Conversion

// Convert pounds to kilograms
w2w.units.lbsToKg(154);     // Returns: 70

// Convert kilograms to pounds  
w2w.units.kgToLbs(70);      // Returns: 154

Height Conversion

// Convert feet and inches to centimeters
w2w.units.feetInchesToCm(5, 9);     // Returns: 175

// Convert centimeters to feet and inches
w2w.units.cmToFeetInches(175);      
// Returns: { feet: 5, inches: 9, text: "5′9″" }

BMI Calculation

Basic BMI Calculation

// Calculate BMI with different unit combinations
w2w.units.calculateBMI(70, 175, 'kg', 'cm');           // Returns: 22.86
w2w.units.calculateBMI(154, 69, 'lbs', 'in');          // Returns: 22.86
w2w.units.calculateBMI(70, 5.75, 'kg', 'ft');          // Returns: 22.86

BMI with Feet and Inches

// Calculate BMI using separate feet and inches values
w2w.units.calculateBMIWithFeetInches(154, 5, 9, 'lbs'); // Returns: 22.86
w2w.units.calculateBMIWithFeetInches(70, 5, 9, 'kg');   // Returns: 22.86

BMI Level Classification

// Get BMI level classification
w2w.units.getBMILevel(22.5);        // Returns: 'normal'
w2w.units.getBMILevel(17.0);        // Returns: 'underweight'
w2w.units.getBMILevel(27.0);        // Returns: 'overweight'
w2w.units.getBMILevel(32.0);        // Returns: 'obese'

// Get detailed BMI information
w2w.units.getBMILevelWithDescription(22.5);
// Returns: {
//   level: 'normal',
//   description: 'Normal weight (BMI 18.5-24.9)',
//   bmi: 22.5
// }

Usage Examples

Advanced Callback Usage

// Weight block callback example
function(data) {
  if (data.unit_system === 'metric') {
    console.log(`User entered ${data.kg} kg (${data.lbs} lbs)`);
  } else {
    console.log(`User entered ${data.lbs} lbs (${data.kg} kg)`);
  }
  
  // Values are automatically saved to user_properties
  // No need to manually call w2w.setUserProperty
}

// Height block callback example
function(data) {
  console.log(`Height: ${data.cm}cm or ${data.ft}'${data.in}"`);
  
  // Values are automatically saved to user_properties
  // No need to manually call w2w.setUserProperty
}

BMI Calculation Example

// Calculate BMI using stored user properties
function calculateUserBMI() {
  const weight = window.user_properties.weight_kg;
  const height = window.user_properties.height_cm;
  
  if (weight && height) {
    const bmi = w2w.units.calculateBMI(weight, height, 'kg', 'cm');
    const bmiInfo = w2w.units.getBMILevelWithDescription(bmi);
    
    w2w.setUserProperty('user_bmi', bmi);
    w2w.setUserProperty('user_bmi_level', bmiInfo.level);
    w2w.setUserProperty('user_bmi_description', bmiInfo.description);
    
    console.log(`BMI: ${bmi} (${bmiInfo.description})`);
  }
}