or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bootstrap-platform.mddocument-management.mdevent-management.mdhydration.mdindex.mdsecurity-sanitization.mdtesting-debug.md
tile.json

event-management.mddocs/

Event Management

Browser event handling system with plugin architecture supporting standard DOM events and gesture recognition via Hammer.js integration. Provides extensible event management for Angular applications.

Capabilities

Event Manager

Central service for managing browser events through a plugin-based architecture.

/**
 * An injectable service that provides event management for Angular
 * through a browser plug-in system.
 */
class EventManager {
  /**
   * Initializes an instance of the event-manager service
   * @param plugins - Array of EventManagerPlugin instances
   * @param _zone - NgZone for handling zone-aware event execution
   */
  constructor(plugins: EventManagerPlugin[], _zone: NgZone);

  /**
   * Registers a handler for a specific element and event
   * @param element - The HTML element to listen on
   * @param eventName - The name of the event to listen for
   * @param handler - The event handler function
   * @param options - Optional listener options (passive, once, capture, etc.)
   * @returns A function that removes the event listener when called
   */
  addEventListener(element: HTMLElement, eventName: string, handler: Function, options?: ListenerOptions): Function;

  /**
   * Gets the NgZone instance used by this EventManager
   * @returns The NgZone instance
   */
  getZone(): NgZone;
}

Usage Examples:

import { EventManager, EVENT_MANAGER_PLUGINS } from "@angular/platform-browser";
import { NgZone, Injectable } from "@angular/core";

@Injectable()
export class CustomEventService {
  constructor(
    private eventManager: EventManager,
    private zone: NgZone
  ) {}

  addCustomListener(element: HTMLElement, eventName: string, callback: Function) {
    // Add event listener through the event manager
    const removeListener = this.eventManager.addEventListener(
      element, 
      eventName, 
      (event) => {
        this.zone.run(() => callback(event));
      }
    );

    return removeListener;
  }

  addMultipleListeners(element: HTMLElement, events: Record<string, Function>) {
    const removeListeners: Function[] = [];

    Object.entries(events).forEach(([eventName, handler]) => {
      const remove = this.eventManager.addEventListener(element, eventName, handler);
      removeListeners.push(remove);
    });

    return () => removeListeners.forEach(remove => remove());
  }
}

Event Manager Plugin

Base class for creating custom event manager plugins to handle specific event types.

/**
 * Base class for event manager plugins.
 * Plugins extend the event system to handle specific event types or patterns.
 */
abstract class EventManagerPlugin {
  /** Reference to the EventManager instance */
  manager: EventManager;

  /**
   * Determines if this plugin supports the given event name
   * @param eventName - The event name to check
   * @returns True if this plugin can handle the event
   */
  abstract supports(eventName: string): boolean;

  /**
   * Adds an event listener for the supported event
   * @param element - The HTML element to listen on
   * @param eventName - The name of the event to listen for
   * @param handler - The event handler function
   * @returns A function that removes the event listener when called
   */
  abstract addEventListener(element: HTMLElement, eventName: string, handler: Function, options?: ListenerOptions): Function;
}

Usage Examples:

import { EventManagerPlugin } from "@angular/platform-browser";

// Custom plugin for handling special events
export class CustomEventPlugin extends EventManagerPlugin {
  supports(eventName: string): boolean {
    return eventName.startsWith('custom:');
  }

  addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
    const actualEventName = eventName.substring(7); // Remove 'custom:' prefix
    
    const wrappedHandler = (event: Event) => {
      // Add custom processing
      const customEvent = this.processEvent(event);
      handler(customEvent);
    };

    element.addEventListener(actualEventName, wrappedHandler);
    
    return () => element.removeEventListener(actualEventName, wrappedHandler);
  }

  private processEvent(event: Event): CustomEvent {
    // Add custom event processing logic
    return new CustomEvent('processed', { detail: event });
  }
}

Hammer.js Gesture Support

Integration with Hammer.js for touch gesture recognition in Angular applications.

/**
 * NgModule that provides Hammer.js gesture support for Angular applications
 */
class HammerModule {}

/**
 * Configuration class for customizing Hammer.js gesture recognition
 */
class HammerGestureConfig {
  /** List of hammer events that should be handled */
  events: string[];
  
