-
Notifications
You must be signed in to change notification settings - Fork 6
/
outlineParser.js
90 lines (77 loc) · 3.24 KB
/
outlineParser.js
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
var OutlineParser = {
content: function(text){
var parse = function(arr){
var newarr = [];
for(var i = 0, len = arr.length; i < len; i++){
if (/[\S]+/.test(arr[i])){
var subarr = arr[i].replace(/([ ]{2,4}(?=[\S])|\t(?=[\S]))/gm, "").split(/[\n](?=[\S])/m);
if (subarr.length > 1){
newarr.push([subarr[0], parse(subarr.slice(1))]);
} else {
newarr.push(subarr[0]);
}
}
}
return newarr;
};
return parse(text.split(/[\n](?=[\S])/m));
},
// Copyright 2006,2007 Bontrager Connection, LLC
// http://bontragerconnection.com/ and http://www.willmaster.com/
// http://www.willmaster.com/blog/css/floating-layer-at-cursor-position.php
toggleContent: function toggleContent(d, dipslaystyle){
if(d.length < 1) { return; }
var dd = document.getElementById(d);
dd.style.display = (dd.style.display == "none" || dd.style.display == "") ? "block" : "none";
//dd.style.display = dipslaystyle;
},
// http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element
clearContent: function clearContent(container){
if (container && container.hasChildNodes()){
while (container.childNodes.length >= 1){
container.removeChild(container.firstChild);
}
}
},
parseContent: function parseContent(content, container, idx){
if (!content || !container) { return; }
idx = (idx || 0) + 1;
if (idx == 1){
this.clearContent(container);
}
// each element is in the form [a, [a]], [a, []], or [a]
for(var i = 0, len = content.length; i < len; i++){
var el = content[i];
var caption = el[0];
var details = el[1];
if (typeof(el) == 'string'){
caption = el;
details = null;
}
// add caption
var headercontainer = document.createElement('div');
container.appendChild(headercontainer);
var header = document.createElement('span');
header.innerHTML = caption;
header.className = 'header h' + idx;
headercontainer.appendChild(header);
// add details
var detailswitch = document.createElement('span');
detailswitch.innerHTML = '+/-';
detailswitch.className = (details) ? 'detailswitch' : 'detailswitch invisible';
headercontainer.insertBefore(detailswitch, header);
if (details){
Event.add(detailswitch, 'click', function(c, scope){
return function () {
return scope.toggleContent(c);
};
}(caption, this));
var detailcontainer = document.createElement('div');
detailcontainer.id = caption;
detailcontainer.className = 'detail d' + idx;
container.appendChild(detailcontainer);
this.parseContent(details, detailcontainer, idx);
}
}
}
}