Skip to main content

Overview

The UIController 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.
Initialization steps:
  1. Caches all DOM element references
  2. Binds event listeners to interactive elements
  3. Updates footer year to current year
  4. Sets default model selection to “gpt-4o”

Properties

elements

Cached references to all DOM elements used by the controller.
HTMLTextAreaElement
Main text input area (#text-input)
HTMLSelectElement
Model selection dropdown (#model-select)
HTMLButtonElement
Clear button (#clear-btn)
HTMLElement
Container for visual token display (#tokens-container)
HTMLElement
Container for token list with IDs (#tokens-array)
HTMLElement
Token count display (#token-count)
HTMLElement
Character count display (#char-count)
HTMLElement
Word count display (#word-count)
HTMLElement
Cost estimate display (#cost-estimate)

Methods

initializeElements()

Initializes and caches DOM element references.
Object
Object containing all cached DOM elements
This method is called automatically by the constructor. It uses document.getElementById() to retrieve and cache elements for efficient access.
If any required DOM elements are missing, methods that use them will safely skip operations.

bindEvents()

Binds event listeners to interactive elements.
Events bound:
  • input on text area → triggers onTextChange callback
  • change on model select → triggers onModelChange callback
  • click on clear button → triggers onClear callback
  • keydown (Ctrl+K) → focuses text input

setEventHandlers()

Sets callback functions for UI events.
Object
required
Object containing event handler functions
Function
Called when text input changes
Function
Called when model selection changes
Function
Called when clear button is clicked

getTextInput()

Retrieves the current text from the input area.
string
Current text content (empty string if element not found)

getSelectedModel()

Retrieves the currently selected model ID.
string
Selected model ID (defaults to “gpt-4o” if not found)

clearTextInput()

Clears the text input and focuses it.
Sets the input value to empty string and focuses the element for immediate typing.

updateStatistics()

Updates the statistics display with new values.
Object
default:"{}"
Statistics object with token, character, word counts and cost
number
Number of tokens
number
Number of characters
number
Number of words
number
Estimated cost in dollars
Numbers are automatically formatted with thousand separators (e.g., 1,247) for better readability.

updateModelInfo()

Updates the model information display.
string
required
Model identifier (e.g., “gpt-4o”)
TokenizationService
required
Tokenization service instance for retrieving algorithm names
Updates displayed:
  • 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<Object>
default:"[]"
Array of token objects from tokenization
Creates <span> elements for each token with:
  • CSS class based on token type (for color coding)
  • approximate-id class if token ID is approximate
  • Click handler to highlight token in list
Click any token in the visualization to scroll to and highlight it in the detailed token list.

updateTokensList()

Updates the detailed token list with IDs and types.
Array<Object>
default:"[]"
Array of token objects
Display includes:
  • 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.
string
required
Token ID to highlight (e.g., “token_0”)
Behavior:
  1. Removes previous highlights
  2. Highlights selected token with primary color
  3. Scrolls token into view (smooth scroll)
  4. Automatically removes highlight after 2 seconds

resetVisualizations()

Resets both token visualizations to empty state.
Displays placeholder messages:
  • Token container: “Escribe algo para ver los tokens…”
  • Token list: “Los tokens aparecerán aquí…“

showLoading()

Displays loading state during analysis.
Shows “Analizando tokens…” message in the token container.

triggerModelChange()

Manually triggers the model change event.
Calls the onModelChange callback if it has been set. Useful for initialization or programmatic model switching.

bindKeyboardShortcuts()

Binds keyboard shortcuts for improved UX.
Shortcuts:
  • Ctrl+K: Focus text input
Keyboard shortcuts improve accessibility and power user experience.

updateYear()

Updates the copyright year in the footer.
Sets the #current-year element to the current year from Date.

Usage Examples

DOM Element IDs

The UIController expects these elements in the HTML:
  • text-input - Main textarea for text input
  • model-select - Model selection dropdown
  • clear-btn - Clear button
  • token-count - Token count display
  • char-count - Character count display
  • word-count - Word count display
  • cost-estimate - Cost estimate display
  • selected-model-name - Model name display
  • context-limit - Context limit display
  • tokenization-type - Tokenizer type display
  • cost-per-1k - Pricing information display
  • active-algorithm - Algorithm description display
  • model-details-link - Link to model documentation
  • tokens-container - Visual token display area
  • tokens-array - Detailed token list area

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