mxGraph淺入之 makeDraggable

makeDraggable

主要用來構建拖動源,鼠標拖動實現向graph圖中添加圖形。文章轉譯自mxGraph官方github,

官方定義

將給定的DOM元素配置爲指定graph的拖動源。返回一個新的mxDragSource對象。如果mxDragSource.guidesEnabled爲true,則必須在funct中使用x和y參數來匹配預覽位置。

makeDraggable: function(element,   // 要被拖動的DOM元素
                        graphF,    // 一個mxGraph對象,作爲drop到的目標或者接受鼠標事件並返回當前mxGraph的一個函數。
                        funct,     // 拖動成功之後執行的方法
                        dragElement,  // 可選的DOM節點用於拖動預覽
                        dx,        // 可選,光標和拖動預覽框之間的水平偏移量
                        dy,        // 可選,光標和拖動預覽框之間的垂直偏移量
                        autoscroll,// 可選的布爾值,指定是否使用autoscroll,默認是mxGraph.autoscroll(默認是true,即從拖動面板拖動圖元到graph區域的邊界時,會進行方向上的移動,類似滾動條效果)
                        scalePreview, // 可選的布爾值,指定預覽元素是否應該根據圖形縮放比例進行縮放。如果爲true,那麼偏移量也會被縮放。默認爲false
                        highlightDropTargets,  // 可選的布爾值,指定drop target是否應該高亮顯示,默認爲true
                        getDropTarget)  // 可選的函數,用於返回drop target的給定位置(x,y),默認是mxGraph.getCellAt

官方demo:

<html>
<head>
	<title>Dragsource example for mxGraph</title>

	<!-- Sets the basepath for the library if not in same directory -->
	<script type="text/javascript">
		mxBasePath = '../src';
	</script>

	<!-- Loads and initializes the library -->
	<script type="text/javascript" src="../src/js/mxClient.js"></script>

	<script type="text/javascript">
		function main()
		{
			// 檢查瀏覽器是否支持
			if (!mxClient.isBrowserSupported())
			{
				// 不支持,提示信息
				mxUtils.error('Browser is not supported!', 200, false);
			}
			else
			{
				// 拖動時顯示導航線
				mxGraphHandler.prototype.guidesEnabled = true;
				
			    // 拖動時,按下Alt鍵不顯示導航線
			    mxGuide.prototype.isEnabledForEvent = function(evt)
			    {
			    	return !mxEvent.isAltDown(evt);
			    };
				
				// Enables snapping waypoints to terminals
				mxEdgeHandler.prototype.snapToTerminals = true;
				
				var graphs = [];
				
				// 創建兩個graph區域
				for (var i = 0; i < 2; i++)
				{
					var container = document.createElement('div');
					container.style.overflow = 'hidden';
					container.style.position = 'relative';
					container.style.width = '321px';
					container.style.height = '241px';
					container.style.background = 'url(\'editors/images/grid.gif\')';
					container.style.cursor = 'default';

					document.body.appendChild(container);
					
					var graph = new mxGraph(container);
					graph.gridSize = 30;
					
					// Uncomment the following if you want the container
					// to fit the size of the graph
					//graph.setResizeContainer(true);
					
					// Enables rubberband selection
					new mxRubberband(graph);
					
                    //獲取用於插入新單元格的默認父元素。這通常是根的第一個子結點
					var parent = graph.getDefaultParent();
									
					// 事務開始
					graph.getModel().beginUpdate();
					try
					{
                        // 繪製初始graph上的圖形內容
						var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
						var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
						var e1 = graph.insertEdge(parent, null, '', v1, v2);
					}
					finally
					{
						// 事務結束
						graph.getModel().endUpdate();
					}
					
					graphs.push(graph);
				}
				
				// 返回鼠標移入的graph對象(即該參數的第二種情況)
				var graphF = function(evt)
				{
					var x = mxEvent.getClientX(evt);
					var y = mxEvent.getClientY(evt);
					var elt = document.elementFromPoint(x, y);
					
					for (var i = 0; i < graphs.length; i++)
					{
						if (mxUtils.isAncestorNode(graphs[i].container, elt))
						{
							return graphs[i];
						}
					}
					
					return null;
				};
				
				// 在指定位置插入cell(拖動完成後執行的方法)
				var funct = function(graph, evt, target, x, y)
				{
					var cell = new mxCell('Test', new mxGeometry(0, 0, 120, 40));
					cell.vertex = true;
					var cells = graph.importCells([cell], x, y, target);

					if (cells != null && cells.length > 0)
					{
						graph.scrollCellToVisible(cells[0]);
						graph.setSelectionCells(cells);
					}
				};
				
				// 創建一個作爲拖動源得到DOM元素
				var img = mxUtils.createImage('images/icons48/gear.png');
				img.style.width = '48px';
				img.style.height = '48px';
				document.body.appendChild(img);
				
				// 在IE中禁用內置DnD
				if (mxClient.IS_IE)
				{
					mxEvent.addListener(img, 'dragstart', function(evt)
					{
						evt.returnValue = false;
					});
				}
				// 創建拖動預覽元素.
				var dragElt = document.createElement('div');
				dragElt.style.border = 'dashed black 1px';
				dragElt.style.width = '120px';
				dragElt.style.height = '40px';
                // 構建mxDragSource實例
				var ds = mxUtils.makeDraggable(img, graphF, funct, dragElt, null, null, graph.autoscroll, true);
				
				// Redirects feature to global switch. Note that this feature should only be used
				// if the the x and y arguments are used in funct to insert the cell.
				ds.isGuidesEnabled = function()
				{
					return graph.graphHandler.guidesEnabled;
				};
				// 在graph外部顯示原始的拖動源圖標樣子
				ds.createDragElement = mxDragSource.prototype.createDragElement;
			}
		};
	</script>
</head>

<!-- 作爲graph的容器 -->
<body οnlοad="main();">
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章