DownloadjQuery 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
-
Workbook Creation: Single `Workbook` instance shared across all selected elements
-
Sheet Mapping: Each element becomes a sheet, named by ID or `data-sheet` attribute
-
Data Structure Detection: Automatic detection of flat vs. sheet-structured data
-
Cross-Sheet Updates: `updateAllCellsInWorkbook()` handles updates across all sheets
Variable Integration
-
Name Manager: Variables registered with `workbook.nameManager.define(name, reference)`
-
Resolution: Calx core resolves variable names to cell references during calculation
-
Scope: Variables are workbook-scoped, accessible from any sheet
Type System
-
Attribute Parsing: `data-type` parsed during HTML processing
-
Type Setting: Direct mapping to `DataType` enum values
-
Default Type: NUMBER (as configured in Cell.ts)
-
Type Conversion: Automatic based on cell type
Formatter System
-
Registration: Formatters stored in `formatterRegistry`
-
Resolution: Formatter name → formatter object lookup
-
Application: Applied during cell value rendering to DOM
-
Priority: Custom formatter → cell.format → getFormattedValue()
Function System
-
Dual Registration: Both jQuery plugin registry and Calx core
-
Name Normalization: Function names converted to uppercase
-
Execution: Functions executed by Calx calculation engine
-
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
-
Build Size: 386 KB (includes all Calx core functionality)
-
Compilation: ~3.3 seconds for jQuery plugin build
-
Runtime: Efficient with shared workbook architecture
-
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)
-
Lazy Loading: Split FormulaJS into separate chunk
-
Worker Support: Offload calculations to Web Workers
-
Virtual Scrolling: Support for large datasets
-
Undo/Redo: History management for cell changes
-
Export/Import: Save/load workbook state
-
Real-time Sync: Synchronize multiple clients
-
Chart Integration: Built-in charting support
Conclusion
All 5 requested features have been successfully implemented:
-
✅ Multiple elements as sheets - Full cross-sheet reference support
-
✅ Variables via name manager - Clean, maintainable formulas
-
✅ Data-type attribute - Explicit type control
-
✅ Custom formatters - Flexible display formatting
-
✅ 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.
|