Ajax TreeCreated by Colin Mollenhour DescriptionThis tree class can be used with or without Ajax. You simply define node types and supply options, hooks, handlers, etc. for each type and start creating nodes. All node types can be created using the same constructor by passing it the type as a string. The way it handles data and reacts to user input is completely customizable for each individual node type. LIVE DEMOhttp://colin.mollenhour.com- /ajaxtree- /ajaxtreetest.html Summary | Created by Colin Mollenhour | | Usage specifications and examples. | | | | | | The server response is expected to be in JSON format. | | | | | | | | Use Ajax.Tree.create to create a customized tree class. | | | | Returns a constructor for a new class that is specific to the structure passed. | | All options are unique per type, and can be accessed inside class functions by “this.options.<option>”. | | The className for the newly created element div and span elements, defaults to the node type | | | | If true, the <mark> will get the className ‘leaf’ and clicks will not fire a <toggleChildren>. | | If specified, <getContents> will be called on clicking the <mark>. | | If true, the new element id will be prepended with it’s parent’s id. | | The insertion function used to handle the node “data”. | | | | Use this class as the base for your own customized Ajax.Tree class. | | | | The DOM elements created on instantiation of a new node. | | Hooks are provided for fine control over interactivity. | | Called to build query parameters for the Ajax.Request. | | Called after a node’s children have been cleared. | | Called on user clicking the mark. | | Called after all new nodes have been constructed. | | Called after a node has been disposed. | | Called immediately after the Ajax.Request is sent. | | Called after the Ajax.Request onComplete | | Called by the constructor after the basic node element has been built. | | All flags listed here are accessed by this.<flag> and initialized to the values on the left. | | | | | | All properties are accessed by this.<property>. |
Using the JavascriptAjax.Tree is designed to be used in two steps:
Handling Server RequestsThe default format for server requests, which can be customized via the <callback> hook| getContents | ’tree=getContents&id=’this.element.id |
Now just handle those post variables however you like and use correct format in your Server Response.
Server ResponseThe server response is expected to be in JSON format. It can be sent either in the responseText, or an X-JSON header. The X-JSON header evaluated result is check for the presence of a nodes key, which is expected to be an Array. If the X-JSON header does not contain a nodes key, the responseText is evaluated. If the evaluated responseText does not have a nodes key, no nodes are built and this is not an error. Any related hooks will still be called and passed the server response variables as usual.
Specifications- Must contain a “nodes” array of nodes.
- If the “nodes” array does not exist, no nodes will be created. However, the <onContentLoaded> and <onGetContentsComplete> hooks will still be called.
- Each element of “nodes” must contain:
| id | The new node’s element id. See <prependParentId>. | | type | The new node’s <type> keyword, as defined in the structure passed to Ajax.Tree.create. | | data | Either a string (for default <insertion>) or some other object to be passed to <insertion>. |
- Each node can contain nested nodes by adding a “nodes” array to a node.
- Other attributes can be scattered throughout the response data. It can be accessed by the <onContentLoaded> and <getContentsComplete> hooks or for each individual node through the <insertion> option.
ExampleThis response example coincides with the Ajax.Tree.Usage example{ nodes: [ { id: 'dir-1', type: 'directory', data: 'Work', nodes: [ { id: 'dir-1_subdir-1', type: 'directory', data: 'Accounting' } ] }, { id: 'file-1', type: 'file', data: { name: 'Presentation.ppt', size: '30.2KB', fileid: 53255596 }, } ] }
Ajax.TreeAjax.Tree is the utility class for Ajax.Tree.Base. Using create, you can create a new class that is an extension of Ajax.Tree.Base. Using this new constructor, you can build a tree dynamically using multiple node types without the need for separate constructors. The node types are defined in a hash passed to create which also defines settings and handlers for each type. Summary | Use Ajax.Tree.create to create a customized tree class. | | | | Returns a constructor for a new class that is specific to the structure passed. | | All options are unique per type, and can be accessed inside class functions by “this.options.<option>”. | | The className for the newly created element div and span elements, defaults to the node type | | | | If true, the <mark> will get the className ‘leaf’ and clicks will not fire a <toggleChildren>. | | If specified, <getContents> will be called on clicking the <mark>. | | If true, the new element id will be prepended with it’s parent’s id. | | The insertion function used to handle the node “data”. |
UsageUse Ajax.Tree.create to create a customized tree class. This example creates a simple file browser tree. It also demonstrates the use of an overridden insertion, and the <onClick> hook. See Ajax.Tree.Base.Usage for information on using the constructor produced by this example. For more detail on defining your own types and handling server responses, see Options and Ajax.Tree.Base. Ajax.FileBrowser = Ajax.Tree.create({ types: { directory: { page: 'filebrowser.php' }, file: { leafNode: true, insertion: function(element,data){ Element.update(element, data.name+' Size: '+data.size); this.dlLink = Builder.node('span',{ id: data.fileid, className: 'download' },['download']); this.dlLinkObserver = this.options.download.bindAsEventListener(this); Event.observe(this.dlLink,'click',this.dlLinkObserver); element.appendChild(this.dlLink); }, dispose: function(){ Event.stopObserving(this.dlLink,'click',this.dlLinkObserver); }, onClick: function(event){ showThumbnail(this.element.id); }, download: function(event){ window.location.href = 'getfile.php?fileid='+Event.element(event).id; } } } });
create| create: function( | structure | ) |
|
Returns a constructor for a new class that is specific to the structure passed. This new class is an extension of Ajax.Tree.Base | structure | The structure that defines node types and their options and hooks. |
OptionsAll options are unique per type, and can be accessed inside class functions by “this.options.<option>”.
classNameThe className for the newly created element div and span elements, defaults to the node type
leafNodeIf true, the <mark> will get the className ‘leaf’ and clicks will not fire a <toggleChildren>.
pageIf specified, <getContents> will be called on clicking the <mark>.
prependParentIdIf true, the new element id will be prepended with it’s parent’s id.
Ajax. Tree.BaseAjax.Tree.Base is designed to be extended using Ajax.Tree.create. The extension of this base class lets you add nodes to a tree in any way you like. Summary | Use this class as the base for your own customized Ajax.Tree class. | | | | The DOM elements created on instantiation of a new node. | | Hooks are provided for fine control over interactivity. | | Called to build query parameters for the Ajax.Request. | | Called after a node’s children have been cleared. | | Called on user clicking the mark. | | Called after all new nodes have been constructed. | | Called after a node has been disposed. | | Called immediately after the Ajax.Request is sent. | | Called after the Ajax.Request onComplete | | Called by the constructor after the basic node element has been built. | | All flags listed here are accessed by this.<flag> and initialized to the values on the left. | | | | | | All properties are accessed by this.<property>. |
UsageUse this class as the base for your own customized Ajax.Tree class. The Ajax.Tree.Base class is not intended to be used directly. Instead, create your own extension of this class with Ajax.Tree.create. With the extended class, you may now start creating nodes using a typical javascript constructor.
constructorThe class returned from Ajax.Tree.create can be instantied with the following argumentsArguments| parent | The id or element of the new node’s parent | | id | The new node’s id | | type | The new node’s type, corresponding to one of the keys of the types hash passed to Ajax.Tree.create | | data | A string or other object containing data to be processed by the type’s insertion function |
Example codeSee Ajax.Tree.Usage for the corresponding, more detailed usage example of Ajax.Tree.create <div id="file_browser"></div> <script type="text/javascript"> Ajax.FileBrowser = Ajax.Tree.create({...}); new Ajax.FileBrowser('file_browser','root','directory','My Files'); </script>
DOM ElementsThe DOM elements created on instantiation of a new node. All DOM elements are accessible using this.<element>. | parent | (div) - the node’s parent element | | element | (div) - the primary div, contains mark, span and children. has className: ‘treenode’ and this.options.className || this.type | | mark | (span) - the expanded/collapsed mark. has className: ‘mark’ and one of ‘expanded’, ‘collapsed’, ‘leaf’ | | span | (span) - the primary container for node data. has className: ‘treedata’ and this.options.className || this.type |
HooksHooks are provided for fine control over interactivity. Implement hooks separately for each node type. NOTEFor all hooks, this is a reference to the node’s class instance. ExampleAlerts the user as to how many child nodes are loaded. TreeOfNodes = Ajax.Tree.create({ types: { node: { page: 'nodes.php', onContentLoaded: function(xhr,json){ alert('Got '+$H(json.nodes).keys().length+' new nodes'); } } } });
callbackCalled to build query parameters for the Ajax.Request. Should return a query string. | id | The id of the node’s element. |
onClearContentsCalled after a node’s children have been cleared.
onClickCalled on user clicking the mark. | event | The click event object |
onContentLoadedCalled after all new nodes have been constructed. | xhr | The XMLHttpRequest transport | | json | The evaluated X-JOSN header object |
onDisposeCalled after a node has been disposed.
onGetContentsCalled immediately after the Ajax.Request is sent. | request | The Ajax.Request object |
onGetContentsCompleteCalled after the Ajax.Request onComplete | xhr | The XMLHttpRequest transport | | json | The evaluated X-JOSN header object |
insertionCalled by the constructor after the basic node element has been built. | element | The tree node’s span DOM element | | data | The data value from the <getContents> response |
DefaultElement.update(element,data)
FlagsAll flags listed here are accessed by this.<flag> and initialized to the values on the left. | loaded | false - The data below this node has been loaded (<loadContents> sets to true, <getContents> sets to false) | | opening | false - Ajax.Request is in progress (<getContents> sets to true, <getContentsComplete> sets to false) |
Functions| clearContents | clears the node’s children and sets loaded to false | | dispose | calls clearContents and cleans up the node’s events, references, etc.. | | getContents | triggers the Ajax.Request if loaded is true, otherwise, calls clearContents | | hide | hides the node’s element | | hideChildren | hides all of the node’s children’s elements and sets the mark’s class to ‘collapsed’ | | show | shows the node’s element | | showChildren | shows all of the node’s children’s elements and sets the mark’s class to ‘expanded’ | | toggle | calls show/hide as appropriate | | toggleChildren | calls showChildren/hideChildren as appropriate |
PropertiesAll properties are accessed by this.<property>. | children | An Array (with Prototype extensions) of all children of the current node | | element.treeNode | Reference to the tree node (this) | | parent.treeNode | The node’s parent’s tree node (if exists, else undefined) | | type | The tree node’s type (string) |
|