Home Reference Source

vgui/15-element.js

/**
 * Represents an element in the res file
 */
export class Element extends Node {
	constructor(id) {
		super(id);

		this._attrHandler = {};
		this._attrHandler["xpos"] = "int";
		this._attrHandler["ypos"] = "int";
		this._attrHandler["wide"] = "int";
		this._attrHandler["tall"] = "int";
		this._attrHandler["visible"] = "int";
		this._attrHandler["enabled"] = "int";
		this._attrHandler["fieldName"] = "string";

		this._eventHandler = {};
	}

	/**
	 * Returns the fieldName of the element
	 */
	get id() {
		var result = this.getAttribute("fieldName");
		if (result == "")
			return null;
		return result;
	}

	/**
	 * Sets an attribute on the element
	 * In order to support a new attribute an overloading class needs to add the setting type to _attrHandler
	 */
	setAttribute(name, value) {
		if (!name)
			throw new Error("Parameter name not set");
		var handler = this._attrHandler[name];
		if (handler == undefined) {
			handler = "string";
			console.log("Unknown attribute " + name + ", assuming string type");
		}

		switch(handler) {
			case "int":
				this._setAttrInt(name, value);
				break;
			case "string":
				this._setAttrString(name, value);
				break;
			case "float":
				this._setAttrFloat(name, value);
				break;
		}
	}
	
	/**
	 * Gets an attribute on the element
	 * In order to support a new attribute an overloading class needs to add the setting type to _attrHandler
	 */
	getAttribute(name) {
		if (!name)
			throw new Error("Parameter name not set");
		var handler = this._attrHandler[name];
		if (handler == undefined) {
			handler = "string";
			console.log("Unknown attribute " + name + ", assuming string type");
		}

		switch(handler) {
			case "int":
				return this._getAttrInt(name);
				break;
			case "string":
				return this._getAttrString(name);
				break;
			case "float":
				return this._getAttrFloat(name);
				break;
		}
	}

	/**
	 * Dispatch an event on this element.
	 * The event will bubble up until document unless a handler calls stopPropagation
	 */
	dispatchEvent(event) {
		if (!event)
			throw new Error("Unable to dispatch invalid event");

		event._target = this;
		if (this._eventHandler[event.type] !== undefined)
        {
    		var handlers = this._eventHandler[event.type];
    		for(var i = 0; i < handlers.length; i++) {
    			handlers[i](event);
    		}
        }

		if (!event._defaultPrevented && event.bubbles && this.parentNode !== null)
			return this.parentNode.dispatchEvent(event);

		return !event._defaultPrevented;
	}

	/**
	 * Adds a listener to a type of events
	 * See {@link Event} for subclasses for supported types
	 */
	addEventListener(type, listener) {
		if (this._eventHandler[type] == undefined)
			this._eventHandler[type] = [];
		this._eventHandler[type].push(listener);
	}

	/**
	 * Starts a VGUI animation on child elements.
	 * See {@link https://developer.valvesoftware.com/wiki/Understanding_VGUI2_Animation} for information about defining animations.
	 * @param {string} animationName - The name of the animation to start.
	 * @returns {bool} - True if animation was started succesfully
	 */
	startAnimation(animationName)
	{
		if (!animationName)
			throw new Error("Parameter animationName not set");
		return __vgui_StartAnimation(this._id, animationName);
	}

	/**
	 * Removes a registered event listener
	 */
	removeEventListener(type, listener) {
		if (this._eventHandler[type] == undefined)
			this._eventHandler[type] = [];

		this._eventHandler[type] = this._eventHandler[type].filter((e) => e != listener);
		if (this._eventHandler[type].length == 0)
			this._eventHandler[type] = undefined;
	}
}

/**
 * Polyfill for SetSettingBool (represented as int internally)
 * @ignore
 */
export var __vgui_SetSettingBool = (panel, name, value) => __vgui_SetSettingInt(panel, name, value == true);

/**
 * Polyfill for GetSetting calls (everything is a string internally)
 * @ignore
 */
export var __vgui_GetSettingBool = (panel, name) => __vgui_GetSettingString(panel, name) == true;
/**
 * Polyfill for GetSetting calls (everything is a string internally)
 * @ignore
 */
export var __vgui_GetSettingInt = (panel, name) => parseInt(__vgui_GetSettingString(panel, name));
/**
 * Polyfill for GetSetting calls (everything is a string internally)
 * @ignore
 */
export var __vgui_GetSettingFloat = (panel, name) => parseFloat(__vgui_GetSettingString(panel, name));