File: docs/JQUERY_PLUGIN_ENHANCEMENTS.md

Recommend this page to a friend!
  Packages of ikhsan   jQuery Calx   docs/JQUERY_PLUGIN_ENHANCEMENTS.md   Download  
File: docs/JQUERY_PLUGIN_ENHANCEMENTS.md
Role: Example script
Content type: text/markdown
Description: Example script
Class: jQuery Calx
Calculate form input values based on formulas
Author: By
Last change:
Date: 3 months ago
Size: 9,398 bytes
 

Contents

Class file image Download

jQuery Plugin Enhancement Summary

Completed Features (v3.0)

1. ✅ Multiple Elements as Sheets in One Workbook

Implementation: - Multiple HTML elements can be combined using jQuery selector: $('#Sales, #Tax, #Report').calx() - Each element becomes a named sheet in a single workbook - Sheet names are derived from element IDs or data-sheet attributes - Cross-sheet references work using SheetName!CellAddress syntax

Example:

$('#Sales, #Tax, #Report').calx({
    autoCalculate: true
});

// In HTML: <span data-formula="Sales!A1*Tax!B2"></span>

Code Location: src/jquery.calx.ts - processSheet() function

2. ✅ Variables Handled by Name Manager

Implementation: - Variables defined in options are registered with workbook.nameManager - Variables can reference cells from any sheet using absolute references - Variables can be used in formulas as simple names

Example:

$('#Config, #Order').calx({
    variables: {
        TaxRate: 'Config!A1',
        DiscountRate: 'Config!A2'
    }
});

// In HTML: <span data-formula="B1*TaxRate"></span>

Code Location: src/jquery.calx.ts - Variable processing in $.fn.calx() initialization

3. ✅ Data-Type Attribute Support

Implementation: - Added data-type attribute parsing in processDataAttributes() function - Supported types: number, text, date, boolean - Type is set on the cell using cell.type = DataType.NUMBER - Automatic type coercion based on specified type

Example:

<input data-cell="A1" data-type="number" value="42" />
<input data-cell="B1" data-type="text" value="Hello" />
<input type="date" data-cell="C1" data-type="date" value="2024-01-15" />

Code Location: src/jquery.calx.ts - processDataAttributes() function

4. ✅ Custom Formatter Support

Implementation: - Global formatter registry: formatterRegistry: Record<string, FormatterInterface> - Registration method: $.calx.registerFormatter(name, formatter) - Formatters applied when rendering cells to DOM - Formatters can be referenced by name in data-format attribute

Example:

$.calx.registerFormatter('currency', {
    format: function(value) {
        return '$' + value.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    }
});

// In HTML: <span data-cell="A1" data-format="currency"></span>

Code Location: - src/jquery.calx.ts - Global registry and $.calx.registerFormatter() method - updateAllCells() and processSheet() - Formatter application during rendering

5. ✅ Function Registry for Custom Functions

Implementation: - Global function registry: functionRegistry: Record<string, Function> - Registration method: $.calx.registerFunction(name, func) - Functions registered with both jQuery plugin and Calx core (Calx.setFormula()) - Custom functions available in all formulas

Example:

$.calx.registerFunction('TRIPLE', function(value) {
    return value * 3;
});

// In HTML: <span data-formula="TRIPLE(A1)"></span>

Code Location: - src/jquery.calx.ts - Global registry and $.calx.registerFunction() method - Integration with Calx.setFormula() for core registration

Technical Architecture

Multi-Sheet Implementation

  1. Workbook Creation: Single `Workbook` instance shared across all selected elements
  2. Sheet Mapping: Each element becomes a sheet, named by ID or `data-sheet` attribute
  3. Data Structure Detection: Automatic detection of flat vs. sheet-structured data
  4. Cross-Sheet Updates: `updateAllCellsInWorkbook()` handles updates across all sheets

Variable Integration

  1. Name Manager: Variables registered with `workbook.nameManager.define(name, reference)`
  2. Resolution: Calx core resolves variable names to cell references during calculation
  3. Scope: Variables are workbook-scoped, accessible from any sheet

Type System

  1. Attribute Parsing: `data-type` parsed during HTML processing
  2. Type Setting: Direct mapping to `DataType` enum values
  3. Default Type: NUMBER (as configured in Cell.ts)
  4. Type Conversion: Automatic based on cell type

Formatter System

  1. Registration: Formatters stored in `formatterRegistry`
  2. Resolution: Formatter name → formatter object lookup
  3. Application: Applied during cell value rendering to DOM
  4. Priority: Custom formatter → cell.format → getFormattedValue()

