Overview
TheUIController class handles all user interface operations in Tokenizador. It manages DOM elements, event handlers, and visual updates for statistics, token visualization, and model information.
This controller follows the separation of concerns principle, isolating all DOM manipulation from business logic.
Constructor
Creates a new UIController instance and initializes the interface.- Caches all DOM element references
- Binds event listeners to interactive elements
- Updates footer year to current year
- Sets default model selection to “gpt-4o”
Properties
elements
Cached references to all DOM elements used by the controller.Main text input area (
#text-input)Model selection dropdown (
#model-select)Clear button (
#clear-btn)Container for visual token display (
#tokens-container)Container for token list with IDs (
#tokens-array)Token count display (
#token-count)Character count display (
#char-count)Word count display (
#word-count)Cost estimate display (
#cost-estimate)Methods
initializeElements()
Initializes and caches DOM element references.Object containing all cached DOM elements
document.getElementById() to retrieve and cache elements for efficient access.
bindEvents()
Binds event listeners to interactive elements.- input on text area → triggers
onTextChangecallback - change on model select → triggers
onModelChangecallback - click on clear button → triggers
onClearcallback - keydown (Ctrl+K) → focuses text input
setEventHandlers()
Sets callback functions for UI events.Object containing event handler functions
Called when text input changes
Called when model selection changes
Called when clear button is clicked
getTextInput()
Retrieves the current text from the input area.Current text content (empty string if element not found)
getSelectedModel()
Retrieves the currently selected model ID.Selected model ID (defaults to “gpt-4o” if not found)
clearTextInput()
Clears the text input and focuses it.updateStatistics()
Updates the statistics display with new values.Statistics object with token, character, word counts and cost
Number of tokens
Number of characters
Number of words
Estimated cost in dollars
Numbers are automatically formatted with thousand separators (e.g., 1,247) for better readability.
updateModelInfo()
Updates the model information display.Model identifier (e.g., “gpt-4o”)
Tokenization service instance for retrieving algorithm names
- Model name
- Context limit (formatted with thousands separator)
- Tokenizer type (e.g., “Tokenizador GPT-4”)
- Pricing (input and output costs per 1M tokens)
- Active tokenization algorithm
- Model details link (if URL available)
updateTokenVisualization()
Updates the visual token display with color-coded tokens.Array of token objects from tokenization
<span> elements for each token with:
- CSS class based on token type (for color coding)
approximate-idclass if token ID is approximate- Click handler to highlight token in list
updateTokensList()
Updates the detailed token list with IDs and types.Array of token objects
- Precision indicator (real tiktoken IDs vs approximate)
- Token text (escaped for HTML)
- Sequential number
- Token type
- Token ID with precision indicator (= for real, ≈ for approximate)
highlightTokenInList()
Highlights a specific token in the detailed list.Token ID to highlight (e.g., “token_0”)
- Removes previous highlights
- Highlights selected token with primary color
- Scrolls token into view (smooth scroll)
- Automatically removes highlight after 2 seconds
resetVisualizations()
Resets both token visualizations to empty state.- Token container: “Escribe algo para ver los tokens…”
- Token list: “Los tokens aparecerán aquí…“
showLoading()
Displays loading state during analysis.triggerModelChange()
Manually triggers the model change event.onModelChange callback if it has been set. Useful for initialization or programmatic model switching.
bindKeyboardShortcuts()
Binds keyboard shortcuts for improved UX.- Ctrl+K: Focus text input
updateYear()
Updates the copyright year in the footer.#current-year element to the current year from Date.
Usage Examples
DOM Element IDs
The UIController expects these elements in the HTML:Input & Controls
Input & Controls
text-input- Main textarea for text inputmodel-select- Model selection dropdownclear-btn- Clear button
Statistics Display
Statistics Display
token-count- Token count displaychar-count- Character count displayword-count- Word count displaycost-estimate- Cost estimate display
Model Information
Model Information
selected-model-name- Model name displaycontext-limit- Context limit displaytokenization-type- Tokenizer type displaycost-per-1k- Pricing information displayactive-algorithm- Algorithm description displaymodel-details-link- Link to model documentation
Visualizations
Visualizations
tokens-container- Visual token display areatokens-array- Detailed token list area
Footer
Footer
CSS Classes
Tokens are assigned these CSS classes for styling:.token
Base token class
.palabra
Word token
.subword
Subword token
.number
Numeric token
.punctuation
Punctuation token
.special
Special character
.espacio_en_blanco
Whitespace token
.approximate-id
Approximate token ID indicator
See Also
TokenAnalyzer
Main application orchestrator
TokenizationService
Tokenization logic
StatisticsCalculator
Statistics calculations
Architecture
System architecture overview