<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
	<title>Dynatree - Example</title>

    <link type="text/css" rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js" type="text/javascript"></script>

	<script src="../jquery/jquery.cookie.js" type="text/javascript"></script>

	<link href="../src/skin/ui.dynatree.css" rel="stylesheet" type="text/css">
	<script src="../src/jquery.dynatree.js" type="text/javascript"></script>

	<!-- jquery-contextmenu (https://github.com/mar10/jquery-contextmenu/) -->
<!-- 
    <script src="../../eclipsews/jquery-contextmenu/jquery.contextmenu.js" type="text/javascript"></script>
 -->
    <script src="jquery.contextmenu.js/jquery.contextmenu.js" type="text/javascript"></script>


	<!-- Start_Exclude: This block is not part of the sample code -->
	<link href="prettify.css" rel="stylesheet">
	<script src="prettify.js" type="text/javascript"></script>
	<link href="sample.css" rel="stylesheet" type="text/css">
	<script src="sample.js" type="text/javascript"></script>
	<!-- End_Exclude -->

<style type="text/css">
.ui-menu{
    min-width: 100px;
}
</style>

<script type="text/javascript">

// --- Implement Cut/Copy/Paste --------------------------------------------
var clipboardNode = null;
var pasteMode = null;

function copyPaste(action, node) {
    // Strip leading '#'
    action = action.replace(/^#/, "");
	switch( action ) {
	case "cut":
	case "copy":
		clipboardNode = node;
		pasteMode = action;
		break;
	case "paste":
		if( !clipboardNode ) {
			alert("Clipoard is empty.");
			break;
		}
		if( pasteMode == "cut" ) {
			// Cut mode: check for recursion and remove source
			var isRecursive = false;
			var cb = clipboardNode.toDict(true, function(dict){
				// If one of the source nodes is the target, we must not move
				if( dict.key == node.data.key )
					isRecursive = true;
			});
			if( isRecursive ) {
				alert("Cannot move a node to a sub node.");
				return;
			}
			node.addChild(cb);
			clipboardNode.remove();
		} else {
			// Copy mode: prevent duplicate keys:
			var cb = clipboardNode.toDict(true, function(dict){
				dict.title = "Copy of " + dict.title;
				delete dict.key; // Remove key, so a new one will be created
			});
			node.addChild(cb);
		}
		clipboardNode = pasteMode = null;
		break;
	default:
		alert("Unhandled clipboard action '" + action + "'");
	}
};

// --- Init dynatree during startup ----------------------------------------

$(function(){

$("#tree").dynatree({
	persist: true,
	onActivate: function(node) {
		$("#echoActivated").text(node.data.title + ", key=" + node.data.key);
	},
	onKeydown: function(node, event) {
		// Eat keyboard events, when a menu is open
		if( $(".contextMenu:visible").length > 0 ){
			return false;
		}
		switch( event.which ) {

		// Open context menu on [Space] key (simulate right click)
		case 32: // [Space]
			$("#tree").contextmenu("open", $(".dynatree-title", node.span));
			return false;

		// Handle Ctrl-C, -X and -V
		case 67:
			if( event.ctrlKey ) { // Ctrl-C
				copyPaste("copy", node);
				return false;
			}
			break;
		case 86:
			if( event.ctrlKey ) { // Ctrl-V
				copyPaste("paste", node);
				return false;
			}
			break;
		case 88:
			if( event.ctrlKey ) { // Ctrl-X
				copyPaste("cut", node);
				return false;
			}
			break;
		}
	},
	/*Load lazy content (to show that context menu will work for new items too)*/
	onLazyRead: function(node){
		node.appendAjax({
			url: "sample-data2.json"
		});
	},
	/* D'n'd, just to show it's compatible with a context menu.
	   See http://code.google.com/p/dynatree/issues/detail?id=174 */
	dnd: {
		preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
		onDragStart: function(node) {
			return true;
		},
		onDragEnter: function(node, sourceNode) {
			if(node.parent !== sourceNode.parent)
				return false;
			return ["before", "after"];
		},
		onDrop: function(node, sourceNode, hitMode, ui, draggable) {
			sourceNode.move(node, hitMode);
		}
	}
}); // $.dynatree

/* Enable the context menu for all node titles */
$("#tree").contextmenu({
    delegate: ".dynatree-title",
    menu: "#menu",
    select: function(event, ui) {
        var cmd = ui.item.find(">a").attr("href"),
            target = event.relatedTarget,
            node = $.ui.dynatree.getNode(target);
        window.console && console.log(cmd + " - " + node);
        node.activate();
        copyPaste(cmd, node);
    }
});

}); // $(function){...}
</script>
</head>

<body class="example">
	<h1>Example: Context Menu</h1>
	<p class="description">
		Implementation of a context menu. Right-click a node and see what happens.<br>
		Also [space] key is supported to open the menu.<br>
		<br>
		This example also demonstrates, how to copy or move branches and how
		to implement clipboard functionality.
		<br>
		A keyboard handler implements Cut, Copy, and Paste with <code>Ctrl-X</code>,
		<code>Ctrl-C</code>, <code>Ctrl-V</code>.
	</p>
	<p class="description">
        <b>NOTE:</b><br>
		This sample uses the jquery-contextmenu plugin (which in turn depends 
		on jQuery UI 1.9+).
		Visit  <a href="https://github.com/mar10/jquery-contextmenu/" target="_blank">the project page at github</a> for usage and more information.
		<br>
		Please understand, that this separate plugin is not part of dynatree.
		Questions and bug reports should be directed there.
	</p>

	<!-- Definition tree structure -->
	<div id="tree">
		<ul>
			<li id="id1" title="Look, a tool tip!">item1 with key and tooltip
			<li id="id2" class="activate">item2: activated on init
			<li id="id3" class="folder">Folder with some children
				<ul>
					<li id="id3.1">Sub-item 3.1
					<li id="id3.2">Sub-item 3.2
				</ul>

			<li id="id4" class="expanded">Document with some children (expanded on init)
				<ul>
					<li id="id4.1">Sub-item 4.1
					<li id="id4.2">Sub-item 4.2
				</ul>

			<li id="id5" class="lazy folder">Lazy folder
		</ul>
	</div>

	<div>Selected node: <span id="echoActivated">-</span></div>

    <!-- Definition of the menu -->
    <ul id="menu" class="ui-helper-hidden">
        <li><a href="#cut"><span class="ui-icon ui-icon-scissors"></span>Cut</a></li>
        <li><a href="#copy"><span class="ui-icon ui-icon-copy"></span>Copy</a></li>
        <li><a href="#paste"><span class="ui-icon ui-icon-clipboard"></span>Paste</a></li>
        <li>----</li>
        <li><a href="#edit" class="ui-state-disabled"><span class="ui-icon ui-icon-pencil"></span>Edit</a></li>
        <li><a href="#delete" class="ui-state-disabled"><span class="ui-icon ui-icon-trash"></span>Delete</a></li>
    </ul>
	<!-- Start_Exclude: This block is not part of the sample code -->
	<hr>
	<p class="sample-links  no_code">
		<a class="hideInsideFS" href="http://dynatree.googlecode.com">jquery.dynatree.js project home</a>
		<a class="hideOutsideFS" href="#">Link to this page</a>
		<a class="hideInsideFS" href="samples.html">Example Browser</a>
		<a href="#" id="codeExample">View source code</a>
	</p>
	<pre id="sourceCode" class="prettyprint" style="display:none"></pre>
	<!-- End_Exclude -->
</body>
</html>
