forked from manuelkiessling/nodebeginner.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTOC.js
More file actions
executable file
·126 lines (75 loc) · 3.04 KB
/
TOC.js
File metadata and controls
executable file
·126 lines (75 loc) · 3.04 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
/*
---
description: TOC is a MooTools Plugin which turn a HTML headings into a Table of Content.
authors:
- Adrian Statescu (http://thinkphp.ro).
license:
- MIT-style license.
requires:
core/1.2.1: '*'
provides: [TOC]
...
*/
var TOC = new Class({
/* public method - constructor of class initialize */
initialize: function(elem) {
this.toc = $(elem);
if(!this.toc) {return};
this.createToc();
},
/* private method */
createToc: function() {
var out = '<ul>';
var nodes = this.getElementsByTagNamess('h2,h3,h4,h5');
var weight, oldweight, nextweight;
nodes.each(function(o,k){
var id = o.id;
if(id == '') {
id = 'head' + k;
o.id = id;
}
weight = o.nodeName.substr(1,1);
if(k>0 && weight>oldweight) {
out += '<ul>';
}
out += '<li><a href="#'+o.id+'">'+o.innerHTML+'</a>';
if(nodes[k+1]) {
nextweight = nodes[k+1].nodeName.substr(1,1);
if(weight > nextweight) {
out += '</li></ul></li>';
}
if(weight == nextweight) {
out += '</li>';
}
}//endif
oldweight = weight;
});//end each nodes
out += '</li></ul>';
this.toc.set('html',this.toc.get('html') + out);
},
/* private method */
getElementsByTagNamess: function(list,obj) {
if (!obj) var obj = document;
var tagNames = list.split(',');
var resultArray = new Array();
for (var i=0;i<tagNames.length;i++) {
var tags = obj.getElementsByTagName(tagNames[i]);
for (var j=0;j<tags.length;j++) {
resultArray.push(tags[j]);
}
}
var testNode = resultArray[0];
if (!testNode) return [];
if (testNode.sourceIndex) {
resultArray.sort(function (a,b) {
return a.sourceIndex - b.sourceIndex;
});
}
else if (testNode.compareDocumentPosition) {
resultArray.sort(function (a,b) {
return 3 - (a.compareDocumentPosition(b) & 6);
});
}
return resultArray;
}
});//end class TOC