Fork me on GitHub

Treema

Everything you need to know to get started.

Install

Download and embed the following files:

  1. Treema JS, CSS
  2. 3rd Party
    Note that these are the versions used in development of Treema. Go to their sites/github pages for other versions or packages.
    • JQuery
    • TV4 For validating the data. This is pretty required.
    • JQuery UI This is used for dragging and dropping elements in an Array (sortable) and when typing in new properties of objects (autocomplete). Make sure if you roll your own that you include those two components. Treema will not break without these, but these features will not work.

Setup

Once you have the above files all set up, you should have TreemaNode in the global namespace. This is the superclass which all specialized nodes subclass from (StringTreemaNode, NumberTreemaNode, etc) and contains class methods for general use.

To make a Treema, you've got two options, either call the jQuery function treema:

var options = { schema:your_schema, data:your_data };
var el = $("<div></div>"); // or fetch an existing block element
var treema = el.treema(options); // hold onto this treema object for later
treema.build();
// el now has the treema interface, append it where you will

Or call the function directly through TreemaNode:

var el = $("<div></div>");
var treema = TreemaNode.make(el, options);
treema.build();

If the first argument of make() is null, then Treema makes its own empty div element.

Getting the Data

That object that make() and treema() return? Its data property has the latest version of the data. Note that it's not necessarily valid data. Use the same object's isValid() to check for validity.

Making Custom Nodes

Subclassing

To make a subclass, use the convenience method extend. Don't forget to include a constructor that calls the superclass's constructor method.

    MyTreemaNode = function () {
      this['super']('constructor').apply(this, arguments);
    };
    TreemaNode.extend(MyTreemaNode);

There are several properties and functions you should overwrite at the very least. To start, define a CSS class so you can hook unique styling to the node.

    MyTreemaNode.prototype.valueClass = 'treema-my';

Add a getDefaultValue() function which will be used when the property is first added.

    MyTreemaNode.prototype.getDefaultValue = function () { return 'No name'; }

Add buildValueForDisplay and buildValueForEditing functions which define what the row is when displaying and editing. Both receive an empty jQuery element that you can set up as you wish. Use this.data to access the current value of the node.

    MyTreemaNode.prototype.buildValueForDisplay = function (valEl) {
      valEl.text('"'+this.data+'"');
    }
    
    MyTreemaNode.prototype.buildValueForEditing = function (valEl) {
      valEl.append($('<input></input>').val(this.data));
    }

And finally, add a saveChanges function which takes the same jQuery-wrapped value element that you built in buildValueForEditing, and saves the new values to this.data.

    MyTreemaNode.prototype.saveChanges = function (valEl) {
      this.data = valEl.find('input').val();
    }

Registering Subclasses

Now that you have a proper subclass, register it for a type or format. For this you need to decide which type, or format string you want your custom node to get used for. When deciding what Node class to use, Treema checks the following equalities in this order:

If the schema includes neither, or neither is a match, it uses a special 'any' node which can handle any form of JSON data.

If, for example, you wanted to add a node for 'simple' strings, you would do:

    TreemaNode.setNodeSubclass('simple', MyTreemaNode);

Now whenever an entity has the schema format set to 'simple', your Treema will use this custom node.

For more examples about how to build TreemaNode subclasses, take a look at the core.coffee and extra.coffee files.

Database Search Nodes

Treema comes bundled with one node which implements most of the logic for searching and choosing references to other entities in your database: DatabaseSearchTreemaNode. This is in the global namespace, so just subclass it like so:

    MyTreemaNode = function () {
      this['super']('constructor').apply(this, arguments);
    };
    DatabaseSearchTreemaNode.extend(MyTreemaNode);

And override the url property:

    MyTreemaNode.prototype.url = '/db/things/search'

And the formatDocument(doc) function, which takes a single entity (which was fetched from the search url) and returns a string representation of that entity.

    MyTreemaNode.prototype.formatDocument = function (doc) {
      return doc.name;
    }

Then all you have to do is implement the handler for that url. It should search your database using the GET parameter term, which is what the user typed into the search field. That handler returns a JSON array of results.

Check out the demo to see how it works in action.

Tips and Tricks

Custom Styling

A single node in a treema looks something like this:

div.treema-node(.treema-root.treema-open.treema-closed.treema-selected.treema-last-selected)
 |
 ├-- span.treema-toggle
 |
 ├-- div.treema-row
 |   ├-- span.treema-key
 |   ├-- " : "
 |   ├-- div.treema-value(.treema-edit.treema-display)
 |
 ├-- div.treema-children
 |   ├-- div.treema-node
 |   ├-- div.treema-node
 |   ├-- ... (however many more children)
 |   ├-- input.treema-new-prop
 |   ├-- div.treema-add-child
 |
 ├-- div.treema-error
 |
 |-- span.treema-temp-error

Many of those elements do not exist in cases where they are not needed, such as div.treema-children, which is only part of the node for arrays and objects. There are also some state classes:

State class Applied to Means
treema-root treema-node This is the origin node.
treema-open treema-node This is a collection node and it is open.
treema-closed treema-node This is a collection node and it is closed.
treema-selected treema-node This node is selected.
treema-last-selected treema-node This was the last node to be selected.
treema-has-error treema-node This node (or possibly one of its children if it's closed) has a validation error.
treema-full treema-node This is a collection node and no more children may be added to it.
treema-edit treema-value This node is currently being edited.
treema-display treema-value This node is currently being displayed.

And finally, each Node object puts another class on div.treema-value to denote its type. So a standard string property will have treema-string. If you create a custom node, what you set for the valueClass property will show up on the div.treema-value of instances of that node. Use all of these classes to apply your own custom styling.

See the /src/style directory to see how we do it by default.