scripts/modules/root-app.js

var libs;
/**
 * @module CircuitJS
 */

/**
 * @example
 * // accessing CircuitRoot root application
 * App.RootApp()
 * 	.simulator().isRunning(true)
 */

var RootApp = function () {
	var app = new ApplicationPrototype();

	/**
	 * @type {CircuitJSCommandName}
	 */
	app.CommandName = {};

	function getContext(contextName) {
		var contexts = {};
		var i, name;
		for (i=0;i<window.frames.length;i++) {
			name = (window.frames[i] || {}).$moduleName || (((window.frames[i] || {}).__gwtModuleFunction || {}).name || '') + '';
			if (name) {
				contexts[name] = window.frames[i];
			}
		}
		if (contextName) {
			return contexts[contextName] || null;
		}
		return contexts;
	}

	var _commands = new ApplicationPrototype();

	var ufirst = function (s) {
		return s[0].toUpperCase() + s.substr(1);
	};

	app.bind(function processEvent(name, value, type) {
		switch (type) {
			case "register-menu-command":
				var commandName = name + ufirst(value);
				app.CommandName[commandName] = commandName;
				_commands.bind(commandName, function () {
					app.runCommand(name, value);
				});
			break;
		}
	});

	app.bind(
		/**
		 * get context of GWT circuitJS
		 * @method commands
		 * @memberof module:CircuitJS
		 * @returns {Object<CircuitJSCommandName,Function>}
		 */
		function commands() {
			return _commands;
		}
	);

	app.bind(
		/**
		 * get context of GWT circuitJS
		 * @method getSimulator
		 * @memberof module:CircuitJS
		 * @returns {object}
		 */
		function getSimulatorContext() {
			return getContext('circuitjs1') || null;
		}
	);

	var _simulator = libs.simulator(app);

	app.bind(
		/**
		 * @method simulator
		 * @memberof module:CircuitJS
		 * @returns {module:CircuitJS.Simulator}
		 */
		function simulator() {
			return _simulator;
		}
	);

	/**
	 * set new circuit configuration
	 * @method runCommand
	 * @memberof module:CircuitJS
	 * @param {CircuitJSCommandName} name
	 * @param {string} value
	 */

	var _socket = new libs.WindowSocket();
	app.bind(
		/**
		 * cross window communication protocol
		 * @method socket
		 * @memberof module:CircuitJS
		 * @returns {WindowSocket}
		 */
		function socket() {
			return _socket;
		}
	);

	if (window.parent) {
		app.socket().start();

		window.parent.postMessage("CircuitJS", "*");

		app.socket().on("message", function () {
			console.log("root:message", arguments);
		});

		app.socket().on('get:content', function (callback) {
			callback(null, app.simulator().getContent());
		});

		app.socket().on('set:content', function (content, callback) {
			app.simulator().setContent(content);
			callback(null);
		});

		app.socket().on("message:raw", function (ev) {
			console.log("root:message:raw", arguments);
			if (ev.data === "ping") {
				ev.target.postMessage("pong", "circuitjs");
			}
		});
	};

	return app;
};


Application.require(
	[
		"simulator :: simulator",
		"WindowSocket :: ../../../components/window-socket/index"
	]
).then(function () {
	libs = arguments[0];
	module.exports = RootApp;
}, console.error);