Home Reference Source

vgui/obj/20-document.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; }; }();

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

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; }

var instance = false;

/**
 * Represents the root node of the Element tree.
 * This is the VguiJavascriptContext panel or a subclass.
 */

var Document = exports.Document = function (_Element) {
	_inherits(Document, _Element);

	/**
  * Creates a new Document. There can only be one document per context.
  * @private
  */
	function Document() {
		_classCallCheck(this, Document);

		var _this = _possibleConstructorReturn(this, (Document.__proto__ || Object.getPrototypeOf(Document)).call(this, __vgui_GetMe()));

		if (instance) throw new Error("Can't construct a second Document");
		instance = true;
		return _this;
	}

	/**
  * @override
  */


	_createClass(Document, [{
		key: "dispatchEvent",


		/**
   * @override
   */
		value: function dispatchEvent(e) {
			// Try handling locally
			if (!_get(Document.prototype.__proto__ || Object.getPrototypeOf(Document.prototype), "dispatchEvent", this).call(this, e)) return false;

			// Otherwise dispatch to Source Engine
			if (!e.bubbles) return;

			if (!(e instanceof SourceEvent)) return;

			if (!e.canEscape) return;

			__vgui_EmitEvent(e.data._serialize(e._type));
			return true;
		}

		/**
   * Looks recursively in child elements to find an Element that has an matching id.
   * @param {!string} id - The id to look for
   */

	}, {
		key: "getElementById",
		value: function getElementById(id) {
			var _rec = undefined;
			_rec = function rec(e, id) {
				if (e.id == id) return e;

				var children = e.childNodes;
				for (var i = 0; i < children.length; i++) {
					var result = _rec(children[i], id);
					if (result != null) return result;
				}
				return null;
			};
			return _rec(this, id);
		}

		/**
   * Opens the Vgui Editor for this panel
   */

	}, {
		key: "activateBuildMode",
		value: function activateBuildMode() {}

		/**
   * Registers a binding with the client.
   * This should trigger an immediate {@link SettingsEvent} on Document so register a listener before registering.
   * @param {string} key - The key to bind to
   * @param {string} type - The type of the bind, can be ["int", "string", "bool", "float]
   */

	}, {
		key: "registerBind",
		value: function registerBind(key) {
			var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "int";

			switch (type) {
				case "int":
					__vgui_SubscribeBindingInt(key);
					break;
				case "string":
					__vgui_SubscribeBindingString(key);
					break;
				case "bool":
					__vgui_SubscribeBindingBool(key);
					break;
				case "float":
					__vgui_SubscribeBindingFloat(key);
					break;
				default:
					throw new Error("Unknown binding type " + type);
			}
		}

		/**
   * Plays a sound effect.
   * soundName be defined in the empires soundscripts.
   * @param {string} soundName - The key of the sound in the soundscript to play.
   */

	}, {
		key: "playSound",
		value: function playSound(soundName) {
			if (!soundName) throw new Error("soundName not set");
			__vgui_PlaySound(soundName);
		}

		/**
   * @override
   */

	}, {
		key: "toString",
		value: function toString() {
			return "Document";
		}

		/**
   * Converts a source engine event to a DOM event and throws it at the right panel
   */

	}, {
		key: "__constructEvent",
		value: function __constructEvent(panelId, type, keyValues) {

			var target = this;
			if (panelId != 0) target = new Node(panelId)._specialize();
			var e = SourceEvent.__createSpecific(type, keyValues);

			try {
				target.dispatchEvent(e);
			} catch (err) {
				console.log("Error during " + e.type + " event dispatch: " + err.stack);
			}
		}
	}, {
		key: "parentNode",
		get: function get() {
			// Prevent traversal to panels above me
			return null;
		}
	}]);

	return Document;
}(Element);

var root = new Document();
if (!exports.Node) exports.Node = {};
exports.Node._cache = exports.Node._cache || {};
exports.Node._cache[root._id] = root;

/**
 * @ignore
 */
var document = exports.document = root;
global.document = root;

/**
 * @ignore
 */
var __vgui_SettingsChanged = exports.__vgui_SettingsChanged = function __vgui_SettingsChanged() {
	var event = new SettingsEvent();
	try {
		root.dispatchEvent(event);
	} catch (e) {
		console.error("Error occured during external event: " + e.stack);
	}
};

/**
 * @ignore
 */
var __vgui_BindTrigger = exports.__vgui_BindTrigger = function __vgui_BindTrigger(key, value) {
	var event = new BindEvent(key, value);
	try {
		root.dispatchEvent(event);
	} catch (e) {
		console.error("Error occured during bind event: " + e.stack);
	}
};