Function System

  1. Dual Registration: Both jQuery plugin registry and Calx core
  2. Name Normalization: Function names converted to uppercase
  3. Execution: Functions executed by Calx calculation engine
  4. Scope: Functions available globally across all workbooks

Files Modified/Created

Core Plugin File

  • `src/jquery.calx.ts` - Complete rewrite with all 5 features

Build Configuration

  • `webpack.jquery.config.js` - Webpack configuration for jQuery plugin build

Documentation

  • `JQUERY_PLUGIN.md` - Comprehensive documentation with all new features
  • `JQUERY_PLUGIN_OLD.md` - Backup of original documentation

Examples

  • `examples/jquery-basic.html` - Simple calculator example
  • `examples/jquery-advanced.html` - Advanced features with hidden cells
  • `examples/jquery-multisheet.html` - NEW - Multi-sheet with cross-references
  • `examples/jquery-datatypes.html` - NEW - Data type demonstrations
  • `examples/jquery-test-suite.html` - NEW - Automated feature tests

Build Output

  • `dist/jquery.calx.js` - Bundled jQuery plugin (386 KB)
  • `dist/jquery.calx.js.map` - Source map (1.8 MB)

Testing

Automated Test Suite

Location: examples/jquery-test-suite.html

Tests Included: 1. ✅ Basic Calculation 2. ✅ Multi-Sheet Cross-Reference 3. ✅ Named Variables 4. ✅ Data Type (Number) 5. ✅ Data Type (Text) 6. ✅ Custom Formatter 7. ✅ Custom Function 8. ✅ Hidden Cells

How to Run: 1. Build the jQuery plugin: npm run build:jquery 2. Open examples/jquery-test-suite.html in a browser 3. All tests run automatically and display pass/fail status

API Reference

Global Methods

// Register custom formatter
$.calx.registerFormatter(name: string, formatter: FormatterInterface)

// Register custom function
$.calx.registerFunction(name: string, func: Function)

// Get registered formatter
$.calx.getFormatter(name: string): FormatterInterface | undefined

Initialization Options

$('#element').calx({
    autoCalculate: boolean,           // Auto-recalculate on changes
    data: Object,                     // Cell configurations
    variables: Record<string, string>,// Named variable definitions
    functions: Record<string, Function>, // Custom functions
    formatters: Record<string, FormatterInterface> // Custom formatters
});

Data Attributes

  • `data-cell="A1"` - Cell address
  • `data-formula="SUM(A1:A10)"` - Cell formula
  • `data-format="currency"` - Formatter name
  • `data-type="number"` - Cell data type
  • `data-sheet="SheetName"` - Sheet name

Instance Methods

$('#element').calx('getCell', 'A1')
$('#element').calx('getCellValue', 'A1')
$('#element').calx('setCellValue', 'A1', value)
$('#element').calx('setCellFormula', 'A1', formula)
$('#element').calx('calculate')
$('#element').calx('build')
$('#element').calx('getWorkbook')
$('#element').calx('getSheet', 'SheetName')
$('#element').calx('destroy')

Performance Considerations

  1. Build Size: 386 KB (includes all Calx core functionality)
  2. Compilation: ~3.3 seconds for jQuery plugin build
  3. Runtime: Efficient with shared workbook architecture
  4. Memory: Single workbook instance reduces memory overhead

Backward Compatibility

Maintained

  • ✅ All v2.x HTML attribute patterns
  • ✅ Basic initialization syntax
  • ✅ Core calculation methods
  • ✅ Event handling patterns

Enhanced

  • ⚡ Multi-sheet support (optional, single sheet still works)
  • ⚡ Variable system (optional enhancement)
  • ⚡ Type specification (optional, auto-detection still works)
  • ⚡ Custom formatters (optional, replaces numeral.js)
  • ⚡ Custom functions (optional, extends built-in functions)

Breaking Changes

  • None for basic usage
  • Advanced users: workbook architecture changed for multi-sheet support

Next Steps (Optional Future Enhancements)

  1. Lazy Loading: Split FormulaJS into separate chunk
  2. Worker Support: Offload calculations to Web Workers
  3. Virtual Scrolling: Support for large datasets
  4. Undo/Redo: History management for cell changes
  5. Export/Import: Save/load workbook state
  6. Real-time Sync: Synchronize multiple clients
  7. Chart Integration: Built-in charting support

Conclusion

All 5 requested features have been successfully implemented:

  1. Multiple elements as sheets - Full cross-sheet reference support
  2. Variables via name manager - Clean, maintainable formulas
  3. Data-type attribute - Explicit type control
  4. Custom formatters - Flexible display formatting
  5. Function registry - Extensible calculation engine

The jQuery plugin maintains backward compatibility while adding powerful new features for advanced use cases. The implementation is well-documented, tested, and ready for production use.