📄 treeviewtest4.xml
字号:
<?xml version="1.0"?><!-- this application uses an XML document as the tree model --><Application> <Window caption="Tree View Test" width="500" height="400"> <TreeView id="tree-view" left="5" top="5" bottom="5" right="80"/> <Button id="toggle-button" right="5" top="5" width="70" text="Toggle" toolTipText="Toggles row 2"/> <Button id="add-button" right="5" top="33" width="70" text="Add" toolTipText="Adds to row 4"/> <Button id="remove-button" right="5" top="61" width="70" text="Remove" toolTipText="Removes row 4"/> </Window> <Resources> <Script><![CDATA[function XmlTreeDataModel( oDocument ){ BiTreeDataModel.call( this, oDocument );}_p = XmlTreeDataModel.prototype = new BiTreeDataModel;_p.getHasChildren = function ( y ){ var n = this.getNodeAt( y ); return n.hasChildNodes();};_p.getNodeHashCode = function ( oNode ){ if ( !BiBrowserCheck.ie ) return BiObject.toHashCode( oNode ); // In MSXML there is no way to get an unique number of the IDomNode objects // and COM objects do not allow expando properties. What we do is pretty // ugly but it works for this demo. Use an array that stores all DOM nodes // and use the index in the array as the base. This means that the lookup is // O(n) :'( if ( !this._nodeList ) this._nodeList = []; var index = this._nodeList.indexOf( oNode ); if ( index == -1 ) { this._nodeList.push( oNode ); return "msxml-" + (this._nodeList.length - 1); } return "msxml-" + index;};_p.getNodeChildren = function ( oNode ){ return oNode.childNodes;};_p.getCellText = function ( x, y ){ switch ( x ) { case 0: return this.getNodeAt( y ).nodeName; case 1: return this.getNodeAt( y ).nodeType; case 2: return BiLabel.htmlToText( String(this.getNodeAt( y ).nodeValue) ); }};_p.getColumnCount = function () { return 3; };_p.getHeaderCellText = function ( x ){ switch ( x ) { case 0: return "nodeName"; case 1: return "nodeType"; case 2: return "nodeValue"; }};_p.getSortFunction = function ( x ){ switch ( x ) { case 0: return function ( n1, n2 ) { if ( n1.nodeName < n2.nodeName ) return -1; if ( n1.nodeName > n2.nodeName ) return 1; return 0; }; case 1: return function ( n1, n2 ) { if ( n1.nodeType < n2.nodeType ) return -1; if ( n1.nodeType > n2.nodeType ) return 1; return 0; }; case 2: return function ( n1, n2 ) { if ( String(n1.nodeValue) < String(n2.nodeValue) ) return -1; if ( String(n1.nodeValue) > String(n2.nodeValue) ) return 1; return 0; }; }}; ]]></Script> <Script><![CDATA[function TreeViewTest4(){ var d0 = new Date; var win = application.getWindow(); var g = application.getComponentById( "tree-view" ); var dm = new XmlTreeDataModel( application.getAdf().getDocument() ); g.setDataModel( dm ); g.getViewManager().getIndentColumn = function () { return 0; }; g.getViewManager().getShowRowHeaders = function () { return false; }; win.add( g ); win.setCaption( win.getCaption() + " - [" + (new Date - d0) + "]" ); // define context menu to use. This is a bit ineffecient since it creates // a new menu every time dm.getContextMenu = function ( x, y ) { var m = new BiMenu; var node = dm.getNodeAt( y ); var mi = new BiMenuItem( "Remove" ); mi.addEventListener( "action", function ( e ) { removeNode( y ); }); var mi2 = new BiMenuItem( "Append" ); mi2.addEventListener( "action", function ( e ) { addNodes( y, null ); }); var mi3 = new BiMenuItem( "Insert Before" ); mi3.addEventListener( "action", function ( e ) { addNodes( dm.getParent( y ), y ); }); var mi4 = new BiMenuItem; if ( dm.getHasChildren( y ) ) mi4.setHtml( "<b>" + (dm.getExpanded( y ) ? "Collapse" : "Expand") + "</b>" ); else { mi4.setHtml( "<b>Expand</b>" ); mi4.setEnabled( false ); } mi4.addEventListener( "action", function ( e ) { toggleRow( y ); }); m.add( mi4 ); m.add( new BiMenuSeparator ); m.add( mi ); m.add( mi2 ); m.add( mi3 ); return m; }; // Define tool tip. At least this time we reause the object and update it // in beforeshow var tt = new BiToolTip; tt.addEventListener( "beforeshow", function ( e ) { tt.setText( dm.getNodeAt( tt._rowIndex ).xml ); delete tt._rowIndex; }); dm.getToolTip = function ( x, y ) { tt._rowIndex = y; return tt; }; // add button event handlers application.getComponentById( "toggle-button" ).addEventListener( "action", function (e) { toggleRow( 2 ); } ); application.getComponentById( "add-button" ).addEventListener( "action", function (e) { addNodes( 4, null ); } ); application.getComponentById( "remove-button").addEventListener( "action", function (e) { removeNode( 4 ); } ); function removeNode( y ) { var oNode = dm.getNodeAt( y ); var p = oNode.parentNode; p.removeChild( oNode ); dm.removeRowAt( y ); } function addNodes( nParent, nBefore ) { var oParent = dm.getNodeAt( nParent ); var oBefore = null; if ( nBefore != null ) oBefore = dm.getNodeAt( nBefore ); var d = application.getAdf().getDocument(); var el = d.createElement( "New" ); el.setAttribute( "createdAt", String(new Date) ); var el2 = d.createElement( "New2" ); el2.setAttribute( "createdAt", String(new Date) ); el.appendChild( el2 ); oParent.insertBefore( el, oBefore ); dm.insertRowAt( el, nParent, nBefore != null ? nBefore - nParent - 1 : dm.getNodeChildren( oParent ).length - 1 ); } function toggleRow( y ) { dm.setExpanded( y, !dm.getExpanded( y ) ); }}TreeViewTest4.main = function () { new TreeViewTest4; }; ]]></Script> </Resources></Application>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -