/[pub]/test/html-webhacc/cc-script.js
Suika

Contents of /test/html-webhacc/cc-script.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations) (download) (as text)
Sat Aug 16 13:09:08 2008 UTC (15 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +159 -11 lines
File MIME type: application/javascript
++ ChangeLog	16 Aug 2008 13:08:56 -0000
	* Makefile: Generate Japanese version of error description document.

	* cc-script.js, cc-style.css: Support for help popup.

	* error-description-source.xml: Descriptions are added
	to the description of WebHACC itself and descriptions
	of error levels.  Old error level table is removed.

	* mkdescription.pl: Assume the second argument is
	the language.  Support for d:cat/d:desc.

2008-08-16  Wakaba  <wakaba@suika.fam.cx>

1 function addSourceToParseErrorList (idPrefix, dlId) {
2 var parseErrorsList = document.getElementById
3 (idPrefix + dlId);
4 if (!parseErrorsList) return;
5 var childs = parseErrorsList.childNodes;
6 var childsL = childs.length;
7 var line = 0;
8 var column = 0;
9 for (var i = 0; i < childsL; i++) {
10 var child = childs[i];
11 if (child.nodeType != 1) continue;
12 if (child.nodeName == 'DT') {
13 line = parseInt (child.getAttribute ('data-line') || 0);
14 column = parseInt (child.getAttribute ('data-column') || 0);
15 } else if (child.nodeName == 'DD') {
16 if (line > 0) {
17 var lineEl = document.getElementById (idPrefix + 'line-' + line);
18 if (lineEl) {
19 lineText = lineEl.innerHTML
20 .replace (/<var>U\+([0-9A-F]{4})<\/var>/g, function (s) {
21 return String.fromCharCode (parseInt (s, 16));
22 })
23 .replace (/&lt;/g, '<')
24 .replace (/&gt;/g, '>')
25 .replace (/&nbsp;/g, '\u00A0')
26 .replace (/&quot;/g, '"')
27 .replace (/&amp;/g, '&');
28 var p = document.createElement ('p');
29 p.className = 'source-fragment';
30 var code = document.createElement ('code');
31 if (lineText.length > 50) {
32 if (column - 25 > 0) {
33 p.appendChild (document.createElement ('var')).innerHTML
34 = '...';
35 lineText = lineText.substring (column - 25, column + 24);
36 code.appendChild (document.createTextNode
37 (lineText.substring (0, 24)));
38 code.appendChild (document.createElement ('mark'))
39 .appendChild (document.createTextNode
40 (lineText.charAt (24)));
41 code.appendChild (document.createTextNode
42 (lineText.substring (25, lineText.length)));
43 p.appendChild (code);
44 p.appendChild (document.createElement ('var')).innerHTML
45 = '...';
46 } else {
47 lineText = lineText.substring (0, 50);
48 if (column > 0) {
49 code.appendChild (document.createTextNode
50 (lineText.substring (0, column - 1)));
51 code.appendChild (document.createElement ('mark'))
52 .appendChild (document.createTextNode
53 (lineText.charAt (column - 1)));
54 code.appendChild (document.createTextNode
55 (lineText.substring (column, lineText.length)));
56 } else {
57 code.appendChild (document.createTextNode
58 (lineText.substring (0, 50)));
59 }
60 p.appendChild (code);
61 p.appendChild (document.createElement ('var')).innerHTML
62 = '...';
63 }
64 } else {
65 if (column > 0) {
66 code.appendChild (document.createTextNode
67 (lineText.substring (0, column - 1)));
68 code.appendChild (document.createElement ('mark'))
69 .appendChild (document.createTextNode
70 (lineText.charAt (column - 1)));
71 code.appendChild (document.createTextNode
72 (lineText.substring (column, lineText.length)));
73 } else {
74 code.appendChild (document.createTextNode (lineText));
75 }
76 p.appendChild (code);
77 }
78 child.appendChild (p);
79 }
80 }
81 line = 0;
82 column = 0;
83 }
84 }
85 } // addSourceToParseErrorList
86
87 function insertNavSections (parentId) {
88 parentId = parentId || '';
89 var el = document.createElement ('nav');
90 el.id = parentId + 'nav-sections';
91 el.innerHTML = '<ul></ul>';
92
93 if (parentId == '') {
94 document.body.appendChild (el);
95 document.webhaccSections = {};
96 document.body.setAttribute ('data-scripted', '');
97 } else {
98 var section = document.getElementById (parentId);
99 section.appendChild (el);
100 section.webhaccSections = {};
101 }
102 } // insertNavSections
103
104 function addSectionLink (id, label, parentId) {
105 parentId = parentId || '';
106
107 var el = document.createElement ('li');
108 el.innerHTML = '<a></a>';
109 el.firstChild.href = '#' + id;
110 el.firstChild.innerHTML = label;
111 document.getElementById (parentId + 'nav-sections')
112 .firstChild.appendChild (el);
113
114 var sections = document.webhaccSections;
115 if (parentId != '') {
116 sections = document.getElementById (parentId).webhaccSections;
117 }
118 sections[id] = document.getElementById (id);
119 sections[id].tabElement = el;
120
121 if (id == 'input' || id == 'input-url') {
122 showTab (id);
123 document.webhaccNavigated = false;
124 } else if (id == 'document-info' && !document.webhaccNavigated) {
125 showTab (id);
126 document.webhaccNavigated = false;
127 } else if (id.match (/-document-info$/)) {
128 sections[id].tabElement.setAttribute ('data-active', '');
129 } else {
130 sections[id].style.display = 'none';
131 }
132 } // addSectionLink
133
134 function showTab (id) {
135 var ids = [];
136 if (id.match (/^line-/)) {
137 ids = ['source-string'];
138 } else if (id.match (/^node-/)) {
139 ids = ['document-tree'];
140 } else if (id.match (/^index-/)) {
141 ids = ['document-structure'];
142 } else if (id.match (/^subdoc-[^-]+-/)) {
143 var m;
144 ids = [''];
145 while (true) {
146 if (m = id.match (/^subdoc-[^-]+-/)) {
147 ids.push (ids[ids.length - 1] + m[0]);
148 id = id.substring (m[0].length);
149 } else {
150 break;
151 }
152 }
153 if (id.length > 0) {
154 if (id.match (/^line-/)) {
155 ids.push (ids[ids.length - 1] + 'source-string');
156 } else if (id.match (/^node-/)) {
157 ids.push (ids[ids.length - 1] + 'document-tree');
158 } else if (id.match (/^index-/)) {
159 ids.push (ids[ids.length - 1] + 'document-structure');
160 } else {
161 ids.push (ids[ids.length - 1] + id);
162 }
163 }
164 ids.shift (); // ''
165 } else if (id.match (/^input-/)) {
166 ids = ['input', id];
167 } else {
168 ids = [id];
169 }
170
171 var sections = document.webhaccSections;
172 while (ids.length > 0) {
173 var myid = ids.shift ();
174 _showTab (sections, myid);
175 sections = sections[myid].webhaccSections;
176 if (!sections) break;
177 }
178 } // showTab
179
180 function _showTab (sections, id) {
181 if (sections[id]) {
182 for (var i in sections) {
183 sections[i].style.display = 'none';
184 sections[i].tabElement.removeAttribute ('data-active');
185 }
186 sections[id].style.display = 'block';
187 sections[id].tabElement.setAttribute ('data-active', '');
188 sections[id].tabElement.scrollIntoView ();
189
190 document.webhaccNavigated = true;
191 }
192 } // _showTab
193
194 function getAncestorElements (e) {
195 var ret = {};
196 do {
197 if (e.nodeName == 'A' || e.nodeName == 'AREA') {
198 ret.a = e;
199 if (ret.aside) {
200 return ret;
201 }
202 } else if (e.nodeName == 'ASIDE' || e.nodeName == 'aside') {
203 ret.aside = e;
204 if (ret.a) {
205 ret.aInAside = true;
206 return ret;
207 }
208 }
209 e = e.parentNode;
210 } while (e);
211 return ret;
212 } // getAncestorElements
213
214 function showHelp (id, context) {
215 if (document.webhaccHelp === undefined) {
216 loadHelp ('../error-description', function () {
217 _showHelp (id, context);
218 });
219 return true;
220 } else if (document.webhaccHelp === null) {
221 return false;
222 } else {
223 _showHelp (id, context);
224 }
225 } // showHelp
226
227 function loadHelp (url, code) {
228 document.webhaccHelp = null;
229 var iframe = document.createElement ('iframe');
230 var iframecode = function () {
231 var doc;
232 var docel;
233 try {
234 doc = iframe.contentWindow.document;
235 docel = doc.getElementById ('error-description');
236 } catch (e) { }
237 if (docel) {
238 document.webhaccHelp = doc;
239 code ();
240 } else if (url != '../error-description.en.html.u8') {
241 // doc would be a 406 error.
242 loadHelp ('../error-description.en.html.u8', code);
243 iframe.parentNode.removeChild (iframe);
244 /*
245 |iframe| is removed from the document after another |iframe|
246 is inserted by nested |loadHelp| call, otherwise Safari 3
247 would reuse(?) removed |iframe| and it sometimes (not always)
248 requests old URL even when another URL is set to the |src| attribute.
249 */
250 }
251 iframe.onreadystatechange = null;
252 iframe.onload = null;
253 };
254 iframe.onreadystatechange = function () {
255 if (this.readyState == 'complete') {
256 iframecode ();
257 }
258 };
259 iframe.onload = iframecode;
260 iframe.src = url;
261 document.body.appendChild (iframe);
262 } // loadHelp
263
264 function _showHelp (id, context) {
265 var helpDataEl = document.webhaccHelp.getElementById (id);
266 if (!helpDataEl) {
267 helpDataEl = document.webhaccHelp.getElementById ('help-not-available');
268 if (!helpDataEl) {
269 helpDataEl = document.createElement ('div');
270 helpDataEl.innerHTML = '<p>There is no help for this item available.';
271 }
272 }
273
274 if (id != 'help-not-available' &&
275 helpDataEl.getElementsByTagName ('p').length == 0) {
276 _showHelp ('help-not-available', context);
277 return;
278 }
279
280 var helpBox = document.createElement ('aside');
281 helpBox.className = 'help';
282 helpBox.innerHTML = helpDataEl.innerHTML; /* adoptNode + appendChild */
283 document.body.appendChild (helpBox);
284
285 var vp = document.documentElement;
286 if (vp.clientHeight < document.body.clientHeight) {
287 vp = document.body;
288 /*
289 |vp| is the element that is associated with the viewport.
290 In standard mode, the viewport element is the root element, i.e.
291 the |document.documentElement|. However, in Opera 9, the viewport
292 element is the |body| element. If the document element is not the
293 viewport element, its |clientHeight| takes less value than that
294 of the |body| element. (I don't know whether this is always true.)
295 */
296 }
297
298
299 var left = context.x;
300 var top = context.y;
301 if (left > vp.clientWidth * 0.5) {
302 helpBox.style.left = '45%';
303 } else if (left < 10) {
304 helpBox.style.left = '10px';
305 } else {
306 helpBox.style.left = left + 'px';
307 }
308 if (top > vp.clientHeight - 100) {
309 helpBox.style.bottom = '10px';
310 } else if (top < 10) {
311 helpBox.style.top = '10px';
312 } else {
313 helpBox.style.top = top + 'px';
314 }
315
316 if (helpBox.offsetTop + helpBox.clientHeight > vp.clientHeight) {
317 helpBox.style.top = '50%';
318 helpBox.style.bottom = '10px';
319 }
320 } // _showHelp
321
322 function removeHelps () {
323 var asides = document.getElementsByTagName ('aside');
324 while (asides.length > 0) {
325 var aside = asides[0];
326 aside.parentNode.removeChild (aside);
327 }
328 } // removeHelps
329
330 function onbodyclick (ev) {
331 var aels = getAncestorElements (ev.target || ev.srcElement);
332
333 if (!aels.aside) {
334 removeHelps ();
335 }
336
337 if (aels.a) {
338 var href = aels.a.getAttribute ('href');
339 if (href) {
340 var m;
341 if (href.match (/^#/)) {
342 var id = decodeURIComponent (href.substring (1));
343 showTab (id);
344 return true;
345 } else if ((aels.a.rel == 'help' /* relList.has ('help') */) &&
346 (m = href.match (/#(.+)$/) /* aels.a.hash, in fact... */)) {
347 var id = decodeURIComponent (m[1]);
348 showHelp (id, {x: ev.clientX, y: ev.clientY});
349 return false;
350 } else if (href.match (/^.\/#/)) {
351 var id = decodeURIComponent (href.substring (3));
352 showTab (id);
353 if (aels.aInAside) {
354 removeHelps ();
355 }
356 return true;
357 }
358 }
359 }
360
361 return true;
362 } // onbodyclick
363
364 function onbodyload () {
365 // This block should be executed at the end of initialization process,
366 // since |decodeURIComponent| might throw.
367 if (!document.webhaccNavigated) {
368 var fragment = location.hash;
369 if (fragment) {
370 var id = decodeURIComponent (fragment.substring (1));
371 showTab (id);
372 var el = document.getElementById (id);
373 if (el) el.scrollIntoView ();
374 } else if (document.webhaccSections['result-summary']) {
375 showTab ('result-summary');
376 } else {
377 showTab ('input');
378 }
379 }
380 } // onbodyload
381
382 // $Date: 2008/08/16 07:42:20 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24