  /** Override configuration for specific gestures */
  overrides: { [key: string]: Object };

  /**
   * Creates a Hammer.js instance for the given element
   * @param element - The HTML element to attach gestures to
   * @returns A configured Hammer.js instance
   */
  buildHammer(element: HTMLElement): HammerInstance;
}

Usage Examples:

import { NgModule } from "@angular/core";
import { HammerModule, HammerGestureConfig, HAMMER_GESTURE_CONFIG } from "@angular/platform-browser";

// Custom Hammer configuration
export class CustomHammerConfig extends HammerGestureConfig {
  overrides = {
    'swipe': { velocity: 0.4, threshold: 20 },
    'pinch': { enable: false },
    'rotate': { enable: false }
  };

  events: string[] = [
    'tap',
    'press',
    'swipe',
    'swipeleft',
    'swiperight',
    'swipeup',
    'swipedown'
  ];
}

@NgModule({
  imports: [HammerModule],
  providers: [
    {
      provide: HAMMER_GESTURE_CONFIG,
      useClass: CustomHammerConfig
    }
  ]
})
export class GestureModule {}

// Component using gestures
@Component({
  selector: 'app-swipe-card',
  template: `
    <div class="card" 
         (swipeleft)="onSwipeLeft()" 
         (swiperight)="onSwipeRight()"
         (tap)="onTap()"
         (press)="onPress()">
      Swipe me!
    </div>
  `
})
export class SwipeCardComponent {
  onSwipeLeft() {
    console.log('Swiped left');
  }

  onSwipeRight() {
    console.log('Swiped right');
  }

  onTap() {
    console.log('Tapped');
  }

  onPress() {
    console.log('Long pressed');
  }
}

Hammer.js Configuration Tokens

Injection tokens for configuring Hammer.js behavior and loading.

/**
 * Injection token for configuring Hammer.js gesture recognition
 */
const HAMMER_GESTURE_CONFIG: InjectionToken<HammerGestureConfig>;

/**
 * Injection token for providing a Hammer.js loader function
 */
const HAMMER_LOADER: InjectionToken<HammerLoader>;

/**
 * Injection token for providing event manager plugins
 */
const EVENT_MANAGER_PLUGINS: InjectionToken<EventManagerPlugin[]>;

Usage Examples:

import { 
  HAMMER_GESTURE_CONFIG, 
  HAMMER_LOADER, 
  HammerGestureConfig 
} from "@angular/platform-browser";

// Custom Hammer loader for lazy loading
const customHammerLoader: HammerLoader = () => {
  return import('hammerjs').then(() => {
    console.log('Hammer.js loaded');
  });
};

// Providers configuration
@NgModule({
  providers: [
    {
      provide: HAMMER_LOADER,
      useValue: customHammerLoader
    },
    {
      provide: HAMMER_GESTURE_CONFIG,
      useClass: CustomHammerConfig
    }
  ]
})
export class AppModule {}

Types

/**
 * Interface defining the Hammer.js instance API
 */
interface HammerInstance {
  /** Add an event listener */
  on(eventName: string, callback?: Function): void;
  
  /** Remove an event listener */
  off(eventName: string, callback?: Function): void;
  
  /** Destroy the Hammer instance */
  destroy?(): void;
}

/**
 * Type for Hammer.js loader function
 */
type HammerLoader = () => Promise<void>;

/**
 * Options for configuring event listeners (from @angular/core)
 */
type ListenerOptions = boolean | AddEventListenerOptions;

Supported Hammer Events:

const hammerEvents = [
  // Pan events
  'pan', 'panstart', 'panmove', 'panend', 'pancancel',
  'panleft', 'panright', 'panup', 'pandown',
  
  // Pinch events
  'pinch', 'pinchstart', 'pinchmove', 'pinchend', 'pinchcancel',
  'pinchin', 'pinchout',
  
  // Press events
  'press', 'pressup',
  
  // Rotate events
  'rotate', 'rotatestart', 'rotatemove', 'rotateend', 'rotatecancel',
  
  // Swipe events
  'swipe', 'swipeleft', 'swiperight', 'swipeup', 'swipedown',
  
  // Tap events
  'tap'
];