1 | //------------------------------------------------------------------------------ |
---|
2 | // JS for displaying information of FCM documents. |
---|
3 | //------------------------------------------------------------------------------ |
---|
4 | FCM = { |
---|
5 | // List of heading tags for the contents |
---|
6 | CONTENT_INDEX_OF: {'H2': 0, 'H3': 1, 'H4': 2, 'H5': 3, 'H6': 4}, |
---|
7 | // ID in a document, for the content |
---|
8 | ID_OF_CONTENT: 'fcm-content', |
---|
9 | // ID in a document, for the maintenance information |
---|
10 | ID_OF_MAINTENANCE: 'fcm-js-maintenance', |
---|
11 | // ID in a document, for the link to its PDF version |
---|
12 | ID_OF_PDF: 'fcm-js-pdf', |
---|
13 | // ID in a document, for the breadcrumb trail |
---|
14 | ID_OF_TRAIL: 'fcm-js-trail', |
---|
15 | // URL to the log browser of the FCM trunk |
---|
16 | URL_OF_LOG_BROWSER: 'http://fcm1/projects/FCM/intertrac/log:/FCM/trunk/', |
---|
17 | // URL to the FCM team page |
---|
18 | URL_OF_TEAM: null, |
---|
19 | |
---|
20 | // Making DOM manipulation less painful |
---|
21 | DOM: { |
---|
22 | // The current document |
---|
23 | DOC: document, |
---|
24 | |
---|
25 | // Adds an array of "nodes" (Node or String) to "node" |
---|
26 | append: function(node, items) { |
---|
27 | if (node == null || items == null) { |
---|
28 | return; |
---|
29 | } |
---|
30 | for (var i = 0; i < items.length; i++) { |
---|
31 | if (items[i] == null) { |
---|
32 | continue; |
---|
33 | } |
---|
34 | if (items[i].nodeType == null) { |
---|
35 | node.appendChild(this.DOC.createTextNode(items[i])); |
---|
36 | } |
---|
37 | else { |
---|
38 | node.appendChild(items[i]); |
---|
39 | } |
---|
40 | } |
---|
41 | }, |
---|
42 | |
---|
43 | // Returns the base name of a URL, relative to current document |
---|
44 | basename: function(url) { |
---|
45 | if (url == null) { |
---|
46 | url = this.DOC.URL; |
---|
47 | } |
---|
48 | return url.substr(this.DOC.URL.lastIndexOf('/') + 1); |
---|
49 | }, |
---|
50 | |
---|
51 | // Creates and returns an element |
---|
52 | create: function(tag, items, attribs) { |
---|
53 | var element = this.DOC.createElement(tag); |
---|
54 | this.append(element, items); |
---|
55 | if (attribs == null) { |
---|
56 | return element; |
---|
57 | } |
---|
58 | for (var i = 0; i < attribs.length; i++) { |
---|
59 | element[attribs[i][0]] = attribs[i][1]; |
---|
60 | } |
---|
61 | return element; |
---|
62 | }, |
---|
63 | |
---|
64 | // Shorthand for root[attrib] |
---|
65 | get_attrib: function(root, attrib) { |
---|
66 | return root[attrib]; |
---|
67 | }, |
---|
68 | |
---|
69 | // Shorthand for this.DOC.getElementById(id) |
---|
70 | get_by_id: function(id) { |
---|
71 | return this.DOC.getElementById(id); |
---|
72 | }, |
---|
73 | |
---|
74 | // Shorthand for root.getElementsByTagName(name)[i] |
---|
75 | get_by_name: function(root, name, i) { |
---|
76 | var elements = root.getElementsByTagName(name); |
---|
77 | if (i == null) { |
---|
78 | return elements; |
---|
79 | } |
---|
80 | if (elements == null || i >= elements.length) { |
---|
81 | return null; |
---|
82 | } |
---|
83 | return elements[i]; |
---|
84 | }, |
---|
85 | |
---|
86 | map: function(action_on, items) { |
---|
87 | var ret = []; |
---|
88 | for (var i = 0; i < items.length; i++) { |
---|
89 | ret = ret.concat(action_on(items[i], i)); |
---|
90 | } |
---|
91 | return ret; |
---|
92 | } |
---|
93 | }, |
---|
94 | |
---|
95 | // Creates and returns the content |
---|
96 | create_content: function() { |
---|
97 | var root = $D.get_attrib($D.DOC, 'body'); |
---|
98 | var stack = [{content: null, item: null}]; |
---|
99 | for (var node = root.firstChild; node != null; node = node.nextSibling) { |
---|
100 | if ( |
---|
101 | node.nodeType != 1 |
---|
102 | || node.id == null |
---|
103 | || node.id == '' |
---|
104 | || this.CONTENT_INDEX_OF[node.tagName] == null |
---|
105 | || this.CONTENT_INDEX_OF[node.tagName] + 1 > stack.length |
---|
106 | ) { |
---|
107 | continue; |
---|
108 | } |
---|
109 | while (this.CONTENT_INDEX_OF[node.tagName] + 1 < stack.length) { |
---|
110 | stack.shift(); |
---|
111 | } |
---|
112 | if (stack[0].content == null) { |
---|
113 | stack[0].content = $D.create('ul'); |
---|
114 | if (stack[0].item != null) { |
---|
115 | $D.append(stack[0].item, [stack[0].content]); |
---|
116 | } |
---|
117 | } |
---|
118 | var item = $D.create( |
---|
119 | 'li', |
---|
120 | [$D.create('a', [node.innerHTML], [['href', '#' + node.id]])] |
---|
121 | ); |
---|
122 | $D.append(stack[0].content, [item]); |
---|
123 | stack.unshift({content: null, item: item}); |
---|
124 | } |
---|
125 | return [stack[stack.length - 1].content]; |
---|
126 | }, |
---|
127 | |
---|
128 | // Creates and returns the maintenance information for "path" |
---|
129 | create_info_for_maintenance: function(path) { |
---|
130 | if (this.URL_OF_TEAM == null) { |
---|
131 | return []; |
---|
132 | } |
---|
133 | return [ |
---|
134 | 'Maintained by: ', |
---|
135 | $D.create('a', ['FCM team'], [['href', FCM.URL_OF_TEAM]]), |
---|
136 | '. History: ', |
---|
137 | $D.create( |
---|
138 | 'a', |
---|
139 | ['log'], |
---|
140 | [['href', this.URL_OF_LOG_BROWSER + path + $D.basename()]] |
---|
141 | ), |
---|
142 | '.' |
---|
143 | ]; |
---|
144 | }, |
---|
145 | |
---|
146 | // Creates and returns the PDF link for "name" |
---|
147 | create_info_for_pdf: function(name) { |
---|
148 | return [ |
---|
149 | 'For printing, please use the ', |
---|
150 | $D.create('a', ['PDF'], [['href', 'fcm-' + name + '.pdf']]), |
---|
151 | ' version of the document.' |
---|
152 | ]; |
---|
153 | }, |
---|
154 | |
---|
155 | // Creates a breadcrumb trail from a list of [[href, text], ...] |
---|
156 | create_trail: function(items) { |
---|
157 | if (items == null) { |
---|
158 | return []; |
---|
159 | } |
---|
160 | return [].concat( |
---|
161 | $D.map( |
---|
162 | function(item, i) { |
---|
163 | return [].concat( |
---|
164 | (i != 0 ? ' > ' : []), |
---|
165 | $D.create('a', [item[1]], [['href', item[0]]]) |
---|
166 | ); |
---|
167 | }, |
---|
168 | items |
---|
169 | ), |
---|
170 | ' > ', |
---|
171 | $D.get_by_name($D.DOC, 'title', 0).innerHTML |
---|
172 | ); |
---|
173 | }, |
---|
174 | |
---|
175 | // A simple facade for doing the onload tasks |
---|
176 | load: function(maintenance_path, pdf_name, trail_list) { |
---|
177 | var tasks = [ |
---|
178 | { |
---|
179 | id: this.ID_OF_CONTENT, |
---|
180 | task: function() { |
---|
181 | return FCM.create_content(); |
---|
182 | } |
---|
183 | }, |
---|
184 | { |
---|
185 | id: this.ID_OF_MAINTENANCE, |
---|
186 | task: function() { |
---|
187 | return FCM.create_info_for_maintenance(maintenance_path); |
---|
188 | } |
---|
189 | }, |
---|
190 | { |
---|
191 | id: this.ID_OF_PDF, |
---|
192 | task: function() { |
---|
193 | return FCM.create_info_for_pdf(pdf_name); |
---|
194 | } |
---|
195 | }, |
---|
196 | { |
---|
197 | id: this.ID_OF_TRAIL, |
---|
198 | task: function() { |
---|
199 | return FCM.create_trail(trail_list); |
---|
200 | } |
---|
201 | } |
---|
202 | ]; |
---|
203 | for (var i = 0; i < tasks.length; i++) { |
---|
204 | var node = $D.get_by_id(tasks[i].id); |
---|
205 | if (node == null) { |
---|
206 | continue; |
---|
207 | } |
---|
208 | $D.append(node, tasks[i].task()); |
---|
209 | } |
---|
210 | } |
---|
211 | }; |
---|
212 | $D = FCM.DOM; |
---|