Location: Session Resource Workspace @ 439f3f595a77 / JavaScript_template.txt

Author:
Dougal Cowan <devnull@localhost>
Date:
2012-11-15 08:29:09+13:00
Desc:
updated metadata template
Permanent Source URI:
https://models.physiomeproject.org/workspace/sessionresources/rawfile/439f3f595a7799ee785a2b32e174c20e9d9e62ca/JavaScript_template.txt

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="layout-diagram" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" style="overflow: auto" onload="setupDocument()">
<hbox>
	<scale id="zoom_scale" value="10" min="7" max="14" flex="4"/>
	<label value="Zoom" control="zoom_scale" flex="1"/>
	<button id="reset_button" label="Reset View" flex="1"/>
	<spacer flex="34"/>
</hbox>
<script>
<![CDATA[
window.model_entities =
  {

			CLICKABLE_ID:

			{	id: "Ca_i",
				y: 'COMPONENT_NAME/VARIABLE',
				x: 'COMPONENT_NAME/VARIABLE',
				graph: 'NAME_OF_GRAPH_TO_DISPLAY_TRACE',
				colour: '#FF0000',
				linestyle: 'none'
			}
			
 // Repeat the above section for each controllable graph trace.
 // Remember to add a comma to each repeat after the final },
 // except for the final one!
 
};

function flushVisibilityInformation(entity_id, entity_colour)
{
	var message = "";
	var entity;

	if (typeof pcenv != "undefined")
	{
		for (var i in window.model_entities)
		{
			entity = window.model_entities[i];
			if (typeof entity_id == "undefined" || entity_id == window.model_entities[i].id)
			{
				pcenv.selectTrace
				(
					entity.graph,
					entity.x,
					entity.y,
					typeof entity_colour == "undefined" ? "" : entity_colour,
					entity.linestyle
				);
			}
			if (entity.linestyle != "none")
				message += i + ", ";
		}
		pcenv.status(message == "" ? "No fluxes displayed" : "Displaying flux of " + message.slice(0, -2));
	}
}

function processSelectEntity(event)
{
	if (typeof pcenv != "undefined")
		pcenv.status("In processSelectEntity");

	var entity = window.model_entities[window.svgIdToName[event.currentTarget.id]];

	switch(entity.linestyle)
	{
	case "none":
		entity.linestyle = "lines";
		highlightEntity(event.currentTarget.id);
		break;
	case "lines":
		entity.linestyle = "none";
		unlightEntity(event.currentTarget.id);
		break;
	}

	flushVisibilityInformation(entity.id);
}

function processContext(event)
{
	// if (event.button != 2)
	//   return true;

	var entity = window.model_entities[window.svgIdToName[event.currentTarget.id]];

	if (entity.context == null)
		return true;

	var menu = document.getElementById("entityContextMenu");

	for (var c = menu.firstChild, x = null; c != null; c = x)
	{
		x = c.nextSibling;
		menu.removeChild(c);
	}

	for (var i in entity.context)
	{
		var item = entity.context[i];

		var mitem = document.createElementNS
		(
			"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
			"menuitem"
		);

		mitem.setAttribute("label", item.label);
		mitem.setAttribute("url", item.url);
		mitem.addEventListener("command", processShowEntityURL, false);
		menu.appendChild(mitem);
	}

	menu.showPopup(window.diagram, event.screenX, event.screenY, "context");

	event.stopPropagation;
	return false;
}

function processShowEntityURL(event)
{
	url = event.target.getAttribute("url");

	window.open(url);

	var hl = document.getElementById("hidden-link");
	hl.href = url;

	// This is ugly, but it is one way to force everything through the proper
	// external handler...
	var evt = document.createEvent("HTMLEvents");
	evt.initEvent("click", true, true);
	hl.dispatchEvent(evt);
}

function highlightEntity(id)
{
	for (var path = document.getElementById(id + "_path1"), i = 1; path != null; path = document.getElementById(id + "_path" + ++i))
	{
		if (!(i in window.model_entities[window.svgIdToName[id]].path_colours))
			window.model_entities[window.svgIdToName[id]].path_colours[i] = path.attributes.getNamedItem("stroke").value;
		path.attributes.getNamedItem("stroke").value = "#ff0000";
	}
}

function highlightEntityOnRollover(event) {
	if (window.model_entities[window.svgIdToName[event.currentTarget.id]].colour)
		flushVisibilityInformation(event.currentTarget.id, "#ffffff");

	if (window.model_entities[window.svgIdToName[event.currentTarget.id]].linestyle == "none")
		highlightEntity(event.currentTarget.id);
}

function unlightEntity(id)
{
	for (var path = document.getElementById(id + "_path1"), i = 1; path != null; path = document.getElementById(id + "_path" + ++i))
		path.attributes.getNamedItem("stroke").value = window.model_entities[window.svgIdToName[id]].path_colours[i];
}

function unlightEntityOnRollover(event) {
	if (window.model_entities[window.svgIdToName[event.currentTarget.id]].colour)
		flushVisibilityInformation(event.currentTarget.id, window.model_entities[window.svgIdToName[event.currentTarget.id]].colour);

	if (window.model_entities[window.svgIdToName[event.currentTarget.id]].linestyle == "none")
		unlightEntity(event.currentTarget.id);
}

var mouseDown = false;
var initial_x;
var initial_y;
var viewBox;

function startDrag(event)
{
	if (event.button)
		return true;

	mouseDown = true;

	initial_x = parseInt(currentZoom * event.pageX + parseInt(viewBox.value.match(/^-?\d+/)[0]));
	initial_y = parseInt(currentZoom * event.pageY + parseInt(viewBox.value.match(/^-?\d+\s+(-?(\d+))/)[1]));
}

function stopDrag(event)
{
	if (!event.button)
		mouseDown = false;
}

function moveDrag(event)
{
	if (mouseDown == true)
		viewBox.value = viewBox.value.replace(/^-?\d+\s+-?\d+/, parseInt(initial_x - currentZoom * event.pageX) + " " + parseInt(initial_y - currentZoom * event.pageY));
}

function reset()
{
	var zoom_scale = document.getElementById("zoom_scale")
	zoom_scale.value = zoom_scale.originalValue;
	viewBox.value = viewBox.originalValue;
}

var initialZoom;
var currentZoom = 1;
var initialHeight;
var initialWidth;

function zoomDiagram(event)
{
	currentZoom = initialZoom / event.currentTarget.value;
	viewBox.value = viewBox.value.replace(/\d+\s+\d+$/, parseInt(initialHeight * currentZoom) + " " + parseInt(initialWidth * currentZoom));
}

function setupDocument()
{
	flushVisibilityInformation();

	window.diagram = document.getElementById("sachse");

	window.svgIdToName = {};

	for (var name in window.model_entities)
	{
		var id = window.model_entities[name].id;
		window.model_entities[name].path_colours = [];

		var svg = document.getElementById(id);
		window.svgIdToName[id] = name;

		svg.addEventListener("click", processSelectEntity, false);
		svg.addEventListener("contextmenu", processContext, false);
		svg.addEventListener("mouseover", highlightEntityOnRollover ,false);
		svg.addEventListener("mouseout", unlightEntityOnRollover, false);
	}

	document.getElementsByTagName("svg")[0].addEventListener("mousedown", startDrag, false);
	document.addEventListener("mouseup", stopDrag, false);
	document.addEventListener("mousemove", moveDrag, false);
	document.getElementById("reset_button").addEventListener("click", reset, false);

	document.getElementById("zoom_scale").addEventListener("change", zoomDiagram, false);
	document.getElementById("zoom_scale").originalValue = document.getElementById("zoom_scale").value;

	viewBox = document.getElementsByTagName("svg")[0].attributes.getNamedItem("viewBox");
	viewBox.originalValue = viewBox.value;

	initialZoom = document.getElementById("zoom_scale").value;
	initialHeight = parseInt(viewBox.value.match(/(\d+)\s+\d+$/)[1]);
	initialWidth = parseInt(viewBox.value.match(/\d+$/)[0]);
}

]]>
</script>

<popupset>
  <menupopup id="entityContextMenu" />
</popupset>

// REMOVE THE XML HEADER FROM YOUR SVG DIAGRAM
// AND THEN PASTE IT IN THIS SPACE, REPLACING THIS COMMENT!

</window>