Home Reference Source

vgui/obj/15-element.js

"use strict";

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
 * Represents an element in the res file
 */
var Element = exports.Element = function (_Node) {
	_inherits(Element, _Node);

	function Element(id) {
		_classCallCheck(this, Element);

		var _this = _possibleConstructorReturn(this, (Element.__proto__ || Object.getPrototypeOf(Element)).call(this, 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 = {};
		return _this;
	}

	/**
  * Returns the fieldName of the element
  */


	_createClass(Element, [{
		key: "setAttribute",


		/**
   * Sets an attribute on the element
   * In order to support a new attribute an overloading class needs to add the setting type to _attrHandler
   */
		value: function 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
   */

	}, {
		key: "getAttribute",
		value: function 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
   */

	}, {
		key: "dispatchEvent",
		value: function 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
   */

	}, {
		key: "addEventListener",
		value: function 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
   */

	}, {
		key: "startAnimation",
		value: function startAnimation(animationName) {
			if (!animationName) throw new Error("Parameter animationName not set");
			return __vgui_StartAnimation(this._id, animationName);
		}

		/**
   * Removes a registered event listener
   */

	}, {
		key: "removeEventListener",
		value: function removeEventListener(type, listener) {
			if (this._eventHandler[type] == undefined) this._eventHandler[type] = [];

			this._eventHandler[type] = this._eventHandler[type].filter(function (e) {
				return e != listener;
			});
			if (this._eventHandler[type].length == 0) this._eventHandler[type] = undefined;
		}
	}, {
		key: "id",
		get: function get() {
			var result = this.getAttribute("fieldName");
			if (result == "") return null;
			return result;
		}
	}]);

	return Element;
}(Node);

/**
 * Polyfill for SetSettingBool (represented as int internally)
 * @ignore
 */


var __vgui_SetSettingBool = exports.__vgui_SetSettingBool = function __vgui_SetSettingBool(panel, name, value) {
	return __vgui_SetSettingInt(panel, name, value == true);
};

/**
 * Polyfill for GetSetting calls (everything is a string internally)
 * @ignore
 */
var __vgui_GetSettingBool = exports.__vgui_GetSettingBool = function __vgui_GetSettingBool(panel, name) {
	return __vgui_GetSettingString(panel, name) == true;
};
/**
 * Polyfill for GetSetting calls (everything is a string internally)
 * @ignore
 */
var __vgui_GetSettingInt = exports.__vgui_GetSettingInt = function __vgui_GetSettingInt(panel, name) {
	return parseInt(__vgui_GetSettingString(panel, name));
};
/**
 * Polyfill for GetSetting calls (everything is a string internally)
 * @ignore
 */
var __vgui_GetSettingFloat = exports.__vgui_GetSettingFloat = function __vgui_GetSettingFloat(panel, name) {
	return parseFloat(__vgui_GetSettingString(panel, name));
};