Skip to content

Commit 8164acd

Browse files
author
Paul Sandulescu
committed
added treeize
1 parent 1b1196b commit 8164acd

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

javascript/treeize/treeize.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#inputString {
2+
width: 100%;
3+
height: 400px;
4+
}
5+
.container {
6+
border: solid 1px red;
7+
width: 98%;
8+
margin: 0;
9+
padding: 5px;
10+
border-radius: 4px;
11+
}
12+
.title {
13+
width: 96%;
14+
}

javascript/treeize/treeize.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<body>
3+
<textarea id="inputString">- Root
4+
- Item 1
5+
- Item 5
6+
- Item 6
7+
- Item 2
8+
- Item 3
9+
- Item 4</textarea>
10+
<div id="pollux" class="container"></div>
11+
</body>
12+
</html>

javascript/treeize/treeize.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function treeize(container, tree) {
2+
function rnd(max) {
3+
return Math.floor(Math.random() * max);
4+
}
5+
6+
function rndColour() {
7+
return [rnd(25)+230, rnd(25)+230, rnd(25)+230];
8+
}
9+
10+
var key, item, div, text, colour;
11+
12+
container = $(container) || $('body');
13+
14+
for (key in tree) {
15+
item = tree[key];
16+
d = $('<div class="container"/>');
17+
colour = rndColour();
18+
d.css('background-color', 'rgb(' + colour.join(', ') + ')');
19+
d.css('color', 'rgb(' + colour.map(function (el) {
20+
return 255 - el;
21+
}).join(', ') + ')');
22+
23+
text = $('<div class="title"/>');
24+
text.css('display', 'block');
25+
text.css('width', '100%');
26+
text.html(item.title);
27+
28+
d.append(text);
29+
container.append(d);
30+
31+
if (item['children'] != undefined) treeize(d, item.children);
32+
}
33+
}
34+
35+
function parseMarkup(str) {
36+
function levelRegex(level) { return new RegExp('^\\s{' + level + '}'); }
37+
function parseMarkupLevel(level, lines) {
38+
var curLvl = levelRegex(level);
39+
var nextLvl = levelRegex(level+1);
40+
var results = [];
41+
while ( lines.length && lines[0].match(curLvl) ) {
42+
var line = lines.shift();
43+
var matches = line.match(/- (.+)$/);
44+
results.push({title: matches[1]});
45+
if ( lines.length && lines[0].match(nextLvl) ) results[results.length-1].children = parseMarkupLevel(level+1, lines);
46+
}
47+
48+
return results;v
49+
}
50+
var lines = str.split("\n");
51+
return parseMarkupLevel(0, str.split("\n"));
52+
}
53+
54+
var testTree = [{
55+
text: "Root",
56+
children: [{
57+
text: "Item 1"
58+
}, {
59+
text: "Item 2",
60+
children: [{
61+
text: "Item 3"
62+
}, {
63+
text: "Item 4"
64+
}]
65+
}]
66+
}];
67+
68+
$('#inputString').keypress(function(event) {
69+
$('#pollux').empty();
70+
treeize($('#pollux'), parseMarkup($('#inputString').val()));
71+
});
72+
$('#pollux').empty();
73+
treeize($('#pollux'), parseMarkup($('#inputString').val()));

0 commit comments

Comments
 (0)