OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / skins / default / nicetitle.js
1 // from http://www.kryogenix.org
2 // by Scott Andrew - http://scottandrew.com
3 // add an eventlistener to browsers that can do it somehow.
4 function addEvent(obj, evType, fn)
5         {
6         if (obj.addEventListener)
7                 {
8                 obj.addEventListener(evType, fn, false);
9                 return true;
10                 }
11         else if (obj.attachEvent)
12                 {
13                 var r = obj.attachEvent('on'+evType, fn);
14                 return r;
15                 }
16         else
17                 {
18                 return false;
19                 }
20         }
21
22 function floatImages()
23         {
24         // adapted from http://www.dithered.com/javascript/browser_detect/
25         //**************************************************************//
26         // sniff user agent
27         var userAgent = navigator.userAgent.toLowerCase();
28
29         // if Mozilla 1.4 then quit
30         if ((userAgent.indexOf('gecko') != -1) && (userAgent.indexOf('gecko/') + 14 == userAgent.length) && (parseFloat(userAgent.substring(userAgent.indexOf('rv:') + 3)) == '1.4')) return;
31
32         // if Opera then quit
33         if (document.all && window.Event) return;
34         //**************************************************************//
35
36         // check this browser can cope with what we want to do
37         if (!document.getElementById) return;
38         var blogDiv = document.getElementById('blog');
39         if (!blogDiv) return;
40         if (!blogDiv.offsetWidth) return;
41
42         blogDiv.className = (blogDiv.offsetWidth >= 500) ? "float-images" : "block-images";
43         }
44
45 // Blockquote citations
46
47 // Simon Willison's work:
48 // http://simon.incutio.com/archive/2002/12/20/#blockquoteCitations
49
50 // Also Dunstan Orchard's work:
51 // http://1976design.com/blog/archive/2003/11/10/updates/
52 function blockquoteCite()
53         {
54         if (!document.createElementNS)
55                 {
56                 document.createElementNS = function(ns, elt)
57                         {
58                         return document.createElement(elt);
59                         }
60                 }
61         quotes = document.getElementsByTagName('blockquote');
62         for (i = 0; i < quotes.length; i++)
63                 {
64                 var cite = quotes[i].getAttribute('cite');
65                 // value of cite attribute should only contain URI, not any other
66                 if ((cite) && (cite != ''))
67                         {
68                         newlink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
69                         newlink.setAttribute('href', cite);
70                         newlink.className = 'cite-link';
71                         newlink.appendChild(document.createTextNode(cite));
72                         newdiv = document.createElementNS('http://www.w3.org/1999/xhtml', 'cite');
73                         newdiv.className = 'blockquote-cite';
74                         newdiv.appendChild(document.createTextNode('Source: '));
75                         newdiv.appendChild(newlink);
76                         quotes[i].appendChild(newdiv);
77                         quotes[i].removeAttribute('cite');
78                         }
79                 }
80         }
81
82 // Ins and Del tags citations
83 function insdelCite()
84         {
85         if (!document.createElementNS)
86                 {
87                 document.createElementNS = function(ns, elt)
88                         {
89                         return document.createElement(elt);
90                         }
91                 }
92         var insdel = new Array(2);
93         insdel[0] = document.getElementsByTagName('ins');
94         insdel[1] = document.getElementsByTagName('del');
95         for (var i=0; i<insdel.length; i++)
96                 {
97                 if (insdel[i])
98                         {
99                         for (var id=0; id<insdel[i].length; id++)
100                                 {
101                                 var isdl = insdel[i][id].getAttribute('cite');
102                                 if ((isdl) && (isdl != ""))
103                                         {
104                                         idlink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
105                                         idlink.setAttribute('href', isdl);
106                                         idlink.className = 'cite-link ' + (i == 0 ? 'ins-cite' : 'del-cite');
107                                         idlink.setAttribute('title','citation of ' + (i == 0 ? 'added' : 'deleted') + ' text');
108                                         idlink.appendChild(document.createTextNode('#'));
109                                         insdel[i][id].appendChild(idlink);
110                                         insdel[i][id].removeAttribute('cite');
111                                         }
112                                 }
113                         }
114                 }
115         }
116
117 // Force IE not to show alternate text as tooltip
118 function noAltTooltip()
119         {
120         images = document.getElementsByTagName('img');
121         for (var i = 0; i < images.length; i++)
122                 {
123                 var title = images[i].getAttribute('title');
124                 var alt = images[i].getAttribute('alt');
125                 if ((document.all) && (alt) && (!title))
126                         {
127                         images[i].setAttribute('title', '');
128                         }
129                 }
130         }
131
132 // Nice Titles
133
134 // original code by Stuart Langridge 2003-11
135 // with additions to the code by other good people
136 // http://www.kryogenix.org/code/browser/nicetitle/
137 // thank you, sir
138
139 // modified by Peter Janes 2003-03-25
140 // http://peterjanes.ca/blog/archives/2003/03/25/nicetitles-for-ins-and-del
141 // added in ins and del tags
142
143 // modified by Dunstan Orchard 2003-11-18
144 // http://1976design.com/blog/
145 // added in accesskey information
146 // tried ever-so-hard, but couldn't work out how to do what Ethan did
147
148 // final genius touch by by Ethan Marcotte 2003-11-18
149 // http://www.sidesh0w.com/
150 // worked out how to delay showing the popups to make them more like the browser's own
151
152 // set the namespace
153 var XHTMLNS = 'http://www.w3.org/1999/xhtml';
154 var CURRENT_NICE_TITLE;
155
156 // browser sniff
157 var browser = new Browser();
158
159 // determine browser and version.
160 function Browser()
161         {
162         var ua, s, i;
163
164         this.isIE = false;
165         this.isNS = false;
166         this.version = null;
167
168         ua = navigator.userAgent;
169
170         s = 'MSIE';
171         if ((i = ua.indexOf(s)) >= 0)
172                 {
173                 this.isIE = true;
174                 this.version = parseFloat(ua.substr(i + s.length));
175                 return;
176                 }
177
178         s = 'Netscape6/';
179         if ((i = ua.indexOf(s)) >= 0)
180                 {
181                 this.isNS = true;
182                 this.version = parseFloat(ua.substr(i + s.length));
183                 return;
184                 }
185
186         // treat any other 'Gecko' browser as NS 6.1.
187         s = 'Gecko';
188         if ((i = ua.indexOf(s)) >= 0)
189                 {
190                 this.isNS = true;
191                 this.version = 6.1;
192                 return;
193                 }
194         }
195
196 // 2003-11-19 sidesh0w
197 // set delay vars to emulate normal hover delay
198 var delay;
199 var interval = 0.60;
200
201 // this function runs on window load
202 // it runs through all the links on the page as starts listening for actions
203 function makeNiceTitles()
204         {
205         if (!document.createElement || !document.getElementsByTagName) return;
206         if (!document.createElementNS)
207                 {
208                 document.createElementNS = function(ns, elt)
209                         {
210                         return document.createElement(elt);
211                         }
212                 }
213
214         // do regular links
215         if (!document.links)
216                 {
217                 document.links = document.getElementsByTagName('a');
218                 }
219         for (var ti=0; ti<document.links.length; ti++)
220                 {
221                 var lnk = document.links[ti];
222                 // * I added specific class names here..
223                 if (lnk.title)
224                         {
225                         lnk.setAttribute('nicetitle', lnk.title);
226                         lnk.removeAttribute('title');
227                         addEvent(lnk, 'mouseover', showNiceTitle);
228                         addEvent(lnk, 'mousemove', moveNiceTitle);
229                         addEvent(lnk, 'mouseout', hideNiceTitle);
230                         addEvent(lnk, 'focus', showNiceTitle);
231                         addEvent(lnk, 'blur', hideNiceTitle);
232                         }
233                 }
234
235         // 2003-03-25 Peter Janes
236         // do ins and del tags
237         var tags = new Array(2);
238         tags[0] = document.getElementsByTagName('ins');
239         tags[1] = document.getElementsByTagName('del');
240         for (var tt=0; tt<tags.length; tt++)
241                 {
242                 if (tags[tt])
243                         {
244                         for (var ti=0; ti<tags[tt].length; ti++)
245                                 {
246                                 var tag = tags[tt][ti];
247                                 if (tag.dateTime)
248                                         {
249                                         var strDate = tag.dateTime;
250                                         // HTML/ISO8601 date: yyyy-mm-ddThh:mm:ssTZD (Z, -hh:mm, +hh:mm)
251                                         var month = strDate.substring(5,7);
252                                         var day = strDate.substring(8,10);
253                                         if (month[0] == '0')
254                                                 {
255                                                 month = month[1];
256                                                 }
257                                         if (day[0] == '0')
258                                                 {
259                                                 day = day[1];
260                                                 }
261                                         var dtIns = new Date(strDate.substring(0,4), month-1, day, strDate.substring(11,13), strDate.substring(14,16), strDate.substring(17,19));
262                                         tag.setAttribute('nicetitle', (tt == 0 ? 'Added' : 'Deleted') + ' on ' + dtIns.toString());
263                                         addEvent(tag, 'mouseover', showNiceTitle);
264                                         addEvent(tag, 'mousemove', moveNiceTitle);
265                                         addEvent(tag, 'mouseout', hideNiceTitle);
266                                         addEvent(tag, 'focus', showNiceTitle);
267                                         addEvent(tag, 'blur', hideNiceTitle);
268                                         }
269                                 }
270                         }
271                 }
272         }
273
274 function findPosition(oLink)
275         {
276         if (oLink.offsetParent)
277                 {
278                 for (var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent)
279                         {
280                         posX += oLink.offsetLeft;
281                         posY += oLink.offsetTop;
282                         }
283                 return [posX, posY];
284                 }
285         else
286                 {
287                 return [oLink.x, oLink.y];
288                 }
289         }
290
291 function getParent(el, pTagName)
292         {
293         if (el == null)
294                 {
295                 return null;
296                 }
297         // gecko bug, supposed to be uppercase
298         else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
299                 {
300                 return el;
301                 }
302         else
303                 {
304                 return getParent(el.parentNode, pTagName);
305                 }
306         }
307
308 // 2003-11-19 sidesh0w
309 // trailerpark wrapper function
310 function showDelay()
311         {
312         if (CURRENT_NICE_TITLE)
313                 {
314                 CURRENT_NICE_TITLE.style.display = '';
315                 }
316         }
317
318 // build and show the nice titles
319 function showNiceTitle(e)
320         {
321         if (CURRENT_NICE_TITLE) hideNiceTitle(CURRENT_NICE_TITLE);
322         if (!document.getElementsByTagName) return;
323
324         if (window.event && window.event.srcElement)
325                 {
326                 lnk = window.event.srcElement
327                 }
328         else if (e && e.target)
329                 {
330                 lnk = e.target
331                 }
332         if (!lnk) return;
333
334         // lnk is a textnode or an elementnode that's not ins/del
335         if (lnk.nodeType == 3 || (lnk.nodeType == 1 && lnk.tagName.toLowerCase() != 'ins' && lnk.tagName.toLowerCase() != 'del'))
336                 {
337                 // ascend parents until we hit a link
338                 lnk = getParent(lnk, 'a');
339                 }
340
341          nicetitle = lnk.getAttribute('nicetitle');
342
343         var d = document.createElementNS(XHTMLNS, 'div');
344         d.className = 'nicetitle';
345         var dc = document.createElementNS(XHTMLNS, 'div');
346         dc.className = 'nicetitle-content';
347         d.appendChild(dc);
348         tnt = document.createTextNode(nicetitle);
349         pat = document.createElementNS(XHTMLNS, 'p');
350         pat.className = 'titletext';
351         pat.appendChild(tnt);
352
353         // 2003-11-18 Dunstan Orchard
354         // added in accesskey info
355         if (lnk.accessKey)
356                 {
357                 axs = document.createTextNode(' [' + lnk.accessKey + ']');
358                 axsk = document.createElementNS(XHTMLNS, 'span');
359                 axsk.className = 'accesskey';
360                 axsk.appendChild(axs);
361                 pat.appendChild(axsk);
362                 }
363         dc.appendChild(pat);
364
365         if (lnk.href)
366                 {
367                 tnd = document.createTextNode(lnk.href);
368                 pad = document.createElementNS(XHTMLNS, 'p');
369                 pad.className = 'destination';
370                 pad.appendChild(tnd);
371                 dc.appendChild(pad);
372                 }
373
374         STD_WIDTH = 300;
375
376         if (lnk.href)
377                 {
378                 h = lnk.href.length;
379                 }
380         else
381                 {
382                 h = nicetitle.length;
383                 }
384
385         if (nicetitle.length)
386                 {
387                 t = nicetitle.length;
388                 }
389
390         h_pixels = h*6;
391         t_pixels = t*10;
392
393         if (t_pixels > STD_WIDTH)
394                 {
395                 w = t_pixels;
396                 }
397         else if ((STD_WIDTH > t_pixels) && (t_pixels > h_pixels))
398                 {
399                 w = t_pixels;
400                 }
401         else if ((STD_WIDTH > h_pixels) && (h_pixels > t_pixels))
402                 {
403                 w = h_pixels;
404                 }
405         else
406                 {
407                 w = STD_WIDTH;
408                 }
409
410         d.style.width = w + 'px';
411         d.style.display = 'none';
412
413         document.getElementsByTagName('body')[0].appendChild(d);
414
415         CURRENT_NICE_TITLE = d;
416
417         moveNiceTitle(e);
418
419         delay = setTimeout("showDelay()", interval * 1000);
420         }
421
422 function moveNiceTitle(e)
423         {
424         if (!document.getElementsByTagName) return;
425         if (!CURRENT_NICE_TITLE) return;
426
427         if (document.all)
428                 {
429                 X = document.body.scrollLeft + window.event.clientX;
430                 }
431         else if (document.layers || document.getElementById)
432                 {
433                 X = e.pageX;
434                 }
435         w_title = Number(CURRENT_NICE_TITLE.style.width.replace(/^(\d+).*$/, '$1'));
436 <<<<<<< HEAD
437         if (w_title + X > (window.innerWidth - 20))
438                 {
439                 X = window.innerWidth - w_title - 20;
440 =======
441         if (w_title + X > (window.innerWidth + (document.documentElement.scrollLeft || window.pageXOffset) - 20))
442                 {
443                 X = (window.innerWidth + (document.documentElement.scrollLeft || window.pageXOffset) - 20) - w_title;
444 >>>>>>> skinnable-master
445                 }
446
447         if (document.all)
448                 {
449                 Y = document.body.scrollTop + window.event.clientY;
450                 }
451         else if (document.layers || document.getElementById)
452                 {
453                 Y = e.pageY;
454                 }
455 <<<<<<< HEAD
456         if (Y > (window.innerHeight - 60))
457 =======
458         if (Y > (window.innerHeight + (document.documentElement.scrollTop || window.pageYOffset) - 60))
459 >>>>>>> skinnable-master
460                 {
461                 Y = Y - 25 - 60;
462                 }
463         
464         CURRENT_NICE_TITLE.style.left = X + 'px';
465         CURRENT_NICE_TITLE.style.top = (Y + 25) + 'px';
466         }
467
468 function hideNiceTitle(e)
469         {
470         // 2003-11-19 sidesh0w
471         // clearTimeout
472         if (delay) clearTimeout(delay);
473         if (!document.getElementsByTagName) return;
474         if (CURRENT_NICE_TITLE)
475                 {
476                 document.getElementsByTagName('body')[0].removeChild(CURRENT_NICE_TITLE);
477                 CURRENT_NICE_TITLE = null;
478                 }
479         }
480
481 addEvent(window, "load", floatImages);
482 addEvent(window, "resize", floatImages);
483 addEvent(window, "load", blockquoteCite);
484 addEvent(window, "load", insdelCite);
485 addEvent(window, "load", noAltTooltip);
486 addEvent(window, "load", makeNiceTitles);
487
488 // I'm very poor in JavaScript. Please correct me if I'm wrong.