forked from codecombat/treema
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusing.html
More file actions
405 lines (330 loc) · 12.1 KB
/
using.html
File metadata and controls
405 lines (330 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" media="screen">
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/using.css">
<title>Treema by sderickson</title>
</head>
<body>
<div id="content">
<a href="https://github.com/codecombat/treema">
<img style="position: absolute; top: 0; left: 0; border: 0;"
src="https://s3.amazonaws.com/github/ribbons/forkme_left_green_007200.png" alt="Fork me on GitHub">
</a>
<h1>Treema</h1>
<ul class="nav nav-pills navbar navbar-default">
<li><a href="index.html">Overview</a></li>
<li><a href="demo.html">Demo</a></li>
<li class="active"><a href="using.html">Usage</a></li>
<li><a href="develop.html">Dev</a></li>
<li id="tests-link"><a href="test.html">Run Tests</a></li>
<li id="github-link"><a href="https://github.com/codecombat/treema">Treema on Github</a></li>
</ul>
<p>Everything you need to know to get started.</p>
<h3>Dependencies</h3>
<p>
Treema has the following 3rd party dependencies:
</p>
<ul>
<li>
<a href="https://jquery.com/">JQuery <i>(1.8.1)</i></a>
</li>
<li>
<a href="https://geraintluff.github.io/tv4/">TV4 <i>(1.0.15)</i></a>
For validating the data. This is pretty required.
</li>
<li>
<a href="https://jqueryui.com/">JQuery UI <i>(1.10.3)</i></a>
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.
</li>
</ul>
<h3>Install</h3>
<p>
Treema can be easily installed using the <a href="http://bower.io/">Bower Package Manager</a>:
</p>
<pre>
bower install treema --save</pre>
<p>
Afterwards simply include the necessary files into your file:
</p>
<pre>
<!-- Stylesheets -->
<link rel="stylesheet" href="/bower_components/jquery-ui/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/bower_components/treema/treema.css">
<!-- JavaScripts -->
<script src="/bower_components/jquery/jquery.js"></script>
<script src="/bower_components/jquery-ui/ui/jquery-ui.js"></script>
<script src="/bower_components/tv4/tv4.js"></script>
<script src="/bower_components/treema/treema.js"></script></pre>
<p>
<strong>Note:</strong><br/>
The paths might vary depending on
if you changed the default path of your <code>bower_components</code> folder. The correct paths can be
found running:
</p>
<h3>Setup</h3>
<p>
Once you have the above files all set up, you should have <code>TreemaNode</code> in the global namespace.
This is the superclass which all specialized nodes subclass from (StringTreemaNode, NumberTreemaNode, etc)
and contains class methods for general use.
</p>
<p>
To make a Treema, you've got two options, either call the jQuery function <code>treema</code>:
</p>
<pre>
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</pre>
<p>
Or call the function directly through <code>TreemaNode</code>:
</p>
<pre>
var el = $("<div></div>");
var treema = TreemaNode.make(el, options);
treema.build();</pre>
<p>
If the first argument of <code>make()</code> is null, then Treema makes its own empty div element.
</p>
<h3>Getting the Data</h3>
<p>
That object that <code>make()</code> and <code>treema()</code> return?
Its <code>data</code> property has the latest version of the data.
Note that it's not necessarily <em>valid</em> data.
Use the same object's <code>isValid()</code> to check for validity.
</p>
<h3>Making Custom Nodes</h3>
<h4>Subclassing</h4>
<p>
To make a subclass, use the convenience method <code>extend</code>.
Don't forget to include a constructor that calls the superclass's <code>constructor</code> method.
</p>
<pre>
MyTreemaNode = function () {
this['super']('constructor').apply(this, arguments);
};
TreemaNode.extend(MyTreemaNode);</pre>
<p>
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.
</p>
<pre>
MyTreemaNode.prototype.valueClass = 'treema-my';</pre>
<p>
Add a <code>getDefaultValue()</code> function which will be used when the property is first added.
</p>
<pre>
MyTreemaNode.prototype.getDefaultValue = function () { return 'No name'; }</pre>
<p>
Add <code>buildValueForDisplay</code> and <code>buildValueForEditing</code> 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 <code>this.data</code> to access the current value of the node.
</p>
<pre>
MyTreemaNode.prototype.buildValueForDisplay = function (valEl) {
valEl.text('"'+this.data+'"');
}
MyTreemaNode.prototype.buildValueForEditing = function (valEl) {
valEl.append($('<input></input>').val(this.data));
}</pre>
<p>
And finally, add a <code>saveChanges</code> function which takes the same jQuery-wrapped value element
that you built in <code>buildValueForEditing</code>, and saves the new values to <code>this.data</code>.
</p>
<pre>
MyTreemaNode.prototype.saveChanges = function (valEl) {
this.data = valEl.find('input').val();
}</pre>
<h4>Registering Subclasses</h4>
<p>
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:
</p>
<ul>
<li>schema.format</li>
<li>schema.type</li>
</ul>
<p>
If the schema includes neither, or neither is a match,
it uses a special 'any' node which can handle any form of JSON data.
</p>
<p>
If, for example, you wanted to add a node for 'simple' strings,
you would do:
</p>
<pre>
TreemaNode.setNodeSubclass('simple', MyTreemaNode);</pre>
<p>
Now whenever an entity has the schema format set to 'simple',
your Treema will use this custom node.
</p>
<p>
For more examples about how to build TreemaNode subclasses, take a look at the
<a href="https://github.com/codecombat/treema/blob/master/src/core.coffee">core.coffee</a> and
<a href="https://github.com/codecombat/treema/blob/master/src/extra.coffee">extra.coffee</a> files.
</p>
<h4>Database Search Nodes</h4>
<p>
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:
</p>
<pre>
MyTreemaNode = function () {
this['super']('constructor').apply(this, arguments);
};
DatabaseSearchTreemaNode.extend(MyTreemaNode);</pre>
<p>
And override the <code>url</code> property:
</p>
<pre>
MyTreemaNode.prototype.url = '/db/things/search'</pre>
<p>
And the <code>formatDocument(doc)</code> function,
which takes a single entity (which was fetched from the search url)
and returns a string representation of that entity.
</p>
<pre>
MyTreemaNode.prototype.formatDocument = function (doc) {
return doc.name;
}</pre>
<p>
Then all you have to do is implement the handler for that url.
It should search your database using the GET parameter <code>term</code>,
which is what the user typed into the search field.
That handler returns a JSON array of results.
</p>
<p>
Check out the
<a href="demo.html#dbsearch">demo</a>
to see how it works in action.
</p>
<h4>Tips and Tricks</h4>
<ul>
<li>
At this point all functions and properties in TreemaNode are fair game for changing,
but the above five are the least likely to change.
If you're experimenting with making custom nodes,
be aware that new versions of Treema may deprecate things without warning.
</li>
<li>
If you're using CoffeeScript, you can use <code>class MyTreemaNode extends TreemaNode:</code>
for making subclasses more easily, without even having to explicitly define the constructor.
</li>
<li>
Schemas are good places to put special values which your custom nodes may use,
even properties which are not included in the
<a href="http://json-schema.org/documentation.html">JSON-Schema specs</a>.
You're allowed to add whatever properties you wish to schemas,
so feel free to use that flexibility to add your own properties.
For example, the AceNode looks for the 'aceTheme' and 'aceMode' properties in a schema
object for determining Ace settings.
</li>
</ul>
<h3>Custom Styling</h3>
<p>
A single node in a treema looks something like this:
</p>
<pre><code class="bash">div.treema-node(.treema-root.treema-open.treema-closed.treema-selected.treema-last-selected)
|
├-- span.treema-toggle-hit-area
| ├-- 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</code></pre>
<p>
Many of those elements do not exist in cases where they are not needed,
such as <code>div.treema-children</code>, which is only part of the node for arrays and objects.
There are also some state classes:
</p>
<table class="table table-condensed table-striped">
<tr>
<th>State class</th>
<th>Applied to</th>
<th>Means</th>
</tr>
<tr>
<td>treema-root</td>
<td>treema-node</td>
<td>This is the origin node.</td>
</tr>
<tr>
<td>treema-open</td>
<td>treema-node</td>
<td>This is a collection node and it is open.</td>
</tr>
<tr>
<td>treema-closed</td>
<td>treema-node</td>
<td>This is a collection node and it is closed.</td>
</tr>
<tr>
<td>treema-selected</td>
<td>treema-node</td>
<td>This node is selected.</td>
</tr>
<tr>
<td>treema-last-selected</td>
<td>treema-node</td>
<td>This was the last node to be selected.</td>
</tr>
<tr>
<td>treema-has-error</td>
<td>treema-node</td>
<td>This node (or possibly one of its children if it's closed) has a validation error.</td>
</tr>
<tr>
<td>treema-full</td>
<td>treema-node</td>
<td>This is a collection node and no more children may be added to it.</td>
</tr>
<tr>
<td>treema-edit</td>
<td>treema-value</td>
<td>This node is currently being edited.</td>
</tr>
<tr>
<td>treema-display</td>
<td>treema-value</td>
<td>This node is currently being displayed.</td>
</tr>
</table>
<p>
And finally, each Node object puts another class on <code>div.treema-value</code> to denote its type.
So a standard string property will have <code>treema-string</code>.
If you create a custom node, what you set for the <code>valueClass</code> property will show up on the
<code>div.treema-value</code> of instances of that node.
Use all of these classes to apply your own custom styling.
</p>
<p>
See the
<a href="https://github.com/codecombat/treema/tree/master/src/style">/src/style directory</a>
to see how we do it by default.
</p>
<footer><div></div><div></div></footer>
</div>
</div>
</body>
</html>