Home Reference Source

vgui/10-event.js

/**
 * Represents an event that bubbles up
 */
export class Event {
	/**
	 * Creates a default Event
	 */
	constructor() {
		this._target = global.document;
		this._cancelable = false;
		this._defaultPrevented = false;
		this._propagating = true;
	}

	/**
	 * The type of the event
	 */
	get type() {
		return "Event";
	}

	/**
	 * The element where the Event originated
	 */
	get target() {
		return this._target;
	}

	/**
	 * Wether this event should bubble up the DOM tree
	 */
	get bubbles() {
		return this._propagating;
	}

	/**
	 * Wether preventDefault can be called to stop the default action
	 */
	get cancelable() {
		return this._cancelable;
	}

	/**
	 * @access private
	 * @protected
	 * Should be overridden for cancelation effects
	 */
	_cancel() {
		//Virtual
	}

	/**
	 * Stops the event from bubbling up.
	 */
	stopPropagation() {
		this._propagating = false;
	}

	/**
	 * Prevents the default action from happening.
	 * This does not stop propagation.
	 */
	preventDefault() {
		if (!this.cancelable)
			return;
		if (this._defaultPrevented)
			return;
		this._cancel();
	}
}