Home Reference Source

vgui/obj/10-node.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"); } }

/** Represents a placeholder for a more specific Node type
 * Should not be used directly
 */
var Node = exports.Node = function () {
	/**
  * Creates a Node with a given VPANEL id
  */
	function Node(id) {
		_classCallCheck(this, Node);

		if (!id) throw new Error("Invalid Panel id");
		this._id = id;

		if (Node._cache == undefined) Node._cache = {};
	}

	/**
  * Returns a Node that is the parent of this node. If there is no such node, like if this node is the top of the tree or if doesn't participate in a tree, this property returns null.
  */


	_createClass(Node, [{
		key: "toString",


		/**
   * Returns a string representation of the Node
   */
		value: function toString() {
			return this._className;
		}

		/**
   * Return a specialized version of this node based on Panel type
   * Tries to return the same object given the same id
   * Does not support ingame panel deletion (might cause duplicate ids)
   * @access private
   */

	}, {
		key: "_specialize",
		value: function _specialize() {
			if (Node._cache[this._id] != undefined) return Node._cache[this._id];
			var result = this;
			switch (this._className) {
				case "Label":
					result = new Label(this._id);
					break;
				case "CVguiJavascriptContext":
					result = new JsChild(this._id);
					break;
				case "Panel":
					result = new Panel(this._id);
					break;
				case "EditablePanel":
					result = new EditablePanel(this._id);
					break;
				case "CEmpVguiSteamUser":
					result = new SteamUser(this._id);
					break;
				case "Button":
					result = new Button(this._id);
					break;
				case "CAvatarImagePanel":
					result = new CAvatarImage(this._id);
					break;
				case "ImagePanel":
					result = new ImagePanel(this._id);
					break;
				default:
					result = new Element(this._id);
					console.log("Unknown element class " + this._className + " found. Assuming Element");
					break;
			}

			Node._cache[this._id] = result;
			return result;
		}
	}, {
		key: "_getAttrInt",
		value: function _getAttrInt(key) {
			return __vgui_GetSettingInt(this._id, key);
		}
	}, {
		key: "_getAttrString",
		value: function _getAttrString(key) {
			return __vgui_GetSettingString(this._id, key);
		}
	}, {
		key: "_getAttrFloat",
		value: function _getAttrFloat(key) {
			return __vgui_GetSettingFloat(this._id, key);
		}
	}, {
		key: "_setAttrInt",
		value: function _setAttrInt(key, value) {
			__vgui_SetSettingInt(this._id, key, value);
		}
	}, {
		key: "_setAttrString",
		value: function _setAttrString(key, value) {
			__vgui_SetSettingString(this._id, key, value);
		}
	}, {
		key: "_setAttrFloat",
		value: function _setAttrFloat(key, value) {
			__vgui_SetSettingFloat(this._id, key, value);
		}
	}, {
		key: "parentNode",
		get: function get() {
			var id = __vgui_GetParent(this._id);
			if (id === 0) return null;
			var node = new Node(id);
			return node._specialize();
		}

		/**
   * @access protected
   * Returns the name of the C++ class that implements this control
   */

	}, {
		key: "_className",
		get: function get() {
			return __vgui_GetType(this._id);
		}

		/**
   * Returns the children of this node
   */

	}, {
		key: "childNodes",
		get: function get() {
			var noChildren = __vgui_GetChildCount(this._id);
			var result = [];
			for (var i = 0; i < noChildren; i++) {
				var id = __vgui_GetNthChild(this._id, i);
				if (id == 0) continue;

				result.push(new Node(id)._specialize());
			}
			return result;
		}
	}]);

	return Node;
}();